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

 ##########
 File path: python/mxnet/numpy_op_fallback.py
 ##########
 @@ -49,6 +49,49 @@ 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')
 
 Review comment:
   I think it is suitable to use numpy.argsort to get the index in forward.

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