|
29 | 29 | namespace mxnet { |
30 | 30 | namespace op { |
31 | 31 |
|
32 | | -MXNET_OPERATOR_REGISTER_BINARY_SCALAR(_equal_scalar) |
33 | | -.set_attr<FCompute>("FCompute<cpu>", BinaryScalarOp::Compute<cpu, mshadow_op::eq>) |
| 32 | +#define MXNET_OPERATOR_REGISTER_BINARY_SCALAR_LOGIC(__name$, __kernel$) \ |
| 33 | + MXNET_OPERATOR_REGISTER_BINARY_SCALAR(__name$) \ |
| 34 | + .set_attr<FInferStorageType>("FInferStorageType", BinaryScalarLogicStorageType<__kernel$>) \ |
| 35 | + .set_attr<FCompute>("FCompute<cpu>", BinaryScalarOp::Compute<cpu, __kernel$>) \ |
| 36 | + .set_attr<FComputeEx>("FComputeEx<cpu>", BinaryScalarOp::LogicComputeEx<cpu, __kernel$>) |
| 37 | + |
| 38 | +template<typename OP> |
| 39 | +static bool BinaryScalarLogicStorageType(const nnvm::NodeAttrs& attrs, |
| 40 | + const int dev_mask, |
| 41 | + DispatchMode* dispatch_mode, |
| 42 | + std::vector<int> *in_attrs, |
| 43 | + std::vector<int> *out_attrs) { |
| 44 | + CHECK_EQ(in_attrs->size(), 1); |
| 45 | + CHECK_EQ(out_attrs->size(), 1); |
| 46 | + const auto in_stype = in_attrs->at(0); |
| 47 | + auto &out_stype = out_attrs->at(0); |
| 48 | + bool dispatched = false; |
| 49 | + const double alpha = nnvm::get<double>(attrs.parsed); |
| 50 | + bool is_sparse = OP::Map(static_cast<double>(0), alpha) == 0; |
| 51 | + if (!dispatched && in_stype == kDefaultStorage) { |
| 52 | + // dns -> dns |
| 53 | + dispatched = storage_type_assign(&out_stype, kDefaultStorage, |
| 54 | + dispatch_mode, DispatchMode::kFCompute); |
| 55 | + } |
| 56 | + if (!dispatched && in_stype == kRowSparseStorage && is_sparse) { |
| 57 | + // rsp -> rsp |
| 58 | + dispatched = storage_type_assign(&out_stype, kRowSparseStorage, |
| 59 | + dispatch_mode, DispatchMode::kFComputeEx); |
| 60 | + } |
| 61 | + if (!dispatched && in_stype == kCSRStorage && is_sparse) { |
| 62 | + // csr -> csr |
| 63 | + dispatched = storage_type_assign(&out_stype, kCSRStorage, |
| 64 | + dispatch_mode, DispatchMode::kFComputeEx); |
| 65 | + } |
| 66 | + if (!dispatched) { |
| 67 | + dispatched = dispatch_fallback(out_attrs, dispatch_mode); |
| 68 | + } |
| 69 | + return dispatched; |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +MXNET_OPERATOR_REGISTER_BINARY_SCALAR_LOGIC(_equal_scalar, mshadow_op::eq) |
34 | 74 | .set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes) |
35 | 75 | .add_alias("_EqualScalar"); |
36 | 76 |
|
37 | | -MXNET_OPERATOR_REGISTER_BINARY_SCALAR(_not_equal_scalar) |
38 | | -.set_attr<FCompute>("FCompute<cpu>", BinaryScalarOp::Compute<cpu, mshadow_op::ne>) |
| 77 | +MXNET_OPERATOR_REGISTER_BINARY_SCALAR_LOGIC(_not_equal_scalar, mshadow_op::ne) |
39 | 78 | .set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes) |
40 | 79 | .add_alias("_NotEqualScalar"); |
41 | 80 |
|
42 | | -MXNET_OPERATOR_REGISTER_BINARY_SCALAR(_greater_scalar) |
43 | | -.set_attr<FCompute>("FCompute<cpu>", BinaryScalarOp::Compute<cpu, mshadow_op::gt>) |
| 81 | +MXNET_OPERATOR_REGISTER_BINARY_SCALAR_LOGIC(_greater_scalar, mshadow_op::gt) |
44 | 82 | .set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes) |
45 | 83 | .add_alias("_GreaterScalar"); |
46 | 84 |
|
47 | | -MXNET_OPERATOR_REGISTER_BINARY_SCALAR(_greater_equal_scalar) |
48 | | -.set_attr<FCompute>("FCompute<cpu>", BinaryScalarOp::Compute<cpu, mshadow_op::ge>) |
| 85 | +MXNET_OPERATOR_REGISTER_BINARY_SCALAR_LOGIC(_greater_equal_scalar, mshadow_op::ge) |
49 | 86 | .set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes) |
50 | 87 | .add_alias("_GreaterEqualScalar"); |
51 | 88 |
|
52 | | -MXNET_OPERATOR_REGISTER_BINARY_SCALAR(_lesser_scalar) |
53 | | -.set_attr<FCompute>("FCompute<cpu>", BinaryScalarOp::Compute<cpu, mshadow_op::lt>) |
| 89 | +MXNET_OPERATOR_REGISTER_BINARY_SCALAR_LOGIC(_lesser_scalar, mshadow_op::lt) |
54 | 90 | .set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes) |
55 | 91 | .add_alias("_LesserScalar"); |
56 | 92 |
|
57 | | -MXNET_OPERATOR_REGISTER_BINARY_SCALAR(_lesser_equal_scalar) |
58 | | -.set_attr<FCompute>("FCompute<cpu>", BinaryScalarOp::Compute<cpu, mshadow_op::le>) |
| 93 | +MXNET_OPERATOR_REGISTER_BINARY_SCALAR_LOGIC(_lesser_equal_scalar, mshadow_op::le) |
59 | 94 | .set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes) |
60 | 95 | .add_alias("_LesserEqualScalar"); |
61 | 96 |
|
|
0 commit comments