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_r288286763
 
 

 ##########
 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):
+    """Given operator name and handle, fetch operator arguments - number of 
arguments,
+    argument names, argument types.
+
+    Parameters
+    ----------
+    op_name: str
+        Name of the operator
+
+    op_handle: OpHandle
+        Handle for the operator
+
+    Returns
+    -------
+    (narg, arg_names, arg_types)
+    """
+    real_name = ctypes.c_char_p()
+    desc = ctypes.c_char_p()
+    num_args = mx_uint()
+    arg_names = ctypes.POINTER(ctypes.c_char_p)()
+    arg_types = ctypes.POINTER(ctypes.c_char_p)()
+    arg_descs = ctypes.POINTER(ctypes.c_char_p)()
+    key_var_num_args = ctypes.c_char_p()
+    ret_type = ctypes.c_char_p()
+
+    check_call(_LIB.MXSymbolGetAtomicSymbolInfo(
+        op_handle, ctypes.byref(real_name), ctypes.byref(desc),
+        ctypes.byref(num_args),
+        ctypes.byref(arg_names),
+        ctypes.byref(arg_types),
+        ctypes.byref(arg_descs),
+        ctypes.byref(key_var_num_args),
+        ctypes.byref(ret_type)))
+
+    narg = int(num_args.value)
+    arg_names = [py_str(arg_names[i]) for i in range(narg)]
+    arg_types = [py_str(arg_types[i]) for i in range(narg)]
+
+    return narg, arg_names, arg_types
+
+
+def _set_op_arguments(mx_operators):
+    """Fetch and set operator arguments - nargs, arg_names, arg_types
+    """
+    for op_name in mx_operators:
+        op_handle = _get_op_handles(op_name)
+        narg, arg_names, arg_types = _get_op_arguments(op_name, op_handle)
+        mx_operators[op_name]["params"] = {"narg": narg,
+                                           "arg_names": arg_names,
+                                           "arg_types": arg_types}
+
+
+def _get_all_mxnet_operators():
+    # Step 1 - Get all registered op names
+    mx_operators = _get_all_registered_ops()
+
+    # Step 2 - Get all parameters for the operators
+    _set_op_arguments(mx_operators)
+    return mx_operators
+
+
+def prepare_op_inputs(arg_params, arg_values):
+    # For each default value combination, prepare inputs
+    inputs = []
+    for arg_value in arg_values:
+        inp = {}
+        for arg_name in arg_params["params"]["arg_names"]:
+            inp[arg_name] = arg_value[arg_name]
+        inputs.append(inp)
+    return inputs
+
+
+def get_all_broadcast_binary_operators():
+    """Gets all binary broadcast operators registered with MXNet.
+
+    Returns
+    -------
+    {"operator_name": {"has_backward", "nd_op_handle", "params"}}
+    """
+    # Get all mxnet operators
+    mx_operators = _get_all_mxnet_operators()
+
+    # Filter for binary broadcast operators
+    binary_broadcast_mx_operators = {}
+    for op_name, op_params in mx_operators.items():
+        if op_name.startswith("broadcast_") and op_params["params"]["narg"] == 
2 and \
+                "lhs" in op_params["params"]["arg_names"] and \
+                "rhs" in op_params["params"]["arg_names"]:
+            binary_broadcast_mx_operators[op_name] = mx_operators[op_name]
+    return binary_broadcast_mx_operators
+
+
+def get_all_element_wise_binary_operators():
 
 Review comment:
   can we rename to "elemwise" for consistency with the C source files?

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to