Skip to content

Commit aafb334

Browse files
committed
Support quantized Pad op.
1 parent 814bdba commit aafb334

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

python/tvm/relay/frontend/tflite.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,8 +1426,19 @@ def convert_pad(self, op):
14261426
# convert list of lists to tuple of tuples
14271427
paddings = tuple(tuple(l) for l in pad_list)
14281428

1429-
# Use default pad_value 0 because TFLite does not support constant_values parameter
1430-
out = _op.nn.pad(in_expr, paddings)
1429+
# Set the pad value
1430+
pad_value = 0
1431+
if input_tensor.qnn_params:
1432+
# Check that input and output tensor have same qnn params.
1433+
output_tensors = self.get_output_tensors(op)
1434+
output_tensor = output_tensors[0]
1435+
assert self.has_same_qnn_params(input_tensor, output_tensor), \
1436+
"TFLite reshape requires input and output scale and zero points to be equal"
1437+
1438+
# The pad value for quantized pad is the input zero point.
1439+
pad_value = float(input_tensor.qnn_params['zero_point'].data.asnumpy())
1440+
1441+
out = _op.nn.pad(in_expr, pad_width=paddings, pad_value=pad_value)
14311442
return out
14321443

14331444
def convert_pack(self, op):

tests/python/frontend/tflite/test_forward.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,16 +1117,27 @@ def test_forward_squeeze():
11171117
# Pad
11181118
# ---
11191119

1120-
def _test_pad(data):
1120+
def _test_pad(data, quantized=False):
11211121
""" One iteration of PAD """
11221122

11231123
assert len(data) == 2
11241124

11251125
# Test with tensor and constant
11261126
with tf.Graph().as_default():
1127-
in_data = [array_ops.placeholder(shape=data[0].shape, dtype=data[0].dtype, name='in')]
1128-
out = array_ops.pad(in_data[0], ops.convert_to_tensor(data[1], dtype=data[1].dtype))
1129-
compare_tflite_with_tvm([data[0]], ['in:0'], in_data, [out])
1127+
in_data = [array_ops.placeholder(shape=data[0].shape, dtype='float32', name='in')]
1128+
1129+
if quantized:
1130+
min_value, max_value = -100, 100
1131+
# fake_quant will keep the tensors in float32 until the conversion in the session
1132+
inq_data = [tf.quantization.fake_quant_with_min_max_args(in_data[0],
1133+
min=-100,
1134+
max=100,
1135+
name="inq_0")]
1136+
out = array_ops.pad(inq_data[0], ops.convert_to_tensor(data[1], dtype=data[1].dtype))
1137+
compare_tflite_with_tvm([data[0]], ['inq_0:0'], inq_data, [out], quantized=True)
1138+
else:
1139+
out = array_ops.pad(in_data[0], ops.convert_to_tensor(data[1], dtype=data[1].dtype))
1140+
compare_tflite_with_tvm([data[0]], ['in:0'], in_data, [out])
11301141

11311142

11321143
def test_forward_pad():
@@ -1139,6 +1150,8 @@ def test_forward_pad():
11391150
np.array([[1, 1], [2, 2]], dtype=np.int32)])
11401151
_test_pad([np.arange(1.0, 4.0, dtype=np.float32).reshape((1, 3)),
11411152
np.array([[1, 1], [2, 2]], dtype=np.int32)])
1153+
_test_pad([np.arange(0, 256, dtype=np.uint8).reshape((1, 256)),
1154+
np.array([[1, 1], [2, 2]], dtype=np.int32)], quantized=True)
11421155

11431156

11441157
#######################################################################

0 commit comments

Comments
 (0)