onnxruntime
5652d3fa - Fix ReshapeFusion crash when fused Reshape's shape input is node-produced (#33091)

Commit
35 days ago
Fix ReshapeFusion crash when fused Reshape's shape input is node-produced (#33091) ### Description **What happens** `FuseContiguousReshapes` folds a chain of `Reshape` / `Squeeze` / `Unsqueeze` nodes into a single `Reshape`: - The replacement node consumes only the **data input** of the first node; its shape comes from a **new constant initializer** derived from the last node's inferred output shape. - `FinalizeNodeFusion` moves the first node's input edges onto the replacement **by tensor name** and asserts every moved name is an input of the replacement. - A shape input **produced by another node** (e.g. a `Where`) has no matching input on the replacement, so the assert fires in `GetIndexFromName` and **session initialization fails**. Initializer-backed shape inputs carry no edge, which is why only dynamic shapes hit this. **The fix** (in `onnxruntime/core/optimizer/reshape_fusion.cc`): 1. Before the fusion, remove the edge feeding the first node's shape input when a node produces it. 2. After the fusion, delete that producer when the fusion removed its last consumer and it does not feed a graph output. This mirrors the handling `SimplifiedLayerNormFusion` uses for inputs the replacement does not consume (#29196). **Regression tests** (in `onnxruntime/test/optimizer/graph_transform_test.cc`): - `ReshapeFusionContiguousReshapesWithNodeProducedShape` — transformer level: the chain collapses to one `Reshape` with constant shape `{6}` and the `Where` producer is deleted. - `ReshapeFusionContiguousReshapesWithNodeProducedShapeExecution` — full `InferenceSession` run: initialization succeeds and `Run()` returns the input reshaped to `[6]`. - Both reproduce the reported error without the fix. ### Motivation and Context **Fixes #32837** A model whose first of two chained `Reshape`s takes a runtime-computed shape fails session creation under `ORT_ENABLE_BASIC` and above: `Where(cond, target_shape, target_shape) → Reshape(x, shape1) → Reshape(r1, shape2) → y` > `GetIndexFromName ... Attempting to get index by a name which does not exist: shape1 for node: reshape_dynamic_new_reshape` - The only workaround is disabling graph optimizations; `ORT_DISABLE_ALL` runs fine, so the model itself is valid. - The fusion is semantically correct for these graphs — the target shape is fully determined by the last node's inferred output shape — so the fix **repairs the edge handling and keeps the optimization** rather than bailing out. **Verified locally** (macOS arm64, Release, CPU EP): - `GraphTransformationTests.ReshapeFusion*`: 24/24 pass, including both new tests - `onnxruntime_test_all`: 2267 passed / 39 skipped / 0 failed - `lintrunner`: clean
Author
Parents
Loading