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/11/08 07:49:49 UTC
[GitHub] [incubator-mxnet] hgt312 commented on a change in pull request
#16750: [Numpy] add custom op sort
hgt312 commented on a change in pull request #16750: [Numpy] add custom op sort
URL: https://github.com/apache/incubator-mxnet/pull/16750#discussion_r344021464
##########
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 = None if kind == 'None' else kind
+
+ def list_arguments(self):
+ return ['a']
+
+ def infer_shape(self, in_shape):
+ if self._axis is None and len(in_shape[0]) == 0:
+ out_shape = (1,)
+ elif self._axis is None:
+ out_shape = (np.prod(in_shape[0]),)
+ elif (self._axis + 1) > len(in_shape[0]) or abs(self._axis) > len(in_shape[0]):
Review comment:
same here
----------------------------------------------------------------
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