Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TFLite][Frontend] Support quantized LOCAL_RESPONSE_NORMALIZATION #17111

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions python/tvm/relay/frontend/tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,9 +803,6 @@ def convert_lrn(self, op):
except ImportError:
raise ImportError("The tflite package must be installed")

if self.is_quantized(op):
raise tvm.error.OpNotImplemented("TFlite quantized LRN operator is not supported yet.")

input_tensors = self.get_input_tensors(op)
assert len(input_tensors) == 1, "input tensors length should be 1"
input_tensor = input_tensors[0]
Expand All @@ -825,7 +822,12 @@ def convert_lrn(self, op):
size = (radius * 2) + 1
alpha = alpha * size
axis = 3 # NHWC format

if input_tensor.qnn_params:
in_expr = self.dequantize(in_expr, input_tensor)
out = _op.nn.lrn(in_expr, size=size, axis=axis, bias=bias, alpha=alpha, beta=beta)
if output_tensors.qnn_params:
out = self.quantize(out, output_tensors)

return out

Expand Down
11 changes: 9 additions & 2 deletions tests/python/frontend/tflite/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -3943,10 +3943,13 @@ def test_forward_unpack():
# ----------------------------


def _test_local_response_normalization(data, depth_radius, bias, alpha, beta):
def _test_local_response_normalization(data, depth_radius, bias, alpha, beta, quantized=False):
"""One iteration of LOCAL_RESPONSE_NORMALIZATION"""
with tf.Graph().as_default():
in_data = array_ops.placeholder(shape=data.shape, dtype="float32", name="in_0")
if quantized:
in_data = array_ops.placeholder(shape=data.shape, dtype=tf.qint8, name="in_0")
else:
in_data = array_ops.placeholder(shape=data.shape, dtype="float32", name="in_0")
out = nn_ops.local_response_normalization(
in_data, depth_radius=depth_radius, bias=bias, alpha=alpha, beta=beta
)
Expand All @@ -3956,9 +3959,13 @@ def _test_local_response_normalization(data, depth_radius, bias, alpha, beta):
def test_forward_local_response_normalization():
"""LOCAL_RESPONSE_NORMALIZATION"""
data = np.random.uniform(size=(1, 6, 4, 3)).astype("float32")
data_quant = np.random.uniform(size=(1, 6, 4, 3)).astype(tf.qint8)
# LOCAL_RESPONSE_NORMALIZATION come with TFLite >= 1.14.0 fbs schema
if package_version.parse(tf.VERSION) >= package_version.parse("1.14.0"):
_test_local_response_normalization(data, depth_radius=5, bias=1, alpha=1, beta=0.5)
_test_local_response_normalization(
data_quant, depth_radius=5, bias=1, alpha=1, beta=0.5, quantized=True
)


#######################################################################
Expand Down
Loading