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/12/01 11:04:06 UTC

[GitHub] [tvm] lhutton1 commented on a diff in pull request #13212: [TVMC][microNPU] tvmc option for printing which operators are offloaded to Ethos-U

lhutton1 commented on code in PR #13212:
URL: https://github.com/apache/tvm/pull/13212#discussion_r1035009043


##########
python/tvm/driver/tvmc/compiler.py:
##########
@@ -257,6 +274,10 @@ def compile_model(
     dump_code : list, optional
         Dump the generated code for the specified source types, on
         the requested target.
+    dump_offloads : str
+        Dump the the information about the partition of input model's layers by external codegen.

Review Comment:
   nit: s/the the/the



##########
python/tvm/relay/analysis/operations_distribution.py:
##########
@@ -0,0 +1,86 @@
+# 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.
+"""Utilities that enable analyze Relay and get mappings for unique
+input module layer name to the tuple of compiler and operation name"""
+import tvm
+from tvm import relay
+from tvm.relay.expr_functor import ExprVisitor
+
+
+class AnalyzeOperationsDistribution(ExprVisitor):
+    """A visitor pass that maintai the dictionary unic_op_ids where

Review Comment:
   nit: maintains



##########
python/tvm/driver/tvmc/compiler.py:
##########
@@ -459,3 +493,65 @@ def save_dumps(module_name: str, dumps: Dict[str, str], dump_root: str = "."):
         dump_name = module_name + "." + dump_format
         with open(Path(dump_root, dump_name), "w") as f:
             f.write(dumps[dump_format])
+
+
+def dump_operation_offloads(mod: tvm.ir.IRModule, initial_relay_astext: list, dump_path: str):
+    """This helper function forms a line-by-line output of the initial Relay lines,
+    indicating which operations are ported to which backend,
+    indicating the composite that includes those operations e.g
+    'device1    <- device2.qnn_conv2d'
+    'device1    <-        %0 = qnn.conv2d(%tfl.quantize, %v_param_1, ...'
+    'device1    <-        %1 = nn.bias_add(%0, %v_param_2, axis=3);'
+    'device1    <-        %2 = qnn.requantize(%1, meta[relay.Constant]...'
+    'device2    <- device2.reshape'
+    'device2    <-        %3 = reshape(%206, newshape=[1, 1001]);'

Review Comment:
   Perhaps its worth showing an example of the generic case here as well?



##########
python/tvm/relay/frontend/common.py:
##########
@@ -997,3 +998,57 @@ def try_resolve_var_to_const(x, graph_params):
         return _op.const(value, dtype)
 
     return x
+
+
+# Span filling to Relay for TFLite taken from
+# commit https://github.com/apache/tvm/pull/9723

Review Comment:
   Its seems like this implementation was reverted in #10072, but #13402 looks to be achieving something similar, do you think it would be possible to rebase this PR ontop of #13402?



##########
python/tvm/driver/tvmc/compiler.py:
##########
@@ -459,3 +493,65 @@ def save_dumps(module_name: str, dumps: Dict[str, str], dump_root: str = "."):
         dump_name = module_name + "." + dump_format
         with open(Path(dump_root, dump_name), "w") as f:
             f.write(dumps[dump_format])
+
+
+def dump_operation_offloads(mod: tvm.ir.IRModule, initial_relay_astext: list, dump_path: str):
+    """This helper function forms a line-by-line output of the initial Relay lines,
+    indicating which operations are ported to which backend,
+    indicating the composite that includes those operations e.g
+    'device1    <- device2.qnn_conv2d'
+    'device1    <-        %0 = qnn.conv2d(%tfl.quantize, %v_param_1, ...'
+    'device1    <-        %1 = nn.bias_add(%0, %v_param_2, axis=3);'
+    'device1    <-        %2 = qnn.requantize(%1, meta[relay.Constant]...'
+    'device2    <- device2.reshape'
+    'device2    <-        %3 = reshape(%206, newshape=[1, 1001]);'
+
+    Parameters
+    ----------
+    mod : tvm.ir.IRModule
+        The IRModule that gets generated from a relay frontend.
+    initial_relay_astext : list
+        List of input model IR strings.
+    dump_path: str
+        Value of the "dump_offloads" compiler atribute.
+        Could be dash ("-") or file path or empty string for
+        printing to console, file or doing nothing respectively.
+    """
+    print_to_console = dump_path == "-"
+    save_to_file = all([dump_path != "-", dump_path != ""])
+
+    if print_to_console or save_to_file:
+
+        operations_distribution = analyze_operations_distribution(mod)
+        output = []
+        prev_op_name = ""
+        for s in initial_relay_astext:
+            result = re.search(r"(output_name: )(.*)(:0:0)(.*)\*/", s)
+            if result:
+                op = result.group(2)
+                hardw_id = ""

Review Comment:
   nit: just to avoid possible confusion, calling this `target_id` might be better as this doesn't strictly need to be different hardware (e.g. `arm_compute_library`)



##########
python/tvm/relay/analysis/operations_distribution.py:
##########
@@ -0,0 +1,86 @@
+# 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.
+"""Utilities that enable analyze Relay and get mappings for unique
+input module layer name to the tuple of compiler and operation name"""
+import tvm
+from tvm import relay
+from tvm.relay.expr_functor import ExprVisitor
+
+
+class AnalyzeOperationsDistribution(ExprVisitor):

Review Comment:
   It would be great if we could add some stand alone tests for this pass



##########
python/tvm/driver/tvmc/compiler.py:
##########
@@ -459,3 +493,65 @@ def save_dumps(module_name: str, dumps: Dict[str, str], dump_root: str = "."):
         dump_name = module_name + "." + dump_format
         with open(Path(dump_root, dump_name), "w") as f:
             f.write(dumps[dump_format])
+
+
+def dump_operation_offloads(mod: tvm.ir.IRModule, initial_relay_astext: list, dump_path: str):
+    """This helper function forms a line-by-line output of the initial Relay lines,
+    indicating which operations are ported to which backend,
+    indicating the composite that includes those operations e.g
+    'device1    <- device2.qnn_conv2d'
+    'device1    <-        %0 = qnn.conv2d(%tfl.quantize, %v_param_1, ...'
+    'device1    <-        %1 = nn.bias_add(%0, %v_param_2, axis=3);'
+    'device1    <-        %2 = qnn.requantize(%1, meta[relay.Constant]...'
+    'device2    <- device2.reshape'
+    'device2    <-        %3 = reshape(%206, newshape=[1, 1001]);'
+
+    Parameters
+    ----------
+    mod : tvm.ir.IRModule
+        The IRModule that gets generated from a relay frontend.
+    initial_relay_astext : list

Review Comment:
   Perhaps I missed it, what's the reason for parsing the initial Relay as a string, rather than traversing a copy of the IRModule?



##########
tests/python/driver/tvmc/test_compiler.py:
##########
@@ -48,6 +51,144 @@ def test_save_dumps(tmpdir_factory):
     assert path.exists("{}/{}".format(tmpdir, "fake_module.relay"))
 
 
+def test_save_dump_offloads_ethosu(tmp_path_factory):
+    def representative_dataset_random_gen():
+        np.random.seed(0)
+        data = np.random.random_sample(input_shape)
+        yield [data.astype(np.float32)]
+
+    tflite = pytest.importorskip("tflite")
+    tensorflow = pytest.importorskip("tensorflow")
+    pytest.importorskip("ethosu.vela")
+
+    from tensorflow.keras import layers
+    from tensorflow.keras.models import Model
+    from tvm.driver.tvmc.model import TVMCModel
+
+    inp = (224, 224, 8)
+    input_shape = (1, *inp)
+
+    inputs = layers.Input(shape=inp)
+    conv_1 = layers.Conv2D(32, (3, 3), padding="same", strides=2)(inputs)
+    dns_1 = layers.Dense(12, activation="linear")(conv_1)
+    outputs = layers.ZeroPadding2D()(dns_1)
+
+    tf_model = Model(inputs, outputs)
+
+    converter = tensorflow.lite.TFLiteConverter.from_keras_model(tf_model)
+    converter.optimizations = [tensorflow.lite.Optimize.DEFAULT]
+    converter.representative_dataset = representative_dataset_random_gen
+    converter.target_spec.supported_ops = [tensorflow.lite.OpsSet.TFLITE_BUILTINS_INT8]
+    converter.inference_input_type = tensorflow.int8
+    converter.inference_output_type = tensorflow.int8
+    tflite_model_buf = converter.convert()

Review Comment:
   nit: Is it possible to make use of?https://github.com/apache/tvm/blob/a4840e7de38c5a2000917f2101f3ec4a374bcd39/tests/python/contrib/test_ethosu/infra.py#L288



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