piiswrong closed pull request #8540: div_scalar for sparse URL: https://github.com/apache/incubator-mxnet/pull/8540
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/src/operator/tensor/elemwise_binary_scalar_op_basic.cc b/src/operator/tensor/elemwise_binary_scalar_op_basic.cc index 0f0004c6bc..742c142053 100644 --- a/src/operator/tensor/elemwise_binary_scalar_op_basic.cc +++ b/src/operator/tensor/elemwise_binary_scalar_op_basic.cc @@ -130,6 +130,15 @@ MXNET_OPERATOR_REGISTER_BINARY_SCALAR(_rminus_scalar) .add_alias("_RMinusScalar"); MXNET_OPERATOR_REGISTER_BINARY_SCALAR(_mul_scalar) +.describe(R"doc(Multiply an array with a scalar. + +``_mul_scalar`` only operates on data array of input if input is sparse. + +For example, if input of shape (100, 100) has only 2 non zero elements, +i.e. input.data = [5, 6], scalar = nan, +it will result output.data = [nan, nan] instead of 10000 nans. + +)doc" ADD_FILELINE) .set_attr<FInferStorageType>("FInferStorageType", BinaryScalarStorageType) .set_attr<FCompute>("FCompute<cpu>", BinaryScalarOp::Compute<cpu, mshadow::op::mul>) .set_attr<FComputeEx>("FComputeEx<cpu>", BinaryScalarOp::ComputeEx<cpu, mshadow::op::mul>) @@ -143,10 +152,28 @@ MXNET_OPERATOR_REGISTER_BINARY_SCALAR(_backward_mul_scalar) .set_attr<FComputeEx>("FComputeEx<cpu>", BinaryScalarOp::ComputeEx<cpu, mshadow::op::mul>); MXNET_OPERATOR_REGISTER_BINARY_SCALAR(_div_scalar) +.describe(R"doc(Divide an array with a scalar. + +``_div_scalar`` only operates on data array of input if input is sparse. + +For example, if input of shape (100, 100) has only 2 non zero elements, +i.e. input.data = [5, 6], scalar = nan, +it will result output.data = [nan, nan] instead of 10000 nans. + +)doc" ADD_FILELINE) +.set_attr<FInferStorageType>("FInferStorageType", BinaryScalarStorageType) .set_attr<FCompute>("FCompute<cpu>", BinaryScalarOp::Compute<cpu, mshadow::op::div>) -.set_attr<nnvm::FGradient>("FGradient", ElemwiseGradUseNone{"_div_scalar"}) +.set_attr<FComputeEx>("FComputeEx<cpu>", BinaryScalarOp::ComputeEx<cpu, mshadow::op::div>) +.set_attr<nnvm::FGradient>("FGradient", ElemwiseGradUseNone{"_backward_div_scalar"}) .add_alias("_DivScalar"); +MXNET_OPERATOR_REGISTER_BINARY_SCALAR(_backward_div_scalar) +.set_attr<nnvm::TIsBackward>("TIsBackward", true) +.set_attr<FInferStorageType>("FInferStorageType", BinaryScalarStorageType) +.set_attr<FCompute>("FCompute<cpu>", BinaryScalarOp::Compute<cpu, mshadow::op::div>) +.set_attr<FComputeEx>("FComputeEx<cpu>", BinaryScalarOp::ComputeEx<cpu, mshadow::op::div>); + + MXNET_OPERATOR_REGISTER_BINARY_SCALAR(_rdiv_scalar) .set_attr<FCompute>("FCompute<cpu>", BinaryScalarOp::Compute<cpu, mshadow_op::rdiv>) .set_attr<nnvm::FGradient>("FGradient", ElemwiseGradUseIn{"_backward_rdiv_scalar"}) diff --git a/src/operator/tensor/elemwise_binary_scalar_op_basic.cu b/src/operator/tensor/elemwise_binary_scalar_op_basic.cu index afbb46b418..32e51d3990 100644 --- a/src/operator/tensor/elemwise_binary_scalar_op_basic.cu +++ b/src/operator/tensor/elemwise_binary_scalar_op_basic.cu @@ -45,7 +45,12 @@ NNVM_REGISTER_OP(_backward_mul_scalar) .set_attr<FComputeEx>("FComputeEx<gpu>", BinaryScalarOp::ComputeEx<gpu, mshadow::op::mul>); NNVM_REGISTER_OP(_div_scalar) -.set_attr<FCompute>("FCompute<gpu>", BinaryScalarOp::Compute<gpu, mshadow::op::div>); +.set_attr<FCompute>("FCompute<gpu>", BinaryScalarOp::Compute<gpu, mshadow::op::div>) +.set_attr<FComputeEx>("FComputeEx<gpu>", BinaryScalarOp::ComputeEx<gpu, mshadow::op::div>); + +NNVM_REGISTER_OP(_backward_div_scalar) +.set_attr<FCompute>("FCompute<gpu>", BinaryScalarOp::Compute<gpu, mshadow::op::div>) +.set_attr<FComputeEx>("FComputeEx<gpu>", BinaryScalarOp::ComputeEx<gpu, mshadow::op::div>); NNVM_REGISTER_OP(_rdiv_scalar) .set_attr<FCompute>("FCompute<gpu>", BinaryScalarOp::Compute<gpu, mshadow_op::rdiv>); diff --git a/tests/python/unittest/test_sparse_ndarray.py b/tests/python/unittest/test_sparse_ndarray.py index 0bbb5ffaaa..cb564906df 100644 --- a/tests/python/unittest/test_sparse_ndarray.py +++ b/tests/python/unittest/test_sparse_ndarray.py @@ -246,7 +246,7 @@ def check_binary(fn, stype): check_binary(lambda x, y: x == y, stype) -def test_sparse_nd_binary_rop(): +def test_sparse_nd_binary_scalar_op(): N = 3 def check(fn, stype): for _ in range(N): @@ -269,6 +269,7 @@ def check(fn, stype): check(lambda x: 0.5 >= x, stype) check(lambda x: 0.5 <= x, stype) check(lambda x: 0.5 == x, stype) + check(lambda x: x / 2, stype) def test_sparse_nd_binary_iop(): N = 3 ---------------------------------------------------------------- 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