yzhliu commented on a change in pull request #11126: [MXNET-386] ongoing 
maintenance on NDArray
URL: https://github.com/apache/incubator-mxnet/pull/11126#discussion_r192964239
 
 

 ##########
 File path: scala-package/core/src/main/scala/org/apache/mxnet/NDArray.scala
 ##########
 @@ -105,6 +105,57 @@ object NDArray {
     })
   }
 
+  /**
+    * Used by NDArrayMacro for New Scala NDArray API.
+    * Invoke this function by passing in parameters.
+    * Parameters
+    * ----------
+    * @param kwargs Key-value arguments of input scalars
+    * @return The result NDArrays of result of computation.
+    */
+  private[mxnet] def genericNewAPINDArrayFunctionInvoke(
+    funcName: String, kwargs: Map[String, Any] = null) : NDArrayFuncReturn = {
+    val function = functions(funcName)
+    val ndArgs = ArrayBuffer.empty[NDArray]
+    val updatedKwargs: Map[String, String] =
+      Option(kwargs).getOrElse(Map.empty[String, String]).filter{ case (key, 
value) =>
+        !value.isInstanceOf[NDArray] && !value.isInstanceOf[NDArrayFuncReturn]
+      } .map { case (k, v) => k -> v.toString }
+
+    Option(kwargs).getOrElse(Map.empty[String, String]).filter{ case (key, 
value) =>
+          value.isInstanceOf[NDArray] || value.isInstanceOf[NDArrayFuncReturn]
+        } .filter{ case (key, value) => key != "out"}.foreach{
+      case (key, value) => value match {
+        case nd : NDArray =>
+          ndArgs.append(nd.asInstanceOf[NDArray])
+        case arrFunRet: NDArrayFuncReturn =>
+          
arrFunRet.asInstanceOf[NDArrayFuncReturn].arr.foreach(ndArgs.append(_))
+      }
+    }
 
 Review comment:
   If I didn't misunderstand, for `foo(arg1=ndarr1, arg2=ndarr2)`, macro 
generates a function call `genericNDArrayFunctionInvoke("foo", args=null, 
kwargs=Map("arg1"->ndarr1, "arg2"->ndarr2))`, then you assume  when iterating 
over `kwargs`, it is in the same order as you inserted into the Map, i.e., 
"arg1", "arg2" in this case - This is not correct:
   ```scala
   scala> val map = scala.collection.mutable.Map[String, Any]()
   map: scala.collection.mutable.Map[String,Any] = Map()
   scala> map("hello") = 1
   scala> map("apple")=2
   scala> map
   res2: scala.collection.mutable.Map[String,Any] = Map(apple -> 2, hello -> 1)
   scala> map.toMap.foreach { case (k, v) => println(k) }
   apple
   hello
   ```
   Can we simply create `ndArgs` in macros, and leave this function unchanged?

----------------------------------------------------------------
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

Reply via email to