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/12/10 14:40:10 UTC

[GitHub] [tvm] jacobbohlin opened a new pull request #9707: [microNPU] Added checks for out of range shifts

jacobbohlin opened a new pull request #9707:
URL: https://github.com/apache/tvm/pull/9707


   Bugfix, removed an invalid assert.
   Added explicit handling of quantization `scale` and `shift` values that are outside the valid range of the NPU.
   


-- 
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 #9707: [microNPU] Added checks for out of range shifts

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



##########
File path: tests/python/contrib/test_ethosu/test_codegen.py
##########
@@ -242,6 +242,93 @@ def representative_dataset():
     infra.verify_source(compiled_models, accel_type)
 
 
+@pytest.mark.parametrize("weight_min, weight_max", [(0.0, 1e-11), (-1e10, 1e10)])
+def test_out_of_range_scaling(weight_min, weight_max):
+    ifm_shape = (1, 6, 6, 2)
+    dtype = "int8"
+    strides = (1, 1)
+    kernel_shape = (1, 1)
+    dilation = (1, 1)
+    padding = "SAME"
+    activation = "RELU"
+    accel_type = "ethos-u55-128"
+
+    def create_tflite_graph():
+        class Model(tf.Module):
+            @tf.function
+            def tf_function(self, x):
+                # Use tf.nn API to create the model
+                tf_strides = [1, strides[0], strides[1], 1]
+                weights = np.random.uniform(size=[kernel_shape[-1], kernel_shape[1], 2, 2])

Review comment:
       It doesn't make an effective difference here, but I think `kernel_shape[-1]` and `kernel_shape[1]` refer to the same element

##########
File path: tests/python/contrib/test_ethosu/test_codegen.py
##########
@@ -242,6 +242,93 @@ def representative_dataset():
     infra.verify_source(compiled_models, accel_type)
 
 
+@pytest.mark.parametrize("weight_min, weight_max", [(0.0, 1e-11), (-1e10, 1e10)])
+def test_out_of_range_scaling(weight_min, weight_max):
+    ifm_shape = (1, 6, 6, 2)
+    dtype = "int8"
+    strides = (1, 1)
+    kernel_shape = (1, 1)
+    dilation = (1, 1)
+    padding = "SAME"
+    activation = "RELU"
+    accel_type = "ethos-u55-128"
+
+    def create_tflite_graph():
+        class Model(tf.Module):
+            @tf.function
+            def tf_function(self, x):
+                # Use tf.nn API to create the model
+                tf_strides = [1, strides[0], strides[1], 1]
+                weights = np.random.uniform(size=[kernel_shape[-1], kernel_shape[1], 2, 2])
+                # Overwrite to force quantization that produces out of range shift values
+                weights[0][0][0][0] = weight_min
+                weights[0][0][1][0] = weight_max
+                op = tf.nn.conv2d(
+                    x,
+                    filters=tf.constant(
+                        weights,
+                        dtype=tf.float32,
+                    ),
+                    strides=tf_strides,
+                    padding=padding,
+                    dilations=dilation,
+                )
+                if activation:
+                    op = tf.nn.relu(op)
+                return op
+
+        model = Model()
+        concrete_func = model.tf_function.get_concrete_function(
+            tf.TensorSpec(ifm_shape, dtype=tf.float32)
+        )
+
+        # Convert the model
+        def representative_dataset():
+            for _ in range(100):
+                data = np.random.rand(*tuple(ifm_shape))
+                yield [data.astype(np.float32)]
+
+        converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
+        converter.optimizations = [tf.lite.Optimize.DEFAULT]
+        converter.representative_dataset = representative_dataset
+        converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
+        converter.inference_input_type = tf.int8
+        converter.inference_output_type = tf.int8
+        tflite_model = converter.convert()
+        return tflite_model
+
+    tflite_graph = create_tflite_graph()
+    tflite_model = tflite.Model.Model.GetRootAsModel(tflite_graph, 0)
+
+    relay_module, params = relay.frontend.from_tflite(
+        tflite_model,
+        shape_dict={"input": ifm_shape},
+        dtype_dict={"input": dtype},
+    )
+    mod = partition_for_ethosu(relay_module, params)
+    print(mod)

Review comment:
       remove print




-- 
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 edited a comment on pull request #9707: [microNPU] Added checks for out of range shifts

Posted by GitBox <gi...@apache.org>.
manupa-arm edited a comment on pull request #9707:
URL: https://github.com/apache/tvm/pull/9707#issuecomment-991095347


   Thanks @jacobbohlin .
   
   Similiar comment to other PR.
   Let us add a test case, however in this case I think a small end to end test might be meaningful because it is a low-level change, we might want to make sure this is the right change that produces a CS that runs. WDYT ?


-- 
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 #9707: [microNPU] Added checks for out of range shifts

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


   Thanks @jacobbohlin .
   
   Similiar comment to other PR.
   Let us a test case, however in this case I think a small end to end test might be meaningful because it is a low-level change, we might want to make sure this is the right change that produces a CS that runs. WDYT ?


-- 
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] jacobbohlin commented on a change in pull request #9707: [microNPU] Added checks for out of range shifts

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



##########
File path: tests/python/contrib/test_ethosu/test_codegen.py
##########
@@ -242,6 +242,93 @@ def representative_dataset():
     infra.verify_source(compiled_models, accel_type)
 
 
+@pytest.mark.parametrize("weight_min, weight_max", [(0.0, 1e-11), (-1e10, 1e10)])
+def test_out_of_range_scaling(weight_min, weight_max):
+    ifm_shape = (1, 6, 6, 2)
+    dtype = "int8"
+    strides = (1, 1)
+    kernel_shape = (1, 1)
+    dilation = (1, 1)
+    padding = "SAME"
+    activation = "RELU"
+    accel_type = "ethos-u55-128"
+
+    def create_tflite_graph():
+        class Model(tf.Module):
+            @tf.function
+            def tf_function(self, x):
+                # Use tf.nn API to create the model
+                tf_strides = [1, strides[0], strides[1], 1]
+                weights = np.random.uniform(size=[kernel_shape[-1], kernel_shape[1], 2, 2])

Review comment:
       You are right, I fixed this and refactored to use Matthew's functions

##########
File path: tests/python/contrib/test_ethosu/test_codegen.py
##########
@@ -242,6 +242,93 @@ def representative_dataset():
     infra.verify_source(compiled_models, accel_type)
 
 
+@pytest.mark.parametrize("weight_min, weight_max", [(0.0, 1e-11), (-1e10, 1e10)])
+def test_out_of_range_scaling(weight_min, weight_max):
+    ifm_shape = (1, 6, 6, 2)
+    dtype = "int8"
+    strides = (1, 1)
+    kernel_shape = (1, 1)
+    dilation = (1, 1)
+    padding = "SAME"
+    activation = "RELU"
+    accel_type = "ethos-u55-128"
+
+    def create_tflite_graph():
+        class Model(tf.Module):
+            @tf.function
+            def tf_function(self, x):
+                # Use tf.nn API to create the model
+                tf_strides = [1, strides[0], strides[1], 1]
+                weights = np.random.uniform(size=[kernel_shape[-1], kernel_shape[1], 2, 2])
+                # Overwrite to force quantization that produces out of range shift values
+                weights[0][0][0][0] = weight_min
+                weights[0][0][1][0] = weight_max
+                op = tf.nn.conv2d(
+                    x,
+                    filters=tf.constant(
+                        weights,
+                        dtype=tf.float32,
+                    ),
+                    strides=tf_strides,
+                    padding=padding,
+                    dilations=dilation,
+                )
+                if activation:
+                    op = tf.nn.relu(op)
+                return op
+
+        model = Model()
+        concrete_func = model.tf_function.get_concrete_function(
+            tf.TensorSpec(ifm_shape, dtype=tf.float32)
+        )
+
+        # Convert the model
+        def representative_dataset():
+            for _ in range(100):
+                data = np.random.rand(*tuple(ifm_shape))
+                yield [data.astype(np.float32)]
+
+        converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
+        converter.optimizations = [tf.lite.Optimize.DEFAULT]
+        converter.representative_dataset = representative_dataset
+        converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
+        converter.inference_input_type = tf.int8
+        converter.inference_output_type = tf.int8
+        tflite_model = converter.convert()
+        return tflite_model
+
+    tflite_graph = create_tflite_graph()
+    tflite_model = tflite.Model.Model.GetRootAsModel(tflite_graph, 0)
+
+    relay_module, params = relay.frontend.from_tflite(
+        tflite_model,
+        shape_dict={"input": ifm_shape},
+        dtype_dict={"input": dtype},
+    )
+    mod = partition_for_ethosu(relay_module, params)
+    print(mod)

Review comment:
       Done




-- 
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] jacobbohlin commented on pull request #9707: [microNPU] Added checks for out of range shifts

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


   @manupa-arm Sounds good, I will add a small end to end test that takes this code path.


-- 
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 #9707: [microNPU] Added checks for out of range shifts

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


   Thanks @jacobbohlin @ekalda @lhutton1. 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] manupa-arm merged pull request #9707: [microNPU] Added checks for out of range shifts

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


   


-- 
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] jacobbohlin commented on pull request #9707: [microNPU] Added checks for out of range shifts

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


   @manupa-arm @NicolaLancellotti @ekalda @lhutton1 @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