Skip to content

[Relax][Frontend][TFLite] Fix and test MATRIX_DIAG, MATRIX_SET_DIAG, SPARSE_TO_DENSE#19408

Merged
MasterJH5574 merged 3 commits into
apache:mainfrom
Aharrypotter:tflite-op-tests-18971
Apr 16, 2026
Merged

[Relax][Frontend][TFLite] Fix and test MATRIX_DIAG, MATRIX_SET_DIAG, SPARSE_TO_DENSE#19408
MasterJH5574 merged 3 commits into
apache:mainfrom
Aharrypotter:tflite-op-tests-18971

Conversation

@Aharrypotter

Copy link
Copy Markdown
Contributor

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, and SPARSE_TO_DENSE were broken due to calling non-existent Relax ops:

  • relax.op.matrix_set_diag - never registered in Relax
  • relax.op.sparse_to_dense - never registered in Relax

These ops only exist as TOPI packed functions (topi.matrix_set_diag, topi.sparse_to_dense).

Fix: Replace direct op calls with call_dps_packed to 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_diag
  • test_matrix_set_diag
  • test_sparse_to_dense

Validation

Ran:

pytest tests/python/relax/test_frontend_tflite.py \
  -k 'test_matrix_diag or test_matrix_set_diag or test_sparse_to_dense' -v

Result:

tests/python/relax/test_frontend_tflite.py::test_matrix_diag PASSED
tests/python/relax/test_frontend_tflite.py::test_matrix_set_diag PASSED
tests/python/relax/test_frontend_tflite.py::test_sparse_to_dense PASSED
============================== 3 passed ==============================

Refs: #18971

…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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tests/python/relax/test_frontend_tflite.py
The assertion was accidentally removed during rebase conflict resolution.
Restore it to properly verify the generated IR structure.
@Aharrypotter

Copy link
Copy Markdown
Contributor Author

Please take a look @tlopex

@tlopex tlopex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MasterJH5574 MasterJH5574 merged commit 75a6b30 into apache:main Apr 16, 2026
8 checks passed
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants