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/02/27 23:52:28 UTC

[GitHub] [incubator-tvm] anwang2009 opened a new pull request #4960: [Relay][Pass] Add submodule extraction pass

anwang2009 opened a new pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960
 
 
   This pass allows submodule extraction per fused function for any IRModule. 
   
   With this pass as a foundation, in the future we can build post-fusion autotuning support.

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] anwang2009 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
anwang2009 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r391166976
 
 

 ##########
 File path: src/relay/pass/extract_functions.cc
 ##########
 @@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ *
+ * \file extract_functions.cc
+ * \brief Extract functions from an expr post-fusion.
+ */
+#include <tvm/relay/analysis.h>
+#include <tvm/relay/expr_functor.h>
+#include <tvm/relay/transform.h>
+
+namespace tvm {
+namespace relay {
+
+class FunctionExtractor : private ExprVisitor {
+ public:
+  Map<Integer, Function> Extract(const Expr& expr) {
+    std::vector<transform::Pass> passes = {transform::SimplifyInference(), transform::FuseOps(3)};
+    auto mod = IRModule::FromExpr(expr);
+    auto seq = transform::Sequential(passes);
+    mod = seq(mod);
+
+    VisitExpr(mod->Lookup("main"));
+    return this->hash_to_function;
+  }
+
+ private:
+  Map<Integer, Function> hash_to_function;
+
+  void VisitExpr_(const FunctionNode* n) final {
+    if (n->IsPrimitive()) {
+      Function func = FunctionNode::make(n->params, n->body, n->ret_type, n->type_params, n->attrs);
+      size_t hash_ = StructuralHash()(func);
+      this->hash_to_function.Set(hash_, func);
+    }
+
+    ExprVisitor::VisitExpr_(n);
+  }
+};
+
+Map<Integer, Function> TestExtractor(const Expr& e) { return FunctionExtractor().Extract(e); }
 
 Review comment:
   I've created a ModulePass as suggested and modified IRModule.functions to include the fused functions

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tmoreau89 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tmoreau89 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r390036823
 
 

 ##########
 File path: tests/python/relay/test_pass_extract_functions.py
 ##########
 @@ -0,0 +1,116 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Test function extraction"""
+import tvm
+from tvm import relay
+from tvm.relay.testing.resnet import get_workload
+
+
+def get_conv_net():
+    """This gets the net for a case described in fuse_ops.cc:
+
+            conv2d
+            /  |  \
+           /   |   \
+         op    op   op
+          \    |    /
+           \   |   /
+          elemwise add
+               |
+    """
+    dshape = (1, 1, 5, 1)
+    x = relay.var("x", shape=dshape)
+    y = relay.nn.conv2d(x, relay.var("w1"),
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        channels=1)
+
+    x1 = relay.nn.conv2d(y, relay.var("w2"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x2 = relay.nn.conv2d(y, relay.var("w3"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x3 = relay.nn.conv2d(y, relay.var("w4"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+
+    z = relay.add(x1, x2)
+    z = relay.add(x3, z)
+
+    return tvm.IRModule.from_expr(z)
+
+
+def get_conv2d():
+    x = relay.var("x", shape=(1, 56, 56, 64))
+    weight1 = relay.var('weight1', shape=(3, 3, 64, 32))
+    y = relay.nn.conv2d(x, weight1,
+                        channels=32,
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        data_layout='NHWC',
+                        kernel_layout='HWIO')
+    return tvm.IRModule.from_expr(y)
+
+
+def test_extract_identity():
+    mod = get_conv2d()
+    mdict = relay.analysis.extract_functions(mod['main'])
+    assert len(mdict) == 1
+
+    mod["main"] = mod["main"].set_attribute(
+        "Primitive", tvm.tir.IntImm("int32", 1))
+    relay.analysis.assert_graph_equal(list(mdict.values())[0], mod["main"])
+
+
+def test_extract_convoluted_network():
 
 Review comment:
   ah here too, we should change `convoluted` to `conv` or `convolutional`

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] jwfromm commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
jwfromm commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r390009947
 
 

 ##########
 File path: tests/python/relay/test_pass_extract_functions.py
 ##########
 @@ -0,0 +1,114 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Test function extraction"""
+import tvm
+from tvm import relay
+from tvm.relay.testing.resnet import get_workload
+
+
+def convoluted_network():
+    """This gets the net for a case described in fuse_ops.cc:
+
+            conv2d
+            /  |  \
+           /   |   \
+         op    op   op
+          \    |    /
+           \   |   /
+          elemwise add
+               |
+    """
+    dshape = (1, 1, 5, 1)
+    x = relay.var("x", shape=dshape)
+    y = relay.nn.conv2d(x, relay.var("w1"),
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        channels=1)
+
+    x1 = relay.nn.conv2d(y, relay.var("w2"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x2 = relay.nn.conv2d(y, relay.var("w3"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x3 = relay.nn.conv2d(y, relay.var("w4"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+
+    z = relay.add(x1, x2)
+    z = relay.add(x3, z)
+
+    return tvm.IRModule.from_expr(z)
+
+
+def get_conv2d():
+    x = relay.var("x", shape=(1, 56, 56, 64))
+    weight1 = relay.var('weight1', shape=(3, 3, 64, 32))
+    y = relay.nn.conv2d(x, weight1,
+                        channels=32,
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        data_layout='NHWC',
+                        kernel_layout='HWIO')
+    return tvm.IRModule.from_expr(y)
+
+
+def test_extract_identity():
+    mod = get_conv2d()
+    mdict = relay.analysis.extract_functions(mod['main'])
+    assert len(mdict) == 1
+
+    mod["main"] = mod["main"].set_attribute(
+        "Primitive", tvm.tir.IntImm("int32", 1))
+    relay.analysis.assert_graph_equal(list(mdict.values())[0], mod["main"])
+
+
+def test_extract_convoluted_network():
+    mod = convoluted_network()
+    mdict = relay.analysis.extract_functions(mod['main'])
+    assert len(mdict) == 2
+    xs = list(mdict.values())
+    x = xs[0]
+    y = xs[1]
+
+    def is_conv(func):
+        conv2d = relay.op.op.get("nn.conv2d")
+        call_node = func.body
+        return call_node.op == conv2d
+
+    def is_conv_add(func):
+        add = relay.op.op.get("add")
+        call_node = func.body
+        maybe_conv_module = tvm.IRModule.from_expr(call_node.args[0])
+        return call_node.op == add and is_conv(maybe_conv_module["main"])
+
+    # Function traversal order isn't obvious, so checking both orders is more consistent
+    return (is_conv(x) and is_conv_add(y)) or (is_conv_add(x) and is_conv(y))
+
+
+def test_extract_resnet():
+    mod, _params = get_workload()
+    relay.analysis.extract_functions(mod['main'])
 
 Review comment:
   is it worth adding a check to make sure the extraction worked or is the goal just to make sure it can be applied without an error? Maybe just checking the number of extracted functions matches the expected count would be enough to add 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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tqchen commented on issue #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tqchen commented on issue #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#issuecomment-592243459
 
 
   Thanks @anwang2009 , it would be great to discuss where do we want to put the related features. We can either treat is as a transform pass, or analysis pass.
   
   - T0: we should make it ```relay.analysis.extract_primitive_tasks```
   - T1: in the later case, we might want to make it as a ModulePass, and make it return a single IRModule, whose top-level functions are the the-deduplicated function that is extracted.
   
   
   
   

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] zhiics commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
zhiics commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r391775149
 
 

 ##########
 File path: src/relay/pass/extract_functions.cc
 ##########
 @@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file extract_functions.cc
 
 Review comment:
   extract_fused_functions.cc and also file 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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tmoreau89 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tmoreau89 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r385466775
 
 

 ##########
 File path: tests/python/relay/test_pass_extract_submodules.py
 ##########
 @@ -0,0 +1,119 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Test task extraction for autotvm"""
+import tvm
+from tvm import relay
+from tvm.relay.extract_submodules import extract_hashed_submodules
+from tvm.relay.testing.resnet import get_workload
+
+
+def convoluted_network():
+    """This gets the net for a case described in fuse_ops.cc:
+
+            conv2d
+            /  |  \
+           /   |   \
+         op    op   op
+          \    |    /
+           \   |   /
+          elemwise add
+               |
+    """
+    dshape = (1, 1, 5, 1)
+    x = relay.var("x", shape=dshape)
+    y = relay.nn.conv2d(x, relay.var("w1"),
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        channels=1)
+
+    x1 = relay.nn.conv2d(y, relay.var("w2"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x2 = relay.nn.conv2d(y, relay.var("w3"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x3 = relay.nn.conv2d(y, relay.var("w4"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+
+    z = relay.add(x1, x2)
+    z = relay.add(x3, z)
+
+    return tvm.IRModule.from_expr(z)
+
+
+def get_conv2d():
+    x = relay.var("x", shape=(1, 56, 56, 64))
+    weight1 = relay.var('weight1', shape=(3, 3, 64, 32))
+    y = relay.nn.conv2d(x, weight1,
+                        channels=32,
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        data_layout='NHWC',
+                        kernel_layout='HWIO')
+    return tvm.IRModule.from_expr(y)
+
+
+def test_extract_identity():
+    mod = get_conv2d()
+    mdict = extract_hashed_submodules(mod)
+    assert len(mdict) == 1
+    relay.analysis.assert_graph_equal(list(mdict.values())[0], mod)
+
+
+def test_extract_convoluted_network():
+    mod = convoluted_network()
+    mdict = extract_hashed_submodules(mod)
+    assert len(mdict) == 2
+    xs = list(mdict.values())
+    x = xs[0]
+    y = xs[1]
+
+    expected_conv_add = relay.fromtext(r"""
+        v0.0.4
+        def @main(%p0: Tensor[(1, 1, 5, 1), float32], %p1: Tensor[(1, 1, 3, 3), float32], %p2: Tensor[(1, 1, 5, 1), float32]) -> Tensor[(1, 1, 5, 1), float32] {
+            %0 = nn.conv2d(%p0, %p1, padding=[1, 1, 1, 1], channels=1, kernel_size=[3, 3]) /* ty=Tensor[(1, 1, 5, 1), float32] */;
+            add(%0, %p2) /* ty=Tensor[(1, 1, 5, 1), float32] */
+        }
+    """)
+    expected_conv = relay.fromtext(r"""
+        v0.0.4
+        def @main(%p0: Tensor[(1, 1, 5, 1), float32], %p1: Tensor[(1, 1, 3, 3), float32]) -> Tensor[(1, 1, 5, 1), float32] {
+            nn.conv2d(%p0, %p1, padding=[1, 1, 1, 1], channels=1, kernel_size=[3, 3]) /* ty=Tensor[(1, 1, 5, 1), float32] */
+        }
+    """)
+
+    # Order in dicts isn't consistent, so checking both orders is necessary
 
 Review comment:
   oh I see what you meant, thank you for clarifying

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] anwang2009 edited a comment on issue #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
anwang2009 edited a comment on issue #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#issuecomment-597790227
 
 
   Thanks for everyone's reviews! In line with @zhiics's suggestion I have made this a analysis ModulePass that gathers the fused primitive functions in a result IRModule's functions. 

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] zhiics commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
zhiics commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r390019935
 
 

 ##########
 File path: src/relay/pass/extract_functions.cc
 ##########
 @@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ *
+ * \file extract_functions.cc
+ * \brief Extract functions from an expr post-fusion.
+ */
+#include <tvm/relay/analysis.h>
+#include <tvm/relay/expr_functor.h>
+#include <tvm/relay/transform.h>
+
+namespace tvm {
+namespace relay {
+
+class FunctionExtractor : private ExprVisitor {
+ public:
+  Map<Integer, Function> Extract(const Expr& expr) {
+    std::vector<transform::Pass> passes = {transform::SimplifyInference(), transform::FuseOps(3)};
+    auto mod = IRModule::FromExpr(expr);
+    auto seq = transform::Sequential(passes);
+    mod = seq(mod);
+
+    VisitExpr(mod->Lookup("main"));
+    return this->hash_to_function;
+  }
+
+ private:
+  Map<Integer, Function> hash_to_function;
+
+  void VisitExpr_(const FunctionNode* n) final {
+    if (n->IsPrimitive()) {
+      Function func = FunctionNode::make(n->params, n->body, n->ret_type, n->type_params, n->attrs);
+      size_t hash_ = StructuralHash()(func);
+      this->hash_to_function.Set(hash_, func);
+    }
+
+    ExprVisitor::VisitExpr_(n);
+  }
+};
+
+Map<Integer, Function> TestExtractor(const Expr& e) { return FunctionExtractor().Extract(e); }
 
 Review comment:
   Rename to FusedFunctionExtractor? and the class name could be renamed to something like FusedFunctionExtratorWrapper

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tqchen merged pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tqchen merged pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960
 
 
   

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] anwang2009 commented on issue #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
anwang2009 commented on issue #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#issuecomment-596795439
 
 
   I have made edits in accordance with the above RFC

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tqchen edited a comment on issue #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tqchen edited a comment on issue #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#issuecomment-592243459
 
 
   Thanks @anwang2009 , it would be great to discuss where do we want to put the related features. We can either treat is as a transform pass, or analysis pass.
   
   - T0: we should make it ```relay.analysis.extract_primitive_tasks```
   - T1: in the later case, we might want to make it as a ModulePass, and make it return a single IRModule, whose top-level functions are the the-deduplicated function that is extracted.
   
   In both cases, given that this is a new feature that others can reuse, perhaps we should open a discuss forum RFC post to describe the motivation a bit and API setups

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] anwang2009 commented on issue #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
anwang2009 commented on issue #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#issuecomment-597790227
 
 
   Thanks for everyone's reviews! In line with @zhiics's suggestion I have made this a transform ModulePass that gathers the fused primitive functions in a result IRModule's functions. We decided in the RFC to make this an analysis pass; however, the only existing ModulePass examples I found were registered as transform passes. 
   The "spirit" of the pass is still analysis, but we should register this as a transform pass for consistency, as it modifies an IRModule.  

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tqchen commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tqchen commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r391770669
 
 

 ##########
 File path: tests/python/relay/test_pass_extract_functions.py
 ##########
 @@ -0,0 +1,117 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
 
 Review comment:
   consider rename to test_analysis_extract_fused_functions

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] jwfromm commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
jwfromm commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r390082464
 
 

 ##########
 File path: tests/python/relay/test_pass_extract_functions.py
 ##########
 @@ -0,0 +1,116 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Test function extraction"""
+import tvm
+from tvm import relay
+from tvm.relay.testing.resnet import get_workload
+
+
+def get_conv_net():
+    """This gets the net for a case described in fuse_ops.cc:
+
+            conv2d
+            /  |  \
+           /   |   \
+         op    op   op
+          \    |    /
+           \   |   /
+          elemwise add
+               |
+    """
+    dshape = (1, 1, 5, 1)
+    x = relay.var("x", shape=dshape)
+    y = relay.nn.conv2d(x, relay.var("w1"),
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        channels=1)
+
+    x1 = relay.nn.conv2d(y, relay.var("w2"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x2 = relay.nn.conv2d(y, relay.var("w3"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x3 = relay.nn.conv2d(y, relay.var("w4"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+
+    z = relay.add(x1, x2)
+    z = relay.add(x3, z)
+
+    return tvm.IRModule.from_expr(z)
+
+
+def get_conv2d():
+    x = relay.var("x", shape=(1, 56, 56, 64))
+    weight1 = relay.var('weight1', shape=(3, 3, 64, 32))
+    y = relay.nn.conv2d(x, weight1,
+                        channels=32,
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        data_layout='NHWC',
+                        kernel_layout='HWIO')
+    return tvm.IRModule.from_expr(y)
+
+
+def test_extract_identity():
+    mod = get_conv2d()
+    mdict = relay.analysis.extract_functions(mod['main'])
+    assert len(mdict) == 1
+
+    mod["main"] = mod["main"].set_attribute(
+        "Primitive", tvm.tir.IntImm("int32", 1))
+    relay.analysis.assert_graph_equal(list(mdict.values())[0], mod["main"])
+
+
+def test_extract_convoluted_network():
+    mod = get_conv_net()
+    mdict = relay.analysis.extract_functions(mod['main'])
+    assert len(mdict) == 2
+    xs = list(mdict.values())
+    x = xs[0]
+    y = xs[1]
+
+    def is_conv(func):
+        conv2d = relay.op.op.get("nn.conv2d")
+        call_node = func.body
+        return call_node.op == conv2d
+
+    def is_conv_add(func):
+        add = relay.op.op.get("add")
+        call_node = func.body
+        maybe_conv_module = tvm.IRModule.from_expr(call_node.args[0])
+        return call_node.op == add and is_conv(maybe_conv_module["main"])
+
+    # Function traversal order isn't obvious, so checking both orders is more consistent
+    return (is_conv(x) and is_conv_add(y)) or (is_conv_add(x) and is_conv(y))
 
 Review comment:
   Make sure to add an assert to check this resolves to True.

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] anwang2009 commented on issue #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
anwang2009 commented on issue #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#issuecomment-592259448
 
 
   rfc for design considerations here: https://discuss.tvm.ai/t/relay-pass-add-submodule-extraction/5824

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tqchen commented on issue #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tqchen commented on issue #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#issuecomment-598908843
 
 
   Thanks @anwang2009 @zhiics @jwfromm @tmoreau89 !

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] anwang2009 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
anwang2009 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r390019572
 
 

 ##########
 File path: tests/python/relay/test_pass_extract_functions.py
 ##########
 @@ -0,0 +1,114 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Test function extraction"""
+import tvm
+from tvm import relay
+from tvm.relay.testing.resnet import get_workload
+
+
+def convoluted_network():
+    """This gets the net for a case described in fuse_ops.cc:
+
+            conv2d
+            /  |  \
+           /   |   \
+         op    op   op
+          \    |    /
+           \   |   /
+          elemwise add
+               |
+    """
+    dshape = (1, 1, 5, 1)
+    x = relay.var("x", shape=dshape)
+    y = relay.nn.conv2d(x, relay.var("w1"),
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        channels=1)
+
+    x1 = relay.nn.conv2d(y, relay.var("w2"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x2 = relay.nn.conv2d(y, relay.var("w3"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x3 = relay.nn.conv2d(y, relay.var("w4"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+
+    z = relay.add(x1, x2)
+    z = relay.add(x3, z)
+
+    return tvm.IRModule.from_expr(z)
+
+
+def get_conv2d():
+    x = relay.var("x", shape=(1, 56, 56, 64))
+    weight1 = relay.var('weight1', shape=(3, 3, 64, 32))
+    y = relay.nn.conv2d(x, weight1,
+                        channels=32,
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        data_layout='NHWC',
+                        kernel_layout='HWIO')
+    return tvm.IRModule.from_expr(y)
+
+
+def test_extract_identity():
+    mod = get_conv2d()
+    mdict = relay.analysis.extract_functions(mod['main'])
+    assert len(mdict) == 1
+
+    mod["main"] = mod["main"].set_attribute(
+        "Primitive", tvm.tir.IntImm("int32", 1))
+    relay.analysis.assert_graph_equal(list(mdict.values())[0], mod["main"])
+
+
+def test_extract_convoluted_network():
+    mod = convoluted_network()
+    mdict = relay.analysis.extract_functions(mod['main'])
+    assert len(mdict) == 2
+    xs = list(mdict.values())
+    x = xs[0]
+    y = xs[1]
+
+    def is_conv(func):
+        conv2d = relay.op.op.get("nn.conv2d")
+        call_node = func.body
+        return call_node.op == conv2d
+
+    def is_conv_add(func):
+        add = relay.op.op.get("add")
+        call_node = func.body
+        maybe_conv_module = tvm.IRModule.from_expr(call_node.args[0])
+        return call_node.op == add and is_conv(maybe_conv_module["main"])
+
+    # Function traversal order isn't obvious, so checking both orders is more consistent
+    return (is_conv(x) and is_conv_add(y)) or (is_conv_add(x) and is_conv(y))
+
+
+def test_extract_resnet():
+    mod, _params = get_workload()
+    relay.analysis.extract_functions(mod['main'])
 
 Review comment:
   modified to check for expected count

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tqchen commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tqchen commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r391771643
 
 

 ##########
 File path: python/tvm/relay/analysis.py
 ##########
 @@ -408,3 +408,17 @@ def structural_hash(value):
         msg = ("found value of type {0} expected" +
                "relay.Expr or relay.Type").format(type(value))
         raise TypeError(msg)
+
+
+def extract_fused_functions():
 
 Review comment:
   API style: the analysis API so far are not functors(unlike the Pass in the transform namespace). So we should directly take a IRModule and return a collection of functions 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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tqchen commented on issue #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tqchen commented on issue #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#issuecomment-598309735
 
 
   cc @zhiics please also 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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tqchen commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tqchen commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r391756346
 
 

 ##########
 File path: python/tvm/relay/transform.py
 ##########
 @@ -657,6 +657,20 @@ def un_cps(func):
     return _transform.un_cps(func)
 
 
+def extract_fused_functions():
+    """Turn IRModule into IRModule of only fused primitive functions.
 
 Review comment:
   Should we move it to relay.analysis instead since this is not an equivalent transformation?

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] anwang2009 commented on issue #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
anwang2009 commented on issue #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#issuecomment-592238050
 
 
   @tmoreau89 @jroesch @jwfromm this is an mvp; post-fusion tuning support requires a deep autotvm rewrite

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] anwang2009 commented on issue #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
anwang2009 commented on issue #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#issuecomment-598304336
 
 
   thanks @tqchen, I've made the requested change

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tqchen commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tqchen commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r391771060
 
 

 ##########
 File path: src/relay/pass/extract_functions.cc
 ##########
 @@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
 
 Review comment:
   move to relay/analysis

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] jwfromm commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
jwfromm commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r390082658
 
 

 ##########
 File path: tests/python/relay/test_pass_extract_functions.py
 ##########
 @@ -0,0 +1,116 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Test function extraction"""
+import tvm
+from tvm import relay
+from tvm.relay.testing.resnet import get_workload
+
+
+def get_conv_net():
+    """This gets the net for a case described in fuse_ops.cc:
+
+            conv2d
+            /  |  \
+           /   |   \
+         op    op   op
+          \    |    /
+           \   |   /
+          elemwise add
+               |
+    """
+    dshape = (1, 1, 5, 1)
+    x = relay.var("x", shape=dshape)
+    y = relay.nn.conv2d(x, relay.var("w1"),
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        channels=1)
+
+    x1 = relay.nn.conv2d(y, relay.var("w2"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x2 = relay.nn.conv2d(y, relay.var("w3"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x3 = relay.nn.conv2d(y, relay.var("w4"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+
+    z = relay.add(x1, x2)
+    z = relay.add(x3, z)
+
+    return tvm.IRModule.from_expr(z)
+
+
+def get_conv2d():
+    x = relay.var("x", shape=(1, 56, 56, 64))
+    weight1 = relay.var('weight1', shape=(3, 3, 64, 32))
+    y = relay.nn.conv2d(x, weight1,
+                        channels=32,
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        data_layout='NHWC',
+                        kernel_layout='HWIO')
+    return tvm.IRModule.from_expr(y)
+
+
+def test_extract_identity():
+    mod = get_conv2d()
+    mdict = relay.analysis.extract_functions(mod['main'])
+    assert len(mdict) == 1
+
+    mod["main"] = mod["main"].set_attribute(
+        "Primitive", tvm.tir.IntImm("int32", 1))
+    relay.analysis.assert_graph_equal(list(mdict.values())[0], mod["main"])
+
+
+def test_extract_convoluted_network():
+    mod = get_conv_net()
+    mdict = relay.analysis.extract_functions(mod['main'])
+    assert len(mdict) == 2
+    xs = list(mdict.values())
+    x = xs[0]
+    y = xs[1]
+
+    def is_conv(func):
+        conv2d = relay.op.op.get("nn.conv2d")
+        call_node = func.body
+        return call_node.op == conv2d
+
+    def is_conv_add(func):
+        add = relay.op.op.get("add")
+        call_node = func.body
+        maybe_conv_module = tvm.IRModule.from_expr(call_node.args[0])
+        return call_node.op == add and is_conv(maybe_conv_module["main"])
+
+    # Function traversal order isn't obvious, so checking both orders is more consistent
+    return (is_conv(x) and is_conv_add(y)) or (is_conv_add(x) and is_conv(y))
+
+
+def test_extract_resnet():
+    mod, _params = get_workload()
+    mdict = relay.analysis.extract_functions(mod['main'])
+    # import pdb; pdb.set_trace()
 
 Review comment:
   Remember to remove this commented out line in the final commit.

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tmoreau89 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tmoreau89 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r385446578
 
 

 ##########
 File path: tests/python/relay/test_pass_extract_submodules.py
 ##########
 @@ -0,0 +1,119 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Test task extraction for autotvm"""
+import tvm
+from tvm import relay
+from tvm.relay.extract_submodules import extract_hashed_submodules
+from tvm.relay.testing.resnet import get_workload
+
+
+def convoluted_network():
+    """This gets the net for a case described in fuse_ops.cc:
+
+            conv2d
+            /  |  \
+           /   |   \
+         op    op   op
+          \    |    /
+           \   |   /
+          elemwise add
+               |
+    """
+    dshape = (1, 1, 5, 1)
+    x = relay.var("x", shape=dshape)
+    y = relay.nn.conv2d(x, relay.var("w1"),
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        channels=1)
+
+    x1 = relay.nn.conv2d(y, relay.var("w2"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x2 = relay.nn.conv2d(y, relay.var("w3"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x3 = relay.nn.conv2d(y, relay.var("w4"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+
+    z = relay.add(x1, x2)
+    z = relay.add(x3, z)
+
+    return tvm.IRModule.from_expr(z)
+
+
+def get_conv2d():
+    x = relay.var("x", shape=(1, 56, 56, 64))
+    weight1 = relay.var('weight1', shape=(3, 3, 64, 32))
+    y = relay.nn.conv2d(x, weight1,
+                        channels=32,
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        data_layout='NHWC',
+                        kernel_layout='HWIO')
+    return tvm.IRModule.from_expr(y)
+
+
+def test_extract_identity():
+    mod = get_conv2d()
+    mdict = extract_hashed_submodules(mod)
+    assert len(mdict) == 1
+    relay.analysis.assert_graph_equal(list(mdict.values())[0], mod)
+
+
+def test_extract_convoluted_network():
+    mod = convoluted_network()
+    mdict = extract_hashed_submodules(mod)
+    assert len(mdict) == 2
+    xs = list(mdict.values())
+    x = xs[0]
+    y = xs[1]
+
+    expected_conv_add = relay.fromtext(r"""
+        v0.0.4
+        def @main(%p0: Tensor[(1, 1, 5, 1), float32], %p1: Tensor[(1, 1, 3, 3), float32], %p2: Tensor[(1, 1, 5, 1), float32]) -> Tensor[(1, 1, 5, 1), float32] {
+            %0 = nn.conv2d(%p0, %p1, padding=[1, 1, 1, 1], channels=1, kernel_size=[3, 3]) /* ty=Tensor[(1, 1, 5, 1), float32] */;
+            add(%0, %p2) /* ty=Tensor[(1, 1, 5, 1), float32] */
+        }
+    """)
+    expected_conv = relay.fromtext(r"""
+        v0.0.4
+        def @main(%p0: Tensor[(1, 1, 5, 1), float32], %p1: Tensor[(1, 1, 3, 3), float32]) -> Tensor[(1, 1, 5, 1), float32] {
+            nn.conv2d(%p0, %p1, padding=[1, 1, 1, 1], channels=1, kernel_size=[3, 3]) /* ty=Tensor[(1, 1, 5, 1), float32] */
+        }
+    """)
+
+    # Order in dicts isn't consistent, so checking both orders is necessary
+    conv_add_0 = relay.analysis.graph_equal(x['main'], expected_conv_add['main'])
+    conv_add_1 = relay.analysis.graph_equal(y['main'], expected_conv_add['main'])
+    conv_0 = relay.analysis.graph_equal(x['main'], expected_conv['main'])
+    conv_1 = relay.analysis.graph_equal(y['main'], expected_conv['main'])
 
 Review comment:
   I wonder if we should be using simpler checks (e.g. check that the function has only 1 or 2 operators, and that op 1 is of type conv2d and op 2 is of type add etc.)
   
   The risk is that if something changes (e.g. relay format to v0.0.5) this may result in an error?

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] zhiics commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
zhiics commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r390020988
 
 

 ##########
 File path: src/relay/pass/extract_functions.cc
 ##########
 @@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ *
+ * \file extract_functions.cc
+ * \brief Extract functions from an expr post-fusion.
+ */
+#include <tvm/relay/analysis.h>
+#include <tvm/relay/expr_functor.h>
+#include <tvm/relay/transform.h>
+
+namespace tvm {
+namespace relay {
+
+class FunctionExtractor : private ExprVisitor {
+ public:
+  Map<Integer, Function> Extract(const Expr& expr) {
+    std::vector<transform::Pass> passes = {transform::SimplifyInference(), transform::FuseOps(3)};
+    auto mod = IRModule::FromExpr(expr);
+    auto seq = transform::Sequential(passes);
+    mod = seq(mod);
+
+    VisitExpr(mod->Lookup("main"));
+    return this->hash_to_function;
+  }
+
+ private:
+  Map<Integer, Function> hash_to_function;
+
+  void VisitExpr_(const FunctionNode* n) final {
+    if (n->IsPrimitive()) {
+      Function func = FunctionNode::make(n->params, n->body, n->ret_type, n->type_params, n->attrs);
+      size_t hash_ = StructuralHash()(func);
+      this->hash_to_function.Set(hash_, func);
+    }
+
+    ExprVisitor::VisitExpr_(n);
+  }
+};
+
+Map<Integer, Function> TestExtractor(const Expr& e) { return FunctionExtractor().Extract(e); }
 
 Review comment:
   I think this pass could take a module directly (We can even create `ModulePass` for this and add `SimplifyInference` and `FuseOp` as required). We may have multiple functions. Also, by doing this, we can remove the packing and unpacking operation in the `Extract` method.

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] jwfromm commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
jwfromm commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r390008819
 
 

 ##########
 File path: tests/python/relay/test_pass_extract_functions.py
 ##########
 @@ -0,0 +1,114 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Test function extraction"""
+import tvm
+from tvm import relay
+from tvm.relay.testing.resnet import get_workload
+
+
+def convoluted_network():
 
 Review comment:
   change to `get_convoluted_network` for consistency with `get_conv2d`.

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tqchen edited a comment on issue #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tqchen edited a comment on issue #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#issuecomment-592243459
 
 
   Thanks @anwang2009 , it would be great to discuss where do we want to put the related features. We can either treat is as a transform pass, or analysis pass.
   
   - T0: we could make it as an analysis under relay.analysis ```relay.analysis.extract_primitive_tasks```
   - T1: we could make it as a ModulePass under relay.transform, and make it return a single IRModule, whose top-level functions are the the-deduplicated function that is extracted.
   
   In both cases, given that this is a new feature that others can reuse, perhaps we should open a discuss forum RFC post to describe the motivation a bit and API setups.
   
   Given that most transforms are expected to be somehow semantically equivalent, perhaps an analysis pass is a better idea

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tmoreau89 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tmoreau89 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r385446054
 
 

 ##########
 File path: tests/python/relay/test_pass_extract_submodules.py
 ##########
 @@ -0,0 +1,119 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Test task extraction for autotvm"""
+import tvm
+from tvm import relay
+from tvm.relay.extract_submodules import extract_hashed_submodules
+from tvm.relay.testing.resnet import get_workload
+
+
+def convoluted_network():
+    """This gets the net for a case described in fuse_ops.cc:
+
+            conv2d
+            /  |  \
+           /   |   \
+         op    op   op
+          \    |    /
+           \   |   /
+          elemwise add
+               |
+    """
+    dshape = (1, 1, 5, 1)
+    x = relay.var("x", shape=dshape)
+    y = relay.nn.conv2d(x, relay.var("w1"),
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        channels=1)
+
+    x1 = relay.nn.conv2d(y, relay.var("w2"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x2 = relay.nn.conv2d(y, relay.var("w3"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x3 = relay.nn.conv2d(y, relay.var("w4"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+
+    z = relay.add(x1, x2)
+    z = relay.add(x3, z)
+
+    return tvm.IRModule.from_expr(z)
+
+
+def get_conv2d():
+    x = relay.var("x", shape=(1, 56, 56, 64))
+    weight1 = relay.var('weight1', shape=(3, 3, 64, 32))
+    y = relay.nn.conv2d(x, weight1,
+                        channels=32,
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        data_layout='NHWC',
+                        kernel_layout='HWIO')
+    return tvm.IRModule.from_expr(y)
+
+
+def test_extract_identity():
+    mod = get_conv2d()
+    mdict = extract_hashed_submodules(mod)
+    assert len(mdict) == 1
+    relay.analysis.assert_graph_equal(list(mdict.values())[0], mod)
+
+
+def test_extract_convoluted_network():
+    mod = convoluted_network()
+    mdict = extract_hashed_submodules(mod)
+    assert len(mdict) == 2
+    xs = list(mdict.values())
+    x = xs[0]
+    y = xs[1]
+
+    expected_conv_add = relay.fromtext(r"""
+        v0.0.4
+        def @main(%p0: Tensor[(1, 1, 5, 1), float32], %p1: Tensor[(1, 1, 3, 3), float32], %p2: Tensor[(1, 1, 5, 1), float32]) -> Tensor[(1, 1, 5, 1), float32] {
+            %0 = nn.conv2d(%p0, %p1, padding=[1, 1, 1, 1], channels=1, kernel_size=[3, 3]) /* ty=Tensor[(1, 1, 5, 1), float32] */;
+            add(%0, %p2) /* ty=Tensor[(1, 1, 5, 1), float32] */
+        }
+    """)
+    expected_conv = relay.fromtext(r"""
+        v0.0.4
+        def @main(%p0: Tensor[(1, 1, 5, 1), float32], %p1: Tensor[(1, 1, 3, 3), float32]) -> Tensor[(1, 1, 5, 1), float32] {
+            nn.conv2d(%p0, %p1, padding=[1, 1, 1, 1], channels=1, kernel_size=[3, 3]) /* ty=Tensor[(1, 1, 5, 1), float32] */
+        }
+    """)
+
+    # Order in dicts isn't consistent, so checking both orders is necessary
 
 Review comment:
   One thing you can use is ordered_dicts: https://www.geeksforgeeks.org/ordereddict-in-python/

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tmoreau89 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tmoreau89 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r390036855
 
 

 ##########
 File path: tests/python/relay/test_pass_extract_functions.py
 ##########
 @@ -0,0 +1,116 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Test function extraction"""
+import tvm
+from tvm import relay
+from tvm.relay.testing.resnet import get_workload
+
+
+def get_conv_net():
+    """This gets the net for a case described in fuse_ops.cc:
+
+            conv2d
+            /  |  \
+           /   |   \
+         op    op   op
+          \    |    /
+           \   |   /
+          elemwise add
+               |
+    """
+    dshape = (1, 1, 5, 1)
+    x = relay.var("x", shape=dshape)
+    y = relay.nn.conv2d(x, relay.var("w1"),
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        channels=1)
+
+    x1 = relay.nn.conv2d(y, relay.var("w2"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x2 = relay.nn.conv2d(y, relay.var("w3"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x3 = relay.nn.conv2d(y, relay.var("w4"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+
+    z = relay.add(x1, x2)
+    z = relay.add(x3, z)
+
+    return tvm.IRModule.from_expr(z)
+
+
+def get_conv2d():
+    x = relay.var("x", shape=(1, 56, 56, 64))
+    weight1 = relay.var('weight1', shape=(3, 3, 64, 32))
+    y = relay.nn.conv2d(x, weight1,
+                        channels=32,
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        data_layout='NHWC',
+                        kernel_layout='HWIO')
+    return tvm.IRModule.from_expr(y)
+
+
+def test_extract_identity():
+    mod = get_conv2d()
+    mdict = relay.analysis.extract_functions(mod['main'])
+    assert len(mdict) == 1
+
+    mod["main"] = mod["main"].set_attribute(
+        "Primitive", tvm.tir.IntImm("int32", 1))
+    relay.analysis.assert_graph_equal(list(mdict.values())[0], mod["main"])
+
+
+def test_extract_convoluted_network():
+    mod = get_conv_net()
+    mdict = relay.analysis.extract_functions(mod['main'])
+    assert len(mdict) == 2
+    xs = list(mdict.values())
+    x = xs[0]
+    y = xs[1]
+
+    def is_conv(func):
+        conv2d = relay.op.op.get("nn.conv2d")
+        call_node = func.body
+        return call_node.op == conv2d
+
+    def is_conv_add(func):
+        add = relay.op.op.get("add")
+        call_node = func.body
+        maybe_conv_module = tvm.IRModule.from_expr(call_node.args[0])
+        return call_node.op == add and is_conv(maybe_conv_module["main"])
+
+    # Function traversal order isn't obvious, so checking both orders is more consistent
+    return (is_conv(x) and is_conv_add(y)) or (is_conv_add(x) and is_conv(y))
+
+
+def test_extract_resnet():
+    mod, _params = get_workload()
+    mdict = relay.analysis.extract_functions(mod['main'])
+    # import pdb; pdb.set_trace()
+    assert len(mdict) == 34
+
+
+if __name__ == '__main__':
+    test_extract_identity()
+    test_extract_convoluted_network()
 
 Review comment:
   idem

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tqchen edited a comment on issue #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tqchen edited a comment on issue #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#issuecomment-592243459
 
 
   Thanks @anwang2009 , it would be great to discuss where do we want to put the related features. We can either treat is as a transform pass, or analysis pass.
   
   - T0: we could make it as an analysis under relay.analysis ```relay.analysis.extract_primitive_tasks```
   - T1: we could make it as a ModulePass under relay.transform, and make it return a single IRModule, whose top-level functions are the the-deduplicated function that is extracted.
   
   In both cases, given that this is a new feature that others can reuse, perhaps we should open a discuss forum RFC post to describe the motivation a bit and API setups.
   
   Given that most transforms are expected to be somehow semantically equivalent, perhaps an analysis pass is a better idea. Still, it would be interesting to ask if it makes sense to make it to return a single IRModule(which contains all the task functions) or a collections of IRModules

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tqchen edited a comment on issue #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tqchen edited a comment on issue #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#issuecomment-592243459
 
 
   Thanks @anwang2009 , it would be great to discuss where do we want to put the related features. We can either treat is as a transform pass, or analysis pass.
   
   - T0: we could make it as an analysis under relay.analysis ```relay.analysis.extract_primitive_tasks```
   - T1: we could make it as a ModulePass under relay.transform, and make it return a single IRModule, whose top-level functions are the the-deduplicated function that is extracted.
   
   In both cases, given that this is a new feature that others can reuse, perhaps we should open a discuss forum RFC post to describe the motivation a bit and API setups

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] anwang2009 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
anwang2009 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r385465576
 
 

 ##########
 File path: tests/python/relay/test_pass_extract_submodules.py
 ##########
 @@ -0,0 +1,119 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Test task extraction for autotvm"""
+import tvm
+from tvm import relay
+from tvm.relay.extract_submodules import extract_hashed_submodules
+from tvm.relay.testing.resnet import get_workload
+
+
+def convoluted_network():
+    """This gets the net for a case described in fuse_ops.cc:
+
+            conv2d
+            /  |  \
+           /   |   \
+         op    op   op
+          \    |    /
+           \   |   /
+          elemwise add
+               |
+    """
+    dshape = (1, 1, 5, 1)
+    x = relay.var("x", shape=dshape)
+    y = relay.nn.conv2d(x, relay.var("w1"),
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        channels=1)
+
+    x1 = relay.nn.conv2d(y, relay.var("w2"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x2 = relay.nn.conv2d(y, relay.var("w3"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+    x3 = relay.nn.conv2d(y, relay.var("w4"),
+                         kernel_size=(3, 3),
+                         padding=(1, 1),
+                         channels=1)
+
+    z = relay.add(x1, x2)
+    z = relay.add(x3, z)
+
+    return tvm.IRModule.from_expr(z)
+
+
+def get_conv2d():
+    x = relay.var("x", shape=(1, 56, 56, 64))
+    weight1 = relay.var('weight1', shape=(3, 3, 64, 32))
+    y = relay.nn.conv2d(x, weight1,
+                        channels=32,
+                        kernel_size=(3, 3),
+                        padding=(1, 1),
+                        data_layout='NHWC',
+                        kernel_layout='HWIO')
+    return tvm.IRModule.from_expr(y)
+
+
+def test_extract_identity():
+    mod = get_conv2d()
+    mdict = extract_hashed_submodules(mod)
+    assert len(mdict) == 1
+    relay.analysis.assert_graph_equal(list(mdict.values())[0], mod)
+
+
+def test_extract_convoluted_network():
+    mod = convoluted_network()
+    mdict = extract_hashed_submodules(mod)
+    assert len(mdict) == 2
+    xs = list(mdict.values())
+    x = xs[0]
+    y = xs[1]
+
+    expected_conv_add = relay.fromtext(r"""
+        v0.0.4
+        def @main(%p0: Tensor[(1, 1, 5, 1), float32], %p1: Tensor[(1, 1, 3, 3), float32], %p2: Tensor[(1, 1, 5, 1), float32]) -> Tensor[(1, 1, 5, 1), float32] {
+            %0 = nn.conv2d(%p0, %p1, padding=[1, 1, 1, 1], channels=1, kernel_size=[3, 3]) /* ty=Tensor[(1, 1, 5, 1), float32] */;
+            add(%0, %p2) /* ty=Tensor[(1, 1, 5, 1), float32] */
+        }
+    """)
+    expected_conv = relay.fromtext(r"""
+        v0.0.4
+        def @main(%p0: Tensor[(1, 1, 5, 1), float32], %p1: Tensor[(1, 1, 3, 3), float32]) -> Tensor[(1, 1, 5, 1), float32] {
+            nn.conv2d(%p0, %p1, padding=[1, 1, 1, 1], channels=1, kernel_size=[3, 3]) /* ty=Tensor[(1, 1, 5, 1), float32] */
+        }
+    """)
+
+    # Order in dicts isn't consistent, so checking both orders is necessary
 
 Review comment:
   Ah, I miscommunicated the issue: the order in which functions are visited is not necessarily the order in which the layers actually appear, so I wanted to avoid making any order-based guarantee. 

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] anwang2009 edited a comment on issue #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
anwang2009 edited a comment on issue #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#issuecomment-597790227
 
 
   Thanks for everyone's reviews! In line with @zhiics's suggestion I have made this a transform ModulePass that gathers the fused primitive functions in a result IRModule's functions. We decided in the RFC to make this an analysis pass; however, the only existing ModulePass examples I found were registered as transform passes. 
   The "spirit" of the pass is still analysis, but we should register this as a transform pass for consistency, as it outputs an IRModule.  

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tmoreau89 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
tmoreau89 commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r390011187
 
 

 ##########
 File path: tests/python/relay/test_pass_extract_functions.py
 ##########
 @@ -0,0 +1,114 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Test function extraction"""
+import tvm
+from tvm import relay
+from tvm.relay.testing.resnet import get_workload
+
+
+def convoluted_network():
 
 Review comment:
   and to be technically accurate, use `get_convolutional_network` or `get_conv_net` for short :)

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] zhiics commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
zhiics commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r390019016
 
 

 ##########
 File path: src/relay/pass/extract_functions.cc
 ##########
 @@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ *
+ * \file extract_functions.cc
+ * \brief Extract functions from an expr post-fusion.
+ */
+#include <tvm/relay/analysis.h>
+#include <tvm/relay/expr_functor.h>
+#include <tvm/relay/transform.h>
+
+namespace tvm {
+namespace relay {
+
+class FunctionExtractor : private ExprVisitor {
 
 Review comment:
   If this only works on fused function, should we rename is to FusedFunctionExtractor?

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


With regards,
Apache Git Services

[GitHub] [incubator-tvm] zhiics commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass

Posted by GitBox <gi...@apache.org>.
zhiics commented on a change in pull request #4960: [Relay][Pass] Add submodule extraction pass
URL: https://github.com/apache/incubator-tvm/pull/4960#discussion_r390018799
 
 

 ##########
 File path: src/relay/pass/extract_functions.cc
 ##########
 @@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ *
 
 Review comment:
   Remove

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


With regards,
Apache Git Services