Repository navigation
[Relax] Fix FoldConstant crash on data-dependent reshape with unknown shape - #20488
cchung100m wants to merge 12 commits into
Conversation
2953448 to
56ad79a
Compare
56ad79a to
5553544
Compare
…hape_with_unknown_shape
…se_ops_is_skipped
2a7512f to
afa1085
Compare
146cd26 to
31bee11
Compare
|
please rebase from main and fix ci errors. @cchung100m |
a5542fc to
90b5bfe
Compare
|
Hi @mshr-h Thanks for reminder, I rebased on main and fixed the CI errors. |
There was a problem hiding this comment.
🟡 Changes recommended
FoldConstant can still emit ill-formed IR when reshape data folds to a constant with a dynamic target, and its type fallback discards safe refinements that prevent incorrect empty-shape decomposition.
2 open issues
What changed in this PR
This PR targets #20260 by aligning Relax’s speculative constant-folding legalization with the shape checks used by LegalizeOps, avoiding legalization attempts on unresolved runtime shapes.
Changes:
- Extracts shared shape-availability checks for both passes.
- Restores call types when folding is skipped and improves shape-binding diagnostics.
- Adds regression tests for data-dependent reshapes with unknown target shapes.
| File | Description |
|---|---|
| tests/python/relax/test_transform_legalize_ops_manipulate.py | Tests that legalization skips unresolved data-dependent reshapes. |
| tests/python/relax/test_transform_fold_constant.py | Tests constant-folding behavior with unknown reshape targets. |
| src/relax/transform/utils.h | Adds shared shape-availability and legalization checks. |
| src/relax/transform/legalize_ops.cc | Replaces the local shape gate with the shared helper. |
| src/relax/transform/fold_constant.cc | Gates speculative legalization and restores skipped-call types. |
| python/tvm/relax/transform/legalize_ops/manipulate.py | Replaces a bare assertion with a descriptive error. |
Review effort: Max
| } | ||
| new_args.push_back(arg); | ||
| } | ||
| Type original_ty = post_call->ty; |
There was a problem hiding this comment.
The failure already occurs after BindParams, before FoldConstant, because InferTypeReshape puts the DataflowVar into the type. Restoring types here wouldn't fix it, so we might track it in a separate issue. Does that work, @mshr-h ?
| } else { | ||
| // Restore the original (well-formed) type instead of the `Missing` type set above. | ||
| return Call(original_ty, post_call->op, post_call->args, post_call->attrs, | ||
| post_call->ty_args, post_call->loc); |
There was a problem hiding this comment.
I couldn't reproduce this: for that example the type is already refined to shape[2] at parse time, so restoring it keeps the refinement.


Hi Committers,
This PR fixes issues #20260. Any suggestions would be appreciated if you are available.
Root Cause
FoldConstanthas a speculative step that calls an op's registeredFLegalizefunction to check if it would fold into an all-constantcall_tir. Unlike the realLegalizeOpspass, this speculative probe never checks whether the call's argument/return shapes are actually known before invokingFLegalize.When
reshape's target shape comes from an unfoldedtensor_to_shapecall (i.e., a genuinely non-constant tensor, so the shape values are unknown), this lets the unresolved shape reachreshape's legalizer (reshape_call_te), which asserts - with no message - that the shape has already resolved to aShapeExpr. That's what actually crashes.Note: the original issue attributes the crash to
fold_constant.cc'sshape_to_tensorspecial case, but that code path is dead for this repro (shape_to_tensorhas had a realFLegalizesince #19957) - it was already stale when the issue was filed. This PR fixes the crash reachable today (thetensor_to_shape->reshapecase). The issue's other example (shape_to_tensorfed directly intoreshape) is unrelated to this crash path. It is now rejected earlier, at graph-construction time, by unrelated, more recent type-inference changes - not by this PR.Solutions
LegalizeOps) into a sharedCanLegalizedCallhelper insrc/relax/transform/utils.h, reused by bothLegalizeOpsandFoldConstant.FoldConstantnow checksCanLegalizeCallbefore its speculative legalize-and-fold probe; when shapes aren't statically known, it skips folding and restores the call's original type (previously reset toType::Missing(), which could let a later stage infer an ill-formed type referencing a block-scopedDataflowVar).reshape/broadcast_to/collapse_sum_*'s sharedFLegalize(reshape_call_te) to raise a descriptiveValueErrorinstead of a bareassert, as defense in depth.