{"action":"created","comment":{"url":"https://api.github.com/repos/intel-analytics/BigDL/pulls/comments/125917733","pull_request_review_id":48353632,"id":125917733,"diff_hunk":"@@ -0,0 +1,182 @@\n+/*\n+ * Copyright 2016 The BigDL Authors.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *     http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package com.intel.analytics.bigdl.optim\n+\n+import com.intel.analytics.bigdl._\n+import com.intel.analytics.bigdl.dataset.{LocalDataSet, MiniBatch, Sample, SampleToBatch}\n+import com.intel.analytics.bigdl.nn.abstractnn.Activity\n+import com.intel.analytics.bigdl.tensor.{Storage, Tensor}\n+import com.intel.analytics.bigdl.tensor.TensorNumericMath.TensorNumeric\n+import com.intel.analytics.bigdl.utils.{Engine, MklBlas}\n+\n+\n+import scala.reflect.ClassTag\n+\n+object LocalPredictor {\n+  def getAndClearWeightBias[T: ClassTag](parameters: (Array[Tensor[T]], Array[Tensor[T]]))\n+                                        (implicit ev: TensorNumeric[T]): Array[Tensor[T]] = {\n+    var i = 0\n+    val weightsBias = new Array[Tensor[T]](parameters._1.length)\n+    while (i < parameters._1.length) {\n+      if (parameters._1(i) != null) {\n+        val wb = parameters._1(i)\n+        weightsBias(i) = Tensor[T](Storage(wb.storage().array()),\n+          wb.storageOffset(), wb.size(), wb.stride())\n+      }\n+      i += 1\n+    }\n+    i = 0\n+    while (i < parameters._1.length) {\n+      if (parameters._1(i) != null) {\n+        parameters._1(i).set()\n+      }\n+      if (parameters._2(i) != null) {\n+        parameters._2(i).set()\n+      }\n+      i += 1\n+    }\n+    weightsBias\n+  }\n+\n+  def apply[T: ClassTag](model: Module[T], weightsBias: Array[Tensor[T]])\n+                        (implicit ev: TensorNumeric[T]): LocalPredictor[T] = {\n+    new LocalPredictor[T](model, weightsBias)\n+  }\n+}\n+\n+class LocalPredictor[T: ClassTag] private[optim](model: Module[T], weightsBias: Array[Tensor[T]])\n+                                                (implicit ev: TensorNumeric[T])\n+  extends Serializable {\n+\n+  val logger = LocalValidator.logger\n+  private val coreNumber = Engine.coreNumber()\n+\n+  private val subModelNumber = Engine.getEngineType match {\n+    case MklBlas => coreNumber\n+    case _ => throw new IllegalArgumentException\n+  }\n+\n+  private val batchSize = 8\n+\n+  def predictClass(dataSet: Array[Sample[T]]): Array[Int] = {\n+    val result = predict(dataSet)\n+    result.map(output => {\n+      val _output = output.toTensor[T]\n+      require(_output.dim() == 1, s\"Predictor.predictClass:\" +\n+        s\"Only support one sample has one lable, but got ${_output.dim()} label\")\n+      ev.toType[Int](_output.max(1)._2.valueAt(1))\n+    })\n+  }\n+\n+  def predictClass(dataSet: LocalDataSet[Sample[T]]): Array[Int] = {\n+    val result = predict(dataSet)\n+    result.map(output => {\n+      val _output = output.toTensor[T]\n+      require(_output.dim() == 1, s\"Predictor.predictClass:\" +\n+        s\"Only support one sample has one lable, but got ${_output.dim()} label\")\n+      ev.toType[Int](_output.max(1)._2.valueAt(1))\n+    })\n+  }\n+\n+  def predict(dataSet: LocalDataSet[Sample[T]]): Array[Activity] = {\n+    val dataset = dataSet.transform(SampleToBatch[T](\n+      batchSize = batchSize, None, None, None,\n+      partitionNum = Some(1))).asInstanceOf[LocalDataSet[MiniBatch[T]]]\n+    val dataIter = dataset.data(train = false)\n+\n+    val workingModels = (1 to subModelNumber).map(_ => {\n+      val submodel = model.cloneModule().evaluate()\n+      putWeightBias(weightsBias, submodel)\n+      submodel\n+    }).toArray\n+\n+    dataIter.map(batch => {\n+      println(\"Enter map\")\n+      val stackSize = batch.size() / subModelNumber\n+      val extraSize = batch.size() % subModelNumber\n+      val parallelism = if (stackSize == 0) extraSize else subModelNumber\n+      val start = System.nanoTime()\n+      val result = Engine.default.invokeAndWait(\n+        (0 until parallelism).map(b =>\n+          () => {\n+            val offset = b * stackSize + math.min(b, extraSize) + 1\n+            val length = stackSize + (if (b < extraSize) 1 else 0)\n+            val currentMiniBatch = batch.slice(offset, length)\n+            val input = currentMiniBatch.getInput()\n+            val output = workingModels(b).forward(input).toTensor[T]\n+            output\n+          }\n+        )\n+      )\n+      val batchResult = result.flatMap(_.split(1)).map(_.asInstanceOf[Activity])\n+      batchResult\n+    }).toArray.flatten\n+\n+  }\n+\n+  def predict(dataSet: Array[Sample[T]]): Array[Activity] = {\n+    val iter = dataSet.iterator\n+    val transformer = SampleToBatch[T](\n+      batchSize = batchSize, None, None, None,","path":"spark/dl/src/main/scala/com/intel/analytics/bigdl/optim/LocalPredictor.scala","position":134,"original_position":134,"commit_id":"03814ab073c880567da3a3c93c6194308d9e8a83","original_commit_id":"03814ab073c880567da3a3c93c6194308d9e8a83","user":{"login":"jason-dai","id":1931082,"avatar_url":"https://avatars0.githubusercontent.com/u/1931082?v=3","gravatar_id":"","url":"https://api.github.com/users/jason-dai","html_url":"https://github.com/jason-dai","followers_url":"https://api.github.com/users/jason-dai/followers","following_url":"https://api.github.com/users/jason-dai/following{/other_user}","gists_url":"https://api.github.com/users/jason-dai/gists{/gist_id}","starred_url":"https://api.github.com/users/jason-dai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jason-dai/subscriptions","organizations_url":"https://api.github.com/users/jason-dai/orgs","repos_url":"https://api.github.com/users/jason-dai/repos","events_url":"https://api.github.com/users/jason-dai/events{/privacy}","received_events_url":"https://api.github.com/users/jason-dai/received_events","type":"User","site_admin":false},"body":"batchSize * subModelNumber","created_at":"2017-07-06T14:34:10Z","updated_at":"2017-07-06T14:34:10Z","html_url":"https://github.com/intel-analytics/BigDL/pull/1168#discussion_r125917733","pull_request_url":"https://api.github.com/repos/intel-analytics/BigDL/pulls/1168","_links":{"self":{"href":"https://api.github.com/repos/intel-analytics/BigDL/pulls/comments/125917733"},"html":{"href":"https://github.com/intel-analytics/BigDL/pull/1168#discussion_r125917733"},"pull_request":{"href":"https://api.github.com/repos/intel-analytics/BigDL/pulls/1168"}}},"pull_request":{"url":"https://api.github.com/repos/intel-analytics/BigDL/pulls/1168","id":129144799,"html_url":"https://github.com/intel-analytics/BigDL/pull/1168","diff_url":"https://github.com/intel-analytics/BigDL/pull/1168.diff","patch_url":"https://github.com/intel-analytics/BigDL/pull/1168.patch","issue_url":"https://api.github.com/repos/intel-analytics/BigDL/issues/1168","number":1168,"state":"open","locked":false,"title":"add  local JVM predictor and examples","user":{"login":"jenniew","id":8434507,"avatar_url":"https://avatars3.githubusercontent.com/u/8434507?v=3","gravatar_id":"","url":"https://api.github.com/users/jenniew","html_url":"https://github.com/jenniew","followers_url":"https://api.github.com/users/jenniew/followers","following_url":"https://api.github.com/users/jenniew/following{/other_user}","gists_url":"https://api.github.com/users/jenniew/gists{/gist_id}","starred_url":"https://api.github.com/users/jenniew/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jenniew/subscriptions","organizations_url":"https://api.github.com/users/jenniew/orgs","repos_url":"https://api.github.com/users/jenniew/repos","events_url":"https://api.github.com/users/jenniew/events{/privacy}","received_events_url":"https://api.github.com/users/jenniew/received_events","type":"User","site_admin":false},"body":"## What changes were proposed in this pull request?\r\n\r\nAdd local predictor and provide local JVM train, test, predict examples.\r\n\r\n## How was this patch tested?\r\n\r\nUse examples to test\r\n\r\n","created_at":"2017-07-06T09:48:15Z","updated_at":"2017-07-06T14:34:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"49cc7e261722eca56dacd00ce75a8e64f72f2923","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/intel-analytics/BigDL/pulls/1168/commits","review_comments_url":"https://api.github.com/repos/intel-analytics/BigDL/pulls/1168/comments","review_comment_url":"https://api.github.com/repos/intel-analytics/BigDL/pulls/comments{/number}","comments_url":"https://api.github.com/repos/intel-analytics/BigDL/issues/1168/comments","statuses_url":"https://api.github.com/repos/intel-analytics/BigDL/statuses/03814ab073c880567da3a3c93c6194308d9e8a83","head":{"label":"jenniew:local","ref":"local","sha":"03814ab073c880567da3a3c93c6194308d9e8a83","user":{"login":"jenniew","id":8434507,"avatar_url":"https://avatars3.githubusercontent.com/u/8434507?v=3","gravatar_id":"","url":"https://api.github.com/users/jenniew","html_url":"https://github.com/jenniew","followers_url":"https://api.github.com/users/jenniew/followers","following_url":"https://api.github.com/users/jenniew/following{/other_user}","gists_url":"https://api.github.com/users/jenniew/gists{/gist_id}","starred_url":"https://api.github.com/users/jenniew/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jenniew/subscriptions","organizations_url":"https://api.github.com/users/jenniew/orgs","repos_url":"https://api.github.com/users/jenniew/repos","events_url":"https://api.github.com/users/jenniew/events{/privacy}","received_events_url":"https://api.github.com/users/jenniew/received_events","type":"User","site_admin":false},"repo":{"id":78898610,"name":"BigDL","full_name":"jenniew/BigDL","owner":{"login":"jenniew","id":8434507,"avatar_url":"https://avatars3.githubusercontent.com/u/8434507?v=3","gravatar_id":"","url":"https://api.github.com/users/jenniew","html_url":"https://github.com/jenniew","followers_url":"https://api.github.com/users/jenniew/followers","following_url":"https://api.github.com/users/jenniew/following{/other_user}","gists_url":"https://api.github.com/users/jenniew/gists{/gist_id}","starred_url":"https://api.github.com/users/jenniew/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jenniew/subscriptions","organizations_url":"https://api.github.com/users/jenniew/orgs","repos_url":"https://api.github.com/users/jenniew/repos","events_url":"https://api.github.com/users/jenniew/events{/privacy}","received_events_url":"https://api.github.com/users/jenniew/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jenniew/BigDL","description":"BigDL: Distributed Deep learning Library for Apache Spark","fork":true,"url":"https://api.github.com/repos/jenniew/BigDL","forks_url":"https://api.github.com/repos/jenniew/BigDL/forks","keys_url":"https://api.github.com/repos/jenniew/BigDL/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jenniew/BigDL/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jenniew/BigDL/teams","hooks_url":"https://api.github.com/repos/jenniew/BigDL/hooks","issue_events_url":"https://api.github.com/repos/jenniew/BigDL/issues/events{/number}","events_url":"https://api.github.com/repos/jenniew/BigDL/events","assignees_url":"https://api.github.com/repos/jenniew/BigDL/assignees{/user}","branches_url":"https://api.github.com/repos/jenniew/BigDL/branches{/branch}","tags_url":"https://api.github.com/repos/jenniew/BigDL/tags","blobs_url":"https://api.github.com/repos/jenniew/BigDL/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jenniew/BigDL/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jenniew/BigDL/git/refs{/sha}","trees_url":"https://api.github.com/repos/jenniew/BigDL/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jenniew/BigDL/statuses/{sha}","languages_url":"https://api.github.com/repos/jenniew/BigDL/languages","stargazers_url":"https://api.github.com/repos/jenniew/BigDL/stargazers","contributors_url":"https://api.github.com/repos/jenniew/BigDL/contributors","subscribers_url":"https://api.github.com/repos/jenniew/BigDL/subscribers","subscription_url":"https://api.github.com/repos/jenniew/BigDL/subscription","commits_url":"https://api.github.com/repos/jenniew/BigDL/commits{/sha}","git_commits_url":"https://api.github.com/repos/jenniew/BigDL/git/commits{/sha}","comments_url":"https://api.github.com/repos/jenniew/BigDL/comments{/number}","issue_comment_url":"https://api.github.com/repos/jenniew/BigDL/issues/comments{/number}","contents_url":"https://api.github.com/repos/jenniew/BigDL/contents/{+path}","compare_url":"https://api.github.com/repos/jenniew/BigDL/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jenniew/BigDL/merges","archive_url":"https://api.github.com/repos/jenniew/BigDL/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jenniew/BigDL/downloads","issues_url":"https://api.github.com/repos/jenniew/BigDL/issues{/number}","pulls_url":"https://api.github.com/repos/jenniew/BigDL/pulls{/number}","milestones_url":"https://api.github.com/repos/jenniew/BigDL/milestones{/number}","notifications_url":"https://api.github.com/repos/jenniew/BigDL/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jenniew/BigDL/labels{/name}","releases_url":"https://api.github.com/repos/jenniew/BigDL/releases{/id}","deployments_url":"https://api.github.com/repos/jenniew/BigDL/deployments","created_at":"2017-01-14T00:45:08Z","updated_at":"2017-01-14T00:45:11Z","pushed_at":"2017-07-06T09:43:01Z","git_url":"git://github.com/jenniew/BigDL.git","ssh_url":"git@github.com:jenniew/BigDL.git","clone_url":"https://github.com/jenniew/BigDL.git","svn_url":"https://github.com/jenniew/BigDL","homepage":"","size":16622,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"intel-analytics:master","ref":"master","sha":"1c065d7e1903a0c124f84d02eb156ad47acc6011","user":{"login":"intel-analytics","id":10941215,"avatar_url":"https://avatars0.githubusercontent.com/u/10941215?v=3","gravatar_id":"","url":"https://api.github.com/users/intel-analytics","html_url":"https://github.com/intel-analytics","followers_url":"https://api.github.com/users/intel-analytics/followers","following_url":"https://api.github.com/users/intel-analytics/following{/other_user}","gists_url":"https://api.github.com/users/intel-analytics/gists{/gist_id}","starred_url":"https://api.github.com/users/intel-analytics/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/intel-analytics/subscriptions","organizations_url":"https://api.github.com/users/intel-analytics/orgs","repos_url":"https://api.github.com/users/intel-analytics/repos","events_url":"https://api.github.com/users/intel-analytics/events{/privacy}","received_events_url":"https://api.github.com/users/intel-analytics/received_events","type":"Organization","site_admin":false},"repo":{"id":66823715,"name":"BigDL","full_name":"intel-analytics/BigDL","owner":{"login":"intel-analytics","id":10941215,"avatar_url":"https://avatars0.githubusercontent.com/u/10941215?v=3","gravatar_id":"","url":"https://api.github.com/users/intel-analytics","html_url":"https://github.com/intel-analytics","followers_url":"https://api.github.com/users/intel-analytics/followers","following_url":"https://api.github.com/users/intel-analytics/following{/other_user}","gists_url":"https://api.github.com/users/intel-analytics/gists{/gist_id}","starred_url":"https://api.github.com/users/intel-analytics/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/intel-analytics/subscriptions","organizations_url":"https://api.github.com/users/intel-analytics/orgs","repos_url":"https://api.github.com/users/intel-analytics/repos","events_url":"https://api.github.com/users/intel-analytics/events{/privacy}","received_events_url":"https://api.github.com/users/intel-analytics/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/intel-analytics/BigDL","description":"BigDL: Distributed Deep Learning Library for Apache Spark","fork":false,"url":"https://api.github.com/repos/intel-analytics/BigDL","forks_url":"https://api.github.com/repos/intel-analytics/BigDL/forks","keys_url":"https://api.github.com/repos/intel-analytics/BigDL/keys{/key_id}","collaborators_url":"https://api.github.com/repos/intel-analytics/BigDL/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/intel-analytics/BigDL/teams","hooks_url":"https://api.github.com/repos/intel-analytics/BigDL/hooks","issue_events_url":"https://api.github.com/repos/intel-analytics/BigDL/issues/events{/number}","events_url":"https://api.github.com/repos/intel-analytics/BigDL/events","assignees_url":"https://api.github.com/repos/intel-analytics/BigDL/assignees{/user}","branches_url":"https://api.github.com/repos/intel-analytics/BigDL/branches{/branch}","tags_url":"https://api.github.com/repos/intel-analytics/BigDL/tags","blobs_url":"https://api.github.com/repos/intel-analytics/BigDL/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/intel-analytics/BigDL/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/intel-analytics/BigDL/git/refs{/sha}","trees_url":"https://api.github.com/repos/intel-analytics/BigDL/git/trees{/sha}","statuses_url":"https://api.github.com/repos/intel-analytics/BigDL/statuses/{sha}","languages_url":"https://api.github.com/repos/intel-analytics/BigDL/languages","stargazers_url":"https://api.github.com/repos/intel-analytics/BigDL/stargazers","contributors_url":"https://api.github.com/repos/intel-analytics/BigDL/contributors","subscribers_url":"https://api.github.com/repos/intel-analytics/BigDL/subscribers","subscription_url":"https://api.github.com/repos/intel-analytics/BigDL/subscription","commits_url":"https://api.github.com/repos/intel-analytics/BigDL/commits{/sha}","git_commits_url":"https://api.github.com/repos/intel-analytics/BigDL/git/commits{/sha}","comments_url":"https://api.github.com/repos/intel-analytics/BigDL/comments{/number}","issue_comment_url":"https://api.github.com/repos/intel-analytics/BigDL/issues/comments{/number}","contents_url":"https://api.github.com/repos/intel-analytics/BigDL/contents/{+path}","compare_url":"https://api.github.com/repos/intel-analytics/BigDL/compare/{base}...{head}","merges_url":"https://api.github.com/repos/intel-analytics/BigDL/merges","archive_url":"https://api.github.com/repos/intel-analytics/BigDL/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/intel-analytics/BigDL/downloads","issues_url":"https://api.github.com/repos/intel-analytics/BigDL/issues{/number}","pulls_url":"https://api.github.com/repos/intel-analytics/BigDL/pulls{/number}","milestones_url":"https://api.github.com/repos/intel-analytics/BigDL/milestones{/number}","notifications_url":"https://api.github.com/repos/intel-analytics/BigDL/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/intel-analytics/BigDL/labels{/name}","releases_url":"https://api.github.com/repos/intel-analytics/BigDL/releases{/id}","deployments_url":"https://api.github.com/repos/intel-analytics/BigDL/deployments","created_at":"2016-08-29T07:59:50Z","updated_at":"2017-07-03T08:11:46Z","pushed_at":"2017-07-06T10:47:08Z","git_url":"git://github.com/intel-analytics/BigDL.git","ssh_url":"git@github.com:intel-analytics/BigDL.git","clone_url":"https://github.com/intel-analytics/BigDL.git","svn_url":"https://github.com/intel-analytics/BigDL","homepage":"https://bigdl-project.github.io/","size":13180,"stargazers_count":1835,"watchers_count":1835,"language":"Scala","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":352,"mirror_url":null,"open_issues_count":122,"forks":352,"open_issues":122,"watchers":1835,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/intel-analytics/BigDL/pulls/1168"},"html":{"href":"https://github.com/intel-analytics/BigDL/pull/1168"},"issue":{"href":"https://api.github.com/repos/intel-analytics/BigDL/issues/1168"},"comments":{"href":"https://api.github.com/repos/intel-analytics/BigDL/issues/1168/comments"},"review_comments":{"href":"https://api.github.com/repos/intel-analytics/BigDL/pulls/1168/comments"},"review_comment":{"href":"https://api.github.com/repos/intel-analytics/BigDL/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/intel-analytics/BigDL/pulls/1168/commits"},"statuses":{"href":"https://api.github.com/repos/intel-analytics/BigDL/statuses/03814ab073c880567da3a3c93c6194308d9e8a83"}}}}
{"actor":{"display_login":"jason-dai"}}