From 70dfaf6588c5dff854a1b7200c0cbedd367a7978 Mon Sep 17 00:00:00 2001 From: Ina_Dobreva Date: Fri, 31 Jan 2020 17:09:28 +0000 Subject: [PATCH] Helper functions for quantize and dequantize. --- python/tvm/relay/frontend/tflite.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/python/tvm/relay/frontend/tflite.py b/python/tvm/relay/frontend/tflite.py index 791c056c4a3d6..db1ededa7aa1d 100644 --- a/python/tvm/relay/frontend/tflite.py +++ b/python/tvm/relay/frontend/tflite.py @@ -267,6 +267,23 @@ def is_quantized(self, op): first_tensor = input_tensors[0] return first_tensor.qnn_params is not None + def quantize(self, expr, tensor_to_quantize): + """ Helper function to quantize a tensor with Relay """ + tensor_type = tensor_to_quantize.tensor.Type() + tensor_type_str = self.get_tensor_type_str(tensor_type) + quantized = _qnn.op.quantize(data=expr, + output_scale=tensor_to_quantize.qnn_params['scale'], + output_zero_point=tensor_to_quantize.qnn_params['zero_point'], + out_dtype=tensor_type_str) + return quantized + + def dequantize(self, expr, tensor): + """ Helper function to dequantize a tensor with Relay """ + dequantized = _qnn.op.dequantize(data=expr, + input_scale=tensor.qnn_params['scale'], + input_zero_point=tensor.qnn_params['zero_point']) + return dequantized + def convert_conv2d(self, op): """Convert TFLite conv2d""" return self.convert_conv(op, "conv2d")