[Fix][Relax] Honor ONNX Reshape zero semantics - #20161
Conversation
ONNX Reshape copies the corresponding input dimension for zero entries unless allowzero is enabled. Normalize those entries before constant folding, and materialize allowzero shapes without triggering Relax reshape zero-copy semantics. Add regression coverage for both cases.
tlopex
left a comment
There was a problem hiding this comment.
allowzero only changes zero semantics, but this branch also bypasses -1 inference. A valid non-constant (3, 4) input with shape [-1, 2] and allowzero=1 reaches runtime with a literal -1 and fails with std::bad_alloc. Please keep the normal reshape path when no literal zero is present and add a regression test.
Thanks for catching this. You’re right that I narrowed the special path to constant target shapes containing zero. With I added the requested regression test and pushed the update in d20b94e. All five Reshape-related tests and the changed-file pre-commit checks pass. |
ONNX Reshape uses zero entries to copy the corresponding input dimension by default. The Relax importer currently constant-folds those shapes through NumPy without applying that rule, so a valid shape such as
[0, 3]fails for a(2, 3)tensor. Conversely, whenallowzero=1, passing the zero throughrelax.op.reshapeinvokes Relax's own zero-copy convention instead of preserving the literal zero dimension.This change normalizes copied dimensions before constant folding when
allowzerois disabled. When it is enabled, the shape tensor is materialized as a symbolicShapeExpr, preserving literal zero dimensions without applying Relax's zero-copy shortcut.The regression tests cover both the default all-constant path and an executable
allowzero=1model with a(0, 2)output.Fixes #20151
Testing:
The first two commands pass, and the broad run reports 493 passed, 9 skipped, and 4 xfailed. Running the entire file also reports three
test_clip_v13failures that reproduce unchanged on a cleanmainworktree in the same environment.