Skip to content

Preserve double constants in graph fusions - #32983

Open
Yuhx141 wants to merge 6 commits into
microsoft:mainfrom
Yuhx141:fix/fusion-double-precision
Open

Yuhx141 wants to merge 6 commits into
microsoft:mainfrom
Yuhx141:fix/fusion-double-precision

Conversation

@Yuhx141

@Yuhx141 Yuhx141 commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Description

This change declines graph fusions when a DOUBLE constant cannot make the required round trip through a float attribute.

  • MatMulScaleFusion leaves a lossy DOUBLE scale as an explicit Mul or Div.
  • LayerNormFusion and SimplifiedLayerNormFusion require epsilon to be a scalar constant exactly representable by the fused float attribute.

Exactly representable DOUBLE values and the existing FLOAT paths still fuse.

Motivation and Context

FusedMatMul and LayerNormalization store these values in float attributes. Narrowing a valid DOUBLE initializer changed model output.

Fixes #32961
Fixes #32962

Testing

Built onnxruntime_test_all with training enabled in Release mode.

Ran 7 focused tests covering the two new regressions plus existing FLOAT and exactly representable DOUBLE fusion paths; all passed.

Copilot AI balanced review requested due to automatic review settings September 30, 2026 11:52
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

Review effort: Lite
Findings: 2 Medium severity · 1 Low severity

Open (3)
What changed in this PR

This PR prevents optimizer fusions that would silently narrow DOUBLE constants into FLOAT attributes, which can change model outputs.

Changes:

  • MatMulScaleFusion now refuses fusion when a DOUBLE scale cannot be represented losslessly as a float attribute.
  • LayerNormFusion/SimplifiedLayerNormFusion require epsilon to be a scalar constant that round-trips exactly through float.
  • Added regression tests covering lossy DOUBLE cases and confirming existing FLOAT/lossless DOUBLE fusions still work.
File Description
onnxruntime/​test/​optimizer/​graph_transform_test_layernorm.cc Adds tests ensuring LayerNorm fusions are rejected for lossy DOUBLE epsilon.
onnxruntime/​test/​optimizer/​graph_transform_test.cc Adds a MatMulScaleFusion test that rejects lossy DOUBLE scales; includes <cmath> for nextafter.
onnxruntime/​core/​optimizer/​matmul_scale_fusion.cc Adds lossless-check plumbing when extracting scalar constants as float for fusion attributes.
onnxruntime/​core/​optimizer/​layer_norm_fusion.cc Tightens epsilon handling to require a scalar initializer and lossless float round-trip before fusing.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread onnxruntime/core/optimizer/layer_norm_fusion.cc Outdated
Comment thread onnxruntime/core/optimizer/matmul_scale_fusion.cc
Comment thread onnxruntime/test/optimizer/graph_transform_test_layernorm.cc Outdated
@mirounga
mirounga self-requested a review September 30, 2026 14:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Div and combined-scale alpha calculations can still narrow DOUBLE values, and one regression test does not exercise its intended path.

Review effort: Balanced
Findings: 1 High severity · 1 Medium severity

Open (2)
Resolved since last review (3)

Comment thread onnxruntime/core/optimizer/matmul_scale_fusion.cc Outdated
Comment thread onnxruntime/test/optimizer/graph_transform_test_layernorm.cc Outdated
@Yuhx141

Yuhx141 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Dmitri Smirnov (@yuslepukhin) The latest commit addresses all review threads and passes the 16 targeted optimizer tests locally. The GitHub Actions workflows are currently waiting for maintainer approval. Could you approve them when convenient?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Cast-folding can double-round integer epsilon values, and positive DOUBLE LayerNorm coverage is missing.

Review effort: Balanced
Findings: 1 High severity · 1 Low severity

Open (2)
Resolved since last review (2)

Comment thread onnxruntime/core/optimizer/layer_norm_fusion.cc Outdated
Comment thread onnxruntime/test/optimizer/graph_transform_test_layernorm.cc
@Yuhx141

Yuhx141 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

The workflows for aadacfc are currently marked action_required and are waiting for approval. I also reproduced the failing WASM Release static-library configuration locally with WASM SIMD, RTTI disabled, and API exception catching enabled; the full build completed successfully through bundling_target with warnings treated as errors. Could a maintainer please approve and run the workflows? Thanks.

@Yuhx141

Yuhx141 commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up pushed in bf0854c. Cast-to-FLOAT epsilon constants now convert directly to float, avoiding double rounding for large integer initializers; exact DOUBLE epsilons remain accepted and lossy ones remain rejected. The three targeted LayerNorm tests pass in a clean training-enabled ORT build.

@mirounga
mirounga requested a balanced review from Copilot October 5, 2026 20:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Individually lossy DOUBLE scales can cancel during accumulation and still be fused, changing results.

1 open finding

🧠 Review effort: Balanced

Comment thread onnxruntime/core/optimizer/matmul_scale_fusion.cc Outdated
Signed-off-by: Yuhx141 <155034285+Yuhx141@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

LayerNorm fusions do not preserve DOUBLE epsilon MatMulScaleFusion narrows a DOUBLE scale to float alpha

2 participants