nswamy closed pull request #11009: [MXNET-434] update scala api to properly track reshaped size URL: https://github.com/apache/incubator-mxnet/pull/11009
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/CONTRIBUTORS.md b/CONTRIBUTORS.md index 4bfafb60cba..f1ab129288a 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -170,4 +170,5 @@ List of Contributors * [Sina Afrooze](https://github.com/safrooze) * [Sergey Sokolov](https://github.com/Ishitori) * [Thomas Delteil](https://github.com/ThomasDelteil) +* [Jesse Brizzi](https://github.com/jessebrizzi) * [Hang Zhang](http://hangzh.com) diff --git a/scala-package/core/src/main/scala/org/apache/mxnet/module/DataParallelExecutorGroup.scala b/scala-package/core/src/main/scala/org/apache/mxnet/module/DataParallelExecutorGroup.scala index c13ebcd6260..1494dc84035 100644 --- a/scala-package/core/src/main/scala/org/apache/mxnet/module/DataParallelExecutorGroup.scala +++ b/scala-package/core/src/main/scala/org/apache/mxnet/module/DataParallelExecutorGroup.scala @@ -266,8 +266,8 @@ class DataParallelExecutorGroup private[module]( symbol: Symbol, contexts: Array[Context], workLoadList: IndexedSeq[Float], - dataShapes: IndexedSeq[DataDesc], - labelShapes: Option[IndexedSeq[DataDesc]] = None, + var dataShapes: IndexedSeq[DataDesc], + var labelShapes: Option[IndexedSeq[DataDesc]] = None, private[module] val paramNames: IndexedSeq[String], forTraining: Boolean, inputsNeedGrad: Boolean, @@ -356,7 +356,7 @@ class DataParallelExecutorGroup private[module]( * @param sharedGroup * @param reshape */ - def bindExec(dataShapes: Seq[DataDesc], labelShapes: Option[Seq[DataDesc]], + def bindExec(dataShapes: IndexedSeq[DataDesc], labelShapes: Option[IndexedSeq[DataDesc]], sharedGroup: Option[DataParallelExecutorGroup], reshape: Boolean = false): Unit = { this.batchSize = -1 dataLayouts = decideSlices(dataShapes) @@ -379,6 +379,9 @@ class DataParallelExecutorGroup private[module]( ).toArray } + this.dataShapes = dataShapes + this.labelShapes = labelShapes + // convenient data structures dataArrays = dataShapes.map(dataDesc => this.execs.zipWithIndex.map { case (e, i) => (this.slices(i), e.argDict(dataDesc.name)) } @@ -427,7 +430,7 @@ class DataParallelExecutorGroup private[module]( * @param dataShapes * @param labelShapes */ - def reshape(dataShapes: Seq[DataDesc], labelShapes: Option[Seq[DataDesc]]): Unit = { + def reshape(dataShapes: IndexedSeq[DataDesc], labelShapes: Option[IndexedSeq[DataDesc]]): Unit = { if (!(dataShapes == this.dataShapes && labelShapes == this.labelShapes)) { if (this._defaultExecs == null) { this._defaultExecs = this.execs.map(x => x) diff --git a/scala-package/core/src/main/scala/org/apache/mxnet/module/Module.scala b/scala-package/core/src/main/scala/org/apache/mxnet/module/Module.scala index d55a42653ce..9cf64b19a6a 100644 --- a/scala-package/core/src/main/scala/org/apache/mxnet/module/Module.scala +++ b/scala-package/core/src/main/scala/org/apache/mxnet/module/Module.scala @@ -341,6 +341,8 @@ class Module(symbolVar: Symbol, require(this.binded) val (tdataShapes, tlabelShapes) = this._parseDataDesc( this.dataNames, this.labelNames, dataShapes, labelShapes) + this.dataShapesVar = tdataShapes + this.labelShapesVar = tlabelShapes this.execGroup.reshape(tdataShapes, tlabelShapes) } diff --git a/scala-package/core/src/test/scala/org/apache/mxnet/ModuleSuite.scala b/scala-package/core/src/test/scala/org/apache/mxnet/ModuleSuite.scala index 22b9c3bdaf3..8234568d7d9 100644 --- a/scala-package/core/src/test/scala/org/apache/mxnet/ModuleSuite.scala +++ b/scala-package/core/src/test/scala/org/apache/mxnet/ModuleSuite.scala @@ -157,6 +157,7 @@ class ModuleSuite extends FunSuite with BeforeAndAfterAll { assert(mod.getOutputsMerged()(0).shape == dShape) assert(mod.getParams._1("fc_bias").toArray.forall(_ == -1f)) + // reshape module dShape = Shape(14, 20) mod.reshape(IndexedSeq(DataDesc("data", dShape, layout = "NT"))) mod.forward(new DataBatch( @@ -166,6 +167,17 @@ class ModuleSuite extends FunSuite with BeforeAndAfterAll { mod.update() assert(mod.getOutputsMerged()(0).shape == dShape) assert(mod.getParams._1("fc_bias").toArray.forall(x => (x - -3f) < 1e-3)) + + // return to original binded shape + dShape = Shape(7, 20) + mod.reshape(IndexedSeq(DataDesc("data", dShape, layout = "NT"))) + mod.forward(new DataBatch( + data = IndexedSeq(NDArray.ones(dShape)), + label = null, index = null, pad = 0)) + mod.backward(Array(NDArray.ones(dShape))) + mod.update() + assert(mod.getOutputsMerged()(0).shape == dShape) + assert(mod.getParams._1("fc_bias").toArray.forall(x => (x - -3f) < 1e-3)) } test ("module setParams") { ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
