[Relax][Frontend][TFLite] Support StableHLO shape ops#19869
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds support for converting the STABLEHLO_RESHAPE operator to Relax in the TFLite frontend, including corresponding unit tests. The feedback suggests replacing the assert statements in the new conversion function with explicit ValueError checks to ensure validation is not bypassed when Python is run with optimization flags.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| input_tensors = self.get_input_tensors(op) | ||
| assert len(input_tensors) == 1 | ||
| output_tensors = self.get_output_tensors(op) | ||
| assert len(output_tensors) == 1 |
There was a problem hiding this comment.
Using assert statements for input validation is discouraged because they can be optimized away when Python is run with the -O (optimize) flag. This would bypass the validation checks entirely and could lead to cryptic errors (like IndexError) later in the execution. It is safer and more robust to explicitly check the conditions and raise a ValueError with a descriptive error message.
| input_tensors = self.get_input_tensors(op) | |
| assert len(input_tensors) == 1 | |
| output_tensors = self.get_output_tensors(op) | |
| assert len(output_tensors) == 1 | |
| input_tensors = self.get_input_tensors(op) | |
| if len(input_tensors) != 1: | |
| raise ValueError(f"STABLEHLO_RESHAPE expects exactly 1 input tensor, but got {len(input_tensors)}") | |
| output_tensors = self.get_output_tensors(op) | |
| if len(output_tensors) != 1: | |
| raise ValueError(f"STABLEHLO_RESHAPE expects exactly 1 output tensor, but got {len(output_tensors)}") |
Summary
Adds Relax TFLite frontend support for the remaining StableHLO shape operators:
STABLEHLO_RESHAPE->R.reshapeSTABLEHLO_SLICE->R.strided_sliceSTABLEHLO_TRANSPOSE->R.permute_dimsRelated to #19519.
Testing
python -m ruff check python/tvm/relax/frontend/tflite/tflite_frontend.py tests/python/relax/test_frontend_tflite.pypython -m py_compile python/tvm/relax/frontend/tflite/tflite_frontend.py tests/python/relax/test_frontend_tflite.pygit diff --checkRelated to #19519.