You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/11/12 21:56:06 UTC

[GitHub] lanking520 closed pull request #12833: [MXNET-1042] Module API extension

lanking520 closed pull request #12833: [MXNET-1042] Module API extension
URL: https://github.com/apache/incubator-mxnet/pull/12833
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/scala-package/core/src/main/scala/org/apache/mxnet/javaapi/IO.scala b/scala-package/core/src/main/scala/org/apache/mxnet/javaapi/IO.scala
index 47b1c367c1c..167718e2737 100644
--- a/scala-package/core/src/main/scala/org/apache/mxnet/javaapi/IO.scala
+++ b/scala-package/core/src/main/scala/org/apache/mxnet/javaapi/IO.scala
@@ -32,3 +32,62 @@ object DataDesc{
 
   def getBatchAxis(layout: String): Int = org.apache.mxnet.DataDesc.getBatchAxis(Some(layout))
 }
+
+class DataBatch(val dataBatch: org.apache.mxnet.DataBatch) {
+  def dispose() : Unit = dataBatch.dispose()
+
+  def provideDataDesc : Array[DataDesc] = dataBatch.provideDataDesc.map(DataDesc.fromDataDesc).toArray
+
+  def provideLabelDesc : Array[DataDesc] = dataBatch.provideLabelDesc.map(DataDesc.fromDataDesc).toArray
+}
+
+object DataBatch {
+  implicit def FromDataBatch(dataBatch : DataBatch) : org.apache.mxnet.DataBatch = {
+    dataBatch.dataBatch
+  }
+
+  implicit def toDataBatch(dataBatch: org.apache.mxnet.DataBatch) : DataBatch = {
+    new DataBatch(dataBatch)
+  }
+
+  class Builder() {
+
+    private val builder : org.apache.mxnet.DataBatch.Builder = new org.apache.mxnet.DataBatch.Builder
+
+    def setData(data : Array[NDArray]) : Builder = {
+      this.builder.setData(data.map(NDArray.toNDArray) : _*)
+      this
+    }
+
+    def setLabel(label : Array[NDArray]) : Builder = {
+      this.builder.setLabel(label.map(NDArray.toNDArray) : _*)
+      this
+    }
+
+    def setIndex(index : Array[Long]) : Builder = {
+      this.builder.setIndex(index : _*)
+      this
+    }
+
+    def setPad(pad : Int) : Builder = {
+      this.builder.setPad(pad)
+      this
+    }
+
+    def setDataDesc(dataDesc : Array[DataDesc]) : Builder = {
+      this.builder.provideDataDesc(dataDesc.map(DataDesc.toDataDesc))
+      this
+    }
+
+    def setLabelDesc(labelDesc : Array[DataDesc]) : Builder = {
+      this.builder.provideLabelDesc(labelDesc.map(DataDesc.toDataDesc))
+      this
+    }
+
+    def build() : DataBatch = {
+      this.builder.build()
+    }
+
+    // TODO: Extends for Bucketing support
+  }
+}
diff --git a/scala-package/core/src/main/scala/org/apache/mxnet/javaapi/Model.scala b/scala-package/core/src/main/scala/org/apache/mxnet/javaapi/Model.scala
new file mode 100644
index 00000000000..4a295f217c0
--- /dev/null
+++ b/scala-package/core/src/main/scala/org/apache/mxnet/javaapi/Model.scala
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mxnet.javaapi
+
+import collection.JavaConverters._
+import scala.collection.mutable
+
+object Model {
+  def saveCheckpoint(prefix : String, epoch : Int, symbol : Symbol,
+                     argParams : java.util.Map[String, NDArray], auxParams : java.util.Map[String, NDArray]) : Unit = {
+    org.apache.mxnet.Model.saveCheckpoint(prefix, epoch, symbol,
+      argParams.asScala.map(ele => (ele._1, NDArray.toNDArray(ele._2))).toMap,
+      auxParams.asScala.map(ele => (ele._1, NDArray.toNDArray(ele._2))).toMap)
+  }
+
+  def loadCheckpoint(prefix : String, epoch : Int) : ModelContent = {
+    val result = org.apache.mxnet.Model.loadCheckpoint(prefix, epoch)
+    val JavaArgParams = mutable.Map[String, NDArray]()
+    val JavaAuxParams = mutable.Map[String, NDArray]()
+    result._2.foreach(ele => JavaArgParams(ele._1) = ele._2)
+    result._3.foreach(ele => JavaAuxParams(ele._1) = ele._2)
+    ModelContent(result._1, JavaArgParams.asJava, JavaAuxParams.asJava)
+  }
+  case class ModelContent(symbol : Symbol,
+                          argParams : java.util.Map[String, NDArray],
+                          auxParams : java.util.Map[String, NDArray])
+}
diff --git a/scala-package/core/src/main/scala/org/apache/mxnet/javaapi/Module.scala b/scala-package/core/src/main/scala/org/apache/mxnet/javaapi/Module.scala
new file mode 100644
index 00000000000..8dda4a84e75
--- /dev/null
+++ b/scala-package/core/src/main/scala/org/apache/mxnet/javaapi/Module.scala
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mxnet.javaapi
+import collection.JavaConverters._
+import scala.collection.mutable
+
+class Module(val module : org.apache.mxnet.module.Module) {
+
+  case class ArgAux(argParams: java.util.Map[String, NDArray], auxParams: java.util.Map[String, NDArray])
+
+  def dataShape : Array[DataDesc] = module.dataShapes.map(DataDesc.fromDataDesc).toArray
+
+  def labelShape : Array[DataDesc] = module.labelShapes.map(DataDesc.fromDataDesc).toArray
+
+  def outputShapes : java.util.Map[String, Shape]
+  = module.outputShapes.map(ele => (ele._1, Shape.fromShape(ele._2))).toMap.asJava
+
+  def outputNames : Array[String] = module.outputNames.toArray
+
+  def getParams : ArgAux = {
+    val result = module.getParams
+    val JavaArgParams = mutable.Map[String, NDArray]()
+    val JavaAuxParams = mutable.Map[String, NDArray]()
+    result._1.foreach(ele => JavaArgParams(ele._1) = ele._2)
+    result._2.foreach(ele => JavaArgParams(ele._1) = ele._2)
+    ArgAux(JavaArgParams.asJava, JavaAuxParams.asJava)
+  }
+
+  def setParams(argParams: java.util.Map[String, NDArray], auxParams: java.util.Map[String, NDArray],
+                allowMissing: Boolean, forceInit : Boolean, allowExtra : Boolean) : Unit = {
+    module.setParams(argParams.asScala.map(ele => (ele._1, NDArray.toNDArray(ele._2))).toMap,
+      auxParams.asScala.map(ele => (ele._1, NDArray.toNDArray(ele._2))).toMap,
+      allowMissing, forceInit, allowExtra)
+  }
+
+  // TODO: Extend the full bind functionalities
+  def bind(dataShapes : Array[DataDesc], forTraining: Boolean, inputsNeedGrad: Boolean,
+           forceRebind: Boolean) : Unit = {
+    module.bind(forTraining, inputsNeedGrad, forceRebind, dataShapes.map(DataDesc.toDataDesc): _*)
+  }
+
+  def forward(dataBatch: DataBatch, isTrain : Boolean) : Unit = {
+    module.forward(dataBatch, Some(isTrain))
+  }
+
+  def backward(outGrads : Array[NDArray]) : Unit = {
+    module.backward(outGrads.map(NDArray.toNDArray))
+  }
+
+  // TODO: Extends with training features
+}
+
+object Module {
+  implicit def toModule(module : org.apache.mxnet.module.Module) : Module = {
+    new Module(module)
+  }
+
+  implicit def fromModule(module : Module) : org.apache.mxnet.module.Module = {
+    module.module
+  }
+
+  def loadCheckpoint(prefix: String, epoch: Int, loadOptimizerStates: Boolean = false,
+                     dataNames: Array[String],
+                     labelNames: Array[String],
+                     contexts: Array[Context],
+                     workLoadList: Array[Float],
+                     fixedParamNames: Set[String]) : Module = {
+    org.apache.mxnet.module.Module.loadCheckpoint(prefix, epoch, loadOptimizerStates,
+      dataNames, labelNames, contexts.map(Context.toContext), Some(workLoadList), Some(fixedParamNames))
+  }
+
+  class Builder(private val modelDef : Symbol) {
+    private val builder : org.apache.mxnet.module.Module.Builder
+    = new org.apache.mxnet.module.Module.Builder(modelDef)
+
+    def setContext(ctx : Array[Context]) : Builder = {
+      builder.setContext(ctx.map(Context.toContext) : _*)
+      this
+    }
+
+    def setDataNames(name : Array[String]) : Builder = {
+      builder.setDataNames(name : _*)
+      this
+    }
+
+    def setLabelNames(name : Array[String]) : Builder = {
+      builder.setLabelNames(name : _*)
+      this
+    }
+
+    def setWorkLoadList(workloads : Array[Float]) : Builder = {
+      builder.setWorkLoadList(workloads : _*)
+      this
+    }
+
+    def setFixedParamNames(name : Array[String]) : Builder = {
+      builder.setFixedParamNames(name : _*)
+      this
+    }
+
+    def build() : Module = {
+      builder.build()
+    }
+  }
+
+}
diff --git a/scala-package/core/src/main/scala/org/apache/mxnet/javaapi/Symbol.scala b/scala-package/core/src/main/scala/org/apache/mxnet/javaapi/Symbol.scala
new file mode 100644
index 00000000000..5d7eb914d4b
--- /dev/null
+++ b/scala-package/core/src/main/scala/org/apache/mxnet/javaapi/Symbol.scala
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mxnet.javaapi
+
+class Symbol(val symbol: org.apache.mxnet.Symbol) {
+  // TODO: Enable full feature of Symbol
+}
+
+object Symbol {
+  implicit def toSymbol(symbol : org.apache.mxnet.Symbol) : Symbol = {
+    new Symbol(symbol)
+  }
+
+  implicit def fromSymbol(symbol : Symbol) : org.apache.mxnet.Symbol = {
+    symbol.symbol
+  }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services