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/20 09:12:53 UTC

[GitHub] [tvm] ekalda commented on a change in pull request #9707: [microNPU] Added checks for out of range shifts

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