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/13 19:39:57 UTC

[GitHub] andrewfayres commented on a change in pull request #13242: [MXNET-918] [Introduce Random module / Refact code generation (#13038)][Cherry pick]

andrewfayres commented on a change in pull request #13242: [MXNET-918] [Introduce Random module / Refact code generation (#13038)][Cherry pick] 
URL: https://github.com/apache/incubator-mxnet/pull/13242#discussion_r233191456
 
 

 ##########
 File path: scala-package/macros/src/main/scala/org/apache/mxnet/APIDocGenerator.scala
 ##########
 @@ -17,196 +17,147 @@
 
 package org.apache.mxnet
 
-import org.apache.mxnet.init.Base._
-import org.apache.mxnet.utils.CToScalaUtils
 import java.io._
 import java.security.MessageDigest
 
-import scala.collection.mutable.{ArrayBuffer, ListBuffer}
+import scala.collection.mutable.ListBuffer
 
 /**
   * This object will generate the Scala documentation of the new Scala API
   * Two file namely: SymbolAPIBase.scala and NDArrayAPIBase.scala
   * The code will be executed during Macros stage and file live in Core stage
   */
-private[mxnet] object APIDocGenerator{
-  case class absClassArg(argName : String, argType : String, argDesc : String, isOptional : Boolean)
-  case class absClassFunction(name : String, desc : String,
-                           listOfArgs: List[absClassArg], returnType : String)
+private[mxnet] object APIDocGenerator extends GeneratorBase {
 
-
-  def main(args: Array[String]) : Unit = {
+  def main(args: Array[String]): Unit = {
     val FILE_PATH = args(0)
     val hashCollector = ListBuffer[String]()
-    hashCollector += absClassGen(FILE_PATH, true)
-    hashCollector += absClassGen(FILE_PATH, false)
+    hashCollector += typeSafeClassGen(FILE_PATH, true)
+    hashCollector += typeSafeClassGen(FILE_PATH, false)
     hashCollector += nonTypeSafeClassGen(FILE_PATH, true)
     hashCollector += nonTypeSafeClassGen(FILE_PATH, false)
-    // Generate Java API documentation
-    hashCollector += javaClassGen(FILE_PATH + "javaapi/")
+    hashCollector += javaClassGen(FILE_PATH)
     val finalHash = hashCollector.mkString("\n")
   }
 
-  def MD5Generator(input : String) : String = {
+  def MD5Generator(input: String): String = {
     val md = MessageDigest.getInstance("MD5")
     md.update(input.getBytes("UTF-8"))
     val digest = md.digest()
     org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(digest)
   }
 
-  def fileGen(filePath : String, packageName : String, packageDef : String,
-              absFuncs : List[String]) : String = {
-    val apacheLicense =
-      """/*
-        |* 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.
-        |*/
-        |""".stripMargin
-    val scalaStyle = "// scalastyle:off"
-    val imports = "import org.apache.mxnet.annotation.Experimental"
-    val absClassDef = s"abstract class $packageName"
+  def typeSafeClassGen(FILE_PATH: String, isSymbol: Boolean): String = {
+    val generated = typeSafeFunctionsToGenerate(isSymbol, isContrib = false)
+      .map { func =>
+        val scalaDoc = generateAPIDocFromBackend(func)
+        val decl = generateAPISignature(func, isSymbol)
+        s"$scalaDoc\n$decl"
+      }
 
-    val finalStr =
-      s"""$apacheLicense
-         |$scalaStyle
-         |$packageDef
-         |$imports
-         |$absClassDef {
-         |${absFuncs.mkString("\n")}
-         |}""".stripMargin
-    val pw = new PrintWriter(new File(filePath + s"$packageName.scala"))
-    pw.write(finalStr)
-    pw.close()
-    MD5Generator(finalStr)
+    writeFile(
+      FILE_PATH,
+      if (isSymbol) "SymbolAPIBase" else "NDArrayAPIBase",
+      "package org.apache.mxnet",
+      generated)
   }
 
-  def absClassGen(filePath : String, isSymbol : Boolean) : String = {
-    val absClassFunctions = getSymbolNDArrayMethods(isSymbol)
-    // Defines Operators that should not generated
-    val notGenerated = Set("Custom")
-    // TODO: Add Filter to the same location in case of refactor
-    val absFuncs = absClassFunctions.filterNot(_.name.startsWith("_"))
-      .filterNot(ele => notGenerated.contains(ele.name))
-      .map(absClassFunction => {
-      val scalaDoc = generateAPIDocFromBackend(absClassFunction)
-      val defBody = generateAPISignature(absClassFunction, isSymbol)
-      s"$scalaDoc\n$defBody"
-    })
-    val packageName = if (isSymbol) "SymbolAPIBase" else "NDArrayAPIBase"
-    val packageDef = "package org.apache.mxnet"
-    fileGen(filePath, packageName, packageDef, absFuncs)
+  def nonTypeSafeClassGen(FILE_PATH: String, isSymbol: Boolean): String = {
+    val absFuncs = functionsToGenerate(isSymbol, isContrib = false)
+      .map { func =>
+        val scalaDoc = generateAPIDocFromBackend(func, false)
+        if (isSymbol) {
+          s"""$scalaDoc
+             |def ${func.name}(name : String = null, attr : Map[String, String] = null)
+             |    (args : org.apache.mxnet.Symbol*)(kwargs : Map[String, Any] = null):
+             |    org.apache.mxnet.Symbol
+           """.stripMargin
+        } else {
+          s"""$scalaDoc
+             |def ${func.name}(kwargs: Map[String, Any] = null)
+             |    (args: Any*): org.apache.mxnet.NDArrayFuncReturn
+             |
+             |$scalaDoc
+             |def ${func.name}(args: Any*): org.apache.mxnet.NDArrayFuncReturn
+           """.stripMargin
+        }
+      }
+
+    writeFile(
+      FILE_PATH,
+      if (isSymbol) "SymbolBase" else "NDArrayBase",
+      "package org.apache.mxnet",
+      absFuncs)
   }
 
   def javaClassGen(filePath : String) : String = {
     val notGenerated = Set("Custom")
-    val absClassFunctions = getSymbolNDArrayMethods(false, true)
-    // TODO: Add Filter to the same location in case of refactor
-    val absFuncs = absClassFunctions.filterNot(_.name.startsWith("_"))
-      .filterNot(ele => notGenerated.contains(ele.name))
-      .map(absClassFunction => {
+    val absClassFunctions = functionsToGenerate(false, false, true)
+    val absFuncs = absClassFunctions.filterNot(ele => notGenerated.contains(ele.name))
+      .groupBy(_.name.toLowerCase).map(ele => {
+      // Pattern matching for not generating depreciated method
 
 Review comment:
   I'm not crazy about this logic but based on how you've explained the problem to me I don't really know what else we can do here. Please add more detail in this comment explaining that we sometimes get two methods with the same name but capitalization differences and the only existing mechanism for to determine which one has been deprecated is the capitalization.

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