Skip to content

[Fix][Relax][ONNX] Expand symbolic input dimensions - #21098

Open
Nanmur wants to merge 3 commits into
apache:mainfrom
Nanmur:codex/fix-onnx-expand-symbolic-input
Open

Nanmur wants to merge 3 commits into
apache:mainfrom
Nanmur:codex/fix-onnx-expand-symbolic-input

Conversation

@Nanmur

@Nanmur Nanmur commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #20873.

The constant-target Expand path reads every input extent through .value, which fails for symbolic extents. Preserve non-IntImm expressions, keep existing static dimension checks, and retain a symbolic input extent when the corresponding requested dimension is 1.

Import-only changes are insufficient: an unknown input extent can be 1 at runtime. The existing TOPI broadcast indices can then incorrectly read successive input rows/columns instead of index 0. Add a symbolic-input fallback to the Python broadcast_to wrapper that selects index 0 for runtime singleton dimensions. The existing C++ shape construction/checks are still invoked, and fully static inputs keep the original fast path. C++-only callers are unchanged.

This does not add a general runtime validator for arbitrary incompatible dynamic inputs, nor change the separate runtime-target Expand shape-merging path or singleton-to-zero static validation.

Regression tests

23 LLVM/Relax VM tests compare against ONNX Runtime and NumPy:

  • Symbolic and concrete shape controls.
  • Runtime extent 1 and non-singleton extents.
  • Singleton target dimensions that must preserve the input extent.
  • Shorter/longer target ranks and an empty target shape.
  • Multiple symbolic dimensions and an empty input.
  • Static incompatible shapes must still raise ValueError.

Initial tests before the frontend fix: 7 failed, 8 passed. A frontend-only change exposed incorrect runtime-singleton results, so the TOPI adjustment is necessary. Expanded final tests: 23 passed.
Ruff 0.12.3 lint/format and git diff --check passed.

Validation scope

Local validation loaded the main-based Expand class and complete TOPI broadcast module verbatim into an existing WSL Ubuntu 22.04 TVM 0.26 importer/runtime (Python 3.10). Compatibility mappings were needed for the tvm_tirx import name, GenericConst/IntImm type names, and constant.value versus constant.data. The old legalizer also captures its TE function at import time, so the harness rebound its broadcast_to legalization callback to the new function. These are local harness adaptations, not source rewrites or PR files.

Models were compiled for LLVM and executed. This is not a full latest-main build, and the complete existing TOPI suite has not been run.

Main-environment command:

python -m pytest tests/python/relax/test_frontend_onnx_expand_symbolic.py -q

@Nanmur

Nanmur commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the CPU structural-IR regressions in 4923c2a without weakening or rewriting the existing expectations.

The symbolic fallback was unnecessarily used even when every input extent was static or structurally identical to its aligned output extent. Such cases do not need runtime singleton checks. They now retain the original C++ fast path/IR; genuinely ambiguous extents still use the guarded indexing required by the numerical regression.

Added two focused IR-preservation tests mirroring the affected broadcast shapes: (n,n)->(1,1,n,n) and (n,1,m)->(a,n,c,m). Both fail before this follow-up and pass afterward. All 25 targeted tests now pass, including the original LLVM/VM numerical cases. Ruff lint/format and diff checks pass.

Local validation uses the existing-runtime compatibility harness documented above, not a complete latest-main build. The three complete CI test functions/full CPU suite still need the fresh CI run; the push has triggered it.

This branch has not been deployed

No deployments
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.

[Bug][Relax][ONNX] Expand with a constant target accesses .value on a symbolic input extent

1 participant