Skip to content

Commit

Permalink
[Relay][Frontend][TFlite] Add support for quantized LOGISTIC
Browse files Browse the repository at this point in the history
 * add qnn implementation
 * add qnn test case for qnn logistic
  • Loading branch information
inadob committed Dec 13, 2019
1 parent 203d2c9 commit 351e8e8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
16 changes: 16 additions & 0 deletions python/tvm/relay/frontend/tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,23 @@ def convert_logistic(self, op):
input_tensor = input_tensors[0]
in_expr = self.get_expr(input_tensor.tensor_idx)

output_tensors = self.get_output_tensors(op)
assert len(output_tensors) == 1, "output tensors length should be 1"
output_tensor = output_tensors[0]
output_tensor_type = output_tensor.tensor.Type()
output_tensor_type_str = self.get_tensor_type_str(output_tensor_type)

if input_tensor.qnn_params:
in_expr = _qnn.op.dequantize(data=in_expr,
input_scale=input_tensor.qnn_params['scale'],
input_zero_point=input_tensor.qnn_params['zero_point'])
out = _op.sigmoid(in_expr)
if output_tensor.qnn_params:
out = _qnn.op.quantize(data=out,
output_scale=output_tensor.qnn_params['scale'],
output_zero_point=output_tensor.qnn_params['zero_point'],
out_dtype=output_tensor_type_str)

return out

def convert_softmax(self, op):
Expand Down
17 changes: 12 additions & 5 deletions tests/python/frontend/tflite/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,17 +995,24 @@ def test_forward_unpack():
# Logistic
# --------

def _test_logistic(data):
def _test_logistic(data, quantized=False):
""" One iteration of LOGISTIC """
with tf.Graph().as_default():
in_data = array_ops.placeholder(shape=data.shape, dtype=data.dtype)
out = math_ops.sigmoid(in_data)
compare_tflite_with_tvm(data, 'Placeholder:0', [in_data], [out])
in_data = [array_ops.placeholder(shape=data.shape, dtype='float32', name='in_0')]

if quantized:
inq_data = [tf.quantization.fake_quant_with_min_max_args(in_data[0], min=-100, max=100, name="inq_0")]
out = math_ops.sigmoid(inq_data)
out = tf.quantization.fake_quant_with_min_max_args(out, min=0, max=1, name="out")
compare_tflite_with_tvm(data, 'inq_0:0', inq_data, [out], quantized=True)
else:
out = math_ops.sigmoid(in_data)
compare_tflite_with_tvm(data, 'in_0:0', in_data, [out])

def test_forward_logistic():
""" LOGISTIC """
_test_logistic(np.arange(6.0, dtype=np.float32).reshape((1, 6)))

_test_logistic(np.array(np.random.uniform(0, 255, (3, 6)), dtype=np.uint8), quantized=True)

#######################################################################
# Softmax
Expand Down

0 comments on commit 351e8e8

Please sign in to comment.