Sfoglia il codice sorgente

Format mxnet-operator.json

Lutz Roeder 8 anni fa
parent
commit
7ecf78211d
5 ha cambiato i file con 306 aggiunte e 77 eliminazioni
  1. 39 0
      src/mxnet-model.js
  2. 218 71
      src/mxnet-operator.json
  3. 5 6
      tools/keras-generate
  4. 3 0
      tools/mxnet-generate
  5. 41 0
      tools/mxnet-operator-json.py

+ 39 - 0
src/mxnet-model.js

@@ -198,6 +198,10 @@ class MXNetNode {
         return MXNetOperatorMetadata.operatorMetadata.getOperatorCategory(this._operator);
     }
 
+    get documentation() {
+        return MXNetOperatorMetadata.operatorMetadata.getOperatorDocumentation(this.operator);
+    }
+
     get name() {
         return this._name;
     }
@@ -462,6 +466,41 @@ class MXNetOperatorMetadata {
         }
         return true;
     }
+
+    getOperatorDocumentation(operator) {
+        var schema = this._map[operator];
+        if (schema) {
+            schema = JSON.parse(JSON.stringify(schema));
+            schema.name = operator;
+            if (schema.description) {
+                schema.description = marked(schema.description);
+            }
+            if (schema.attributes) {
+                schema.attributes.forEach((attribute) => {
+                    if (attribute.description) {
+                        attribute.description = marked(attribute.description);
+                    }
+                });
+            }
+            if (schema.inputs) {
+                schema.inputs.forEach((input) => {
+                    if (input.description) {
+                        input.description = marked(input.description);
+                    }
+                });
+            }
+            if (schema.outputs) {
+                schema.outputs.forEach((output) => {
+                    if (output.description) {
+                        output.description = marked(output.description);
+                    }
+                });
+            }
+            var template = Handlebars.compile(operatorTemplate, 'utf-8');
+            return template(schema);
+        }
+        return '';
+    }
 }
 
 class MXNetError extends Error {

+ 218 - 71
src/mxnet-operator.json

@@ -2,52 +2,109 @@
   {
     "name": "Convolution",
     "schema": {
+      "attributes": [
+        {
+          "default": "False",
+          "name": "cudnn_off"
+        },
+        {
+          "default": "off",
+          "name": "cudnn_tune"
+        },
+        {
+          "default": "(1,)",
+          "name": "dilate"
+        },
+        {
+          "hidden": true,
+          "name": "no_bias"
+        },
+        {
+          "default": "1",
+          "name": "num_group"
+        },
+        {
+          "default": "(0,)",
+          "name": "pad"
+        },
+        {
+          "default": "(1,)",
+          "name": "stride"
+        },
+        {
+          "default": "1024",
+          "name": "workspace"
+        }
+      ],
       "category": "Layer",
       "inputs": [
-        { "name": "input" },
-        { "name": "weight" },
-        { "name": "bias" }
-      ],
-      "attributes": [
-        { "name": "cudnn_off", "default": "False" },
-        { "name": "cudnn_tune", "default": "off" },
-        { "name": "dilate", "default": "(1,)" },
-        { "name": "no_bias", "hidden": true },
-        { "name": "num_group", "default": "1" },
-        { "name": "pad", "default": "(0,)" },
-        { "name": "stride", "default": "(1,)" },
-        { "name": "workspace", "default": "1024" }
+        {
+          "name": "input"
+        },
+        {
+          "name": "weight"
+        },
+        {
+          "name": "bias"
+        }
       ]
     }
   },
   {
     "name": "Deconvolution",
     "schema": {
+      "attributes": [
+        {
+          "hidden": true,
+          "name": "no_bias"
+        },
+        {
+          "default": "1",
+          "name": "num_group"
+        },
+        {
+          "default": "1024",
+          "name": "workspace"
+        }
+      ],
       "category": "Layer",
       "inputs": [
-        { "name": "input" },
-        { "name": "weight" },
-        { "name": "bias" }
-      ],
-      "attributes": [
-        { "name": "no_bias", "hidden": true },
-        { "name": "num_group", "default": "1" },
-        { "name": "workspace", "default": "1024" }
+        {
+          "name": "input"
+        },
+        {
+          "name": "weight"
+        },
+        {
+          "name": "bias"
+        }
       ]
     }
   },
   {
     "name": "FullyConnected",
     "schema": {
+      "attributes": [
+        {
+          "default": "True",
+          "name": "flatten"
+        },
+        {
+          "hidden": true,
+          "name": "no_bias"
+        }
+      ],
       "category": "Layer",
       "inputs": [
-        { "name": "input" },
-        { "name": "weight" },
-        { "name": "bias" }
-      ],
-      "attributes": [
-        { "name": "flatten", "default": "True" },
-        { "name": "no_bias", "hidden": true }
+        {
+          "name": "input"
+        },
+        {
+          "name": "weight"
+        },
+        {
+          "name": "bias"
+        }
       ]
     }
   },
@@ -66,20 +123,48 @@
   {
     "name": "SoftmaxOutput",
     "schema": {
+      "attributes": [
+        {
+          "default": "1",
+          "name": "grad_scale"
+        },
+        {
+          "default": "1",
+          "name": "ignore_label"
+        },
+        {
+          "default": "False",
+          "name": "multi_output"
+        },
+        {
+          "default": "null",
+          "name": "normalization"
+        },
+        {
+          "default": "False",
+          "name": "out_grad"
+        },
+        {
+          "default": "0",
+          "name": "smooth_alpha"
+        },
+        {
+          "default": "False",
+          "name": "use_ignore"
+        },
+        {
+          "default": "False",
+          "name": "preserve_shape"
+        }
+      ],
       "category": "Activation",
       "inputs": [
-        { "name": "input" },
-        { "name": "label" }
-      ],
-      "attributes": [
-        { "name": "grad_scale", "default": "1" },
-        { "name": "ignore_label", "default": "1" },
-        { "name": "multi_output", "default": "False" },
-        { "name": "normalization", "default": "null" },
-        { "name": "out_grad", "default": "False" },
-        { "name": "smooth_alpha", "default": "0" },
-        { "name": "use_ignore", "default": "False" },
-        { "name": "preserve_shape", "default": "False" }
+        {
+          "name": "input"
+        },
+        {
+          "name": "label"
+        }
       ]
     }
   },
@@ -88,7 +173,9 @@
     "schema": {
       "category": "Activation",
       "inputs": [
-        { "name": "input" }
+        {
+          "name": "input"
+        }
       ]
     }
   },
@@ -101,14 +188,29 @@
   {
     "name": "Pooling",
     "schema": {
-      "category": "Pool",
       "attributes": [
-        { "name": "cudnn_off", "default": "False" },
-        { "name": "global_pool", "default": "False" },
-        { "name": "pad", "default": "(0,)" },
-        { "name": "pooling_convention", "default": "valid" },
-        { "name": "stride", "default": "(1,)" }
-      ]
+        {
+          "default": "False",
+          "name": "cudnn_off"
+        },
+        {
+          "default": "False",
+          "name": "global_pool"
+        },
+        {
+          "default": "(0,)",
+          "name": "pad"
+        },
+        {
+          "default": "valid",
+          "name": "pooling_convention"
+        },
+        {
+          "default": "(1,)",
+          "name": "stride"
+        }
+      ],
+      "category": "Pool"
     }
   },
   {
@@ -120,13 +222,22 @@
   {
     "name": "Concat",
     "schema": {
+      "attributes": [
+        {
+          "default": "1",
+          "name": "dim"
+        },
+        {
+          "hidden": true,
+          "name": "num_args"
+        }
+      ],
       "category": "Tensor",
       "inputs": [
-        { "name": "inputs", "option": "variadic" }
-      ],
-      "attributes": [
-        { "name": "dim", "default": "1" },
-        { "name": "num_args", "hidden": true }
+        {
+          "name": "inputs",
+          "option": "variadic"
+        }
       ]
     }
   },
@@ -134,7 +245,10 @@
     "name": "_Plus",
     "schema": {
       "inputs": [
-        { "name": "inputs", "option": "variadic" }
+        {
+          "name": "inputs",
+          "option": "variadic"
+        }
       ]
     }
   },
@@ -142,25 +256,49 @@
     "name": "elemwise_add",
     "schema": {
       "inputs": [
-        { "name": "inputs", "option": "variadic" }
+        {
+          "name": "inputs",
+          "option": "variadic"
+        }
       ]
     }
-  },  
+  },
   {
     "name": "BatchNorm",
     "schema": {
+      "attributes": [
+        {
+          "default": "1",
+          "name": "axis"
+        },
+        {
+          "default": "0.001",
+          "name": "eps"
+        },
+        {
+          "default": "0.9",
+          "name": "momentum"
+        },
+        {
+          "default": "True",
+          "name": "fix_gamma"
+        },
+        {
+          "default": "False",
+          "name": "use_global_stats"
+        }
+      ],
       "category": "Normalization",
       "inputs": [
-        { "name": "input" },
-        { "name": "gamma" },
-        { "name": "beta" }
-      ],
-      "attributes": [
-        { "name": "axis", "default": "1" },
-        { "name": "eps", "default": "0.001" },
-        { "name": "momentum", "default": "0.9" },
-        { "name": "fix_gamma", "default": "True" },
-        { "name": "use_global_stats", "default": "False" }
+        {
+          "name": "input"
+        },
+        {
+          "name": "gamma"
+        },
+        {
+          "name": "beta"
+        }
       ]
     }
   },
@@ -169,9 +307,15 @@
     "schema": {
       "category": "Normalization",
       "inputs": [
-        { "name": "input" },
-        { "name": "gamma" },
-        { "name": "beta" }
+        {
+          "name": "input"
+        },
+        {
+          "name": "gamma"
+        },
+        {
+          "name": "beta"
+        }
       ]
     }
   },
@@ -180,8 +324,11 @@
     "schema": {
       "category": "Normalization",
       "inputs": [
-        { "name": "inputs", "option": "variadic" }
+        {
+          "name": "inputs",
+          "option": "variadic"
+        }
       ]
     }
   }
-]
+]

+ 5 - 6
tools/keras-generate

@@ -2,11 +2,10 @@
 
 mkdir -p ../third_party
 
-identifier=keras
-repository=https://github.com/keras-team/${identifier}.git
+repository=https://github.com/keras-team/keras.git
 
-if [ -d "../third_party/${identifier}" ]; then
-    pushd "../third_party/${identifier}" > /dev/null
+if [ -d "../third_party/keras" ]; then
+    pushd "../third_party/keras" > /dev/null
     echo "Fetch ${repository}..."
     git fetch -p
     echo "Reset ${repository}..."
@@ -19,5 +18,5 @@ else
     popd > /dev/null
 fi
 
-echo "Update '../src/${identifier}-operator.json'"
-python ${identifier}-operator-json.py
+echo "Update '../src/keras-operator.json'"
+python keras-operator-json.py

+ 3 - 0
tools/mxnet-generate

@@ -17,3 +17,6 @@ else
     git clone --recursive ${repository}
     popd > /dev/null
 fi
+
+# echo "Update '../src/mxnet-operator.json'"
+# python mxnet-operator-json.py

+ 41 - 0
tools/mxnet-operator-json.py

@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+
+from __future__ import unicode_literals
+from __future__ import print_function
+
+import io
+import json
+import pydoc
+import re
+import sys
+
+json_file = '../src/mxnet-operator.json'
+json_data = open(json_file).read()
+json_root = json.loads(json_data)
+
+for entry in json_root:
+    name = entry['name']
+    schema = entry['schema']
+    class_name = 'mxnet.symbol.' + name
+    class_definition = pydoc.locate(class_name)
+    if not class_definition:
+        print('NOT FOUND: ' + class_name)
+        # raise Exception('\'' + class_name + '\' not found.')
+    else:
+        docstring = class_definition.__doc__
+        if docstring:
+            schema['description'] = docstring
+    # if not docstring:
+        # print('NO DOCSTRING: ' + class_name)
+        # raise Exception('\'' + class_name + '\' missing __doc__.')
+    # print(docstring)
+ 
+with io.open(json_file, 'w', newline='') as fout:
+    json_data = json.dumps(json_root, sort_keys=True, indent=2)
+    for line in json_data.splitlines():
+        line = line.rstrip()
+        if sys.version_info[0] < 3:
+            line = unicode(line)
+        fout.write(line)
+        fout.write('\n')
+