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 2019/09/10 19:44:42 UTC

[GitHub] [incubator-mxnet] reminisce commented on a change in pull request #16097: [numpy] array ufunc and array function protocols

reminisce commented on a change in pull request #16097: [numpy] array ufunc and array function protocols
URL: https://github.com/apache/incubator-mxnet/pull/16097#discussion_r322928210
 
 

 ##########
 File path: python/mxnet/numpy_dispatch_protocol.py
 ##########
 @@ -0,0 +1,214 @@
+# 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.
+
+"""Utils for registering NumPy array function protocol for mxnet.numpy ops."""
+
+from __future__ import absolute_import
+import functools
+import numpy as _np
+from . import numpy as mx_np  # pylint: disable=reimported
+from .numpy.multiarray import _NUMPY_ARRAY_FUNCTION_DICT, _NUMPY_ARRAY_UFUNC_DICT
+
+
+def _find_duplicate(strs):
+    str_set = set()
+    for s in strs:
+        if s in str_set:
+            return s
+        else:
+            str_set.add(s)
+    return None
+
+
+def _implements(numpy_function):
+    """Register an __array_function__ implementation for MyArray objects."""
+    def decorator(func):
+        _NUMPY_ARRAY_FUNCTION_DICT[numpy_function] = func
+        return func
+    return decorator
+
+
+def with_array_function_protocol(func):
+    """A decorator for functions that expect array function protocol.
+    The decorated function only runs when NumPy version >= 1.17."""
+    from distutils.version import LooseVersion
+    cur_np_ver = LooseVersion(_np.__version__)
+    np_1_17_ver = LooseVersion('1.17')
+
+    @functools.wraps(func)
+    def _run_with_array_func_proto(*args, **kwargs):
+        if cur_np_ver >= np_1_17_ver:
+            try:
+                func(*args, **kwargs)
+            except:
 
 Review comment:
   Good point. I will append the generic message to the real exception.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services