Skip to content

Commit

Permalink
[frontend][tflite] float16 quant support (apache#7736)
Browse files Browse the repository at this point in the history
* [frontend][tflite] float16 quant support

* remove skip conditions in tests
  • Loading branch information
eric authored and Trevor Morris committed May 6, 2021
1 parent c8584ef commit 6161899
Show file tree
Hide file tree
Showing 2 changed files with 349 additions and 149 deletions.
35 changes: 24 additions & 11 deletions python/tvm/relay/frontend/tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -1882,9 +1882,12 @@ def convert_fully_connected(self, op):
# bias tensor type should be INT32 (quantization) or FLOAT32
assert bias_tensor_type in (TensorType.INT32, TensorType.FLOAT32)
bias_tensor_type_str = self.get_tensor_type_str(bias_tensor_type)
bias_expr = self.exp_tab.new_const(
self.get_tensor_value(bias_tensor), dtype=bias_tensor_type_str
)
if self.has_expr(bias_tensor.tensor_idx):
bias_expr = self.get_expr(bias_tensor.tensor_idx)
else:
bias_expr = self.exp_tab.new_const(
self.get_tensor_value(bias_tensor), dtype=bias_tensor_type_str
)
out = _op.nn.bias_add(out, bias_expr)

# Finally if the dense is quantized. Add a requantize at the end.
Expand Down Expand Up @@ -2852,11 +2855,18 @@ def convert_transpose_conv(self, op):
# weights tensor type should be UINT8 (quantization) or FLOAT32
assert weights_tensor_type in (TensorType.INT8, TensorType.UINT8, TensorType.FLOAT32)
weight_tensor_type_str = self.get_tensor_type_str(weights_tensor_type)
weight_value_ohwi = self.get_tensor_value(weights_tensor)
# Relay kernel_layout should be OIHW
# Relay weights layout should be different from kernel_layout - it should be IOHW
weight_value_iohw = np.transpose(weight_value_ohwi, (3, 0, 1, 2))
weight_expr_iohw = self.exp_tab.new_const(weight_value_iohw, dtype=weight_tensor_type_str)

if self.has_expr(weights_tensor.tensor_idx):
weight_expr_iohw = self.get_expr(weights_tensor.tensor_idx)
weight_expr_iohw = _op.transpose(weight_expr_iohw, axes=(3, 0, 1, 2))
else:
weight_value_ohwi = self.get_tensor_value(weights_tensor)
# Relay kernel_layout should be OIHW
# Relay weights layout should be different from kernel_layout - it should be IOHW
weight_value_iohw = np.transpose(weight_value_ohwi, (3, 0, 1, 2))
weight_expr_iohw = self.exp_tab.new_const(
weight_value_iohw, dtype=weight_tensor_type_str
)

# Output shape value
output_shape_value = self.get_tensor_value(output_shape_tensor)
Expand Down Expand Up @@ -2912,9 +2922,12 @@ def convert_transpose_conv(self, op):
# bias tensor type should be INT32 (quantization) or FLOAT32
assert bias_tensor_type in (TensorType.INT32, TensorType.FLOAT32)
bias_tensor_type_str = self.get_tensor_type_str(bias_tensor_type)
bias_expr = self.exp_tab.new_const(
self.get_tensor_value(bias_tensor), dtype=bias_tensor_type_str
)
if self.has_expr(bias_tensor.tensor_idx):
bias_expr = self.get_expr(bias_tensor.tensor_idx)
else:
bias_expr = self.exp_tab.new_const(
self.get_tensor_value(bias_tensor), dtype=bias_tensor_type_str
)
channel_axis = 3
out = _op.nn.bias_add(out, bias_expr, axis=channel_axis)

Expand Down
Loading

0 comments on commit 6161899

Please sign in to comment.