Skip to content

Validate FastGelu fusion scale node - #32016

Merged
Akshay Sonawane (apsonawane) merged 5 commits into
mainfrom
fix/fast-gelu-fusion-node-validation
Aug 14, 2026
Merged

Validate FastGelu fusion scale node#32016
Akshay Sonawane (apsonawane) merged 5 commits into
mainfrom
fix/fast-gelu-fusion-node-validation

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request improves the robustness of the FastGelu fusion optimization by ensuring malformed nodes are properly skipped and adds a test to verify this behavior. The main changes include stricter input validation in the fusion logic and a new unit test.

Fusion logic improvements:

  • Added explicit checks for the number of inputs (InputDefs().size()) in Mul and Pow nodes within the FastGeluFusion optimizer to ensure only well-formed nodes are considered for fusion. [1] [2] [3]

Testing enhancements:

  • Introduced a new test, FastGeluFusionSkipsMalformedScaleMul, that modifies a model to create a malformed Mul node and verifies that the fusion optimizer correctly skips it (i.e., does not produce a FastGelu node).

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.

Pull request overview

This PR hardens the FastGeluFusion graph rewrite by adding input-count validation so malformed Mul/Pow nodes are skipped instead of being processed, and adds a regression test to ensure the fusion does not produce a com.microsoft.FastGelu node when the scale Mul is malformed.

Changes:

  • Add InputDefs().size() == 2 / != 2 checks for key Mul and Pow nodes used by the FastGelu pattern matcher.
  • Fix the first-formula scale-mul validation to actually validate the upstream Mul node being inspected.
  • Add a unit test that mutates the FastGelu test model to create a malformed scale Mul and verifies fusion is skipped.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
onnxruntime/core/optimizer/fast_gelu_fusion.cc Adds stricter input-count validation in FastGelu pattern matching to avoid processing malformed nodes.
onnxruntime/test/optimizer/graph_transform_test.cc Adds a regression test that forces a malformed scale Mul and checks the fusion optimizer does not emit com.microsoft.FastGelu.
Suppressed comments (1)

onnxruntime/core/optimizer/fast_gelu_fusion.cc:122

  • Similar to the pow1_node.InputDefs().size() != 2 guard, the rest of CheckSecondFormula assumes intermediate nodes have 2 inputs and that IndexOfNodeInput returns a valid index before using [(input_index + 1) % 2] (e.g., the checks around lines 135-138 and 174-177). For malformed graphs this can still access out-of-range inputs or match the wrong operand when IndexOfNodeInput returns -1. Consider adding InputDefs().size() == 2 and input_index >= 0 guards before all modulo-based input indexing so the fusion always fails safely.
  if (!graph_utils::IsSupportedOptypeVersionAndDomain(pow1_node, "Pow", {7, 12, 13, 15}) ||
      pow1_node.InputDefs().size() != 2 ||
      !graph_utils::IsSupportedProvider(pow1_node, GetCompatibleExecutionProviders()) ||
      pow1_node.GetOutputEdgesCount() != 1 ||
      !IsSupportedDataType(pow1_node)) {

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread onnxruntime/core/optimizer/fast_gelu_fusion.cc
@apsonawane
Akshay Sonawane (apsonawane) merged commit 68aa29c into main Aug 14, 2026
87 checks passed
@apsonawane
Akshay Sonawane (apsonawane) deleted the fix/fast-gelu-fusion-node-validation branch August 14, 2026 14:24
Tianlei Wu (tianleiwu) pushed a commit that referenced this pull request Aug 17, 2026
This cherry-picks the following commits for the release:

* #32016
* #32042

---------

Co-authored-by: Akshay Sonawane <111780983+apsonawane@users.noreply.github.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.

4 participants