[ORT] Extend reshape fusion#29027
Conversation
d129219 to
9bbf182
Compare
|
@guschmue, PTAL, thanks! |
|
@guschmue, friendly ping, or could you point me to the proper owner? |
|
@yuslepukhin @hariharans29 - In Guenther S's absence, do you know how might be appropriate to review this change? TY. |
I will take a look. (Sadly, going forward, it will be in @yuslepukhin 's absence too). |
There was a problem hiding this comment.
Pull request overview
This PR extends the ReshapeFusion graph transformer to fuse additional reshape-shape construction patterns when tensor shapes involve symbolic (dim_param) dimensions or when the Shape source tensor differs from the reshape root (including a narrow GlobalAveragePool passthrough). It also updates ReshapeHelper’s handling of 0 dimensions when inferring an unknown (-1) dimension, and adds optimizer unit tests for the new fusion matches.
Changes:
- Extend
ReshapeFusion::Match_Shapeto allow matching bydim_paramand (optionally) by only the gathered dimension, plus aGlobalAveragePoolpassthrough for batch-dim extraction. - Update
ReshapeHelperinference when zeros are present alongside an unknown dimension. - Add graph transformation unit tests covering symbolic-dim and cross-tensor
Shapesources, and theGlobalAveragePoolpassthrough case.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| onnxruntime/core/optimizer/reshape_fusion.cc | Expands shape-source matching logic (symbolic dims, gathered-dim-only match, GAP passthrough) to enable additional reshape fusions. |
| onnxruntime/core/providers/cpu/tensor/reshape_helper.h | Adjusts unknown-dimension inference when zero dimensions are involved. |
| onnxruntime/test/optimizer/graph_transform_test.cc | Adds unit tests validating the new reshape fusion patterns. |
9bbf182 to
4af6e52
Compare
|
Thank you, @fdwr, @hariharans29. Comments addressed, PTAL again, thanks! |
Review: PR #29027 — [ORT] Extend reshape fusionApprove with minor observations. Real-world model-driven improvement — three additional What looks correct
Non-blocking observations
SummaryApprove pending full CI green. Correctness of the three new fusion paths and the |
|
@hariharans29, there's one CI failure and I've fixed it in latest commit, PTAL again, thanks! |
|
Hm m - not sure why the CI checks didn't complete. Could you please rebase with main ? I ll kick off another round of CI with that. |
Head branch was pushed to by a user without write access
e06f62c to
e28595f
Compare
Enable reshape fusion for models with symbolic (dynamic) dimensions, covering patterns found in MobileNetV2, vision encoders, and SLMs. Changes: - Match_Shape: allow dimension matching by symbolic dim_param, not just static values. Needed for SLMs (e.g. Qwen) where position_ids takes Shape from input_ids — same dim_param names, no static values. - Match_Shape: allow matching when only the gathered dimension agrees between Shape source and reshape root. Needed for vision encoders (e.g. moondream2) where Shape is taken from pre-projection tensor but Reshape operates on post-projection output. - Match_Shape: add GlobalAveragePool passthrough — accept Shape from pre-pool input when gathering batch dim. Needed for MobileNetV2. - ReshapeHelper: handle zero-dim shapes (allow_zero=true) by computing unknown dim from non-zero dimension product only. Needed for MobileNetV2 reshape fusion output. - Add corresponding unit tests.
@hariharans29, done, please help rerun the CI, thanks! |
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
|
Seems like there are some issues with our CI pipelines that we are working to resolve. I will take the next step on this PR once those are resolved. Thanks. |
Enable reshape fusion for models with symbolic (dynamic) dimensions, covering patterns found in MobileNetV2, vision encoders, and SLMs.
Changes:
position_idstakesShapefrominput_ids— samedim_paramnames, no static values.Shapesource and reshape root. Needed for vision encoders (e.g. moondream2) whereShapeis taken from pre-projection tensor butReshapeoperates on post-projection output.GlobalAveragePoolpassthrough — acceptShapefrom pre-pool input when gathering batch dim. Needed for MobileNetV2.allow_zero=true) by computing unknown dim from non-zero dimension product only. Needed for MobileNetV2 reshape fusion output.