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 2020/06/10 15:08:56 UTC

[GitHub] [incubator-tvm] mbaret opened a new pull request #5766: [BYOC][FIX] Infer types in MergeComposite

mbaret opened a new pull request #5766:
URL: https://github.com/apache/incubator-tvm/pull/5766


   If InferType isn't run between partitioning passes, function calls are inserted which don't have a type. This can result in failures for patterns which want to check types.
   
   This works around it simply by running InferType after every partitioning.


----------------------------------------------------------------
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] [incubator-tvm] zhiics commented on a change in pull request #5766: [BYOC][FIX] Infer types in MergeComposite

Posted by GitBox <gi...@apache.org>.
zhiics commented on a change in pull request #5766:
URL: https://github.com/apache/incubator-tvm/pull/5766#discussion_r438322794



##########
File path: src/relay/transforms/merge_composite.cc
##########
@@ -36,17 +36,24 @@ namespace tvm {
 namespace relay {
 namespace merge_composite {
 
+Function InferType(const Function& expr) {
+  auto mod = IRModule::FromExpr(expr);
+  mod = transform::InferType()(mod);
+  return Downcast<Function>(mod->Lookup("main"));
+}
+
 Expr MergeComposite(const Function& func, const Array<runtime::String>& pattern_names,
                     const Array<DFPattern>& patterns, const std::vector<PackedFunc>& checks) {
   CHECK_EQ(pattern_names.size(), patterns.size());
-  Expr merged_expr = func->body;
+  Function merged_func = func;
   // merge the patterns one-by-one in order
   for (size_t i = 0; i < patterns.size(); i++) {
     Map<String, ObjectRef> attrs;
     attrs.Set("Composite", pattern_names[i]);
-    merged_expr = PartitionPattern(patterns[i], merged_expr, attrs, checks[i]);
+    merged_func = Downcast<Function>(PartitionPattern(patterns[i], merged_func, attrs, checks[i]));
+    merged_func = InferType(merged_func);

Review comment:
       Do we need to infer type here? The resulted function should be added the IRModule since it is FunctionPass.
   
   https://github.com/apache/incubator-tvm/blob/ed583092dbeb4f1b0458ad015f607f0746d61e80/src/relay/ir/transform.cc#L135
   
   This would do type inference implicitly as module is strongly typed. Do I miss something?




----------------------------------------------------------------
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] [incubator-tvm] mbaret commented on pull request #5766: [BYOC][FIX] Infer types in MergeComposite

Posted by GitBox <gi...@apache.org>.
mbaret commented on pull request #5766:
URL: https://github.com/apache/incubator-tvm/pull/5766#issuecomment-642072780


   cc @mbrookhart @comaniac 


----------------------------------------------------------------
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] [incubator-tvm] tqchen commented on pull request #5766: [BYOC][FIX] Infer types in MergeComposite

Posted by GitBox <gi...@apache.org>.
tqchen commented on pull request #5766:
URL: https://github.com/apache/incubator-tvm/pull/5766#issuecomment-643339971


   @zhiics please manage this PR


----------------------------------------------------------------
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] [incubator-tvm] mbaret commented on a change in pull request #5766: [BYOC][FIX] Infer types in MergeComposite

Posted by GitBox <gi...@apache.org>.
mbaret commented on a change in pull request #5766:
URL: https://github.com/apache/incubator-tvm/pull/5766#discussion_r438334823



##########
File path: src/relay/transforms/merge_composite.cc
##########
@@ -36,17 +36,24 @@ namespace tvm {
 namespace relay {
 namespace merge_composite {
 
+Function InferType(const Function& expr) {
+  auto mod = IRModule::FromExpr(expr);
+  mod = transform::InferType()(mod);
+  return Downcast<Function>(mod->Lookup("main"));
+}
+
 Expr MergeComposite(const Function& func, const Array<runtime::String>& pattern_names,
                     const Array<DFPattern>& patterns, const std::vector<PackedFunc>& checks) {
   CHECK_EQ(pattern_names.size(), patterns.size());
-  Expr merged_expr = func->body;
+  Function merged_func = func;
   // merge the patterns one-by-one in order
   for (size_t i = 0; i < patterns.size(); i++) {
     Map<String, ObjectRef> attrs;
     attrs.Set("Composite", pattern_names[i]);
-    merged_expr = PartitionPattern(patterns[i], merged_expr, attrs, checks[i]);
+    merged_func = Downcast<Function>(PartitionPattern(patterns[i], merged_func, attrs, checks[i]));
+    merged_func = InferType(merged_func);

Review comment:
       The type inference needs to happen between every partitioning. This is because partitioning erases type information.




----------------------------------------------------------------
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] [incubator-tvm] mbaret commented on a change in pull request #5766: [BYOC][FIX] Infer types in MergeComposite

Posted by GitBox <gi...@apache.org>.
mbaret commented on a change in pull request #5766:
URL: https://github.com/apache/incubator-tvm/pull/5766#discussion_r438261289



##########
File path: tests/python/relay/test_pass_merge_composite.py
##########
@@ -953,26 +985,28 @@ def _check_type_false(extract):
         return bool(typ.shape[0] != 1)
 
     pattern_table_false = [
+        ("add_relu", make_add_relu_pattern()),
         ("conv_bias_relu", make_conv_bias_relu_pattern(), _check_type_false)
     ]
-    check_result(pattern_table_false, before(), before())
+    check_result(pattern_table_false, before(), expected_false())
 
     pattern_table_true = [
+        ("add_relu", make_add_relu_pattern()),
         ("conv_bias_relu", make_conv_bias_relu_pattern(), _check_type_true)
     ]
-    check_result(pattern_table_true, before(), expected())
+    check_result(pattern_table_true, before(), expected_true())
 
 
 if __name__ == "__main__":
-    test_simple_merge()
-    test_branch_merge()
-    test_multiple_patterns()
-    test_optional_pattern()
-    test_merge_order()
-    test_parallel_merge()
-    test_multiple_input_subgraphs()
-    test_reuse_call_merge()
-    test_tuple_get_item_merge()
-    test_pattern_with_check()
-    test_diamond_not_merge()
+    # test_simple_merge()
+    # test_branch_merge()
+    # test_multiple_patterns()
+    # test_optional_pattern()
+    # test_merge_order()
+    # test_parallel_merge()
+    # test_multiple_input_subgraphs()
+    # test_reuse_call_merge()
+    # test_tuple_get_item_merge()
+    # test_pattern_with_check()
+    # test_diamond_not_merge()

Review comment:
       Ah... Fixed :)




----------------------------------------------------------------
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] [incubator-tvm] mbrookhart commented on pull request #5766: [BYOC][FIX] Infer types in MergeComposite

Posted by GitBox <gi...@apache.org>.
mbrookhart commented on pull request #5766:
URL: https://github.com/apache/incubator-tvm/pull/5766#issuecomment-642173517


   @comaniac Match won't traverse the input expression, it will only compare the function to the exact expression you give. Partition and Rewrite, however, traverse the input expression searching for matches. That's probably the reason for the discrepancy.


----------------------------------------------------------------
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] [incubator-tvm] mbrookhart commented on a change in pull request #5766: [BYOC][FIX] Infer types in MergeComposite

Posted by GitBox <gi...@apache.org>.
mbrookhart commented on a change in pull request #5766:
URL: https://github.com/apache/incubator-tvm/pull/5766#discussion_r438355902



##########
File path: src/relay/transforms/merge_composite.cc
##########
@@ -36,17 +36,24 @@ namespace tvm {
 namespace relay {
 namespace merge_composite {
 
+Function InferType(const Function& expr) {
+  auto mod = IRModule::FromExpr(expr);
+  mod = transform::InferType()(mod);
+  return Downcast<Function>(mod->Lookup("main"));
+}
+
 Expr MergeComposite(const Function& func, const Array<runtime::String>& pattern_names,
                     const Array<DFPattern>& patterns, const std::vector<PackedFunc>& checks) {
   CHECK_EQ(pattern_names.size(), patterns.size());
-  Expr merged_expr = func->body;
+  Function merged_func = func;
   // merge the patterns one-by-one in order
   for (size_t i = 0; i < patterns.size(); i++) {
     Map<String, ObjectRef> attrs;
     attrs.Set("Composite", pattern_names[i]);
-    merged_expr = PartitionPattern(patterns[i], merged_expr, attrs, checks[i]);
+    merged_func = Downcast<Function>(PartitionPattern(patterns[i], merged_func, attrs, checks[i]));
+    merged_func = InferType(merged_func);

Review comment:
       Yes, partitioning doesn't assume the graph already has type information in general, so it doesn't re-infer.




----------------------------------------------------------------
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] [incubator-tvm] mbrookhart commented on a change in pull request #5766: [BYOC][FIX] Infer types in MergeComposite

Posted by GitBox <gi...@apache.org>.
mbrookhart commented on a change in pull request #5766:
URL: https://github.com/apache/incubator-tvm/pull/5766#discussion_r438258138



##########
File path: tests/python/relay/test_pass_merge_composite.py
##########
@@ -953,26 +985,28 @@ def _check_type_false(extract):
         return bool(typ.shape[0] != 1)
 
     pattern_table_false = [
+        ("add_relu", make_add_relu_pattern()),
         ("conv_bias_relu", make_conv_bias_relu_pattern(), _check_type_false)
     ]
-    check_result(pattern_table_false, before(), before())
+    check_result(pattern_table_false, before(), expected_false())
 
     pattern_table_true = [
+        ("add_relu", make_add_relu_pattern()),
         ("conv_bias_relu", make_conv_bias_relu_pattern(), _check_type_true)
     ]
-    check_result(pattern_table_true, before(), expected())
+    check_result(pattern_table_true, before(), expected_true())
 
 
 if __name__ == "__main__":
-    test_simple_merge()
-    test_branch_merge()
-    test_multiple_patterns()
-    test_optional_pattern()
-    test_merge_order()
-    test_parallel_merge()
-    test_multiple_input_subgraphs()
-    test_reuse_call_merge()
-    test_tuple_get_item_merge()
-    test_pattern_with_check()
-    test_diamond_not_merge()
+    # test_simple_merge()
+    # test_branch_merge()
+    # test_multiple_patterns()
+    # test_optional_pattern()
+    # test_merge_order()
+    # test_parallel_merge()
+    # test_multiple_input_subgraphs()
+    # test_reuse_call_merge()
+    # test_tuple_get_item_merge()
+    # test_pattern_with_check()
+    # test_diamond_not_merge()

Review comment:
       Remove comments




----------------------------------------------------------------
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] [incubator-tvm] zhiics merged pull request #5766: [BYOC][FIX] Infer types in MergeComposite

Posted by GitBox <gi...@apache.org>.
zhiics merged pull request #5766:
URL: https://github.com/apache/incubator-tvm/pull/5766


   


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