-
Notifications
You must be signed in to change notification settings - Fork 72
Extensions to transformer fusions #2082
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
18 commits
Select commit
Hold shift + click to select a range
1c9b253
Extend cos_sin_cache fusion to handle 1d position-id
gramalingam f976dfc
Make MatchingTracer a parameter
gramalingam f9e0c17
Update debug use of pattern matching
gramalingam babe46d
Cleanup match reporting
gramalingam 2e610d3
Run lint
gramalingam a0d65a2
Merge with main
gramalingam ce04e19
Add toy test for rotary embedding
gramalingam e52ab7b
Fix test case
gramalingam cf46d11
Fix test case
gramalingam f2ce907
Fix fusion
gramalingam 05b35cd
Fix lint issues
gramalingam a223994
Skip onnxscript code in mypy
gramalingam 12e8178
something
gramalingam 88fb61e
Fix lint issues
gramalingam 4d55a68
Update toml file
gramalingam a2eff29
Merge branch 'main' into rama/fusion_fixes
gramalingam b244112
Run lint
gramalingam 53e462f
Merge branch 'rama/fusion_fixes' of https://github.com/microsoft/onnx…
gramalingam 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
103 changes: 103 additions & 0 deletions
103
onnxscript/rewriter/ort_fusions/_rotary_embedding_models.py
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,103 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
|
||
# Licensed under the MIT License. | ||
|
||
"""Small test case models for rotary embedding.""" | ||
|
||
import numpy | ||
|
||
import onnxscript.ir as ir | ||
from onnxscript import script | ||
from onnxscript.onnx_opset import opset18 as op | ||
from onnxscript.onnx_types import FLOAT, INT64 | ||
|
||
|
||
# x: [B, H, S, E] | ||
# position_ids: [B, S] | ||
@script() | ||
def _test_case_1_script(x: FLOAT[1, 4, 8, 8], position_ids: INT64[1, 8]) -> FLOAT[1, 4, 8, 8]: | ||
inv_freq = op.Constant(value_floats=[1.0, 2.0, 3.0, 4.0]) | ||
inv_freq_3d = op.Unsqueeze(inv_freq, [0, 2]) | ||
position_ids_expanded = op.Unsqueeze(position_ids, [1]) # => [B, 1, S] | ||
position_ids_float = op.Cast(position_ids_expanded, to=ir.DataType.FLOAT) | ||
freqs = op.MatMul(inv_freq_3d, position_ids_float) # [B, E, S] | ||
freqs = op.Transpose(freqs, perm=[0, 2, 1]) # [B, S, E] | ||
emb = op.Concat(freqs, freqs, axis=-1) | ||
cos = op.Cos(emb) | ||
sin = op.Sin(emb) | ||
cos_4d = op.Unsqueeze(cos, 1) | ||
sin_4d = op.Unsqueeze(sin, 1) | ||
|
||
x1 = op.Slice(x, [0], [4], [3], [1]) | ||
x2 = op.Slice(x, [4], [8], [3], [1]) | ||
minus_x2 = op.Neg(x2) | ||
rotated_x = op.Concat(minus_x2, x1, axis=-1) | ||
rotary_embedding = op.Add(x * cos_4d, rotated_x * sin_4d) | ||
return rotary_embedding | ||
|
||
|
||
class _TestCase1: | ||
def get_onnx_model(self): | ||
if not hasattr(self, "_onnx_model"): | ||
model_proto = _test_case_1_script.to_model_proto() | ||
model = ir.serde.deserialize_model(model_proto) | ||
self._onnx_model = model | ||
return self._onnx_model | ||
|
||
def get_ort_inputs(self): | ||
if not hasattr(self, "_ort_inputs"): | ||
inputs = { | ||
"x": numpy.random.rand(1, 4, 8, 8).astype(numpy.float32), | ||
"position_ids": numpy.arange(8, dtype=numpy.int64).reshape(1, 8), | ||
} | ||
self._ort_inputs = inputs | ||
return self._ort_inputs | ||
|
||
|
||
def test_case_1(): | ||
return _TestCase1() | ||
|
||
|
||
# x: [B, H, S, E] | ||
# position_ids: [S] | ||
@script() | ||
def _test_case_2_script(x: FLOAT[1, 4, 8, 8], position_ids: INT64[8]) -> FLOAT[1, 4, 8, 8]: | ||
inv_freq = op.Constant(value_floats=[1.0, 2.0, 3.0, 4.0]) | ||
inv_freq_3d = op.Unsqueeze(inv_freq, [0, 2]) | ||
position_ids_expanded = op.Unsqueeze(position_ids, [0, 1]) # => [1, 1, S] | ||
position_ids_float = op.Cast(position_ids_expanded, to=ir.DataType.FLOAT) | ||
freqs = op.MatMul(inv_freq_3d, position_ids_float) # [B, E, S] | ||
freqs = op.Transpose(freqs, perm=[0, 2, 1]) # [B, S, E] | ||
emb = op.Concat(freqs, freqs, axis=-1) | ||
cos = op.Cos(emb) | ||
sin = op.Sin(emb) | ||
cos_4d = op.Unsqueeze(cos, 1) | ||
sin_4d = op.Unsqueeze(sin, 1) | ||
|
||
x1 = op.Slice(x, [0], [4], [3], [1]) | ||
x2 = op.Slice(x, [4], [8], [3], [1]) | ||
minus_x2 = op.Neg(x2) | ||
rotated_x = op.Concat(minus_x2, x1, axis=-1) | ||
rotary_embedding = op.Add(x * cos_4d, rotated_x * sin_4d) | ||
return rotary_embedding | ||
|
||
|
||
class _TestCase2: | ||
def get_onnx_model(self): | ||
if not hasattr(self, "_onnx_model"): | ||
model_proto = _test_case_2_script.to_model_proto() | ||
model = ir.serde.deserialize_model(model_proto) | ||
self._onnx_model = model | ||
return self._onnx_model | ||
|
||
def get_ort_inputs(self): | ||
if not hasattr(self, "_ort_inputs"): | ||
inputs = { | ||
"x": numpy.random.rand(1, 4, 8, 8).astype(numpy.float32), | ||
"position_ids": numpy.arange(8, dtype=numpy.int64).reshape(8), | ||
} | ||
self._ort_inputs = inputs | ||
return self._ort_inputs | ||
|
||
|
||
def test_case_2(): | ||
return _TestCase2() |
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
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
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
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.
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.