You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2021/09/13 20:44:42 UTC

[GitHub] [tvm] comaniac commented on a change in pull request #8996: [Relay][Pass] Add ExtractOperators pass

comaniac commented on a change in pull request #8996:
URL: https://github.com/apache/tvm/pull/8996#discussion_r707665523



##########
File path: src/relay/analysis/extract_operators.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_operators.cc
+ * \brief Extract unique operators from an IRModule
+ */
+#include <tvm/node/structural_hash.h>
+#include <tvm/relay/analysis.h>
+#include <tvm/relay/expr.h>
+#include <tvm/relay/expr_functor.h>
+
+namespace tvm {
+namespace relay {
+
+class OperatorExtractorWrapper : private ExprVisitor {
+ public:
+  explicit OperatorExtractorWrapper(const IRModule& mod) : mod_(mod) {}
+
+  Array<String> Extract() {
+    VisitExpr(this->mod_->Lookup("main"));
+
+    return this->operators;
+  }
+
+ private:
+  const IRModule mod_;
+  // Array of unique operator names.
+  Array<String> operators;

Review comment:
       1. Please add docstring properly, like
   ```
   /*! \brief Array of unique operator names. */
   Array<String> operators_;
   ```
   
   2. Use `_` as the suffix for private members.

##########
File path: tests/python/relay/test_analysis_extract_operators.py
##########
@@ -0,0 +1,96 @@
+# 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:
+          conv2d
+          /  |
+         /   |
+    conv2d   |
+        \    |
+         \   |
+        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)
+
+    z = relay.add(y, x1)
+
+    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()
+    ops = relay.analysis.extract_operators(mod)
+    assert len(ops) == 1
+    assert ops[0] == "nn.conv2d"
+
+
+def test_extract_conv_net():
+    mod = get_conv_net()
+    ops = relay.analysis.extract_operators(mod)
+    assert len(ops) == 2
+    assert "add" in ops
+    assert "nn.conv2d" in ops
+
+
+def test_extract_resnet():
+    mod, _params = get_workload()
+    expected_ops = [
+        "nn.batch_norm",
+        "nn.conv2d",
+        "nn.relu",
+        "nn.max_pool2d",
+        "add",
+        "nn.global_avg_pool2d",
+        "nn.batch_flatten",
+        "nn.dense",
+        "nn.bias_add",
+        "nn.softmax",
+    ]
+    ops = relay.analysis.extract_operators(mod)
+    assert len(ops) == len(expected_ops)
+    assert all([op in ops for op in expected_ops])
+
+
+if __name__ == "__main__":
+    test_extract_identity()
+    test_extract_conv_net()
+    test_extract_resnet()

Review comment:
       ```suggestion
       pytest.main([__file__])
   ```

##########
File path: python/tvm/relay/analysis/analysis.py
##########
@@ -384,6 +384,21 @@ def extract_fused_functions(mod):
     return ret
 
 
+def extract_operators(mod):

Review comment:
       This name seems not accurate nor can reflect the functionality of this pass. Something like `list_used_op_names` or `list_appear_op_names` might be more precise.

##########
File path: src/relay/analysis/extract_operators.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_operators.cc
+ * \brief Extract unique operators from an IRModule
+ */
+#include <tvm/node/structural_hash.h>
+#include <tvm/relay/analysis.h>
+#include <tvm/relay/expr.h>
+#include <tvm/relay/expr_functor.h>
+
+namespace tvm {
+namespace relay {
+
+class OperatorExtractorWrapper : private ExprVisitor {
+ public:
+  explicit OperatorExtractorWrapper(const IRModule& mod) : mod_(mod) {}
+
+  Array<String> Extract() {
+    VisitExpr(this->mod_->Lookup("main"));
+
+    return this->operators;
+  }
+
+ private:
+  const IRModule mod_;
+  // Array of unique operator names.
+  Array<String> operators;
+
+  void VisitExpr_(const OpNode* n) final {

Review comment:
       IMHO, it would be more useful if this pass can also report how many times each op is used in this model. Moreover, this pass can also report fused ops by visiting FunctionNode with kPrimitive=1.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org