larroy commented on a change in pull request #14977: Add an utility for 
operator benchmarks
URL: https://github.com/apache/incubator-mxnet/pull/14977#discussion_r288285629
 
 

 ##########
 File path: benchmark/opperf/utils/op_registry_utils.py
 ##########
 @@ -0,0 +1,193 @@
+# 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.
+
+"""Utilities to interact with MXNet operator registry."""
+import ctypes
+import sys
+from mxnet.base import _LIB, check_call, py_str, OpHandle, c_str, mx_uint
+
+# We will use all operators inside NDArray Module
+mx_nd_module = sys.modules["mxnet.ndarray.op"]
+
+
+def _get_all_registered_ops(filters=("_backward", "_contrib", "_")):
+    """Get all registered MXNet operators.
+
+    By default, filter out all backward operators that starts with  
'_backward',
+    Contrib operators that starts with '_contrib' and internal operators that
+    starts with '_'.
+
+    Parameters
+    ----------
+    filters: tuple(str)
+        List of operator name prefix to ignore from benchmarking.
+        Default - ("_backward", "_contrib", "_")
+
+    Returns
+    -------
+    {"operator_name": {"has_backward", "nd_op_handle"}}
+    """
+    plist = ctypes.POINTER(ctypes.c_char_p)()
+    size = ctypes.c_uint()
+
+    check_call(_LIB.MXListAllOpNames(ctypes.byref(size),
+                                     ctypes.byref(plist)))
+    mx_operators = {}
+    operators_with_backward = []
+
+    # Prepare master list of all operators.
+    for i in range(size.value):
+        cur_op_name = py_str(plist[i])
+        if not cur_op_name.startswith(filters):
+            mx_operators[cur_op_name] = {"has_backward": False,
+                                         "nd_op_handle": getattr(mx_nd_module, 
cur_op_name)}
+
+        if cur_op_name.startswith("_backward_"):
+            operators_with_backward.append(cur_op_name)
+
+    # Identify all operators that can run backward.
+    for op_with_backward in operators_with_backward:
+        op_name = op_with_backward.split("_backward_")[1]
+        if op_name in mx_operators:
+            mx_operators[op_name]["has_backward"] = True
+
+    return mx_operators
+
+
+def _get_op_handles(op_name):
+    """Get handle for an operator with given name - op_name.
+
+    Parameters
+    ----------
+    op_name: str
+        Name of operator to get handle for.
+    """
+    op_handle = OpHandle()
+    check_call(_LIB.NNGetOpHandle(c_str(op_name), ctypes.byref(op_handle)))
+    return op_handle
+
+
+def _get_op_arguments(op_name, op_handle):
 
 Review comment:
   nice

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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

Reply via email to