-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[Frontend][TFlite] Add parser support for relu6, leaky_relu, relu_n1_to_1, log_softmax #4805
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,10 +94,12 @@ def __init__(self, model, subgraph, exp_tab): | |
'HARD_SWISH': self.convert_hard_swish, | ||
'L2_NORMALIZATION': self.convert_l2_normalization, | ||
'L2_POOL_2D': self.convert_l2_pool2d, | ||
'LEAKY_RELU': self.convert_leaky_relu, | ||
'LESS_EQUAL': self.convert_less_equal, | ||
'LESS': self.convert_less, | ||
'LOCAL_RESPONSE_NORMALIZATION': self.convert_lrn, | ||
'LOG': self.convert_log, | ||
'LOG_SOFTMAX': self.convert_log_softmax, | ||
'LOGICAL_AND': self.convert_logical_and, | ||
'LOGICAL_NOT': self.convert_logical_not, | ||
'LOGICAL_OR': self.convert_logical_or, | ||
|
@@ -121,6 +123,8 @@ def __init__(self, model, subgraph, exp_tab): | |
'REDUCE_MIN': self.convert_reduce_min, | ||
'REDUCE_PROD': self.convert_reduce_prod, | ||
'RELU':self.convert_relu, | ||
'RELU6': self.convert_relu6, | ||
'RELU_N1_TO_1': self.convert_relu_n1_to_1, | ||
'RESHAPE': self.convert_reshape, | ||
'RESIZE_BILINEAR': self.convert_resize_bilinear, | ||
'RESIZE_NEAREST_NEIGHBOR': self.convert_resize_nearest_neighbor, | ||
|
@@ -685,6 +689,136 @@ def _hard_swish(data): | |
|
||
return out | ||
|
||
def convert_relu6(self, op): | ||
"""Convert TFLite ReLU6""" | ||
input_tensors = self.get_input_tensors(op) | ||
assert len(input_tensors) == 1, "input tensors length should be 1" | ||
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] | ||
|
||
if input_tensor.qnn_params: | ||
# Quantize a float value to an quantized integer value | ||
scale_val = get_scalar_from_constant(input_tensor.qnn_params['scale']) | ||
zero_point_val = get_scalar_from_constant(input_tensor.qnn_params['zero_point']) | ||
quantize = lambda x: float(int(round(x / scale_val)) + zero_point_val) | ||
|
||
# Get min/max of the input dtype. This will be used to ensure that | ||
# clip a_min/a_max are not beyond the dtype range. | ||
input_tensor_type_str = self.get_tensor_type_str(input_tensor.tensor.Type()) | ||
qmin = float(tvm.tir.op.min_value(input_tensor_type_str).value) | ||
qmax = float(tvm.tir.op.max_value(input_tensor_type_str).value) | ||
|
||
out = _op.clip(in_expr, | ||
a_min=max(qmin, quantize(0)), | ||
a_max=min(qmax, quantize(6.0))) | ||
else: | ||
out = _op.clip(in_expr, a_min=0, a_max=6) | ||
|
||
if output_tensor.qnn_params: | ||
output_tensor_type_str = self.get_tensor_type_str(output_tensor.tensor.Type()) | ||
out = _qnn.op.requantize(out, | ||
input_scale=input_tensor.qnn_params['scale'], | ||
input_zero_point=input_tensor.qnn_params['zero_point'], | ||
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_leaky_relu(self, op): | ||
"""Convert TFLite LEAKY_RELU""" | ||
try: | ||
from tflite.BuiltinOptions import BuiltinOptions | ||
from tflite.LeakyReluOptions import LeakyReluOptions | ||
except ImportError: | ||
raise ImportError("The tflite package must be installed") | ||
|
||
input_tensors = self.get_input_tensors(op) | ||
assert len(input_tensors) == 1, "input tensors length should be 1" | ||
input_tensor = input_tensors[0] | ||
in_expr = self.get_expr(input_tensor.tensor_idx) | ||
|
||
assert op.BuiltinOptionsType() == BuiltinOptions.LeakyReluOptions | ||
op_options = op.BuiltinOptions() | ||
leaky_relu_options = LeakyReluOptions() | ||
leaky_relu_options.Init(op_options.Bytes, op_options.Pos) | ||
alpha_tensor = leaky_relu_options.Alpha() | ||
|
||
output_tensors = self.get_output_tensors(op) | ||
assert len(output_tensors) == 1, "output tensors length should be 1" | ||
output_tensor = output_tensors[0] | ||
|
||
if input_tensor.qnn_params: | ||
in_expr = self.dequantize(in_expr, input_tensor) | ||
out = _op.nn.leaky_relu(in_expr, alpha_tensor) | ||
if output_tensor.qnn_params: | ||
out = self.quantize(out, output_tensor) | ||
|
||
return out | ||
|
||
def convert_relu_n1_to_1(self, op): | ||
"""Convert TFLite RELU_N1_TO_1""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not important, I think it should be |
||
input_tensors = self.get_input_tensors(op) | ||
assert len(input_tensors) == 1, "input tensors length should be 1" | ||
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] | ||
|
||
if input_tensor.qnn_params: | ||
# Quantize a float value to an quantized integer value | ||
scale_val = get_scalar_from_constant(input_tensor.qnn_params['scale']) | ||
zero_point_val = get_scalar_from_constant(input_tensor.qnn_params['zero_point']) | ||
quantize = lambda x: float(int(round(x / scale_val)) + zero_point_val) | ||
|
||
# Get min/max of the input dtype. This will be used to ensure that | ||
# clip a_min/a_max are not beyond the dtype range. | ||
input_tensor_type_str = self.get_tensor_type_str(input_tensor.tensor.Type()) | ||
qmin = float(tvm.tir.op.min_value(input_tensor_type_str).value) | ||
qmax = float(tvm.tir.op.max_value(input_tensor_type_str).value) | ||
|
||
out = _op.clip(in_expr, | ||
a_min=max(qmin, quantize(-1.0)), | ||
a_max=min(qmax, quantize(1.0))) | ||
else: | ||
out = _op.clip(in_expr, a_min=-1, a_max=1) | ||
|
||
if output_tensor.qnn_params: | ||
output_tensor_type_str = self.get_tensor_type_str(output_tensor.tensor.Type()) | ||
out = _qnn.op.requantize(out, | ||
input_scale=input_tensor.qnn_params['scale'], | ||
input_zero_point=input_tensor.qnn_params['zero_point'], | ||
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_log_softmax(self, op): | ||
"""Convert TFLite LOG_SOFTMAX""" | ||
input_tensors = self.get_input_tensors(op) | ||
assert len(input_tensors) == 1, "input tensors length should be 1" | ||
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] | ||
|
||
if input_tensor.qnn_params: | ||
in_expr = self.dequantize(in_expr, input_tensor) | ||
out = _op.nn.log_softmax(in_expr) | ||
if output_tensor.qnn_params: | ||
out = self.quantize(out, output_tensor) | ||
|
||
return out | ||
|
||
def convert_concatenation(self, op): | ||
"""Convert TFLite concatenation""" | ||
try: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not important, I think it should be
"""Convert TFLite Leaky_ReLU"""
to align with"""One iteration of Leaky_ReLU"""
.