Skip to content

[Relax][Frontend][TFLite] Add segment operator mappings#19491

Open
Aharrypotter wants to merge 1 commit intoapache:mainfrom
Aharrypotter:tflite-scatter-segment-19412
Open

[Relax][Frontend][TFLite] Add segment operator mappings#19491
Aharrypotter wants to merge 1 commit intoapache:mainfrom
Aharrypotter:tflite-scatter-segment-19412

Conversation

@Aharrypotter
Copy link
Copy Markdown
Contributor

Summary

This PR adds Relax TFLite frontend support for the following segment operators from #19412:

  • SEGMENT_SUM
  • UNSORTED_SEGMENT_MIN
  • UNSORTED_SEGMENT_PROD

These operators are lowered through relax.op.scatter_nd with the corresponding reduction modes.

Changes

TFLite Frontend

  1. Add TFLite converter mappings for segment operators:

    • SEGMENT_SUM -> scatter_nd(..., reduction="add")
    • UNSORTED_SEGMENT_MIN -> scatter_nd(..., reduction="min")
    • UNSORTED_SEGMENT_PROD -> scatter_nd(..., reduction="mul")
  2. Add shared segment lowering logic:

    • Convert segment_ids into scatter indices via expand_dims.
    • Build the output shape from num_segments or constant segment_ids.
    • Initialize the scatter base tensor with the correct reduction identity.

Tests

Add TFLite frontend tests for:

  • test_segment_sum
  • test_unsorted_segment_min
  • test_unsorted_segment_prod

Each test verifies the imported Relax IR lowers to R.scatter_nd with the expected reduction mode and base tensor initialization.

Testing

All targeted tests pass:

python -m pytest  \
  tests/python/relax/test_frontend_tflite.py::test_scatter_nd \
  tests/python/relax/test_frontend_tflite.py::test_segment_sum \
  tests/python/relax/test_frontend_tflite.py::test_unsorted_segment_min \
  tests/python/relax/test_frontend_tflite.py::test_unsorted_segment_prod \
  -q

References

Copy link
Copy Markdown
Contributor

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

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 adds support for SEGMENT_SUM, UNSORTED_SEGMENT_MIN, and UNSORTED_SEGMENT_PROD operators in the TFLite frontend by lowering them to relax.op.scatter_nd. The implementation includes helper functions for identity base tensor creation and segment count calculation, along with corresponding unit tests. A syntax error was identified in the SEGMENT_SUM error handling logic where a closing parenthesis is incorrectly placed, which would lead to a failure during execution.

Comment thread python/tvm/relax/frontend/tflite/tflite_frontend.py
Copy link
Copy Markdown
Member

@tlopex tlopex left a comment

Choose a reason for hiding this comment

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

Could you resolve the conflict so that we can merge it in?

@Aharrypotter
Copy link
Copy Markdown
Contributor Author

I'll resolve the conflicts and update the PR shortly.

@Aharrypotter Aharrypotter force-pushed the tflite-scatter-segment-19412 branch from cd79abd to 032b0db Compare May 2, 2026 16:04
@Aharrypotter
Copy link
Copy Markdown
Contributor Author

Please take a look @tlopex. Fixed alphabetical ordering of operator mappings.

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.

2 participants