[Fix][Relax][ONNX] Preserve rank-expanding Expand#19992
Conversation
Signed-off-by: viiccwen <vicwen@apache.org>
|
cc @guan404ming, @tlopex. ✨🙌 |
There was a problem hiding this comment.
Code Review
This pull request updates the ONNX frontend in TVM Relax to correctly handle the Expand operation when expanding a tensor to a higher rank with a same padded shape (for example, from shape [1] to [1, 1]). It ensures that the rank of the target shape matches the rank of the input data shape before skipping the broadcast_to operator. Additionally, a corresponding test case has been added to verify this behavior. There are no review comments, so I have no further feedback to provide.
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.
What changed
Record the original input rank before
Expandleft-pads the input shape for broadcast validation. The no-op fast path now returns the input unchanged only when both the padded shape and the original rank match the target.A regression test covers expanding
[1]to[1, 1]when the target is represented as a RelaxShapeExpr.Why
ONNX
Expandright-aligns dimensions and may increase tensor rank by adding leading dimensions. Previously, a rank-expanding broadcast could look like a no-op after the frontend padded the input shape, causing it to return the original lower-rank tensor. Downstream operators could then receive inconsistent ranks.This fixes the focused bug tracked in #19991 and is part of the investigation and fixes for #19971. It does not close #19971 because the attached model exposes additional independent importer issues after this
Concatfailure is resolved.Fixes #19991
Part of #19971
Validation
python -m pytest tests/python/relax/test_frontend_onnx.py::test_expand -qConcat expects all input tensors to have same ndim, while this change advances beyond thatConcat.