anirudh2290 commented on a change in pull request #10374: Sparse support for Custom Op URL: https://github.com/apache/incubator-mxnet/pull/10374#discussion_r179910335
########## File path: tests/python/unittest/test_operator.py ########## @@ -4059,14 +4080,100 @@ def create_operator(self, ctx, shapes, dtypes): data = mx.symbol.cast(data, dtype='float64') op = mx.symbol.cast(op, dtype='float32') - x = mx.nd.array(np.random.uniform(-1, 1, size=(4, 10))) - aux = mx.nd.zeros_like(x) check_numeric_gradient(op, [x], [aux]) - x.attach_grad() + data = mx.symbol.Variable('data', stype='csr') + aux = mx.symbol.Variable('aux') + op2 = mx.symbol.Custom(data=data, aux=aux, name='sqr', op_type='sqr') + x = x.tostype('csr') + aux = mx.nd.zeros_like(x) + check_numeric_gradient(op2, [x], [aux], grad_stype_dict={"data": "csr"}) + + x2 = mx.nd.array(np.random.uniform(-1, 1, size=(4, 10))) + x2 = x2.tostype('csr') + aux2 = mx.nd.zeros_like(x2) + x2.attach_grad() with mx.contrib.autograd.train_section(): - y = mx.nd.Custom(x, aux, op_type='sqr') + output = mx.nd.Custom(x2, aux2, name='sqr', op_type='sqr') + output.backward() + expected_output = mx.nd.sparse.square(x2) + expected_grad = 2 * x2 + rtol = 1e-4 + atol = 1e-6 + assert_almost_equal(output.asnumpy(), expected_output.asnumpy(), rtol=rtol, atol=atol) + assert_almost_equal(x2.grad.asnumpy(), expected_grad.asnumpy(), rtol=rtol, atol=atol) + + + # test for backward compatibility, i.e. the correctness of default implementation of + # infer storage in custom operator + class Mult(mx.operator.CustomOp): + def forward(self, is_train, req, in_data, out_data, aux): + self.assign(out_data[0], req[0], in_data[0]*in_data[1]) + + def backward(self, req, out_grad, in_data, out_data, in_grad, aux): + self.assign(in_grad[0], req[0], in_data[1]) + self.assign(in_grad[1], req[1], in_data[0]) + + @mx.operator.register("mult") + class MultProp(mx.operator.CustomOpProp): + def __init__(self): + super(MultProp, self).__init__(need_top_grad=True) + + def list_arguments(self): + return ['lhs', 'rhs'] + + def list_outputs(self): + return ['output'] + + def infer_shape(self, in_shape): + return in_shape, [in_shape[0]], [] + + def create_operator(self, ctx, shapes, dtypes): + return Mult() + + lhs = mx.nd.array(np.random.uniform(-1, 1, size=(4, 10))) + rhs = mx.nd.array(np.random.uniform(-1, 1, size=(4, 10))) + lhs.attach_grad() + rhs.attach_grad() + with mx.contrib.autograd.train_section(): Review comment: fixed. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services