You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2021/05/19 14:41:19 UTC

[GitHub] [tvm] xqdan opened a new pull request #8079: Complete register op from python

xqdan opened a new pull request #8079:
URL: https://github.com/apache/tvm/pull/8079


   Thanks for contributing to TVM!   Please refer to guideline https://tvm.apache.org/docs/contribute/ for useful information and tips. After the pull request is submitted, please request code reviews from [Reviewers](https://github.com/apache/incubator-tvm/blob/master/CONTRIBUTORS.md#reviewers) by @ them in the pull request thread.
   
   This is a following up PR based on #8002
   
   1, Support add_type_rel() and others in python
   2, Make register op utilities exposed under relay.op
   
   @zackcquic @tqchen


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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r642744655



##########
File path: src/ir/op.cc
##########
@@ -102,10 +102,66 @@ TVM_REGISTER_GLOBAL("ir.OpResetAttr").set_body_typed([](Op op, String attr_name)
   reg.reset_attr(attr_name);
 });
 
-TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name) {
+TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name, String descr) {
   const OpRegEntry* reg = OpRegistry::Global()->Get(op_name);
   ICHECK(reg == nullptr) << "AttributeError: Operator " << op_name << " is registered before";
-  OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+  auto& op = OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+  op.describe(descr);
+});
+
+TVM_REGISTER_GLOBAL("ir.OpAddTypeRel")
+    .set_body_typed([](Op op, String rel_name, runtime::TVMArgValue value) {
+      auto& reg = OpRegistry::Global()->RegisterOrGet(op->name).set_name();
+      if (value.type_code() == kTVMPackedFuncHandle) {
+        // do an eager copy of the PackedFunc to avoid deleting function from frontend.
+        PackedFunc* fcopy = new PackedFunc(value.operator tvm::runtime::PackedFunc());

Review comment:
       ok, I add more comments here




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



[GitHub] [tvm] xqdan commented on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-846551249


   @zackcquic please take another look


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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r638529020



##########
File path: src/ir/op.cc
##########
@@ -102,10 +102,61 @@ TVM_REGISTER_GLOBAL("ir.OpResetAttr").set_body_typed([](Op op, String attr_name)
   reg.reset_attr(attr_name);
 });
 
-TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name) {
-  const OpRegEntry* reg = OpRegistry::Global()->Get(op_name);
-  ICHECK(reg == nullptr) << "AttributeError: Operator " << op_name << " is registered before";
-  OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name, String descr) {
+  auto& reg = OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+  reg.describe(descr);
+});
+
+TVM_REGISTER_GLOBAL("ir.OpAddTypeRel")
+    .set_body_typed([](Op op, String rel_name, runtime::TVMArgValue value) {
+      auto& reg = OpRegistry::Global()->RegisterOrGet(op->name).set_name();
+      if (value.type_code() == kTVMPackedFuncHandle) {
+        // do an eager copy of the PackedFunc to avoid deleting function from frontend.
+        PackedFunc* fcopy = new PackedFunc(value.operator tvm::runtime::PackedFunc());
+        auto f = [=](const Array<Type>& args, int num_inputs, const Attrs& attrs,
+                     const TypeReporter& reporter) -> bool {
+          Array<Type> input_types(args.begin(), args.end() - 1);
+          // call customized relation functions
+          Type ret_type = (*fcopy)(input_types, attrs);
+          if (ret_type.defined()) {
+            // the last argument is output
+            reporter->Assign(args[args.size() - 1], ret_type);
+            return true;
+          }
+          return false;
+        };
+        // adjust function call to relay type system with TypeReporter
+        auto type_rel = runtime::TypedPackedFunc<bool(const Array<Type>&, int, const Attrs&,
+                                                      const TypeReporter&)>(f);
+        reg.add_type_rel(rel_name, type_rel);
+      } else if (value.type_code() == kTVMNullptr) {
+        // Call relation functions of relay
+        auto func_name = std::string("tvm.relay.type_relation.") + rel_name;

Review comment:
       I can add case for 1. custom type relation goes `if (value.type_code() == kTVMPackedFuncHandle)` branch, no need to search by "tvm.relay.type_relation.") + rel_name;




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



[GitHub] [tvm] zackcquic commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
zackcquic commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r642494514



##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,87 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):
+        """Attach the type function corresponding to the return type.
 
-def register_op(op_name):
-    """Register an operator by name
+        Parameters
+        ----------
+        rel_name : str
+            The type relation name to register.
+
+        type_rel_func: function (args: List[Type], attrs: Attrs) -> Type

Review comment:
       type_rel_func: Optional[function (args: List[Type], attrs: Attrs) -> Type]

##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,87 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):
+        """Attach the type function corresponding to the return type.
 
-def register_op(op_name):
-    """Register an operator by name
+        Parameters
+        ----------
+        rel_name : str
+            The type relation name to register.
+
+        type_rel_func: function (args: List[Type], attrs: Attrs) -> Type
+            The backing relation function which can solve an arbitrary relation on variables.
+            Differences with type_rel_func in C++:
+            1, when type_rel_func is not None:
+               1) OpAddTypeRel on C++ side will adjust type_rel_func with TypeReporter to
+                  calling convention of relay type system.
+               2) type_rel_func returns output argument's type, return None means can't
+                  infer output's type.
+               3) only support single output operators for now, the last argument is output tensor.

Review comment:
       Have you encountered output with multiple operators?
   (Just curious, I can't come up with an example so far)

##########
File path: src/ir/op.cc
##########
@@ -102,10 +102,66 @@ TVM_REGISTER_GLOBAL("ir.OpResetAttr").set_body_typed([](Op op, String attr_name)
   reg.reset_attr(attr_name);
 });
 
-TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name) {
+TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name, String descr) {
   const OpRegEntry* reg = OpRegistry::Global()->Get(op_name);
   ICHECK(reg == nullptr) << "AttributeError: Operator " << op_name << " is registered before";
-  OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+  auto& op = OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+  op.describe(descr);
+});
+
+TVM_REGISTER_GLOBAL("ir.OpAddTypeRel")
+    .set_body_typed([](Op op, String rel_name, runtime::TVMArgValue value) {
+      auto& reg = OpRegistry::Global()->RegisterOrGet(op->name).set_name();
+      if (value.type_code() == kTVMPackedFuncHandle) {
+        // do an eager copy of the PackedFunc to avoid deleting function from frontend.
+        PackedFunc* fcopy = new PackedFunc(value.operator tvm::runtime::PackedFunc());
+        auto f = [=](const Array<Type>& args, int num_inputs, const Attrs& attrs,
+                     const TypeReporter& reporter) -> bool {
+          Array<Type> input_types(args.begin(), args.end() - 1);
+          // call customized relation functions
+          // *fcopy's signature: function (args: List[Type], attrs: Attrs) -> Type
+          Type ret_type = (*fcopy)(input_types, attrs);
+          // when defined ret_type, inference of output type is ok, do type assign
+          // otherwise, inference failure happens
+          if (ret_type.defined()) {
+            // the last argument is output
+            reporter->Assign(args[args.size() - 1], ret_type);
+            return true;
+          }
+          return false;
+        };
+        // adjust function call to call conventions of relay type system with TypeReporter
+        auto type_rel = runtime::TypedPackedFunc<bool(const Array<Type>&, int, const Attrs&,
+                                                      const TypeReporter&)>(f);
+        reg.add_type_rel(rel_name, type_rel);
+      } else if (value.type_code() == kTVMNullptr) {
+        // Call relation functions of relay
+        auto func_name = std::string("tvm.relay.type_relation.") + rel_name;
+        auto* f = runtime::Registry::Get(func_name);
+        CHECK(f != nullptr) << "AddTypeRel error: no type_relation registered.";

Review comment:
       use ICHECK here.

##########
File path: tests/python/relay/test_type_infer.py
##########
@@ -416,6 +417,127 @@ def test_dynamic_function():
     assert mod["main"].params[0].checked_type == s_tt
 
 
+def test_custom_op_infer():
+    """" Tests infer type for custom_op """
+    op_name = "custom_log"
+    _op.register(op_name, r"code(cal log of a tensor.)code")
+    _op.get(op_name).set_num_inputs(1)
+    _op.get(op_name).add_argument("data_0", "Tensor", "The input data tensor.")
+    # call default relation functions
+    _op.get(op_name).add_type_rel("Identity")
+    _op.get(op_name).set_support_level(1)
+    _op.register_pattern(op_name, _op.OpPattern.ELEMWISE)
+    _op.register_stateful(op_name, False)
+
+    def clog(x):
+        return relay.Call(_op.get(op_name), [x])
+
+    tp = relay.TensorType((10, 10), "float32")
+    x = relay.var("x", tp)
+    sb = relay.ScopeBuilder()
+    t1 = sb.let("t1", clog(x))
+    t2 = sb.let("t2", relay.add(t1, x))
+    sb.ret(t2)
+    f = relay.Function([x], sb.get())
+    fchecked = infer_expr(f)
+    assert fchecked.checked_type == relay.FuncType([tp], tp)
+
+
+def test_custom_add_broadcast_op():
+    """ Tests infer type for broadcast custom_op """
+    op_name = "custom_broadcast_add"
+    _op.register(op_name, r"code(Add two tensor with inner broadcasting.)code")
+    _op.get(op_name).set_num_inputs(2)
+    _op.get(op_name).add_argument("data_0", "Tensor", "The input data tensor.")
+    _op.get(op_name).add_argument("data_1", "Tensor", "The input data tensor.")
+    # call default relation functions
+    _op.get(op_name).add_type_rel("Broadcast")
+    _op.get(op_name).set_support_level(1)
+    _op.register_stateful(op_name, False)
+
+    def broadcast_add(x, y):
+        return relay.Call(_op.get(op_name), [x, y])
+
+    x = relay.var("x", shape=(10, 4))
+    y = relay.var("y", shape=(5, 10, 1))
+    z = broadcast_add(x, y)
+    func = relay.Function([x, y], z)
+    t1 = relay.TensorType((10, 4), "float32")
+    t2 = relay.TensorType((5, 10, 1), "float32")
+    t3 = relay.TensorType((5, 10, 4), "float32")
+    expected_ty = relay.FuncType([t1, t2], t3)
+    assert_has_type(func, expected_ty)
+
+
+def test_custom_op_rel_infer():
+    """" Tests infer type for custom_op """
+
+    def custom_log1_rel(arg_types, attrs):
+        assert len(arg_types) == 1, "type relation arg number mismatch!"
+        if attrs:
+            assert isinstance(attrs, DictAttrs)
+        inputa_type = arg_types[0]
+        return relay.TensorType(inputa_type.shape, inputa_type.dtype)
+
+    op_name = "custom_log1"
+    _op.register(op_name, r"code(cal log of a tensor.)code")
+    _op.get(op_name).set_num_inputs(1)
+    _op.get(op_name).add_argument("data_0", "Tensor", "The input data tensor.")
+    _op.get(op_name).set_attrs_type_key("DictAttrs")
+    # call customized relation functions
+    _op.get(op_name).add_type_rel("custom_log1", custom_log1_rel)
+    _op.get(op_name).set_support_level(1)
+    _op.register_pattern(op_name, _op.OpPattern.ELEMWISE)
+    _op.register_stateful(op_name, False)
+
+    def clog(x):
+        return relay.Call(_op.get(op_name), [x])
+
+    tp = relay.TensorType((10, 10), "float32")
+    x = relay.var("x", tp)
+    sb = relay.ScopeBuilder()
+    t1 = sb.let("t1", clog(x))
+    t2 = sb.let("t2", relay.add(t1, x))
+    sb.ret(t2)
+    f = relay.Function([x], sb.get())
+    fchecked = infer_expr(f)
+    assert fchecked.checked_type == relay.FuncType([tp], tp)
+
+
+def test_custom_op_rel_infer_exception():
+    """" Tests infer type for custom_op """
+
+    def custom_log1_rel(arg_types, attrs):
+        assert len(arg_types) == 2, "type relation arg number mismatch!"
+        return None
+
+    op_name = "custom_log2"
+    _op.register(op_name, r"code(cal log of a tensor.)code")
+    _op.get(op_name).set_num_inputs(1)
+    _op.get(op_name).add_argument("data_0", "Tensor", "The input data tensor.")
+    _op.get(op_name).set_attrs_type_key("DictAttrs")
+    # call customized relation functions
+    _op.get(op_name).add_type_rel("custom_log2", custom_log1_rel)
+    _op.get(op_name).set_support_level(1)
+    _op.register_pattern(op_name, _op.OpPattern.ELEMWISE)
+    _op.register_stateful(op_name, False)
+
+    def clog(x):
+        return relay.Call(_op.get(op_name), [x])
+
+    tp = relay.TensorType((10, 10), "float32")
+    x = relay.var("x", tp)
+    sb = relay.ScopeBuilder()
+    t1 = sb.let("t1", clog(x))
+    t2 = sb.let("t2", relay.add(t1, x))
+    sb.ret(t2)
+    f = relay.Function([x], sb.get())
+    try:

Review comment:
       Check error message, eg:
   ``` python
   with pytest.raises(tvm.error.TVMError) as cm:        
       fchecked = infer_type(f)
       assert "type relation arg number mismatch!" in str(cm.execption)
   ```

##########
File path: python/tvm/relay/op/op.py
##########
@@ -40,6 +40,40 @@ def get(op_name):
     return tvm.ir.Op.get(op_name)

Review comment:
       Be consistent with others (remove return) or document why it is specially returned.

##########
File path: python/tvm/relay/op/op.py
##########
@@ -40,6 +40,40 @@ def get(op_name):
     return tvm.ir.Op.get(op_name)
 
 
+def register(op_name, describe=""):
+    """Get the Op for a given name.
+    when the op_name is not registered, create a new empty op with the given name.
+    when the op_name has been registered, abort with an error message.

Review comment:
       Could you add a test case for this ? (registering with existing name)

##########
File path: src/ir/op.cc
##########
@@ -102,10 +102,66 @@ TVM_REGISTER_GLOBAL("ir.OpResetAttr").set_body_typed([](Op op, String attr_name)
   reg.reset_attr(attr_name);
 });
 
-TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name) {
+TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name, String descr) {
   const OpRegEntry* reg = OpRegistry::Global()->Get(op_name);
   ICHECK(reg == nullptr) << "AttributeError: Operator " << op_name << " is registered before";
-  OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+  auto& op = OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+  op.describe(descr);
+});
+
+TVM_REGISTER_GLOBAL("ir.OpAddTypeRel")
+    .set_body_typed([](Op op, String rel_name, runtime::TVMArgValue value) {
+      auto& reg = OpRegistry::Global()->RegisterOrGet(op->name).set_name();
+      if (value.type_code() == kTVMPackedFuncHandle) {
+        // do an eager copy of the PackedFunc to avoid deleting function from frontend.
+        PackedFunc* fcopy = new PackedFunc(value.operator tvm::runtime::PackedFunc());
+        auto f = [=](const Array<Type>& args, int num_inputs, const Attrs& attrs,
+                     const TypeReporter& reporter) -> bool {
+          Array<Type> input_types(args.begin(), args.end() - 1);
+          // call customized relation functions
+          // *fcopy's signature: function (args: List[Type], attrs: Attrs) -> Type
+          Type ret_type = (*fcopy)(input_types, attrs);
+          // when defined ret_type, inference of output type is ok, do type assign
+          // otherwise, inference failure happens
+          if (ret_type.defined()) {
+            // the last argument is output
+            reporter->Assign(args[args.size() - 1], ret_type);

Review comment:
       NIT: Will be more clear to use args.back() (vs.  args[args.zie() - 1]), I think.

##########
File path: python/tvm/relay/op/op.py
##########
@@ -40,6 +40,40 @@ def get(op_name):
     return tvm.ir.Op.get(op_name)
 
 
+def register(op_name, describe=""):
+    """Get the Op for a given name.
+    when the op_name is not registered, create a new empty op with the given name.
+    when the op_name has been registered, abort with an error message.
+
+    Parameters
+    ----------
+    op_name : str
+        The operator name
+
+    describe : str
+        The operator description
+    """
+
+    tvm.ir.register_op(op_name, describe)
+
+
+def register_stateful(op_name, stateful, level=10):
+    """Register operator pattern for an op.
+
+    Parameters
+    ----------
+    op_name : str
+        The name of the op.
+
+    stateful : bool
+        The stateful flag.
+
+    level : int
+        The priority level
+    """
+    return tvm.ir.register_op_attr(op_name, "TOpIsStateful", stateful, level)

Review comment:
       Be consistent with others (remove return) or document that why it is specially returned.

##########
File path: src/ir/op.cc
##########
@@ -102,10 +102,66 @@ TVM_REGISTER_GLOBAL("ir.OpResetAttr").set_body_typed([](Op op, String attr_name)
   reg.reset_attr(attr_name);
 });
 
-TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name) {
+TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name, String descr) {
   const OpRegEntry* reg = OpRegistry::Global()->Get(op_name);
   ICHECK(reg == nullptr) << "AttributeError: Operator " << op_name << " is registered before";
-  OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+  auto& op = OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+  op.describe(descr);
+});
+
+TVM_REGISTER_GLOBAL("ir.OpAddTypeRel")
+    .set_body_typed([](Op op, String rel_name, runtime::TVMArgValue value) {
+      auto& reg = OpRegistry::Global()->RegisterOrGet(op->name).set_name();
+      if (value.type_code() == kTVMPackedFuncHandle) {
+        // do an eager copy of the PackedFunc to avoid deleting function from frontend.
+        PackedFunc* fcopy = new PackedFunc(value.operator tvm::runtime::PackedFunc());

Review comment:
       Will be great to add TODO, and describe design choice here.
   Like @altanh said:
   "should emphasize this python API strictly for user prototyping and not for any checked in code, since the type function exposed through FFI is not the C++ type relation (it's strictly weaker since we don't have access to the type reporter, and cannot propagate constraints to the inputs, only to the output). We should probably not use "type relation" to describe the python API but I'm not sure what other name to use."

##########
File path: python/tvm/relay/op/op.py
##########
@@ -40,6 +40,40 @@ def get(op_name):
     return tvm.ir.Op.get(op_name)
 
 
+def register(op_name, describe=""):

Review comment:
       I think this is duplicated with ```register_op```, right?  Remove one and keep one you like.

##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,87 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):
+        """Attach the type function corresponding to the return type.
 
-def register_op(op_name):
-    """Register an operator by name
+        Parameters
+        ----------
+        rel_name : str
+            The type relation name to register.
+
+        type_rel_func: function (args: List[Type], attrs: Attrs) -> Type
+            The backing relation function which can solve an arbitrary relation on variables.
+            Differences with type_rel_func in C++:
+            1, when type_rel_func is not None:
+               1) OpAddTypeRel on C++ side will adjust type_rel_func with TypeReporter to
+                  calling convention of relay type system.
+               2) type_rel_func returns output argument's type, return None means can't
+                  infer output's type.
+               3) only support single output operators for now, the last argument is output tensor.
+            2, when type_rel_func is None, will call predefined type_rel_funcs in relay
+               accorrding to `tvm.relay.type_relation.` + rel_name.
+        """
+        _ffi_api.OpAddTypeRel(self, rel_name, type_rel_func)
+
+    def add_argument(self, name, type, description):  # pylint: disable=redefined-builtin
+        """Add arguments information to the function.
+
+        Parameters
+        ----------
+        name : str
+            The argument name.
+        type : str
+            The argument type.
+        description : str
+            The argument description.
+        """
+        _ffi_api.OpAddArgument(self, name, type, description)
+
+    def set_support_level(self, level):
+        """Set the support level of op.
+
+        Parameters
+        ----------
+        level : int
+            The support level.
+        """
+        _ffi_api.OpSetSupportLevel(self, level)
+
+    def set_num_inputs(self, n):
+        """Set the support level of op.
+
+        Parameters
+        ----------
+        n : int
+            The input number.
+        """
+        _ffi_api.OpSetNumInputs(self, n)
+
+    def set_attrs_type_key(self, key):
+        """Set the attribute type key of op.
+
+        Parameters
+        ----------
+        key : str
+            The type key.
+        """
+        _ffi_api.OpSetAttrsTypeKey(self, key)
+
+
+def register_op(op_name, describe=""):
+    """Register an operator by name.
+    when the op_name is not registered, create a new empty op with the given name.
+    when the op_name has been registered, abort with an error message.
 
     Parameters
     ----------
     op_name : str
         The name of new operator
+    describe : str

Review comment:
       describe: Optional[str]




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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r642760000



##########
File path: python/tvm/relay/op/op.py
##########
@@ -40,6 +40,40 @@ def get(op_name):
     return tvm.ir.Op.get(op_name)

Review comment:
       another example https://github.com/apache/tvm/blob/254563a3140cf63fe77a46058688209de3aa213c/tests/python/relay/test_ir_op.py#L23




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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r638626298



##########
File path: src/ir/op.cc
##########
@@ -102,10 +102,61 @@ TVM_REGISTER_GLOBAL("ir.OpResetAttr").set_body_typed([](Op op, String attr_name)
   reg.reset_attr(attr_name);
 });
 
-TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name) {
-  const OpRegEntry* reg = OpRegistry::Global()->Get(op_name);
-  ICHECK(reg == nullptr) << "AttributeError: Operator " << op_name << " is registered before";
-  OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name, String descr) {
+  auto& reg = OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+  reg.describe(descr);
+});
+
+TVM_REGISTER_GLOBAL("ir.OpAddTypeRel")
+    .set_body_typed([](Op op, String rel_name, runtime::TVMArgValue value) {
+      auto& reg = OpRegistry::Global()->RegisterOrGet(op->name).set_name();
+      if (value.type_code() == kTVMPackedFuncHandle) {
+        // do an eager copy of the PackedFunc to avoid deleting function from frontend.
+        PackedFunc* fcopy = new PackedFunc(value.operator tvm::runtime::PackedFunc());

Review comment:
       done




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



[GitHub] [tvm] ZihengJiang commented on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
ZihengJiang commented on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-854700301


   Merged. Thanks @xqdan 


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



[GitHub] [tvm] xqdan edited a comment on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan edited a comment on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-851468841


   > Hi @xqdan , I am thinking to use relay IR and TE in a hybrid way but that might beyond this PR. I will approve it and wait to see if @altanh and @zackcquic have any opinion for your updated changes.
   
   Thanks @ZihengJiang 
   Do you mean define custom op thru relay ir like relay.add, relay.sub ..., and then register it?
   We are trying to do this, this is an example:
   ```
   def layernorm_register_func():
       data = relay.var("input", dtype="float16")    
       gamma = relay.var("gamma",  dtype="float16")
       beta = relay.var("beta", dtype="float16")    
       mean = relay.mean(data, -1, True, False)
       sub_mean = relay.subtract(data, mean)
       mean_square = relay.multiply(sub_mean, sub_mean)
       variance = relay.mean(mean_square, -1, True, False)    
       z = rel.npu.scalaradd(variance, 1e-5)
       zz = relay.sqrt(z)
       u = relay.divide(sub_mean, zz)
       v = relay.multiply(u, gamma)
       w = relay.add(v, beta)    
       return relay.Function([data, gamma, beta], w).with_attr("Primitive", 1)
   ```


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



[GitHub] [tvm] xqdan edited a comment on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan edited a comment on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-851468841


   > Hi @xqdan , I am thinking to use relay IR and TE in a hybrid way but that might beyond this PR. I will approve it and wait to see if @altanh and @zackcquic have any opinion for your updated changes.
   
   Thanks @ZihengJiang 
   Do you mean define custom op thru relay ir like nn.add, nn.sub ..., and then register it?
   We are trying to do this, this is an example:
   ```
   def layernorm_register_func():
       data = relay.var("input", dtype="float16")    
       gamma = relay.var("gamma",  dtype="float16")
       beta = relay.var("beta", dtype="float16")    
       mean = relay.mean(data, -1, True, False)
       sub_mean = relay.subtract(data, mean)
       mean_square = relay.multiply(sub_mean, sub_mean)
       variance = relay.mean(mean_square, -1, True, False)    
       z = rel.npu.scalaradd(variance, 1e-5)
       zz = relay.sqrt(z)
       u = relay.divide(sub_mean, zz)
       v = relay.multiply(u, gamma)
       w = relay.add(v, beta)    
       return relay.Function([data, gamma, beta], w).with_attr("Primitive", 1)
   ```


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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r642736564



##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,87 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):
+        """Attach the type function corresponding to the return type.
 
-def register_op(op_name):
-    """Register an operator by name
+        Parameters
+        ----------
+        rel_name : str
+            The type relation name to register.
+
+        type_rel_func: function (args: List[Type], attrs: Attrs) -> Type
+            The backing relation function which can solve an arbitrary relation on variables.
+            Differences with type_rel_func in C++:
+            1, when type_rel_func is not None:
+               1) OpAddTypeRel on C++ side will adjust type_rel_func with TypeReporter to
+                  calling convention of relay type system.
+               2) type_rel_func returns output argument's type, return None means can't
+                  infer output's type.
+               3) only support single output operators for now, the last argument is output tensor.

Review comment:
       We can support when we need, add num_of_output in type_rel_func, the last num_of_output is output,  then we can reporter->Assign outputs.




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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r638619241



##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,76 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):

Review comment:
       tvm will report this, 
   ```
   Traceback (most recent call last):
     10: TVMFuncCall
     9: std::_Function_handler<void (tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TypedPackedFunc<tvm::IRModule (tvm::transform::Pass, tvm::IRModule)>::AssignTypedLambda<tvm::transform::{lambda(tvm::transform::Pass, tvm::IRModule)#10}>(tvm::transform::{lambda(tvm::transform::Pass, tvm::IRModule)#10}, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)::{lambda(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*)#1}>::_M_invoke(std::_Any_data const&, tvm::runtime::TVMArgs&&, tvm::runtime::TVMRetValue*&&)
     8: tvm::transform::Pass::operator()(tvm::IRModule) const
     7: tvm::transform::ModulePassNode::operator()(tvm::IRModule, tvm::transform::PassContext const&) const
     6: std::_Function_handler<void (tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TypedPackedFunc<tvm::IRModule (tvm::IRModule, tvm::transform::PassContext)>::AssignTypedLambda<tvm::parser::{lambda()#5}::operator()() const::{lambda(tvm::IRModule const&, tvm::transform::PassContext const&)#1}>(tvm::parser::{lambda()#5}::operator()() const::{lambda(tvm::IRModule const&, tvm::transform::PassContext const&)#1})::{lambda(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*)#1}>::_M_invoke(std::_Any_data const&, tvm::runtime::TVMArgs&&, tvm::runtime::TVMRetValue*&&)
     5: tvm::parser::ParseModule(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, tvm::runtime::Optional<tvm::IRModule>)
     4: tvm::transform::Pass::operator()(tvm::IRModule) const
     3: tvm::transform::ModulePassNode::operator()(tvm::IRModule, tvm::transform::PassContext const&) const
     2: std::_Function_handler<void (tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TypedPackedFunc<tvm::IRModule (tvm::IRModule, tvm::transform::PassContext)>::AssignTypedLambda<tvm::relay::transform::InferType()::{lambda(tvm::IRModule, tvm::transform::PassContext const&)#1}>(tvm::relay::transform::InferType()::{lambda(tvm::IRModule, tvm::transform::PassContext const&)#1})::{lambda(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*)#1}>::_M_invoke(std::_Any_data const&, tvm::runtime::TVMArgs&&, tvm::runtime::TVMRetValue*&&)
     1: tvm::relay::TypeInferencer::Infer(tvm::GlobalVar, tvm::relay::Function)
     0: tvm::relay::TypeSolver::Solve() [clone .cold]
     12: TVMFuncCall
     11: std::_Function_handler<void (tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TypedPackedFunc<tvm::IRModule (tvm::transform::Pass, tvm::IRModule)>::AssignTypedLambda<tvm::transform::{lambda(tvm::transform::Pass, tvm::IRModule)#10}>(tvm::transform::{lambda(tvm::transform::Pass, tvm::IRModule)#10}, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)::{lambda(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*)#1}>::_M_invoke(std::_Any_data const&, tvm::runtime::TVMArgs&&, tvm::runtime::TVMRetValue*&&)
     10: tvm::transform::Pass::operator()(tvm::IRModule) const
     9: tvm::transform::ModulePassNode::operator()(tvm::IRModule, tvm::transform::PassContext const&) const
     8: std::_Function_handler<void (tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TypedPackedFunc<tvm::IRModule (tvm::IRModule, tvm::transform::PassContext)>::AssignTypedLambda<tvm::parser::{lambda()#5}::operator()() const::{lambda(tvm::IRModule const&, tvm::transform::PassContext const&)#1}>(tvm::parser::{lambda()#5}::operator()() const::{lambda(tvm::IRModule const&, tvm::transform::PassContext const&)#1})::{lambda(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*)#1}>::_M_invoke(std::_Any_data const&, tvm::runtime::TVMArgs&&, tvm::runtime::TVMRetValue*&&)
     7: tvm::parser::ParseModule(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, tvm::runtime::Optional<tvm::IRModule>)
     6: tvm::transform::Pass::operator()(tvm::IRModule) const
     5: tvm::transform::ModulePassNode::operator()(tvm::IRModule, tvm::transform::PassContext const&) const
     4: std::_Function_handler<void (tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TypedPackedFunc<tvm::IRModule (tvm::IRModule, tvm::transform::PassContext)>::AssignTypedLambda<tvm::relay::transform::InferType()::{lambda(tvm::IRModule, tvm::transform::PassContext const&)#1}>(tvm::relay::transform::InferType()::{lambda(tvm::IRModule, tvm::transform::PassContext const&)#1})::{lambda(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*)#1}>::_M_invoke(std::_Any_data const&, tvm::runtime::TVMArgs&&, tvm::runtime::TVMRetValue*&&)
     3: tvm::relay::TypeInferencer::Infer(tvm::GlobalVar, tvm::relay::Function)
     2: tvm::relay::TypeSolver::Solve()
     1: std::_Function_handler<void (tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TypedPackedFunc<bool (tvm::runtime::Array<tvm::Type, void> const&, int, tvm::Attrs const&, tvm::TypeReporter const&)>::AssignTypedLambda<tvm::{lambda(tvm::Op, tvm::runtime::String, tvm::runtime::TVMArgValue)#7}::operator()(tvm::Op, tvm::runtime::String, tvm::runtime::TVMArgValue) const::{lambda(tvm::runtime::Array<tvm::Type, void> const&, int, tvm::Attrs const&, tvm::TypeReporter const&)#1}>(tvm::{lambda(tvm::Op, tvm::runtime::String, tvm::runtime::TVMArgValue)#7}::operator()(tvm::Op, tvm::runtime::String, tvm::runtime::TVMArgValue) const::{lambda(tvm::runtime::Array<tvm::Type, void> const&, int, tvm::Attrs const&, tvm::TypeReporter const&)#1})::{lambda(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*)#1}>::_M_invoke(std::_Any_data const&, tvm::runtime::TVMArgs&&, tvm::runtime::TVMRetValue*&&)
     0: std::_Function_handler<void (tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), TVMFuncCreateFromCFunc::{lambda(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*)#2}>::_M_invoke(std::_Any_data const&, tvm::runtime::TVMArgs&&, tvm::runtime::TVMRetValue*&&) [clone .cold]
     File "/home/xqdan/workspace3/tvm/python/tvm/_ffi/_ctypes/packed_func.py", line 81, in cfun
       rv = local_pyfunc(*pyargs)
     File "python/relay/test_type_infer.py", line 510, in custom_log1_rel
       assert len(arg_types) == 2, "type relation arg number mismatch!"
     File "/home/xqdan/workspace3/tvm/src/relay/analysis/type_solver.cc", line 624
   TVMError:
   ---------------------------------------------------------------
   An error occurred during the execution of TVM.
   For more information, please see: https://tvm.apache.org/docs/errors.html
   ---------------------------------------------------------------
     Check failed: (false) is false: AssertionError: type relation arg number mismatch!
   ```




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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r635765400



##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,76 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):

Review comment:
       I can add a complete testcase.




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



[GitHub] [tvm] Mousius commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
Mousius commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r644938146



##########
File path: include/tvm/ir/op.h
##########
@@ -244,12 +244,18 @@ class OpRegEntry {
       runtime::TypedPackedFunc<bool(const Array<Type>&, int, const Attrs&, const TypeReporter&)>
           type_rel_func);
   /*!
-   * \brief Set the the attrs type key and index to be AttrsType.
+   * \brief Set the attrs type key and index to be AttrsType.
    * \tparam AttrsType the attribute type to b set.
    * \return reference to self.
    */
   template <typename AttrsType>
   inline OpRegEntry& set_attrs_type();
+  /*!
+   * \brief Set the attrs type key and index to be AttrsType.
+   * \tparam key The attribute type key to be set.

Review comment:
       I think this should be `\param` rather than `\tparam`?




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



[GitHub] [tvm] xqdan commented on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-851468841


   > Hi @xqdan , I am thinking to use relay IR and TE in a hybrid way but that might beyond this PR. I will approve it and wait to see if @altanh and @zackcquic have any opinion for your updated changes.
   
   Thanks @ZihengJiang 
   Do you mean define custom op thru relay ir like nn.add, nn.sub ..., and then register it?


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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r642736564



##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,87 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):
+        """Attach the type function corresponding to the return type.
 
-def register_op(op_name):
-    """Register an operator by name
+        Parameters
+        ----------
+        rel_name : str
+            The type relation name to register.
+
+        type_rel_func: function (args: List[Type], attrs: Attrs) -> Type
+            The backing relation function which can solve an arbitrary relation on variables.
+            Differences with type_rel_func in C++:
+            1, when type_rel_func is not None:
+               1) OpAddTypeRel on C++ side will adjust type_rel_func with TypeReporter to
+                  calling convention of relay type system.
+               2) type_rel_func returns output argument's type, return None means can't
+                  infer output's type.
+               3) only support single output operators for now, the last argument is output tensor.

Review comment:
       We can support when we need, add num_of_output in type_rel_func, the last num_of_output is output,  so we can reporter->Assign outputs.




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



[GitHub] [tvm] zackcquic commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
zackcquic commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r642511359



##########
File path: python/tvm/relay/op/op.py
##########
@@ -40,6 +40,40 @@ def get(op_name):
     return tvm.ir.Op.get(op_name)
 
 
+def register(op_name, describe=""):

Review comment:
       I think this is duplicated (API meaning is similar) with ```register_op```, right?  Remove one and keep one you like.




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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r635762635



##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,76 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):

Review comment:
       ```
   
   from __future__ import absolute_import as _abs
   from tvm.relay.op import op as _op
   from tvm import relay
   from tvm.ir.attrs import DictAttrs
   from tx.utils.utils import get_broadcast_result_shape
   
   
   def npu_add_rel(arg_types, attrs):
       """Define npu.add type relation.
   
       Parameters
       ----------
       arg_types : List[Type]
           All input types.
   
       attrs : DictAttrs
           Attributes of the OpNode.
   
       Returns
       ----------
       out : Type
           The output type.
           Return None if error occurs.
   
       """
       assert len(arg_types) == 2, "type relation arg number mismatch!"
       if attrs:
           assert isinstance(attrs, DictAttrs)
   
       inputa_type = arg_types[0]
       inputb_type = arg_types[1]
       if not isinstance(inputa_type, relay.TensorType):
           return None
       if not isinstance(inputb_type, relay.TensorType):
           return None
   
       output_shape = get_broadcast_result_shape(inputa_type.shape, inputb_type.shape)
   
       return relay.TensorType(output_shape, inputa_type.dtype)
   
   
   # All the following steps must be done to compose an user define Operator.
   _op.register("npu.add", r"code(Add two tensor with inner broadcasting.)code")
   _op.get("npu.add").set_num_inputs(2)
   _op.get("npu.add").add_argument("data_0", "Tensor", "The input data tensor.")
   _op.get("npu.add").add_argument("data_1", "Tensor", "The input data tensor.")
   _op.get("npu.add").add_type_rel("NPUAdd", npu_add_rel)
   _op.get("npu.add").set_support_level(1)
   _op.register_pattern("npu.add", _op.OpPattern.ELEMWISE)
   _op.register_stateful("npu.add", False)
   ```
   




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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r642737680



##########
File path: python/tvm/relay/op/op.py
##########
@@ -40,6 +40,40 @@ def get(op_name):
     return tvm.ir.Op.get(op_name)
 
 
+def register(op_name, describe=""):

Review comment:
       Yes, let's keep this one, since it is under relay.op namespace with other similar APIs.




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



[GitHub] [tvm] zackcquic commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
zackcquic commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r635750074



##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,76 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):

Review comment:
       Could you provide example and document how to rewrite ```type_rel_func```? Since it seems different with c++ API.
   
   Also type_rel_func is used in ```InferType``` pass, could you provide a unittest test that applies ```InferType``` on module with custom ops?
   
   

##########
File path: tests/python/relay/test_ir_op.py
##########
@@ -103,11 +104,23 @@ def test_op_register():
     """Tests register_op functionality."""
     op_name = "custom_op"
 
-    tvm.ir.register_op(op_name)
-    tvm.ir.register_op_attr(op_name, "num_inputs", 2, 256)
+    def custom_op_rel(x):
+        return x + 1
 
-    assert tvm.ir.Op.get(op_name).name == op_name
-    assert tvm.ir.Op.get(op_name).num_inputs == 2
+    _op.register(op_name, r"code(Add two tensor with inner broadcasting.)code")
+    _op.get(op_name).set_num_inputs(2)
+    _op.get(op_name).add_argument("data_0", "Tensor", "The input data tensor.")
+    _op.get(op_name).add_argument("data_1", "Tensor", "The input data tensor.")
+    _op.get(op_name).add_type_rel("CustomOp", custom_op_rel)
+    _op.get(op_name).set_support_level(1)
+    _op.register_pattern(op_name, _op.OpPattern.ELEMWISE)
+    _op.register_stateful(op_name, False)
+
+    assert _op.get(op_name).name == op_name
+    assert _op.get(op_name).num_inputs == 2
+    assert _op.get(op_name).get_attr("CustomOp")(1) == 2

Review comment:
       ```add_type_rel``` doesn't add attribute to op but register type relations.




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



[GitHub] [tvm] xqdan commented on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-851193202


   > The PR overall looks good. One question is, is there any support or have you thought about to register TVMCompute via such API?
   
   Thanks for your review, @ZihengJiang 
   
   Do you mean this? https://github.com/apache/tvm/blob/813136401a11a49d6c15e6013c34dd822a5c4ff6/python/tvm/relay/op/op.py#L171
   
   Since we can define custom op thru tvm compute in python, and we can register it in python, I think it's ok for now.


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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r635762934



##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,76 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):

Review comment:
       We register op like this in our in-house project




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



[GitHub] [tvm] xqdan commented on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-851782689


   > Hi @xqdan,
   > 
   > Sorry for the late reply.
   > This PR is very useful.
   > (Actually I already use this PR to do some experiments, thanks a lot 👍)
   > 
   > I am wondering if you have plan to make it more like C++ API (eg export TypeReporter, etc).
   > I agree with @altanh, should clarify what is done and what is not done, so users could quickly catch up.
   > I can only think about adding comments/documents, maybe you and others may come up with better idea?
   
   Thanks for review.  @zackcquic 
   I think it's possible to expose TypeReporter to python side, in this PR, as your suggestion, I put more comments for now.


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



[GitHub] [tvm] xqdan commented on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-844654355


   @FrozenGene 


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



[GitHub] [tvm] ZihengJiang commented on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
ZihengJiang commented on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-851462812


   Hi @xqdan , I am thinking to use relay IR and TE in a hybrid way but that might beyond this PR. I will approve it and wait to see if @altanh and @zackcquic have any opinion for your updated changes.


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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r638626603



##########
File path: python/tvm/relay/op/op.py
##########
@@ -40,6 +40,42 @@ def get(op_name):
     return tvm.ir.Op.get(op_name)
 
 
+def register(op_name, describe=""):

Review comment:
       done

##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,76 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):
+        """Attach the type function corresponding to the return type.
 
-def register_op(op_name):
+        Parameters
+        ----------
+        rel_name : str
+            The type relation name to register.
+
+        type_rel_func: function (args: List[Type], num_inputs:int, attrs: Attrs) -> Type
+            The backing relation function which can solve an arbitrary relation on variables.
+        """
+        _ffi_api.OpAddTypeRel(self, rel_name, type_rel_func)
+
+    def add_argument(self, name, type, description):  # pylint: disable=redefined-builtin
+        """Add arguments information to the function.
+
+        Parameters
+        ----------
+        name : str
+            The argument name.
+        type : str
+            The argument type.
+        description : str
+            The argument description.
+        """
+        _ffi_api.OpAddArgument(self, name, type, description)
+
+    def set_support_level(self, level):
+        """Set the support level of op.
+
+        Parameters
+        ----------
+        level : int
+            The support level.
+        """
+        _ffi_api.OpSetSupportLevel(self, level)
+
+    def set_num_inputs(self, n):
+        """Set the support level of op.
+
+        Parameters
+        ----------
+        n : int
+            The input number.
+        """
+        _ffi_api.OpSetNumInputs(self, n)
+
+    def set_attrs_type_key(self, key):
+        """Set the attribute type key of op.
+
+        Parameters
+        ----------
+        key : str
+            The type key.
+        """
+        _ffi_api.OpSetAttrsTypeKey(self, key)
+
+
+def register_op(op_name, describe=""):

Review comment:
       done




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



[GitHub] [tvm] xqdan edited a comment on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan edited a comment on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-844654355


   also cc @FrozenGene please review


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



[GitHub] [tvm] zackcquic commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
zackcquic commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r643118967



##########
File path: python/tvm/relay/op/op.py
##########
@@ -40,6 +40,40 @@ def get(op_name):
     return tvm.ir.Op.get(op_name)

Review comment:
       Sorry, I just got messed with GitHub interface. You are right.




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



[GitHub] [tvm] xqdan commented on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-847714999


   @altanh @zackcquic @ZihengJiang please take a look


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



[GitHub] [tvm] Mousius commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
Mousius commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r644938146



##########
File path: include/tvm/ir/op.h
##########
@@ -244,12 +244,18 @@ class OpRegEntry {
       runtime::TypedPackedFunc<bool(const Array<Type>&, int, const Attrs&, const TypeReporter&)>
           type_rel_func);
   /*!
-   * \brief Set the the attrs type key and index to be AttrsType.
+   * \brief Set the attrs type key and index to be AttrsType.
    * \tparam AttrsType the attribute type to b set.
    * \return reference to self.
    */
   template <typename AttrsType>
   inline OpRegEntry& set_attrs_type();
+  /*!
+   * \brief Set the attrs type key and index to be AttrsType.
+   * \tparam key The attribute type key to be set.

Review comment:
       I think this should be `\param` rather than `\tparam`?




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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r645228765



##########
File path: include/tvm/ir/op.h
##########
@@ -244,12 +244,18 @@ class OpRegEntry {
       runtime::TypedPackedFunc<bool(const Array<Type>&, int, const Attrs&, const TypeReporter&)>
           type_rel_func);
   /*!
-   * \brief Set the the attrs type key and index to be AttrsType.
+   * \brief Set the attrs type key and index to be AttrsType.
    * \tparam AttrsType the attribute type to b set.
    * \return reference to self.
    */
   template <typename AttrsType>
   inline OpRegEntry& set_attrs_type();
+  /*!
+   * \brief Set the attrs type key and index to be AttrsType.
+   * \tparam key The attribute type key to be set.

Review comment:
       Thanks




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



[GitHub] [tvm] zackcquic commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
zackcquic commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r637993294



##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,76 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):

Review comment:
       Since we are now in python world, what will happened if type_rel_func raises exception?
   Could add testcase for this?

##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,76 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):
+        """Attach the type function corresponding to the return type.
 
-def register_op(op_name):
+        Parameters
+        ----------
+        rel_name : str
+            The type relation name to register.
+
+        type_rel_func: function (args: List[Type], num_inputs:int, attrs: Attrs) -> Type

Review comment:
       From the testcase you provided and ``ir.OpAddTypeRel```, I think the function signature should be function  (args : List[Type], attrs) -> Type and first argument only contains input types while return value has special meaning (None vs defined)
   The callback function is quite different with ```type_rel_func``` in C++, it will be great to document it here.

##########
File path: include/tvm/ir/op.h
##########
@@ -250,6 +250,12 @@ class OpRegEntry {
    */
   template <typename AttrsType>
   inline OpRegEntry& set_attrs_type();
+  /*!
+   * \brief Set the the attrs type key and index to be AttrsType.
+   * \tparam key the attribute type key to be set.

Review comment:
       Will be great to use upper case after param name (the attirbute -> The attribute) to be consistent with other comments

##########
File path: src/ir/op.cc
##########
@@ -102,10 +102,61 @@ TVM_REGISTER_GLOBAL("ir.OpResetAttr").set_body_typed([](Op op, String attr_name)
   reg.reset_attr(attr_name);
 });
 
-TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name) {
-  const OpRegEntry* reg = OpRegistry::Global()->Get(op_name);
-  ICHECK(reg == nullptr) << "AttributeError: Operator " << op_name << " is registered before";
-  OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name, String descr) {
+  auto& reg = OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+  reg.describe(descr);
+});
+
+TVM_REGISTER_GLOBAL("ir.OpAddTypeRel")
+    .set_body_typed([](Op op, String rel_name, runtime::TVMArgValue value) {
+      auto& reg = OpRegistry::Global()->RegisterOrGet(op->name).set_name();
+      if (value.type_code() == kTVMPackedFuncHandle) {
+        // do an eager copy of the PackedFunc to avoid deleting function from frontend.
+        PackedFunc* fcopy = new PackedFunc(value.operator tvm::runtime::PackedFunc());
+        auto f = [=](const Array<Type>& args, int num_inputs, const Attrs& attrs,
+                     const TypeReporter& reporter) -> bool {
+          Array<Type> input_types(args.begin(), args.end() - 1);
+          // call customized relation functions
+          Type ret_type = (*fcopy)(input_types, attrs);
+          if (ret_type.defined()) {
+            // the last argument is output
+            reporter->Assign(args[args.size() - 1], ret_type);
+            return true;
+          }
+          return false;
+        };
+        // adjust function call to relay type system with TypeReporter
+        auto type_rel = runtime::TypedPackedFunc<bool(const Array<Type>&, int, const Attrs&,
+                                                      const TypeReporter&)>(f);
+        reg.add_type_rel(rel_name, type_rel);
+      } else if (value.type_code() == kTVMNullptr) {
+        // Call relation functions of relay
+        auto func_name = std::string("tvm.relay.type_relation.") + rel_name;

Review comment:
       Document this behavior in python world. 
   Also it will be great to add more test cases for these:
   1. search for builtin relation, eg BroadcastRel. (I saw there are some in test cases.)
   2. search for registered custom type relation.
   

##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,76 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):
+        """Attach the type function corresponding to the return type.
 
-def register_op(op_name):
+        Parameters
+        ----------
+        rel_name : str
+            The type relation name to register.
+
+        type_rel_func: function (args: List[Type], num_inputs:int, attrs: Attrs) -> Type

Review comment:
       Just found in ```ir.OpAddTypeRel```,```type_rel_fun``` could be None, then it will looking for registered relations.
   It will be great to document this behavior.

##########
File path: src/ir/op.cc
##########
@@ -102,10 +102,61 @@ TVM_REGISTER_GLOBAL("ir.OpResetAttr").set_body_typed([](Op op, String attr_name)
   reg.reset_attr(attr_name);
 });
 
-TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name) {
-  const OpRegEntry* reg = OpRegistry::Global()->Get(op_name);
-  ICHECK(reg == nullptr) << "AttributeError: Operator " << op_name << " is registered before";
-  OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name, String descr) {
+  auto& reg = OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+  reg.describe(descr);
+});
+
+TVM_REGISTER_GLOBAL("ir.OpAddTypeRel")
+    .set_body_typed([](Op op, String rel_name, runtime::TVMArgValue value) {
+      auto& reg = OpRegistry::Global()->RegisterOrGet(op->name).set_name();
+      if (value.type_code() == kTVMPackedFuncHandle) {
+        // do an eager copy of the PackedFunc to avoid deleting function from frontend.
+        PackedFunc* fcopy = new PackedFunc(value.operator tvm::runtime::PackedFunc());

Review comment:
       It will be great to document ```fcopy```'s function signature and it's special meaning of return value.
   (Because in before, ```TypeReporter``` takes care of these.)
   
   Also, consider some error handling (exception raised?) or limitation documented.

##########
File path: src/ir/op.cc
##########
@@ -102,10 +102,61 @@ TVM_REGISTER_GLOBAL("ir.OpResetAttr").set_body_typed([](Op op, String attr_name)
   reg.reset_attr(attr_name);
 });
 
-TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name) {
-  const OpRegEntry* reg = OpRegistry::Global()->Get(op_name);
-  ICHECK(reg == nullptr) << "AttributeError: Operator " << op_name << " is registered before";
-  OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+TVM_REGISTER_GLOBAL("ir.RegisterOp").set_body_typed([](String op_name, String descr) {
+  auto& reg = OpRegistry::Global()->RegisterOrGet(op_name).set_name();
+  reg.describe(descr);

Review comment:
       Will be great to check if ```op_name``` is registered before or not?

##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,76 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):
+        """Attach the type function corresponding to the return type.
 
-def register_op(op_name):
+        Parameters
+        ----------
+        rel_name : str
+            The type relation name to register.
+
+        type_rel_func: function (args: List[Type], num_inputs:int, attrs: Attrs) -> Type
+            The backing relation function which can solve an arbitrary relation on variables.
+        """
+        _ffi_api.OpAddTypeRel(self, rel_name, type_rel_func)
+
+    def add_argument(self, name, type, description):  # pylint: disable=redefined-builtin
+        """Add arguments information to the function.
+
+        Parameters
+        ----------
+        name : str
+            The argument name.
+        type : str
+            The argument type.
+        description : str
+            The argument description.
+        """
+        _ffi_api.OpAddArgument(self, name, type, description)
+
+    def set_support_level(self, level):
+        """Set the support level of op.
+
+        Parameters
+        ----------
+        level : int
+            The support level.
+        """
+        _ffi_api.OpSetSupportLevel(self, level)
+
+    def set_num_inputs(self, n):
+        """Set the support level of op.
+
+        Parameters
+        ----------
+        n : int
+            The input number.
+        """
+        _ffi_api.OpSetNumInputs(self, n)
+
+    def set_attrs_type_key(self, key):
+        """Set the attribute type key of op.
+
+        Parameters
+        ----------
+        key : str
+            The type key.
+        """
+        _ffi_api.OpSetAttrsTypeKey(self, key)
+
+
+def register_op(op_name, describe=""):
     """Register an operator by name
 
     Parameters
     ----------
     op_name : str
         The name of new operator
+    describe : str
+        The detail describe of new operator
     """
 
-    _ffi_api.RegisterOp(op_name)
+    return _ffi_api.RegisterOp(op_name, describe)

Review comment:
       I considered ```return``` op reference before, but I removed it finally.
   How do you think? Will it be common use case?

##########
File path: src/relay/op/tensor/reduce.cc
##########
@@ -315,6 +315,8 @@ bool ReduceRel(const Array<Type>& types, int num_inputs, const Attrs& attrs,
   return true;
 }
 
+TVM_REGISTER_GLOBAL("tvm.relay.type_relation.ReduceRel").set_body_typed(ReduceRel);

Review comment:
       Maybe we don't need to expose these (which we need to expose all the c++ relations)
   I just found in test case [test_ir_type.py:94](https://github.com/apache/tvm/blob/0b2f30aef2c1c1ed4ec504157b54ceaab182e9ab/tests/python/unittest/test_ir_type.py#L94), may be we can use this way.




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



[GitHub] [tvm] xqdan commented on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-847617680


   > thanks for the PR, being able to register ops fully in python is a great tool for prototyping. I have a few concerns for this approach though:
   > 
   > 1. IMO, we should emphasize this python API strictly for user prototyping and not for any checked in  code, since the type function exposed through FFI is not the C++ type relation (it's strictly weaker since we don't have access to the type reporter, and cannot propagate constraints to the inputs, only to the output). We should probably not use "type relation" to describe the python API but I'm not sure what other name to use.
   
   Since we still call C++ type relation funcs underhood, I keep the name but add more comments in python api, is it ok?
   
   > 2. Related to the previous point, the python API (at least currently) does not have access to diagnostics for reporting type errors, which is not good for user experience (indeed we're trying to port ICHECKs to diagnostics where possible).
   > 
   
   Let me try add a case for python exceptions.
   
   > Ideally we should expose all the machinery needed but for prototyping it might not be necessary. cc @jroesch for thoughts
   
   


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



[GitHub] [tvm] xqdan edited a comment on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan edited a comment on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-847617680


   > thanks for the PR, being able to register ops fully in python is a great tool for prototyping. I have a few concerns for this approach though:
   > 
   > 1. IMO, we should emphasize this python API strictly for user prototyping and not for any checked in  code, since the type function exposed through FFI is not the C++ type relation (it's strictly weaker since we don't have access to the type reporter, and cannot propagate constraints to the inputs, only to the output). We should probably not use "type relation" to describe the python API but I'm not sure what other name to use.
   
   Since we still call C++ type relation funcs underhood(such as test_custom_op_infer), I keep the name but add more comments in python api, is it ok?
   
   > 2. Related to the previous point, the python API (at least currently) does not have access to diagnostics for reporting type errors, which is not good for user experience (indeed we're trying to port ICHECKs to diagnostics where possible).
   > 
   
   Let me try add a case for python exceptions.
   
   > Ideally we should expose all the machinery needed but for prototyping it might not be necessary. cc @jroesch for thoughts
   
   


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



[GitHub] [tvm] xqdan commented on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-844692104


   > @xqdan Thanks a lot for this PR.
   > 
   > I'd really like to complete registering ops from python.
   > Some discussions are in [RFC 9903](https://discuss.tvm.apache.org/t/rfc-register-ops-from-python-site-and-followup/9903/4).
   > 
   > The major problem (why I didn't complete it at once) is that the `TypeRelation` currently is pure C++ with very limited FFI.
   > Also, need to think about how to use TypeReporter while `InferType` from python?
   
   We may need to expose TypeReporter to python side, also some basic TypeRelation function.


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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r642754455



##########
File path: python/tvm/relay/op/op.py
##########
@@ -40,6 +40,40 @@ def get(op_name):
     return tvm.ir.Op.get(op_name)

Review comment:
       I don't touch this code, also the returned value is meaningful, such as `_op.get(op_name).add_type_rel("Identity")`




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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r645228765



##########
File path: include/tvm/ir/op.h
##########
@@ -244,12 +244,18 @@ class OpRegEntry {
       runtime::TypedPackedFunc<bool(const Array<Type>&, int, const Attrs&, const TypeReporter&)>
           type_rel_func);
   /*!
-   * \brief Set the the attrs type key and index to be AttrsType.
+   * \brief Set the attrs type key and index to be AttrsType.
    * \tparam AttrsType the attribute type to b set.
    * \return reference to self.
    */
   template <typename AttrsType>
   inline OpRegEntry& set_attrs_type();
+  /*!
+   * \brief Set the attrs type key and index to be AttrsType.
+   * \tparam key The attribute type key to be set.

Review comment:
       Thanks

##########
File path: include/tvm/ir/op.h
##########
@@ -244,12 +244,18 @@ class OpRegEntry {
       runtime::TypedPackedFunc<bool(const Array<Type>&, int, const Attrs&, const TypeReporter&)>
           type_rel_func);
   /*!
-   * \brief Set the the attrs type key and index to be AttrsType.
+   * \brief Set the attrs type key and index to be AttrsType.
    * \tparam AttrsType the attribute type to b set.
    * \return reference to self.
    */
   template <typename AttrsType>
   inline OpRegEntry& set_attrs_type();
+  /*!
+   * \brief Set the attrs type key and index to be AttrsType.
+   * \tparam key The attribute type key to be set.

Review comment:
       > I think this should be `\param` rather than `\tparam`?
   
   \tparam means template param, so tparam is ok




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



[GitHub] [tvm] ZihengJiang merged pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
ZihengJiang merged pull request #8079:
URL: https://github.com/apache/tvm/pull/8079


   


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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r642742102



##########
File path: python/tvm/relay/op/op.py
##########
@@ -40,6 +40,40 @@ def get(op_name):
     return tvm.ir.Op.get(op_name)
 
 
+def register(op_name, describe=""):

Review comment:
       ok, let's keep this one




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



[GitHub] [tvm] xqdan edited a comment on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan edited a comment on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-844692104


   > @xqdan Thanks a lot for this PR.
   > 
   > I'd really like to complete registering ops from python.
   > Some discussions are in [RFC 9903](https://discuss.tvm.apache.org/t/rfc-register-ops-from-python-site-and-followup/9903/4).
   > 
   > The major problem (why I didn't complete it at once) is that the `TypeRelation` currently is pure C++ with very limited FFI.
   > Also, need to think about how to use TypeReporter while `InferType` from python?
   
   1, We may need to expose some basic TypeRelation function without TypeReporter .
   2, We apply infer result by TypeReporter in ir.OpAddTypeRel, which is c++ side. we rewrite the relation function here.


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



[GitHub] [tvm] xqdan edited a comment on pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan edited a comment on pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#issuecomment-844692104


   > @xqdan Thanks a lot for this PR.
   > 
   > I'd really like to complete registering ops from python.
   > Some discussions are in [RFC 9903](https://discuss.tvm.apache.org/t/rfc-register-ops-from-python-site-and-followup/9903/4).
   > 
   > The major problem (why I didn't complete it at once) is that the `TypeRelation` currently is pure C++ with very limited FFI.
   > Also, need to think about how to use TypeReporter while `InferType` from python?
   
   1, We may need to expose some basic TypeRelation function without TypeReporter .
   2, We apply infer result by TypeReporter in ir.OpAddTypeRel, which is c++ side.


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



[GitHub] [tvm] altanh commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
altanh commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r638235413



##########
File path: python/tvm/relay/op/op.py
##########
@@ -40,6 +40,42 @@ def get(op_name):
     return tvm.ir.Op.get(op_name)
 
 
+def register(op_name, describe=""):

Review comment:
       so when the op_name is not registered, this will create a new (empty?) op with the given name? perhaps update the documentation to mention this

##########
File path: include/tvm/ir/op.h
##########
@@ -250,6 +250,12 @@ class OpRegEntry {
    */
   template <typename AttrsType>
   inline OpRegEntry& set_attrs_type();
+  /*!
+   * \brief Set the the attrs type key and index to be AttrsType.

Review comment:
       ```suggestion
      * \brief Set the attrs type key and index to be AttrsType.
   ```

##########
File path: python/tvm/ir/op.py
##########
@@ -85,17 +85,76 @@ def reset_attr(self, attr_name):
         """
         _ffi_api.OpResetAttr(self, attr_name)
 
+    def add_type_rel(self, rel_name, type_rel_func=None):
+        """Attach the type function corresponding to the return type.
 
-def register_op(op_name):
+        Parameters
+        ----------
+        rel_name : str
+            The type relation name to register.
+
+        type_rel_func: function (args: List[Type], num_inputs:int, attrs: Attrs) -> Type
+            The backing relation function which can solve an arbitrary relation on variables.
+        """
+        _ffi_api.OpAddTypeRel(self, rel_name, type_rel_func)
+
+    def add_argument(self, name, type, description):  # pylint: disable=redefined-builtin
+        """Add arguments information to the function.
+
+        Parameters
+        ----------
+        name : str
+            The argument name.
+        type : str
+            The argument type.
+        description : str
+            The argument description.
+        """
+        _ffi_api.OpAddArgument(self, name, type, description)
+
+    def set_support_level(self, level):
+        """Set the support level of op.
+
+        Parameters
+        ----------
+        level : int
+            The support level.
+        """
+        _ffi_api.OpSetSupportLevel(self, level)
+
+    def set_num_inputs(self, n):
+        """Set the support level of op.
+
+        Parameters
+        ----------
+        n : int
+            The input number.
+        """
+        _ffi_api.OpSetNumInputs(self, n)
+
+    def set_attrs_type_key(self, key):
+        """Set the attribute type key of op.
+
+        Parameters
+        ----------
+        key : str
+            The type key.
+        """
+        _ffi_api.OpSetAttrsTypeKey(self, key)
+
+
+def register_op(op_name, describe=""):

Review comment:
       if `op_name` is registered, does this return the registered op? If so, update documentation




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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r642737680



##########
File path: python/tvm/relay/op/op.py
##########
@@ -40,6 +40,40 @@ def get(op_name):
     return tvm.ir.Op.get(op_name)
 
 
+def register(op_name, describe=""):

Review comment:
       Yes, let's keep this one, since it is under relay.op namespace with other similar APIs.




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



[GitHub] [tvm] xqdan commented on a change in pull request #8079: Complete register op from python

Posted by GitBox <gi...@apache.org>.
xqdan commented on a change in pull request #8079:
URL: https://github.com/apache/tvm/pull/8079#discussion_r645236138



##########
File path: include/tvm/ir/op.h
##########
@@ -244,12 +244,18 @@ class OpRegEntry {
       runtime::TypedPackedFunc<bool(const Array<Type>&, int, const Attrs&, const TypeReporter&)>
           type_rel_func);
   /*!
-   * \brief Set the the attrs type key and index to be AttrsType.
+   * \brief Set the attrs type key and index to be AttrsType.
    * \tparam AttrsType the attribute type to b set.
    * \return reference to self.
    */
   template <typename AttrsType>
   inline OpRegEntry& set_attrs_type();
+  /*!
+   * \brief Set the attrs type key and index to be AttrsType.
+   * \tparam key The attribute type key to be set.

Review comment:
       > I think this should be `\param` rather than `\tparam`?
   
   \tparam means template param, so tparam is ok




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