haojin2 commented on a change in pull request #17254: [numpy] change unary 
infer type
URL: https://github.com/apache/incubator-mxnet/pull/17254#discussion_r381822843
 
 

 ##########
 File path: src/operator/numpy/np_elemwise_unary_op_basic.cc
 ##########
 @@ -82,6 +83,90 @@ NNVM_REGISTER_OP(_np_copy)
   .set_attr<FCompute>("FCompute<cpu>", UnaryOp::Compute<cpu, __kernel$>)       
           \
   .add_argument(__input_name$, "NDArray-or-Symbol", "The input array.")
 
+inline bool MixedUnaryOpType(const nnvm::NodeAttrs& attrs,
+                             std::vector<int>* in_attrs,
+                             std::vector<int>* out_attrs) {
+  CHECK_EQ(in_attrs->size(), 1U);
+  CHECK_EQ(out_attrs->size(), 1U);
+  int a_type = in_attrs->at(0);
+  if (mxnet::common::is_float(a_type)) {
+    TYPE_ASSIGN_CHECK(*out_attrs, 0, in_attrs->at(0));
+  } else if (a_type == mshadow::kInt32 || a_type == mshadow::kInt64) {
+    TYPE_ASSIGN_CHECK(*out_attrs, 0, mshadow::kFloat32);
+  } else {
+    TYPE_ASSIGN_CHECK(*out_attrs, 0, mshadow::kFloat16);
+  }
+  return out_attrs->at(0) != -1;
+}
+
+#define MXNET_OPERATOR_REGISTER_NUMPY_MIXED_TYPE_UNARY(__name$, __input_name$, 
__kernel$) \
+  NNVM_REGISTER_OP(__name$)                                                    
           \
+  .set_num_inputs(1)                                                           
           \
+  .set_num_outputs(1)                                                          
           \
+  .set_attr<mxnet::FInferShape>("FInferShape", ElemwiseShape<1, 1>)            
           \
+  .set_attr<nnvm::FInferType>("FInferType", MixedUnaryOpType)                  
           \
+  .set_attr<nnvm::FInplaceOption>("FInplaceOption",                            
           \
+    [](const NodeAttrs& attrs){                                                
           \
+      return std::vector<std::pair<int, int> >{{0, 0}};                        
           \
+    })                                                                         
           \
+  .set_attr<nnvm::FListInputNames>("FListInputNames",                          
           \
+    [](const NodeAttrs& attrs) {                                               
           \
+      return std::vector<std::string>{__input_name$};                          
           \
+    })                                                                         
           \
+  .set_attr<FCompute>("FCompute<cpu>", UnaryOp::ComputeMixedType<cpu, 
__kernel$>)         \
+  .add_argument(__input_name$, "NDArray-or-Symbol", "The input array.")
+
+#define MXNET_OPERATOR_REGISTER_UNARY_MIXEDTYPE_IN(name)            \
+  NNVM_REGISTER_OP(name)                                            \
+  .set_num_inputs(2)                                                \
+  .set_num_outputs(1)                                               \
+  .set_attr<nnvm::FListInputNames>("FListInputNames",               \
+    [](const NodeAttrs& attrs) {                                    \
+      return std::vector<std::string>{"lhs", "rhs"};                \
+    })                                                              \
+  .set_attr<mxnet::FInferShape>("FInferShape", ElemwiseShape<2, 1>)  \
+  .set_attr<nnvm::FInplaceOption>("FInplaceOption",                 \
+    [](const NodeAttrs& attrs){                                     \
+      return std::vector<std::pair<int, int> >{{0, 0}, {1, 0}};     \
+    })                                                              \
+  .add_argument("lhs", "NDArray-or-Symbol", "first input")          \
+  .add_argument("rhs", "NDArray-or-Symbol", "second input")
+
+/*! \brief Binary launch */
+#define MXNET_OPERATOR_REGISTER_UNARY_MIXEDTYPE_INOUT(name)         \
+  NNVM_REGISTER_OP(name)                                            \
+  .set_num_inputs(3)                                                \
+  .set_num_outputs(1)                                               \
+  .set_attr<nnvm::FListInputNames>("FListInputNames",               \
+    [](const NodeAttrs& attrs) {                                    \
+      return std::vector<std::string>{"lhs", "rhs"};                \
+    })                                                              \
+  .set_attr<mxnet::FInferShape>("FInferShape", ElemwiseShape<3, 1>)  \
+  .set_attr<nnvm::FInplaceOption>("FInplaceOption",                 \
+    [](const NodeAttrs& attrs){                                     \
+      return std::vector<std::pair<int, int> >{{0, 0}, {1, 0}};     \
+    })                                                              \
+  .add_argument("lhs", "NDArray-or-Symbol", "first input")          \
+  .add_argument("rhs", "NDArray-or-Symbol", "second input")
+
+#define MXNET_OPERATOR_REGISTER_UNARY_MIXEDTYPE_IN_CPU(__name$, __kernel$)     
               \
+  MXNET_OPERATOR_REGISTER_UNARY_MIXEDTYPE_IN(__name$)                          
               \
+  .set_attr<FCompute>("FCompute<cpu>", 
ElemwiseBinaryOp::MixedUnaryBackwardUseInCompute<cpu,  \
+                                                                       
__kernel$>)            \
+  .set_attr<FResourceRequest>("FResourceRequest",  /* For Sparse CSR */        
               \
 
 Review comment:
   seems like `FResourceRequest` is not used, you can simply get rid of this 
`set_attr`

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to