-
Notifications
You must be signed in to change notification settings - Fork 109
Add BiasGelu, Erfgelu and SkipLayerNormalization fusions #2222
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
803d565
add additional gelu fusions
shubhambhokare1 5d4a019
add skip_normalization with bias
shubhambhokare1 4e8243e
reorder file
shubhambhokare1 c3a2762
fix test failures
shubhambhokare1 d65da68
update test to onnxscript style
shubhambhokare1 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
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
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,22 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| from __future__ import annotations | ||
|
|
||
| from onnxscript.rewriter import _fusion_utils, pattern | ||
|
|
||
|
|
||
| class BiasGeluFusion(pattern.RewriteRuleClassBase): | ||
| def pattern(self, op, x, y): | ||
| gelu_add = op.Add(x, y) | ||
| return op.Gelu(gelu_add, _domain="com.microsoft") | ||
|
|
||
| def rewrite(self, op, x, y): | ||
| return op.BiasGelu(x, y, _domain="com.microsoft") | ||
|
|
||
|
|
||
| _rule = BiasGeluFusion.rule() | ||
|
|
||
| bias_gelu_rules = pattern.RewriteRuleSet([_rule]) | ||
|
|
||
|
|
||
| fuse_bias_gelu = _fusion_utils.apply_fusion_rules(bias_gelu_rules) |
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,52 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| import unittest | ||
|
|
||
| import numpy as np | ||
|
|
||
| import onnxscript | ||
| 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.bias_gelu import fuse_bias_gelu | ||
|
|
||
| msft_op = onnxscript.values.Opset("com.microsoft", 1) | ||
|
|
||
|
|
||
| class BiasGeluFusionTest(unittest.TestCase): | ||
| def test_bias_gelu_fusion(self): | ||
| @script() | ||
| def bias_gelu_model(x, y): | ||
| gelu_add = op.Add(x, y) | ||
| gelu = msft_op.Gelu(gelu_add) | ||
| return gelu | ||
|
|
||
| model_proto = bias_gelu_model.to_model_proto( | ||
| input_types=[FLOAT[10], FLOAT[10]], | ||
| output_types=[FLOAT[10]], | ||
| ir_version=10, | ||
| ) | ||
| model = ir.serde.deserialize_model(model_proto) | ||
| optimize(model) | ||
|
|
||
| input = { | ||
| "x": np.random.randn(10).astype(np.float32), | ||
| "y": np.random.randn(10).astype(np.float32), | ||
| } | ||
| original_output = test_utils.ort_run("Original", model, input) | ||
|
|
||
| fuse_bias_gelu(model) | ||
| remove_unused_nodes(model) | ||
|
|
||
| self.assertEqual(len(model.graph), 1) | ||
| self.assertEqual(model.graph.node(0).op_type, "BiasGelu") | ||
|
|
||
| optimized_output = test_utils.ort_run("Optimized", model, input) | ||
| test_utils.assert_allclose(original_output, optimized_output) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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
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
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.