reminisce commented on a change in pull request #16750: [Numpy] add custom op 
sort
URL: https://github.com/apache/incubator-mxnet/pull/16750#discussion_r343784245
 
 

 ##########
 File path: python/mxnet/numpy_op_fallback.py
 ##########
 @@ -49,6 +49,48 @@ def _register_helper(prop_cls):
     return _register_helper
 
 
+@use_np  # enforce np shape and array semantics for all the methods in this 
class
+class Sort(operator.CustomOp):
+    """Fallback to NumPy sort operator."""
+    def __init__(self, axis, kind):
+        super(Sort, self).__init__()
+        self._axis = axis
+        self._kind = kind
+
+    def forward(self, is_train, req, in_data, out_data, aux):
+        out = np.sort(in_data[0].asnumpy(), self._axis, self._kind)
+        self.assign(out_data[0], req[0], _mx_np.array(out, dtype=out.dtype, 
ctx=out_data[0].ctx))
+
+    def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
+        raise NotImplementedError('Operator sort does not support gradient 
computation')
+
+
+@register('sort_fallback')
+class SortProp(operator.CustomOpProp):
+    """Fallback sort operator properties."""
+    def __init__(self, axis, kind):
+        super(SortProp, self).__init__(need_top_grad=True)
+        self._axis = ast.literal_eval(axis)
+        self._kind = kind if isinstance(kind, str) else ast.literal_eval(kind)
 
 Review comment:
   `kind` is always a string here, so `isinstance(kind, str)` always returns 
true. According to documentation, which says that kind is one of `{None, 
‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}`, I think you can do
   ```python
   self._kind = None if kind == 'None' else kind
   ```

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