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/10/31 05:53:02 UTC

[GitHub] lanking520 commented on a change in pull request #13038: [MXNET-918] Introduce Random module / Refact code generation

lanking520 commented on a change in pull request #13038: [MXNET-918] Introduce Random module / Refact code generation
URL: https://github.com/apache/incubator-mxnet/pull/13038#discussion_r229569935
 
 

 ##########
 File path: scala-package/macros/src/main/scala/org/apache/mxnet/GeneratorBase.scala
 ##########
 @@ -0,0 +1,162 @@
+/*
+ * 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
+
+import org.apache.mxnet.init.Base.{RefInt, RefLong, RefString, _LIB}
+import org.apache.mxnet.utils.{CToScalaUtils, OperatorBuildUtils}
+
+import scala.collection.mutable.ListBuffer
+import scala.reflect.macros.blackbox
+
+abstract class GeneratorBase {
+  type Handle = Long
+
+  case class Arg(argName: String, argType: String, argDesc: String, isOptional: Boolean) {
+    def safeArgName: String = argName match {
+      case "var" => "vari"
+      case "type" => "typeOf"
+      case _ => argName
+    }
+  }
+
+  case class Func(name: String, desc: String, listOfArgs: List[Arg], returnType: String)
+
+  def functionsToGenerate(isSymbol: Boolean, isContrib: Boolean): List[Func] = {
+    val l = getBackEndFunctions(isSymbol)
+    if (isContrib) {
+      l.filter(func => func.name.startsWith("_contrib_") || !func.name.startsWith("_"))
+    } else {
+      l.filterNot(_.name.startsWith("_"))
+    }
+  }
+
+  def typeSafeFunctionsToGenerate(isSymbol: Boolean, isContrib: Boolean): List[Func] = {
+    // Operators that should not be generated
+    val notGenerated = Set("Custom")
+
+    val l = getBackEndFunctions(isSymbol)
+    val res = if (isContrib) {
+      l.filter(func => func.name.startsWith("_contrib_") || !func.name.startsWith("_"))
+    } else {
+      l.filterNot(_.name.startsWith("_"))
+    }
+    res.filterNot(ele => notGenerated.contains(ele.name))
+  }
+
+  protected def getBackEndFunctions(isSymbol: Boolean): List[Func] = {
+    val opNames = ListBuffer.empty[String]
+    _LIB.mxListAllOpNames(opNames)
+    opNames.map(opName => {
+      val opHandle = new RefLong
+      _LIB.nnGetOpHandle(opName, opHandle)
+      makeAtomicFunction(opHandle.value, opName, isSymbol)
+    }).toList
+  }
+
+  private def makeAtomicFunction(handle: Handle, aliasName: String, isSymbol: Boolean): Func = {
+    val name = new RefString
+    val desc = new RefString
+    val keyVarNumArgs = new RefString
+    val numArgs = new RefInt
+    val argNames = ListBuffer.empty[String]
+    val argTypes = ListBuffer.empty[String]
+    val argDescs = ListBuffer.empty[String]
+
+    _LIB.mxSymbolGetAtomicSymbolInfo(
+      handle, name, desc, numArgs, argNames, argTypes, argDescs, keyVarNumArgs)
+    val paramStr = OperatorBuildUtils.ctypes2docstring(argNames, argTypes, argDescs)
+    val extraDoc: String = if (keyVarNumArgs.value != null && keyVarNumArgs.value.length > 0) {
+      s"This function support variable length of positional input (${keyVarNumArgs.value})."
+    } else {
+      ""
+    }
+    val realName = if (aliasName == name.value) "" else s"(a.k.a., ${name.value})"
+    val docStr = s"$aliasName $realName\n${desc.value}\n\n$paramStr\n$extraDoc\n"
+    // scalastyle:off println
+    if (System.getenv("MXNET4J_PRINT_OP_DEF") != null
+      && System.getenv("MXNET4J_PRINT_OP_DEF").toLowerCase == "true") {
+      println("Function definition:\n" + docStr)
+    }
 
 Review comment:
   I think we can remove 88-94

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