wkcn commented on a change in pull request #17270: Dynamic custom operator GPU 
support
URL: https://github.com/apache/incubator-mxnet/pull/17270#discussion_r373270975
 
 

 ##########
 File path: include/mxnet/lib_api.h
 ##########
 @@ -594,26 +657,58 @@ class CustomOp {
     mutate_inputs = func;
     return *this;
   }
-  CustomOp& setCreateOpState(createOpState_t func) {
-    create_opstate = func;
+  CustomOp& setCreateOpState(createOpState_t func, const char* ctx) {
+    if (create_op_ctx_map.count(ctx) > 0)
+      raiseDuplicateContextError();
+    create_op_ctx_map[ctx] = func;
     return *this;
   }
   CustomOp& setIsSubgraphOp() {
     isSGop = true;
     return *this;
   }
+  void mapToVector() {
+    for (auto kv : forward_ctx_map) {
+      forward_ctx_cstr.push_back(kv.first);
+      forward_fp.push_back(kv.second);
+    }
+    for (auto kv : backward_ctx_map) {
+      backward_ctx_cstr.push_back(kv.first);
+      backward_fp.push_back(kv.second);
+    }
+    for (auto kv : create_op_ctx_map) {
+      create_op_ctx_cstr.push_back(kv.first);
+      create_op_fp.push_back(kv.second);
+    }
+  }
+  ~CustomOp() {}
 
   /*! \brief operator name */
   const char* name;
+
   /*! \brief operator functions */
-  fcomp_t forward;
-  fcomp_t backward;
   parseAttrs_t parse_attrs;
   inferType_t infer_type;
   inferShape_t infer_shape;
   mutateInputs_t mutate_inputs;
-  createOpState_t create_opstate;
   bool isSGop;
+
+  /*! \brief vector repr of ctx map to be easily loaded from c_api */
+  std::vector<const char*> forward_ctx_cstr, backward_ctx_cstr, 
create_op_ctx_cstr;
+  std::vector<fcomp_t> forward_fp, backward_fp;
+  std::vector<createOpState_t> create_op_fp;
+
+ private:
+  void raiseDuplicateContextError() {
+    std::string op_name_str(name);
+    throw std::runtime_error(
+      "Error! Error! Cannot register multiple functions under same context for 
operator '"
+      + op_name_str + "'");
+  }
+
+  /*! \brief dedup context maps - static string ctx to custom function */
+  std::unordered_map<const char*, fcomp_t> forward_ctx_map, backward_ctx_map;
 
 Review comment:
   The key of `forward_ctx_map`, `backward_ctx_map` and `create_op_ctx_map` are 
the pointer value of a string, rather than the content. Although the pointers 
of the same content are the same in a library, the pointers are different in 
different libraries. Using `std::string` as the key may be better.

----------------------------------------------------------------
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