Skip to content

Add onnx-graphsurgeon example for folding legacy GroupNorm pattern#4749

Open
ssam18 wants to merge 2 commits intoNVIDIA:mainfrom
ssam18:add-fold-groupnorm-example
Open

Add onnx-graphsurgeon example for folding legacy GroupNorm pattern#4749
ssam18 wants to merge 2 commits intoNVIDIA:mainfrom
ssam18:add-fold-groupnorm-example

Conversation

@ssam18
Copy link
Copy Markdown

@ssam18 ssam18 commented May 8, 2026

This adds a new onnx-graphsurgeon example that detects the legacy Reshape, InstanceNormalization, Reshape, Mul, Add pattern emitted by PyTorch under 2.5 when exporting nn.GroupNorm at an ONNX opset below 18. The example rewrites every match into a single native GroupNormalization node and bumps the model opset to 21, which is the version where per channel scale and bias are accepted. The motivation is that the legacy template can drift inside the fused norm path when reduction extents are large, so giving users a clean single op gives them a way to route around the issue. The fold has been validated end to end on ONNX Runtime CPU against both a tiny synthesised model and a real SegVit export, with max abs diff staying inside FP32 noise. Fixes #4741

PyTorch versions before 2.5 export nn.GroupNorm at opset under 18 as a Reshape, InstanceNormalization, Reshape, Mul, Add chain. The new example walks through detecting that pattern and rewriting it as a single native GroupNormalization node so users can sidestep the fused-norm path that has shown accuracy drift on large reductions. Verified end to end against ONNX Runtime CPU on both a synthesised toy model and a real SegVit export, with max abs diff 1e-5 in FP32.

Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
@ssam18 ssam18 requested a review from a team as a code owner May 8, 2026 17:19
Comment thread tools/onnx-graphsurgeon/examples/13_folding_groupnorm/fold.py Outdated
Refactors fold.py to build the legacy GroupNorm template once as a GraphPattern
and rely on match_all for discovery, replacing the manual node-by-node walk.

Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
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.

Accuracy mismatch between ONNX Runtime and TensorRT for SegVit

2 participants