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 11:02:21 UTC

[GitHub] [tvm] lhutton1 opened a new pull request #9561: [microNPU] Add NHWC -> NHCWB16 layout transformation pass

lhutton1 opened a new pull request #9561:
URL: https://github.com/apache/tvm/pull/9561


   Adds a layout optimization pass that modifies the ifm/ofm layout of an operation to NHCWB16 where possible. This can occur when the producer or consumer of a tensor is also an NPU operator.
   
   Note: this PR is dependent on #9560.
   
   cc @ekalda @manupa-arm @NicolaLancellotti @dchauhan-arm @mbaret


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



[GitHub] [tvm] manupa-arm commented on pull request #9561: [microNPU] Add NHWC -> NHCWB16 layout transformation pass

Posted by GitBox <gi...@apache.org>.
manupa-arm commented on pull request #9561:
URL: https://github.com/apache/tvm/pull/9561#issuecomment-985386334


   Thanks! @lhutton1 @ekalda . This is merged 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.

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

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



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

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



##########
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:
       Good spot, thanks!




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

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

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



[GitHub] [tvm] manupa-arm merged pull request #9561: [microNPU] Add NHWC -> NHCWB16 layout transformation pass

Posted by GitBox <gi...@apache.org>.
manupa-arm merged pull request #9561:
URL: https://github.com/apache/tvm/pull/9561


   


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



[GitHub] [tvm] manupa-arm commented on pull request #9561: [microNPU] Add NHWC -> NHCWB16 layout transformation pass

Posted by GitBox <gi...@apache.org>.
manupa-arm commented on pull request #9561:
URL: https://github.com/apache/tvm/pull/9561#issuecomment-985371701


   @ekalda ?
   


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



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

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


   Friendly ping for comment/approval :)


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



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

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



##########
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:
       Thanks will do, I realized this as soon as I pushed the PR :)




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

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

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



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

Posted by GitBox <gi...@apache.org>.
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