You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "Aharrypotter (via GitHub)" <gi...@apache.org> on 2026/06/17 10:40:39 UTC
[PR] [Tests][TFLite] Add SQUEEZE, REVERSE_SEQUENCE, UNPACK, and ZEROS_LIKE coverage [tvm]
Aharrypotter opened a new pull request, #19814:
URL: https://github.com/apache/tvm/pull/19814
## Summary
Adds Relax TFLite frontend coverage for the remaining unchecked
non-quantized operator tests tracked in #18971:
- `SQUEEZE`
- `REVERSE_SEQUENCE`
- `UNPACK`
- `ZEROS_LIKE`
The tests use small hand-built TFLite flatbuffers and compare the generated
Relax IR with explicit Expected IRModules. This avoids TensorFlow Lite
converter optimizations that rewrite `SQUEEZE` to `RESHAPE` or fold
`ZEROS_LIKE`, and keeps the operator-focused tests tied directly to the target
TFLite builtin operators.
## Changes
- Add minimal TFLite flatbuffer builders for:
- `SQUEEZE` with `SqueezeOptions`
- `REVERSE_SEQUENCE` with `ReverseSequenceOptions`
- `UNPACK` with `UnpackOptions`
- `ZEROS_LIKE` with `ZerosLikeOptions`
- Add structural Relax IR tests for the four operators in
`tests/python/relax/test_frontend_tflite.py`.
- Fix `REVERSE_SEQUENCE` lowering to use the existing
`topi.reverse_sequence` DPS packed path instead of the nonexistent
`relax.op.reverse_sequence` Python op.
- Guard `REVERSE_SEQUENCE` with non-zero `batch_dim` as unsupported, matching
the currently available TOPI packed wrapper.
- Fix `UNPACK` lowering to build tuple outputs using explicit
`TupleGetItem(split_result, i)` access, avoiding Python iteration over a
Relax expression.
## Tests
Local validation:
```bash
conda run -n tvm-dev bash -lc 'export PYTHONPATH="$(pwd)/python:$(pwd)/.local/python"; python -m ruff format python/tvm/relax/frontend/tflite/tflite_frontend.py tests/python/relax/test_frontend_tflite.py'
conda run -n tvm-dev bash -lc 'export PYTHONPATH="$(pwd)/python:$(pwd)/.local/python"; python -m ruff check python/tvm/relax/frontend/tflite/tflite_frontend.py tests/python/relax/test_frontend_tflite.py'
conda run -n tvm-dev bash -lc 'export PYTHONPATH="$(pwd)/python:$(pwd)/.local/python"; python -m pytest tests/python/relax/test_frontend_tflite.py -k "squeeze or reverse_sequence or unpack or zeros_like" -q'
conda run -n tvm-dev bash -lc 'export PYTHONPATH="$(pwd)/python:$(pwd)/.local/python"; python -m pytest tests/python/relax/test_frontend_tflite.py -q'
```
Result:
```text
ruff format: 1 file reformatted, 1 file left unchanged
ruff check: All checks passed
target pytest: 4 passed, 523 deselected
full TFLite pytest: 527 passed
```
## References
- Tracking issue #18971: TFLite non-quantized operator unit-test coverage
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Relax][TFLite] Add remaining operator tests and reverse_sequence op [tvm]
Posted by "Aharrypotter (via GitHub)" <gi...@apache.org>.
Aharrypotter commented on PR #19814:
URL: https://github.com/apache/tvm/pull/19814#issuecomment-4764397763
> I think `REVERSE_SEQUENCE` needs one more change before merge.
>
> The new lowering emits `R.call_dps_packed("topi.reverse_sequence", ...)` at `python/tvm/relax/frontend/tflite/tflite_frontend.py:5023`. However, the registered `topi.reverse_sequence` packed function in `src/topi/transform.cc` is a TOPI/TE compute wrapper that casts the first two arguments to `te::Tensor`. `call_dps_packed` invokes the symbol at runtime with runtime tensors, so the imported model fails when compiled and executed.
>
> I reproduced this with the new hand-built `REVERSE_SEQUENCE` model: the structural test passes, but running the compiled module fails with:
>
> ```
> TypeError: Cannot convert from type ``ffi.Tensor`` to ``te.Tensor``
> ```
>
> Could we either lower this through a real TIR path, e.g. `BlockBuilder.emit_te(topi.reverse_sequence, ...)`, or provide a proper runtime DPS packed implementation? It would also be good to add a runtime regression test for `REVERSE_SEQUENCE`, since the current test only checks the generated IR shape.
Good catch, thanks @tlopex. Instead of patching the packed path, I registered `REVERSE_SEQUENCE` as a proper Relax op (`relax.reverse_sequence`) that legalizes through `bb.call_te(topi.reverse_sequence, ...)`, so it lowers to real TIR at compile time and the `ffi.Tensor → te.Tensor` issue is gone. This also let me drop the old `batch_dim != 0` restriction, since the op now carries both `seq_axis` and `batch_axis`.
I also added a runtime test: `test_reverse_sequence` now compiles and runs the module on the VM and checks the output against NumPy, plus unit tests for the op and its legalization. PTAL.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Relax][TFLite] Add remaining operator tests and reverse_sequence op [tvm]
Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex merged PR #19814:
URL: https://github.com/apache/tvm/pull/19814
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Tests][TFLite] Add SQUEEZE, REVERSE_SEQUENCE, UNPACK, and ZEROS_LIKE coverage [tvm]
Posted by "Aharrypotter (via GitHub)" <gi...@apache.org>.
Aharrypotter commented on code in PR #19814:
URL: https://github.com/apache/tvm/pull/19814#discussion_r3427549698
##########
python/tvm/relax/frontend/tflite/tflite_frontend.py:
##########
@@ -5009,7 +5009,22 @@ def convert_reverse_sequence(self, op):
batch_axis = options.BatchDim()
seq_axis = options.SeqDim()
- return relax.op.reverse_sequence(in_expr, length_expr, seq_axis, batch_axis)
+ if batch_axis != 0:
+ raise tvm.error.OpNotImplemented(
+ "TFLite REVERSE_SEQUENCE with non-zero batch_dim is not supported yet."
+ )
+
+ output_tensors = self.get_output_tensors(op)
+ assert len(output_tensors) == 1, "output tensors length should be 1"
+ output_tensor = output_tensors[0]
+ output_shape = to_int_list(self.get_tensor_shape(output_tensor))
+ output_dtype = self.get_tensor_type_str(output_tensor.tensor.Type())
+
+ return relax.op.call_dps_packed(
+ "topi.reverse_sequence",
+ (in_expr, length_expr, seq_axis),
+ out_sinfo=relax.TensorStructInfo(output_shape, output_dtype),
+ )
Review Comment:
Thanks for the careful check. I looked at the current packed registration in
`src/topi/transform.cc`, and `topi.reverse_sequence` is registered as a
3-argument packed function:
```cpp
.def_packed("topi.reverse_sequence",
[](ffi::PackedArgs args, ffi::Any* rv) {
*rv = reverse_sequence(args[0].cast<te::Tensor>(),
args[1].cast<te::Tensor>(),
args[2].cast<int>());
})
```
So passing `(data, seq_lengths, seq_axis, batch_axis)` from Relax would not match
the current packed API. The frontend already rejects `batch_dim != 0` before the
call, and the 3-argument packed wrapper therefore intentionally uses TOPI's
default `batch_axis=0`.
I think the current 3-argument call and the matching Expected IR are the safer
form for this PR. If we want to support non-zero `batch_dim` later, the right
follow-up would be to first extend the `topi.reverse_sequence` packed
registration to consume `args[3]`, and then update the TFLite frontend and test
expectation to pass `batch_axis` explicitly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Tests][TFLite] Add SQUEEZE, REVERSE_SEQUENCE, UNPACK, and ZEROS_LIKE coverage [tvm]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #19814:
URL: https://github.com/apache/tvm/pull/19814#discussion_r3427469922
##########
python/tvm/relax/frontend/tflite/tflite_frontend.py:
##########
@@ -5009,7 +5009,22 @@ def convert_reverse_sequence(self, op):
batch_axis = options.BatchDim()
seq_axis = options.SeqDim()
- return relax.op.reverse_sequence(in_expr, length_expr, seq_axis, batch_axis)
+ if batch_axis != 0:
+ raise tvm.error.OpNotImplemented(
+ "TFLite REVERSE_SEQUENCE with non-zero batch_dim is not supported yet."
+ )
+
+ output_tensors = self.get_output_tensors(op)
+ assert len(output_tensors) == 1, "output tensors length should be 1"
+ output_tensor = output_tensors[0]
+ output_shape = to_int_list(self.get_tensor_shape(output_tensor))
+ output_dtype = self.get_tensor_type_str(output_tensor.tensor.Type())
+
+ return relax.op.call_dps_packed(
+ "topi.reverse_sequence",
+ (in_expr, length_expr, seq_axis),
+ out_sinfo=relax.TensorStructInfo(output_shape, output_dtype),
+ )
Review Comment:

The `topi.reverse_sequence` operator expects 4 arguments: `(data, seq_lengths, seq_axis, batch_axis)`. Omitting `batch_axis` can lead to runtime errors (such as arity mismatch) when calling the packed function, especially in non-Python runtimes or environments where default arguments are not automatically resolved. Since `batch_axis` is validated to be `0`, we should explicitly pass it as the fourth argument.
```suggestion
return relax.op.call_dps_packed(
"topi.reverse_sequence",
(in_expr, length_expr, seq_axis, batch_axis),
out_sinfo=relax.TensorStructInfo(output_shape, output_dtype),
)
```
##########
tests/python/relax/test_frontend_tflite.py:
##########
@@ -1456,6 +1456,29 @@ def main(x: R.Tensor((2, 3), dtype="float32")) -> R.Tensor((2, 3), dtype="float3
verify(ReverseV2, Expected)
+def test_reverse_sequence():
+ mod = _load_model_from_buffer(_build_tflite_reverse_sequence_model())
+
+ @I.ir_module
+ class Expected:
+ @R.function
+ def main(
+ tvmgen_tensor_0: R.Tensor((2, 4, 3), dtype="float32"),
+ tvmgen_tensor_1: R.Tensor((2,), dtype="int32"),
+ ) -> R.Tensor((2, 4, 3), dtype="float32"):
+ R.func_attr({"num_input": 2})
+ with R.dataflow():
+ gv: R.Tensor((2, 4, 3), dtype="float32") = R.call_dps_packed(
+ "topi.reverse_sequence",
+ (tvmgen_tensor_0, tvmgen_tensor_1, 1),
+ out_sinfo=R.Tensor((2, 4, 3), dtype="float32"),
+ )
Review Comment:

Update the expected IR module to match the explicit passing of `batch_axis` (which is `0`) to `topi.reverse_sequence`.
```suggestion
gv: R.Tensor((2, 4, 3), dtype="float32") = R.call_dps_packed(
"topi.reverse_sequence",
(tvmgen_tensor_0, tvmgen_tensor_1, 1, 0),
out_sinfo=R.Tensor((2, 4, 3), dtype="float32"),
)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Relax][TFLite] Add remaining operator tests and reverse_sequence op [tvm]
Posted by "Aharrypotter (via GitHub)" <gi...@apache.org>.
Aharrypotter commented on PR #19814:
URL: https://github.com/apache/tvm/pull/19814#issuecomment-4785274672
Just to record the rationale for `relax.reverse_sequence` for future reference:
Making it a first-class op (instead of emitting `emit_te`/`call_tir` straight from the frontend) keeps the design consistent with how the other manipulate ops work. It mirrors `relax.flip` almost exactly — a thin op that infers StructInfo and legalizes to its TOPI compute via `bb.call_te(topi.reverse_sequence, ...)`. The benefits:
- The op-level semantics survive until legalization, so passes (pattern matching, fusion, structural checks) can still reason about it instead of seeing an opaque PrimFunc.
- Any other frontend that needs it (e.g. ONNX `ReverseSequence`) can reuse the same op instead of hand-rolling another lowering.
- The frontend test can assert on `R.reverse_sequence` directly, which is how we verify the lowering stays clean.
So this isn't a one-off wrapper — it follows the established `flip`/`gather_elements`/`squeeze`/... pattern where the Relax op owns the semantics + StructInfo and TOPI owns the compute.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Tests][TFLite] Add SQUEEZE, REVERSE_SEQUENCE, UNPACK, and ZEROS_LIKE coverage [tvm]
Posted by "Aharrypotter (via GitHub)" <gi...@apache.org>.
Aharrypotter commented on code in PR #19814:
URL: https://github.com/apache/tvm/pull/19814#discussion_r3427536769
##########
python/tvm/relax/frontend/tflite/tflite_frontend.py:
##########
@@ -5009,7 +5009,22 @@ def convert_reverse_sequence(self, op):
batch_axis = options.BatchDim()
seq_axis = options.SeqDim()
- return relax.op.reverse_sequence(in_expr, length_expr, seq_axis, batch_axis)
+ if batch_axis != 0:
+ raise tvm.error.OpNotImplemented(
+ "TFLite REVERSE_SEQUENCE with non-zero batch_dim is not supported yet."
+ )
+
+ output_tensors = self.get_output_tensors(op)
+ assert len(output_tensors) == 1, "output tensors length should be 1"
+ output_tensor = output_tensors[0]
+ output_shape = to_int_list(self.get_tensor_shape(output_tensor))
+ output_dtype = self.get_tensor_type_str(output_tensor.tensor.Type())
+
+ return relax.op.call_dps_packed(
+ "topi.reverse_sequence",
+ (in_expr, length_expr, seq_axis),
+ out_sinfo=relax.TensorStructInfo(output_shape, output_dtype),
+ )
Review Comment:
# Reply draft for PR #19814 Gemini review
Thanks for the careful check. I looked at the current packed registration in
`src/topi/transform.cc`, and `topi.reverse_sequence` is registered as a
3-argument packed function:
```cpp
.def_packed("topi.reverse_sequence",
[](ffi::PackedArgs args, ffi::Any* rv) {
*rv = reverse_sequence(args[0].cast<te::Tensor>(),
args[1].cast<te::Tensor>(),
args[2].cast<int>());
})
```
So passing `(data, seq_lengths, seq_axis, batch_axis)` from Relax would not match
the current packed API. The frontend already rejects `batch_dim != 0` before the
call, and the 3-argument packed wrapper therefore intentionally uses TOPI's
default `batch_axis=0`.
I think the current 3-argument call and the matching Expected IR are the safer
form for this PR. If we want to support non-zero `batch_dim` later, the right
follow-up would be to first extend the `topi.reverse_sequence` packed
registration to consume `args[3]`, and then update the TFLite frontend and test
expectation to pass `batch_axis` explicitly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Tests][TFLite] Add SQUEEZE, REVERSE_SEQUENCE, UNPACK, and ZEROS_LIKE coverage [tvm]
Posted by "Aharrypotter (via GitHub)" <gi...@apache.org>.
Aharrypotter commented on code in PR #19814:
URL: https://github.com/apache/tvm/pull/19814#discussion_r3427536769
##########
python/tvm/relax/frontend/tflite/tflite_frontend.py:
##########
@@ -5009,7 +5009,22 @@ def convert_reverse_sequence(self, op):
batch_axis = options.BatchDim()
seq_axis = options.SeqDim()
- return relax.op.reverse_sequence(in_expr, length_expr, seq_axis, batch_axis)
+ if batch_axis != 0:
+ raise tvm.error.OpNotImplemented(
+ "TFLite REVERSE_SEQUENCE with non-zero batch_dim is not supported yet."
+ )
+
+ output_tensors = self.get_output_tensors(op)
+ assert len(output_tensors) == 1, "output tensors length should be 1"
+ output_tensor = output_tensors[0]
+ output_shape = to_int_list(self.get_tensor_shape(output_tensor))
+ output_dtype = self.get_tensor_type_str(output_tensor.tensor.Type())
+
+ return relax.op.call_dps_packed(
+ "topi.reverse_sequence",
+ (in_expr, length_expr, seq_axis),
+ out_sinfo=relax.TensorStructInfo(output_shape, output_dtype),
+ )
Review Comment:
# Reply draft for PR #19814 Gemini review
Thanks for the careful check. I looked at the current packed registration in
`src/topi/transform.cc`, and `topi.reverse_sequence` is registered as a
3-argument packed function:
```cpp
.def_packed("topi.reverse_sequence",
[](ffi::PackedArgs args, ffi::Any* rv) {
*rv = reverse_sequence(args[0].cast<te::Tensor>(),
args[1].cast<te::Tensor>(),
args[2].cast<int>());
})
```
So passing `(data, seq_lengths, seq_axis, batch_axis)` from Relax would not match
the current packed API. The frontend already rejects `batch_dim != 0` before the
call, and the 3-argument packed wrapper therefore intentionally uses TOPI's
default `batch_axis=0`.
I think the current 3-argument call and the matching Expected IR are the safer
form for this PR. If we want to support non-zero `batch_dim` later, the right
follow-up would be to first extend the `topi.reverse_sequence` packed
registration to consume `args[3]`, and then update the TFLite frontend and test
expectation to pass `batch_axis` explicitly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Tests][TFLite] Add SQUEEZE, REVERSE_SEQUENCE, UNPACK, and ZEROS_LIKE coverage [tvm]
Posted by "Aharrypotter (via GitHub)" <gi...@apache.org>.
Aharrypotter commented on code in PR #19814:
URL: https://github.com/apache/tvm/pull/19814#discussion_r3432683927
##########
tests/python/relax/test_frontend_tflite.py:
##########
@@ -1456,6 +1456,29 @@ def main(x: R.Tensor((2, 3), dtype="float32")) -> R.Tensor((2, 3), dtype="float3
verify(ReverseV2, Expected)
+def test_reverse_sequence():
+ mod = _load_model_from_buffer(_build_tflite_reverse_sequence_model())
+
+ @I.ir_module
+ class Expected:
+ @R.function
+ def main(
+ tvmgen_tensor_0: R.Tensor((2, 4, 3), dtype="float32"),
+ tvmgen_tensor_1: R.Tensor((2,), dtype="int32"),
+ ) -> R.Tensor((2, 4, 3), dtype="float32"):
+ R.func_attr({"num_input": 2})
+ with R.dataflow():
+ gv: R.Tensor((2, 4, 3), dtype="float32") = R.call_dps_packed(
+ "topi.reverse_sequence",
+ (tvmgen_tensor_0, tvmgen_tensor_1, 1),
+ out_sinfo=R.Tensor((2, 4, 3), dtype="float32"),
+ )
Review Comment:
Same as above. The current `topi.reverse_sequence` packed registration only consumes three arguments, and this PR already rejects non-zero `batch_dim`, so the Expected IR intentionally matches the current 3-argument packed API.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org