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/08/02 09:28:55 UTC
[GitHub] [tvm] mbaret commented on a change in pull request #8368: [TFLite][Testing] Add infra to write TFLite model buffers directly
mbaret commented on a change in pull request #8368:
URL: https://github.com/apache/tvm/pull/8368#discussion_r680817420
##########
File path: tests/python/frontend/tflite/test_forward.py
##########
@@ -868,74 +869,177 @@ def test_forward_l2_pool2d():
# -----------
-def _test_tflite2_quantized_convolution(
- input_shape, kernel_shape, dilations, strides, padding, data_format
+def _test_tflite2_quantized_conv2d(
+ input_shape,
+ weights_shape,
+ dilations,
+ strides,
+ padding,
+ dtype="int8",
+ quantize_per_channel=False,
):
"""One iteration of TFLite2 quantized convolution with given shapes and attributes"""
- data_format = "channels_last" if "NHWC" else "channels_first"
- data = np.random.uniform(0, 1, input_shape).astype("float32")
- kernel = np.random.uniform(0, 1, kernel_shape).astype("float32")
- data_in = tf.keras.layers.Input(shape=data.shape[1:])
- conv = tf.keras.layers.Conv2D(
- filters=kernel_shape[3],
- kernel_size=(kernel_shape[0], kernel_shape[1]),
- strides=strides,
- padding=padding,
- data_format=data_format,
- activation="relu",
- use_bias=False,
- )(data_in)
- keras_model = tf.keras.models.Model(data_in, conv)
- keras_model.layers[1].set_weights([kernel])
+ dtype_min, dtype_max = test_tflite.get_range_for_dtype_str(dtype)
+ channels = weights_shape[0]
- # To create quantized values with dynamic range of activations, needs representative dataset
- def representative_data_gen():
- for i in range(1):
- yield [data]
+ input_scale = np.random.random() * 0.1
+ input_zp = np.random.randint(dtype_min, dtype_max)
+ in_tensor = test_tflite.Tensor(
+ data_type=dtype,
+ shape=input_shape,
+ quantization=test_tflite.Quantization(scale=[input_scale], zero_point=[input_zp]),
+ )
- tflite_model_quant = _quantize_keras_model(keras_model, representative_data_gen)
+ # Weights in TFLite 2 are symmetric, i.e the zero point is at 0
+ if quantize_per_channel:
+ weights_scale = [np.random.random() * 0.1 for i in range(channels)]
+ weights_zp = [0 for i in range(channels)]
+ else:
+ weights_scale = [np.random.random() * 0.1]
+ weights_zp = [0]
+ weights_quantization = test_tflite.Quantization(
Review comment:
I like the idea of the operators being able to create their own weights/biases. That is somewhat analogous to what happens when you create the operators with Keras/TF. This way if we need to create other Conv2D tests, we don't need to replicate a lot of boilerplate. I'm also inclined to agree that we can default the data type to int8.
--
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