This is an automated email from the ASF dual-hosted git repository.
tlopex pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/main by this push:
new c84f80f9ee [Relax][TFLite] Flatten FC input without keep_num_dims
(#20784)
c84f80f9ee is described below
commit c84f80f9eead390f25e741cebab8eaa1b3233989
Author: Guan-Ming Chiu <[email protected]>
AuthorDate: Wed Nov 18 21:45:30 2026 +0000
[Relax][TFLite] Flatten FC input without keep_num_dims (#20784)
## Why
- TFLite `FULLY_CONNECTED` with `keep_num_dims=false` returns a 2-D
output, but the Relax importer kept the input rank
- A 3-D input therefore produced a 3-D result that did not match TFLite
## How
- Flatten inputs with rank above 2 to `[-1, n_inputs]` when
`keep_num_dims` is false
- Remove the resolved TODO about the `keep_num_dims` output shape
- Add `test_fully_connected_flattens_input_without_keep_num_dims`, a
structural IR test with a 3-D input
---
.../tvm/relax/frontend/tflite/tflite_frontend.py | 3 +-
tests/python/relax/test_frontend_tflite.py | 54 ++++++++++++++++++++++
2 files changed, 56 insertions(+), 1 deletion(-)
diff --git a/python/tvm/relax/frontend/tflite/tflite_frontend.py
b/python/tvm/relax/frontend/tflite/tflite_frontend.py
index b6cc6845d0..3468c52144 100644
--- a/python/tvm/relax/frontend/tflite/tflite_frontend.py
+++ b/python/tvm/relax/frontend/tflite/tflite_frontend.py
@@ -4398,13 +4398,14 @@ class OperatorConverter:
# Weight require a transpose
in_expr = self.get_tensor_expr(input_tensor)
- # TODO: Change the output shape calculation based on keep_dim option
assert op.BuiltinOptionsType() == BuiltinOptions.FullyConnectedOptions
op_options = op.BuiltinOptions()
fully_connected_options = FullyConnectedOptions()
fully_connected_options.Init(op_options.Bytes, op_options.Pos)
fused_activation_fn = fully_connected_options.FusedActivationFunction()
keep_num_dims = fully_connected_options.KeepNumDims()
+ if not keep_num_dims and len(self.get_tensor_shape(input_tensor)) > 2:
+ in_expr = relax.op.reshape(in_expr, [-1, weight_tensor_shape[1]])
# weight tensor type should be INT8/UINT8 (quantization) or FLOAT32
weight_tensor_type = weight_tensor.tensor.Type()
diff --git a/tests/python/relax/test_frontend_tflite.py
b/tests/python/relax/test_frontend_tflite.py
index 4799c3481c..3cad669de8 100644
--- a/tests/python/relax/test_frontend_tflite.py
+++ b/tests/python/relax/test_frontend_tflite.py
@@ -12789,6 +12789,60 @@ def
test_quantized_fully_connected_with_int32_bias_dequantizes_bias():
tvm_tirx.ir.assert_structural_equal(mod, Expected)
+def test_fully_connected_flattens_input_without_keep_num_dims():
+ builder = flatbuffers.Builder(1024)
+
+ t_in = _build_tensor(builder, 0, [2, 3, 4])
+ t_wt = _build_tensor(builder, 0, [5, 4])
+ t_ou = _build_tensor(builder, 0, [6, 5])
+
+ _tfl_fully_connected_options.FullyConnectedOptionsStart(builder)
+ _tfl_fully_connected_options.FullyConnectedOptionsAddKeepNumDims(builder,
False)
+ fc_opts = _tfl_fully_connected_options.FullyConnectedOptionsEnd(builder)
+
+ fc_op = _build_operator(
+ builder,
+ 0,
+ [0, 1],
+ [2],
+ builtin_options_type=_tfl_builtin_options.FullyConnectedOptions,
+ builtin_options=fc_opts,
+ )
+ subgraph = _build_subgraph(
+ builder,
+ tensors=[t_in, t_wt, t_ou],
+ operators=[fc_op],
+ inputs=[0, 1],
+ outputs=[2],
+ )
+ buf = _finish_tflite_model(
+ builder,
+ subgraph=subgraph,
+ operator_codes=[_build_operator_code(builder,
_tfl_builtin_operator.FULLY_CONNECTED)],
+ buffers=[_build_buffer(builder)],
+ )
+ mod = _load_model_from_buffer(buf)
+
+ @I.ir_module
+ class Expected:
+ @R.function
+ def main(
+ tvmgen_tensor_0: R.Tensor((2, 3, 4), dtype="float32"),
+ tvmgen_tensor_1: R.Tensor((5, 4), dtype="float32"),
+ ) -> R.Tensor((6, 5), dtype="float32"):
+ R.func_attr({"num_input": 2})
+ with R.dataflow():
+ lv: R.Tensor((6, 4), dtype="float32") =
R.reshape(tvmgen_tensor_0, R.shape([6, 4]))
+ lv1: R.Tensor((4, 5), dtype="float32") = R.permute_dims(
+ tvmgen_tensor_1, axes=[1, 0]
+ )
+ gv: R.Tensor((6, 5), dtype="float32") = R.matmul(lv, lv1)
+ R.output(gv)
+ return gv
+
+ tvm_tirx.ir.assert_structural_equal(mod, Expected)
+
+
def _build_csr_sparsity(
builder,
*,