[Relax][Frontend][TFLite] Fix and test MATRIX_DIAG, MATRIX_SET_DIAG, SPARSE_TO_DENSE#19408
Merged
Merged
Conversation
…SPARSE_TO_DENSE The original converters for MATRIX_DIAG, MATRIX_SET_DIAG, and SPARSE_TO_DENSE called non-existent relax ops (matrix_set_diag, sparse_to_dense). These ops were never registered in Relax, only as TOPI packed functions. Fix by using call_dps_packed to invoke the corresponding TOPI functions directly. Add unit tests for these operators.
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the TFLite frontend to use call_dps_packed for sparse_to_dense, matrix_set_diag, and matrix_diag operations, mapping them to TOPI functions. It also includes new test cases for these operators. Feedback indicates that a structural equality assertion was accidentally removed from an existing test case, which needs to be restored or refactored to maintain test coverage.
The assertion was accidentally removed during rebase conflict resolution. Restore it to properly verify the generated IR structure.
Contributor
Author
|
Please take a look @tlopex |
tlopex
pushed a commit
that referenced
this pull request
Apr 29, 2026
…d handling (#19421) ## Summary This PR adds test coverage for the TFLite DENSIFY operator as requested in issue #18971 , and fixes several related bugs in the TFLite frontend. DENSIFY converts sparse weight tensors to dense format at **conversion time** (not runtime). The dense weights become constants in the output IR via the `prefetched_nodes` mechanism. ## Changes ### Bug Fixes 1. **`convert_op_to_relax`**: Check `ret is None` before `normalize(ret)` to avoid crash when DENSIFY returns `None`. 2. **`get_tensor_expr`**: Add `is_prefetched()` check before `get_tensor_value()` to handle DENSIFY outputs with empty buffers. 3. **`convert_fully_connected`**: Add prefetched weight handling 4. **`convert_transpose_conv`**: Add prefetched weight handling ### Tests Four test cases covering different DENSIFY usage scenarios: | Test | Downstream Op | Purpose | |------|---------------|---------| | `test_densify` | None | Basic DENSIFY to constant | | `test_densify_with_add` | ADD | Prefetched as regular input | | `test_densify_with_conv2d` | CONV2D | Network-level test (2D conv) | | `test_densify_with_fully_connected` | FULLY_CONNECTED | Network-level test (FC layer) | **Note**: Sparse TFLite models are built manually using flatbuffers API (TensorFlow does not provide an API for creating sparse models). ## Testing All tests pass: ```bash pytest tests/python/relax/test_frontend_tflite.py::test_densify \ tests/python/relax/test_frontend_tflite.py::test_densify_with_add \ tests/python/relax/test_frontend_tflite.py::test_densify_with_conv2d \ tests/python/relax/test_frontend_tflite.py::test_densify_with_fully_connected -v ``` - `test_densify` PASSED - `test_densify_with_add` PASSED - `test_densify_with_conv2d` PASSED - `test_densify_with_fully_connected` PASSED ## References Issue #18971 : TFLite operator test coverage tracking Related: #19408 (MATRIX_DIAG, MATRIX_SET_DIAG, SPARSE_TO_DENSE tests)
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR partially implements test coverage requested in issue #18971 for Relax TFLite frontend operator tests.
Bug Fix
The TFLite frontend converters for
MATRIX_DIAG,MATRIX_SET_DIAG, andSPARSE_TO_DENSEwere broken due to calling non-existent Relax ops:relax.op.matrix_set_diag- never registered in Relaxrelax.op.sparse_to_dense- never registered in RelaxThese ops only exist as TOPI packed functions (
topi.matrix_set_diag,topi.sparse_to_dense).Fix: Replace direct op calls with
call_dps_packedto invoke the TOPI packed functions:convert_matrix_diag: zeros + call_dps_packed("topi.matrix_set_diag", ...)convert_matrix_set_diag: call_dps_packed("topi.matrix_set_diag", ...)convert_sparse_to_dense: call_dps_packed("topi.sparse_to_dense", ...)Added Tests
Added explicit tests in
tests/python/relax/test_frontend_tflite.py:test_matrix_diagtest_matrix_set_diagtest_sparse_to_denseValidation
Ran:
pytest tests/python/relax/test_frontend_tflite.py \ -k 'test_matrix_diag or test_matrix_set_diag or test_sparse_to_dense' -vResult:
Refs: #18971