[GitHub] lanking520 commented on a change in pull request #11477: Copy profiler examples to new location for CI

2018-06-28 Thread GitBox
lanking520 commented on a change in pull request #11477: Copy profiler examples 
to new location for CI
URL: https://github.com/apache/incubator-mxnet/pull/11477#discussion_r199023063
 
 

 ##
 File path: 
scala-package/examples/src/main/scala/org/apache/mxnet/examples/profiler/ProfilerNDArray.scala
 ##
 @@ -0,0 +1,252 @@
+/*
+ * 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.examples.profiler
+
+import org.kohsuke.args4j.{CmdLineParser, Option}
+import org.slf4j.LoggerFactory
+import scala.collection.JavaConverters._
+import java.io.File
+import org.apache.mxnet.Profiler
+import org.apache.mxnet.Random
+import org.apache.mxnet.Shape
+import org.apache.mxnet.NDArray
+import org.apache.mxnet.Context
+
+/**
+ * @author Depeng Liang
+ */
+object ProfilerNDArray {
+  private val logger = LoggerFactory.getLogger(classOf[ProfilerNDArray])
+
+  def testBroadcast(): Unit = {
+val sampleNum = 1000
+def testBroadcastTo(): Unit = {
+  for (i <- 0 until sampleNum) {
+val nDim = scala.util.Random.nextInt(2) + 1
+val targetShape = Shape((0 until nDim).map(i => 
scala.util.Random.nextInt(10) + 1))
+val shape = targetShape.toArray.map { s =>
+if (scala.util.Random.nextInt(2) == 1) 1
+else s
+}
+val dat = NDArray.empty(shape: _*)
+val randomRet = (0 until shape.product)
+  .map(r => scala.util.Random.nextFloat() - 0.5f).toArray
+dat.set(randomRet)
+val ndArrayRet = NDArray.broadcast_to(Map("shape" -> 
targetShape))(dat).get
+require(ndArrayRet.shape == targetShape)
+val err = {
+  // implementation of broadcast
+  val ret = {
+(randomRet /: shape.zipWithIndex.reverse){ (acc, elem) => elem 
match { case (s, i) =>
+  if (s != targetShape(i)) {
+acc.grouped(shape.takeRight(shape.length - i).product).map {g 
=>
+  (0 until targetShape(i)).map(x => g).flatten
+}.flatten.toArray
+  } else acc
+}}
+  }
+  val tmp = ndArrayRet.toArray.zip(ret).map{ case (l, r) => Math.pow(l 
- r, 2) }
+  tmp.sum / tmp.length
+}
+require(err < 1E-8)
+ndArrayRet.dispose()
+dat.dispose()
+  }
+}
+testBroadcastTo()
+  }
+
+  def randomNDArray(dim: Int): NDArray = {
+val tmp = Math.pow(1000, 1.0 / dim).toInt
+val shape = Shape((0 until dim).map(d => scala.util.Random.nextInt(tmp) + 
1))
+Random.uniform(-10f, 10f, shape)
+  }
+
+  def testNDArraySaveload(): Unit = {
+val maxDim = 5
+val nRepeat = 10
+val fileName = s"${System.getProperty("java.io.tmpdir")}/tmpList.bin"
+for (repeat <- 0 until nRepeat) {
+  try {
+val data = (0 until 10).map(i => 
randomNDArray(scala.util.Random.nextInt(4) + 1))
+NDArray.save(fileName, data)
+val data2 = NDArray.load2Array(fileName)
+require(data.length == data2.length)
+for ((x, y) <- data.zip(data2)) {
+  val tmp = x - y
+  require(tmp.toArray.sum == 0)
+  tmp.dispose()
+}
+val dMap = data.zipWithIndex.map { case (arr, i) =>
+  s"NDArray xx $i" -> arr
+}.toMap
+NDArray.save(fileName, dMap)
+ val dMap2 = NDArray.load2Map(fileName)
+ require(dMap.size == dMap2.size)
+ for ((k, x) <- dMap) {
+   val y = dMap2(k)
+   val tmp = x - y
+   require(tmp.toArray.sum == 0)
+   tmp.dispose()
+ }
+data.foreach(_.dispose())
+  } finally {
+val file = new File(fileName)
+file.delete()
+  }
+}
+  }
+
+  def testNDArrayCopy(): Unit = {
+val c = Random.uniform(-10f, 10f, Shape(10, 10))
+val d = c.copyTo(Context.cpu(0))
+val tmp = c - d
+require(tmp.toArray.map(Math.abs).sum == 0)
+c.dispose()
+d.dispose()
+  }
+
+  def reldiff(a: NDArray, b: NDArray): Float = {
+val diff = NDArray.sum(NDArray.abs(a - b)).toScalar
+val norm = NDArray.sum(NDArray.abs(a)).toScalar
 
 Review comment:
   Same in here with `NDArray.api`


This 

[GitHub] lanking520 commented on a change in pull request #11477: Copy profiler examples to new location for CI

2018-06-28 Thread GitBox
lanking520 commented on a change in pull request #11477: Copy profiler examples 
to new location for CI
URL: https://github.com/apache/incubator-mxnet/pull/11477#discussion_r199022241
 
 

 ##
 File path: 
scala-package/examples/src/main/scala/org/apache/mxnet/examples/profiler/ProfilerNDArray.scala
 ##
 @@ -0,0 +1,252 @@
+/*
+ * 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.examples.profiler
+
+import org.kohsuke.args4j.{CmdLineParser, Option}
+import org.slf4j.LoggerFactory
+import scala.collection.JavaConverters._
+import java.io.File
+import org.apache.mxnet.Profiler
+import org.apache.mxnet.Random
+import org.apache.mxnet.Shape
+import org.apache.mxnet.NDArray
+import org.apache.mxnet.Context
+
+/**
+ * @author Depeng Liang
+ */
+object ProfilerNDArray {
+  private val logger = LoggerFactory.getLogger(classOf[ProfilerNDArray])
+
+  def testBroadcast(): Unit = {
+val sampleNum = 1000
+def testBroadcastTo(): Unit = {
+  for (i <- 0 until sampleNum) {
+val nDim = scala.util.Random.nextInt(2) + 1
+val targetShape = Shape((0 until nDim).map(i => 
scala.util.Random.nextInt(10) + 1))
+val shape = targetShape.toArray.map { s =>
+if (scala.util.Random.nextInt(2) == 1) 1
+else s
+}
+val dat = NDArray.empty(shape: _*)
+val randomRet = (0 until shape.product)
+  .map(r => scala.util.Random.nextFloat() - 0.5f).toArray
+dat.set(randomRet)
+val ndArrayRet = NDArray.broadcast_to(Map("shape" -> 
targetShape))(dat).get
 
 Review comment:
   Can you try to use NDArray.api.broadcast_to in here?


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] lanking520 commented on a change in pull request #11477: Copy profiler examples to new location for CI

2018-06-28 Thread GitBox
lanking520 commented on a change in pull request #11477: Copy profiler examples 
to new location for CI
URL: https://github.com/apache/incubator-mxnet/pull/11477#discussion_r199023103
 
 

 ##
 File path: 
scala-package/examples/src/main/scala/org/apache/mxnet/examples/profiler/ProfilerNDArray.scala
 ##
 @@ -0,0 +1,252 @@
+/*
+ * 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.examples.profiler
+
+import org.kohsuke.args4j.{CmdLineParser, Option}
+import org.slf4j.LoggerFactory
+import scala.collection.JavaConverters._
+import java.io.File
+import org.apache.mxnet.Profiler
+import org.apache.mxnet.Random
+import org.apache.mxnet.Shape
+import org.apache.mxnet.NDArray
+import org.apache.mxnet.Context
+
+/**
+ * @author Depeng Liang
+ */
+object ProfilerNDArray {
+  private val logger = LoggerFactory.getLogger(classOf[ProfilerNDArray])
+
+  def testBroadcast(): Unit = {
+val sampleNum = 1000
+def testBroadcastTo(): Unit = {
+  for (i <- 0 until sampleNum) {
+val nDim = scala.util.Random.nextInt(2) + 1
+val targetShape = Shape((0 until nDim).map(i => 
scala.util.Random.nextInt(10) + 1))
+val shape = targetShape.toArray.map { s =>
+if (scala.util.Random.nextInt(2) == 1) 1
+else s
+}
+val dat = NDArray.empty(shape: _*)
+val randomRet = (0 until shape.product)
+  .map(r => scala.util.Random.nextFloat() - 0.5f).toArray
+dat.set(randomRet)
+val ndArrayRet = NDArray.broadcast_to(Map("shape" -> 
targetShape))(dat).get
+require(ndArrayRet.shape == targetShape)
+val err = {
+  // implementation of broadcast
+  val ret = {
+(randomRet /: shape.zipWithIndex.reverse){ (acc, elem) => elem 
match { case (s, i) =>
+  if (s != targetShape(i)) {
+acc.grouped(shape.takeRight(shape.length - i).product).map {g 
=>
+  (0 until targetShape(i)).map(x => g).flatten
+}.flatten.toArray
+  } else acc
+}}
+  }
+  val tmp = ndArrayRet.toArray.zip(ret).map{ case (l, r) => Math.pow(l 
- r, 2) }
+  tmp.sum / tmp.length
+}
+require(err < 1E-8)
+ndArrayRet.dispose()
+dat.dispose()
+  }
+}
+testBroadcastTo()
+  }
+
+  def randomNDArray(dim: Int): NDArray = {
+val tmp = Math.pow(1000, 1.0 / dim).toInt
+val shape = Shape((0 until dim).map(d => scala.util.Random.nextInt(tmp) + 
1))
+Random.uniform(-10f, 10f, shape)
+  }
+
+  def testNDArraySaveload(): Unit = {
+val maxDim = 5
+val nRepeat = 10
+val fileName = s"${System.getProperty("java.io.tmpdir")}/tmpList.bin"
+for (repeat <- 0 until nRepeat) {
+  try {
+val data = (0 until 10).map(i => 
randomNDArray(scala.util.Random.nextInt(4) + 1))
+NDArray.save(fileName, data)
+val data2 = NDArray.load2Array(fileName)
+require(data.length == data2.length)
+for ((x, y) <- data.zip(data2)) {
+  val tmp = x - y
+  require(tmp.toArray.sum == 0)
+  tmp.dispose()
+}
+val dMap = data.zipWithIndex.map { case (arr, i) =>
+  s"NDArray xx $i" -> arr
+}.toMap
+NDArray.save(fileName, dMap)
+ val dMap2 = NDArray.load2Map(fileName)
+ require(dMap.size == dMap2.size)
+ for ((k, x) <- dMap) {
+   val y = dMap2(k)
+   val tmp = x - y
+   require(tmp.toArray.sum == 0)
+   tmp.dispose()
+ }
+data.foreach(_.dispose())
+  } finally {
+val file = new File(fileName)
+file.delete()
+  }
+}
+  }
+
+  def testNDArrayCopy(): Unit = {
+val c = Random.uniform(-10f, 10f, Shape(10, 10))
+val d = c.copyTo(Context.cpu(0))
+val tmp = c - d
+require(tmp.toArray.map(Math.abs).sum == 0)
+c.dispose()
+d.dispose()
+  }
+
+  def reldiff(a: NDArray, b: NDArray): Float = {
+val diff = NDArray.sum(NDArray.abs(a - b)).toScalar
+val norm = NDArray.sum(NDArray.abs(a)).toScalar
+diff / norm
+  }
+
+  def reldiff(a: Array[Float], b: Array[Float]): Float = {
+val diff =
+  (a zip b).map { 

[GitHub] lanking520 commented on a change in pull request #11477: Copy profiler examples to new location for CI

2018-06-28 Thread GitBox
lanking520 commented on a change in pull request #11477: Copy profiler examples 
to new location for CI
URL: https://github.com/apache/incubator-mxnet/pull/11477#discussion_r199023126
 
 

 ##
 File path: 
scala-package/examples/src/main/scala/org/apache/mxnet/examples/profiler/ProfilerNDArray.scala
 ##
 @@ -0,0 +1,252 @@
+/*
+ * 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.examples.profiler
+
+import org.kohsuke.args4j.{CmdLineParser, Option}
+import org.slf4j.LoggerFactory
+import scala.collection.JavaConverters._
+import java.io.File
+import org.apache.mxnet.Profiler
+import org.apache.mxnet.Random
+import org.apache.mxnet.Shape
+import org.apache.mxnet.NDArray
+import org.apache.mxnet.Context
+
+/**
+ * @author Depeng Liang
+ */
+object ProfilerNDArray {
+  private val logger = LoggerFactory.getLogger(classOf[ProfilerNDArray])
+
+  def testBroadcast(): Unit = {
+val sampleNum = 1000
+def testBroadcastTo(): Unit = {
+  for (i <- 0 until sampleNum) {
+val nDim = scala.util.Random.nextInt(2) + 1
+val targetShape = Shape((0 until nDim).map(i => 
scala.util.Random.nextInt(10) + 1))
+val shape = targetShape.toArray.map { s =>
+if (scala.util.Random.nextInt(2) == 1) 1
+else s
+}
+val dat = NDArray.empty(shape: _*)
+val randomRet = (0 until shape.product)
+  .map(r => scala.util.Random.nextFloat() - 0.5f).toArray
+dat.set(randomRet)
+val ndArrayRet = NDArray.broadcast_to(Map("shape" -> 
targetShape))(dat).get
+require(ndArrayRet.shape == targetShape)
+val err = {
+  // implementation of broadcast
+  val ret = {
+(randomRet /: shape.zipWithIndex.reverse){ (acc, elem) => elem 
match { case (s, i) =>
+  if (s != targetShape(i)) {
+acc.grouped(shape.takeRight(shape.length - i).product).map {g 
=>
+  (0 until targetShape(i)).map(x => g).flatten
+}.flatten.toArray
+  } else acc
+}}
+  }
+  val tmp = ndArrayRet.toArray.zip(ret).map{ case (l, r) => Math.pow(l 
- r, 2) }
+  tmp.sum / tmp.length
+}
+require(err < 1E-8)
+ndArrayRet.dispose()
+dat.dispose()
+  }
+}
+testBroadcastTo()
+  }
+
+  def randomNDArray(dim: Int): NDArray = {
+val tmp = Math.pow(1000, 1.0 / dim).toInt
+val shape = Shape((0 until dim).map(d => scala.util.Random.nextInt(tmp) + 
1))
+Random.uniform(-10f, 10f, shape)
+  }
+
+  def testNDArraySaveload(): Unit = {
+val maxDim = 5
+val nRepeat = 10
+val fileName = s"${System.getProperty("java.io.tmpdir")}/tmpList.bin"
+for (repeat <- 0 until nRepeat) {
+  try {
+val data = (0 until 10).map(i => 
randomNDArray(scala.util.Random.nextInt(4) + 1))
+NDArray.save(fileName, data)
+val data2 = NDArray.load2Array(fileName)
+require(data.length == data2.length)
+for ((x, y) <- data.zip(data2)) {
+  val tmp = x - y
+  require(tmp.toArray.sum == 0)
+  tmp.dispose()
+}
+val dMap = data.zipWithIndex.map { case (arr, i) =>
+  s"NDArray xx $i" -> arr
+}.toMap
+NDArray.save(fileName, dMap)
+ val dMap2 = NDArray.load2Map(fileName)
+ require(dMap.size == dMap2.size)
+ for ((k, x) <- dMap) {
+   val y = dMap2(k)
+   val tmp = x - y
+   require(tmp.toArray.sum == 0)
+   tmp.dispose()
+ }
+data.foreach(_.dispose())
+  } finally {
+val file = new File(fileName)
+file.delete()
+  }
+}
+  }
+
+  def testNDArrayCopy(): Unit = {
+val c = Random.uniform(-10f, 10f, Shape(10, 10))
+val d = c.copyTo(Context.cpu(0))
+val tmp = c - d
+require(tmp.toArray.map(Math.abs).sum == 0)
+c.dispose()
+d.dispose()
+  }
+
+  def reldiff(a: NDArray, b: NDArray): Float = {
+val diff = NDArray.sum(NDArray.abs(a - b)).toScalar
+val norm = NDArray.sum(NDArray.abs(a)).toScalar
+diff / norm
+  }
+
+  def reldiff(a: Array[Float], b: Array[Float]): Float = {
+val diff =
+  (a zip b).map { 

[GitHub] lanking520 commented on a change in pull request #11477: Copy profiler examples to new location for CI

2018-06-28 Thread GitBox
lanking520 commented on a change in pull request #11477: Copy profiler examples 
to new location for CI
URL: https://github.com/apache/incubator-mxnet/pull/11477#discussion_r199022352
 
 

 ##
 File path: 
scala-package/examples/src/main/scala/org/apache/mxnet/examples/profiler/ProfilerMatMul.scala
 ##
 @@ -0,0 +1,107 @@
+/*
+ * 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.examples.profiler
+
+import org.kohsuke.args4j.{CmdLineParser, Option}
+import org.slf4j.LoggerFactory
+import scala.collection.JavaConverters._
+import org.apache.mxnet.Context
+import org.apache.mxnet.Profiler
+import java.io.File
+import org.apache.mxnet.Symbol
+import org.apache.mxnet.Shape
+import org.apache.mxnet.Random
+
+/**
+ * @author Depeng Liang
+ */
+object ProfilerMatMul {
+  private val logger = LoggerFactory.getLogger(classOf[ProfilerMatMul])
+
+  def main(args: Array[String]): Unit = {
+val erul = new ProfilerMatMul
+val parser: CmdLineParser = new CmdLineParser(erul)
+try {
+  parser.parseArgument(args.toList.asJava)
+  val ctx = if (erul.gpu >= 0) Context.gpu(erul.gpu) else Context.cpu()
+
+  val path = s"${erul.outputPath}${File.separator}${erul.profilerName}"
+  val kwargs = Map("file_name" -> path, "profile_" + erul.profilerMode -> 
"1")
+  Profiler.profilerSetConfig(kwargs)
+  logger.info(s"profile file save to $path")
+
+  val A = Symbol.Variable("A")
+  val B = Symbol.Variable("B")
+  val C = Symbol.dot()(A, B)()
 
 Review comment:
   Please use `Symbol.api.dot()`


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:
us...@infra.apache.org


With regards,
Apache Git Services