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 2022/04/04 09:43:40 UTC

[GitHub] [tvm] manupa-arm commented on a diff in pull request #10862: [microNPU] Integrate the cascader

manupa-arm commented on code in PR #10862:
URL: https://github.com/apache/tvm/pull/10862#discussion_r841543201


##########
python/tvm/relay/backend/contrib/ethosu/codegen.py:
##########
@@ -361,9 +405,12 @@ def relay_to_tir(mod: tvm.ir.IRModule) -> tvm.ir.IRModule:
     # this should be a single intelligent and a composite scheduler
     # that can perform scheduling based on user inputs such as
     # scratch memory size.
-    mod = LowerToTIR(copy_constants)(mod)
+    if util.enable_cascader() and util.get_accelerator_config() != "ethos-u65-256":

Review Comment:
   We should error out if u65 is not supported yet but if its still given as an input



##########
python/tvm/relay/backend/contrib/ethosu/codegen.py:
##########
@@ -328,6 +330,48 @@ def constant_updater(expr, symbol):  # pylint: disable=unused-argument
     return dict()
 
 
+def _create_cascader(
+    options: CascaderOptions,
+    io_region: MemoryRegion,
+    constant_region: MemoryRegion,
+    working_regions: List[MemoryRegion],
+    device_config: EthosuDeviceConfig,
+) -> Callable:
+    def _cascader(te_graph, const_dict, sch):
+        cascade(
+            sch,
+            te_graph,
+            const_dict,
+            options,
+            io_region,
+            constant_region,
+            working_regions,
+            device_config,
+        )
+
+    return _cascader
+
+
+def _ethos_u55_cascader() -> Callable:
+    flash = MemoryRegion(name="FLASH", size=10 ** 7, read_bandwidth=4, write_bandwidth=4)
+    sram = MemoryRegion(name="SRAM", size=10 ** 6, read_bandwidth=16, write_bandwidth=16)

Review Comment:
   [Maybe for a subsequent PR] Will it be possible plumb these values using this : https://github.com/apache/tvm/blob/fcdf4636d866fefbf3e2a55daa614565ce97203a/src/relay/backend/build_module.cc#L414-L417
   
   An example test : 
   
   https://github.com/apache/tvm/blob/main/tests/python/relay/aot/test_crt_aot_usmp.py#L341-L383



##########
tests/python/contrib/test_ethosu/test_ops_end_to_end_with_cascader.py:
##########
@@ -0,0 +1,132 @@
+# 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.
+# pylint: disable=invalid-name, unused-argument
+import pytest
+
+pytest.importorskip("ethosu.vela")
+
+import numpy as np
+import tensorflow as tf
+
+from . import infra
+
+
+ACCEL_TYPES = ["ethos-u55-256", "ethos-u55-128", "ethos-u55-64", "ethos-u55-32"]
+
+
+@pytest.mark.parametrize("accel_type", ACCEL_TYPES)
+def test_ethosu_cascade_conv2d(accel_type):
+    np.random.seed(0)
+    ifm_shape = (1, 5, 39, 3)
+
+    @tf.function
+    def tf_graph(x):
+        ofm_channels = 5
+        conv2d = tf.nn.conv2d(
+            x,
+            filters=tf.constant(
+                np.random.uniform(size=[3, 2, ifm_shape[3], ofm_channels]),  # HWIO
+                dtype=tf.float32,
+            ),
+            strides=(1, 1),
+            padding="VALID",
+            dilations=(2, 1),
+        )
+        conv2d = tf.nn.conv2d(
+            conv2d,
+            filters=tf.constant(
+                np.random.uniform(size=(1, 1, ofm_channels, 3)),  # HWIO
+                dtype=tf.float32,
+            ),
+            strides=(3, 2),
+            padding="SAME",
+            dilations=(1, 1),
+        )
+
+        return conv2d
+
+    infra.compare_tvm_with_tflite(tf_graph, [ifm_shape], accel_type, enable_cascader=True)

Review Comment:
   For these tests, I think we need to ensure the memory usage is reduced.
   
   We could use calculate_workspace_size TIR analysis utility for that. E.g. : https://github.com/apache/tvm/blob/fcdf4636d866fefbf3e2a55daa614565ce97203a/tests/python/unittest/test_tir_analysis_calculate_workspace.py#L106
   
   OR we could use the final memory calculation in a non-USMP flow as follows : 
   
   https://github.com/apache/tvm/blob/fcdf4636d866fefbf3e2a55daa614565ce97203a/tests/python/relay/aot/test_crt_aot.py#L937-L956



##########
tests/python/contrib/test_ethosu/test_ops_end_to_end_with_cascader.py:
##########
@@ -0,0 +1,132 @@
+# 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.
+# pylint: disable=invalid-name, unused-argument
+import pytest
+
+pytest.importorskip("ethosu.vela")
+
+import numpy as np
+import tensorflow as tf
+
+from . import infra
+
+
+ACCEL_TYPES = ["ethos-u55-256", "ethos-u55-128", "ethos-u55-64", "ethos-u55-32"]
+
+
+@pytest.mark.parametrize("accel_type", ACCEL_TYPES)
+def test_ethosu_cascade_conv2d(accel_type):
+    np.random.seed(0)
+    ifm_shape = (1, 5, 39, 3)
+
+    @tf.function
+    def tf_graph(x):
+        ofm_channels = 5
+        conv2d = tf.nn.conv2d(
+            x,
+            filters=tf.constant(
+                np.random.uniform(size=[3, 2, ifm_shape[3], ofm_channels]),  # HWIO
+                dtype=tf.float32,
+            ),
+            strides=(1, 1),
+            padding="VALID",
+            dilations=(2, 1),
+        )
+        conv2d = tf.nn.conv2d(
+            conv2d,
+            filters=tf.constant(
+                np.random.uniform(size=(1, 1, ofm_channels, 3)),  # HWIO
+                dtype=tf.float32,
+            ),
+            strides=(3, 2),
+            padding="SAME",
+            dilations=(1, 1),
+        )
+
+        return conv2d
+
+    infra.compare_tvm_with_tflite(tf_graph, [ifm_shape], accel_type, enable_cascader=True)

Review Comment:
   For these tests, I think we need to ensure the memory usage is reduced.
   
   We could use calculate_workspace_size TIR analysis utility for that. E.g. : https://github.com/apache/tvm/blob/fcdf4636d866fefbf3e2a55daa614565ce97203a/tests/python/unittest/test_tir_analysis_calculate_workspace.py#L106
   
   OR we could use the final memory calculation in a non-USMP flow as follows : 
   
   https://github.com/apache/tvm/blob/fcdf4636d866fefbf3e2a55daa614565ce97203a/tests/python/relay/aot/test_crt_aot.py#L937-L956



##########
python/tvm/relay/backend/contrib/ethosu/codegen.py:
##########
@@ -328,6 +330,48 @@ def constant_updater(expr, symbol):  # pylint: disable=unused-argument
     return dict()
 
 
+def _create_cascader(
+    options: CascaderOptions,
+    io_region: MemoryRegion,
+    constant_region: MemoryRegion,
+    working_regions: List[MemoryRegion],
+    device_config: EthosuDeviceConfig,
+) -> Callable:
+    def _cascader(te_graph, const_dict, sch):
+        cascade(
+            sch,
+            te_graph,
+            const_dict,
+            options,
+            io_region,
+            constant_region,
+            working_regions,
+            device_config,
+        )
+
+    return _cascader
+
+
+def _ethos_u55_cascader() -> Callable:
+    flash = MemoryRegion(name="FLASH", size=10 ** 7, read_bandwidth=4, write_bandwidth=4)
+    sram = MemoryRegion(name="SRAM", size=10 ** 6, read_bandwidth=16, write_bandwidth=16)

Review Comment:
   [Maybe for a subsequent PR] Will it be possible plumb these values using this : https://github.com/apache/tvm/blob/fcdf4636d866fefbf3e2a55daa614565ce97203a/src/relay/backend/build_module.cc#L414-L417
   
   An example test : 
   
   https://github.com/apache/tvm/blob/main/tests/python/relay/aot/test_crt_aot_usmp.py#L341-L383



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