-
Notifications
You must be signed in to change notification settings - Fork 72
Add Gelu Tanh fusion rule #2132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
from __future__ import annotations | ||
|
||
import math | ||
|
||
from onnxscript import ir | ||
from onnxscript.rewriter import pattern | ||
|
||
_sqrt_two_over_pi = math.sqrt(2.0 / math.pi) | ||
|
||
|
||
class GeluTanhFusion(pattern.RewriteRuleClassBase): | ||
def pattern(self, op, x): | ||
# GELU(x) = 0.5 * x * {1 + Tanh[\sqrt(2/pi) * (x + 0.044715 * x^3)]} | ||
t1 = op.Pow(x, 3) | ||
t2 = op.Mul(0.044715, t1) | ||
t3 = op.Add(x, t2) | ||
|
||
t4 = op.Mul(_sqrt_two_over_pi, t3) | ||
t5 = op.Tanh(t4) | ||
t6 = op.Add(t5, 1) | ||
t7 = op.Mul(x, t6) | ||
result = op.Mul(0.5, t7) | ||
return result | ||
|
||
def rewrite(self, op, x): | ||
return op.Gelu(x, _domain="com.microsoft") | ||
|
||
|
||
_rule = GeluTanhFusion.rule() | ||
|
||
gelu_rules = pattern.RewriteRuleSet([_rule]) | ||
|
||
|
||
def fuse_gelu(model: ir.Model) -> None: | ||
gelu_rules.apply_to_model(model) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
import math | ||
import unittest | ||
|
||
import numpy as np | ||
|
||
import onnxscript.ir as ir | ||
import onnxscript.rewriter.ort_fusions._test_utils as test_utils | ||
from onnxscript import FLOAT, script | ||
from onnxscript import opset18 as op | ||
from onnxscript.optimizer import optimize, remove_unused_nodes | ||
from onnxscript.rewriter.ort_fusions.gelu import fuse_gelu | ||
|
||
|
||
class GeluFusionTest(unittest.TestCase): | ||
def test_gelu_fusion(self): | ||
_sqrt_two_over_pi = math.sqrt(2.0 / math.pi) | ||
|
||
@script() | ||
def gelu_model(x): | ||
# GELU(x) = 0.5 * x * {1 + Tanh[\sqrt(2/pi) * (x + 0.044715 * x^3)]} | ||
t1 = op.Pow(x, 3) | ||
t2 = op.Mul(0.044715, t1) | ||
t3 = op.Add(x, t2) | ||
|
||
t4 = op.Mul(_sqrt_two_over_pi, t3) | ||
t5 = op.Tanh(t4) | ||
t6 = op.Add(t5, 1) | ||
t7 = op.Mul(x, t6) | ||
result = op.Mul(0.5, t7) | ||
return result | ||
|
||
model_proto = gelu_model.to_model_proto( | ||
input_types=[FLOAT[10]], output_types=[FLOAT[10]] | ||
) | ||
model = ir.serde.deserialize_model(model_proto) | ||
|
||
# Eliminate redundant CastLike ops: | ||
optimize(model) | ||
|
||
input = {"x": np.random.randn(10).astype(np.float32)} | ||
original_output = test_utils.ort_run("Original", model, input) | ||
|
||
fuse_gelu(model) | ||
remove_unused_nodes(model) | ||
|
||
self.assertEqual(len(model.graph), 1) | ||
self.assertEqual(model.graph.node(0).op_type, "Gelu") | ||
|
||
optimized_output = test_utils.ort_run("Optimized", model, input) | ||
test_utils.assert_allclose(original_output, optimized_output) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.