Skip to content

Add fuse_gelu_sigmoid_approximation optimization pass#2648

Open
Rakshitha-Ireddi wants to merge 3 commits intoapple:mainfrom
Rakshitha-Ireddi:main
Open

Add fuse_gelu_sigmoid_approximation optimization pass#2648
Rakshitha-Ireddi wants to merge 3 commits intoapple:mainfrom
Rakshitha-Ireddi:main

Conversation

@Rakshitha-Ireddi
Copy link

Description

Add fuse_gelu_sigmoid_approximation optimization pass that detects and fuses the sigmoid approximation of GELU activation function.

The pattern x * sigmoid(1.702 * x) is fused into gelu(x, mode="SIGMOID_APPROXIMATION").

Motivation

The sigmoid approximation of GELU is commonly used in neural networks (e.g., some transformer variants, GPT-style models). Currently, this pattern remains decomposed as mul -> sigmoid -> mul, missing the opportunity for backend optimizations. This pass enables the backend to recognize and potentially accelerate this activation pattern.

Changes

  • Add optimize_gelu_sigmoid.py with fuse_gelu_sigmoid_approximation pass
  • Register the pass in __init__.py
  • Add pass to the default pass pipeline after fuse_gelu_exact
  • Add comprehensive tests in test_gelu_sigmoid_pass.py

Testing

  • Added 8 unit tests covering:
    • Basic fusion
    • Reversed operand order
    • Different tensor shapes (1D to 4D)
    • Negative cases (wrong constant, intermediate outputs, different inputs)
    • FP16 support
    • Numerical correctness

Files Changed

File Change
coremltools/converters/mil/mil/passes/defs/optimize_gelu_sigmoid.py New pass implementation
coremltools/converters/mil/mil/passes/tests/test_gelu_sigmoid_pass.py Test suite
coremltools/converters/mil/mil/passes/__init__.py Import registration
coremltools/converters/mil/mil/passes/pass_pipeline.py Pipeline integration

Contributors

  • Ireddi Rakshitha
  • Yaswanth Devavarapu

This pass detects the pattern x * sigmoid(1.702 * x) which is the sigmoid approximation of GELU, and fuses it into a single gelu op with mode=SIGMOID_APPROXIMATION.

Changes:

- Add optimize_gelu_sigmoid.py with fuse_gelu_sigmoid_approximation pass

- Register the pass in __init__.py

- Add pass to the default pass pipeline after fuse_gelu_exact

- Add comprehensive tests in test_gelu_sigmoid_pass.py
| |
|----------------------------------------

Pattern 2: x -> mul(x) -> sigmoid(1.702 * x) -> output (less common)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not clear to me where this pattern is being detected.

Copy link
Author

Choose a reason for hiding this comment

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

I have removed the Pattern 2 reference from the docstring since only Pattern 1 (x -> mul(1.702) -> sigmoid -> mul(x)) is implemented.

@TobyRoseman
Copy link
Collaborator

@TobyRoseman
Copy link
Collaborator

The new unit tests for this PR are failing on CI. Please take a look:
https://gitlab.com/coremltools1/coremltools/-/pipelines/2315236133

- Remove incorrect early-return check that rejected patterns where final mul is block output
- Change variable comparison to use == instead of .name comparison
- Remove unimplemented Pattern 2 from docstring
- Fix expected_output_shapes to use dict format instead of set
- Remove unused imports (pytest, PASS_REGISTRY)
@Rakshitha-Ireddi
Copy link
Author

Fixed! The issues were:

  1. An incorrect early-return check that rejected patterns where the final mul is a block output (which is the normal case when the result is returned)
  2. The test's expected_output_shapes parameter was incorrectly formatted as a set {(2, 3)} instead of a dictionary {output_name: (2, 3)}

@TobyRoseman
Copy link
Collaborator

@Rakshitha-Ireddi
Copy link
Author

@TobyRoseman
From updated CLI, Only one job was failing.
The MIL tests are now passing. The test_py310_pytorch_executorch failure appears to be unrelated to my changes - it's an import error in coremltools.converters.mil.frontend.torch which my PR doesn't touch. My changes only affect mil/passes/defs/optimize_gelu_sigmoid.py and mil/passes/tests/test_gelu_sigmoid_pass.py

Could you please check and let me know what can i do for this.

@TobyRoseman
Copy link
Collaborator

@Rakshitha-Ireddi - I agree that failure is not related to your PR; I'm seeing the same failure in main. I'll investigate and let you know when I'm ready for you to rebase this branch.

@Rakshitha-Ireddi
Copy link
Author

Thanks for checking! I will hold off on changes and wait for your update on the main branch issue.

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.

2 participants