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/11/23 14:19:23 UTC

[GitHub] [tvm] ekalda commented on a change in pull request #9561: [microNPU] Add NHWC -> NHCWB16 layout transformation pass

ekalda commented on a change in pull request #9561:
URL: https://github.com/apache/tvm/pull/9561#discussion_r755062441



##########
File path: python/tvm/relay/backend/contrib/ethosu/codegen.py
##########
@@ -52,29 +60,147 @@ def constant_updater(expr, symbol):  # pylint: disable=unused-argument
     return dict()
 
 
+class LayoutOptimization(ExprMutator):
+    """A pass to optimize the layout of NPU operations. If both the
+    producer and consumer of a tensor are NPU operators, then the
+    layout is converted from NHWC to NHCWB16.
+    """
+
+    def __init__(self):
+        self.children = {}
+        self.optimize_op = {
+            "contrib.ethosu.conv2d": ethosu_op.ethosu_conv2d,
+            "contrib.ethosu.depthwise_conv2d": ethosu_op.ethosu_depthwise_conv2d,
+            "contrib.ethosu.pooling": ethosu_op.ethosu_pooling,
+            "contrib.ethosu.binary_elementwise": ethosu_op.ethosu_binary_elementwise,
+        }

Review comment:
       While we are at it, maybe add `contrib.ethosu.unary_elementwise` as well?

##########
File path: python/tvm/relay/backend/contrib/ethosu/codegen.py
##########
@@ -52,29 +60,147 @@ def constant_updater(expr, symbol):  # pylint: disable=unused-argument
     return dict()
 
 
+class LayoutOptimization(ExprMutator):
+    """A pass to optimize the layout of NPU operations. If both the
+    producer and consumer of a tensor are NPU operators, then the
+    layout is converted from NHWC to NHCWB16.
+    """
+
+    def __init__(self):
+        self.children = {}

Review comment:
       Maybe you can add a comment about what `self.children` holds and what is it used for? 

##########
File path: python/tvm/relay/backend/contrib/ethosu/codegen.py
##########
@@ -52,29 +60,147 @@ def constant_updater(expr, symbol):  # pylint: disable=unused-argument
     return dict()
 
 
+class LayoutOptimization(ExprMutator):
+    """A pass to optimize the layout of NPU operations. If both the
+    producer and consumer of a tensor are NPU operators, then the
+    layout is converted from NHWC to NHCWB16.
+    """
+
+    def __init__(self):
+        self.children = {}
+        self.optimize_op = {
+            "contrib.ethosu.conv2d": ethosu_op.ethosu_conv2d,
+            "contrib.ethosu.depthwise_conv2d": ethosu_op.ethosu_depthwise_conv2d,
+            "contrib.ethosu.pooling": ethosu_op.ethosu_pooling,
+            "contrib.ethosu.binary_elementwise": ethosu_op.ethosu_binary_elementwise,
+        }
+
+        super().__init__()
+
+    def alter_ethosu_op_layout(self, call: tvm.relay.expr.Call) -> tvm.relay.expr.Call:
+        """Alter the input and output layouts of an NPU operation if needed.
+        Input layout is only altered if the producing operation is an NPU
+        operation. Likewise, the output layout is only altered if the consuming
+        operation is an NPU operation.
+
+        Parameters
+        ----------
+        call : tvm.relay.expr.Call
+            The call pointing to an NPU operation that will be checked if
+            the layout needs altering.
+
+        Returns
+        -------
+        new_call : tvm.relay.expr.Call
+            New call with altered layouts.
+        """
+        assert isinstance(call.attrs, tvm.ir.Attrs), (
+            f"The attributes for operator '{call.op.name}' could not be "
+            "found. Did you register the relay.attrs.Ethosu<opname>Attrs "
+            "object in python api?"
+        )
+
+        new_attrs = dict(call.attrs)
+        parents = []
+
+        # Check if we can rewrite the input layouts
+        layout_count = 0

Review comment:
       Nit: I think name `layout_count` is slightly misleading since we are essentially counting inputs, not layouts...

##########
File path: tests/python/contrib/test_ethosu/test_layout_optimizer.py
##########
@@ -0,0 +1,563 @@
+# 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 the layout optimization pass. This pass is used to
+convert subgraphs to the preferred layout of NCHWB16.

Review comment:
       Nit: NHCWB16

##########
File path: tests/python/contrib/test_ethosu/test_layout_optimizer.py
##########
@@ -0,0 +1,563 @@
+# 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 the layout optimization pass. This pass is used to
+convert subgraphs to the preferred layout of NCHWB16.
+"""
+
+import sys
+
+import pytest
+import numpy as np
+import tensorflow as tf
+import tflite.Model
+
+pytest.importorskip("ethosu.vela")

Review comment:
       Nit: I think in the other tests we have this line right below the pytest import so that we don't go on to import bunch of stuff if we are not going to run the test




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