-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Function-Rewriter] Support BiasSplitGelu operator in `stable_diffusi…
…on_unet` (#1400) 1. Support BiasSplitGelu in diffuser models 2. Add `function_unittest_producer.py` from onnx-rewriter After(left) vs Before(right) ![Screenshot 2024-04-18 102455](https://github.com/microsoft/onnxscript/assets/18010845/c483d575-ca06-4d35-aa43-af3d28d0ee0a)
- Loading branch information
1 parent
9ea1d1c
commit a6a7ffc
Showing
27 changed files
with
570 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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
32 changes: 32 additions & 0 deletions
32
onnxscript/rewriter/onnxruntime/transformers/biassplitgelu.py
This file contains 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,32 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
import onnx | ||
|
||
import onnxscript | ||
from onnxscript.rewriter import function_rule | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class GegluRewriteRule(function_rule.FunctionRewriteRule): | ||
FUNCTION_KEYWORD = "GEGLU" | ||
PACKAGE_NAME = "diffusers" | ||
_version_controller = function_rule.VersionController() | ||
|
||
@_version_controller.register_version() # type: ignore[misc] | ||
def _fusion( | ||
self, function: onnx.FunctionProto | ||
) -> tuple[onnx.FunctionProto, tuple[onnx.OperatorSetIdProto]]: | ||
del function # Unused | ||
op = self.onnx_opset | ||
msft_opset = onnxscript.values.Opset("com.microsoft", 1) | ||
|
||
def ggelu(input, weight, bias): | ||
weight_transpose = op.Transpose(weight, [1, 0]) | ||
matmul_input = op.MatMul(input, weight_transpose) | ||
return msft_opset.BiasSplitGelu(matmul_input, bias) | ||
|
||
function_proto = onnxscript.script(default_opset=op)(ggelu).to_function_proto() # type: ignore[arg-type] | ||
return function_proto, (onnx.helper.make_operatorsetid("com.microsoft", 1),) |
22 changes: 22 additions & 0 deletions
22
onnxscript/rewriter/onnxruntime/transformers/biassplitgelu_test.py
This file contains 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 @@ | ||
from __future__ import annotations | ||
|
||
import unittest | ||
|
||
import numpy as np | ||
|
||
from tests.common import testutils | ||
|
||
|
||
class BiasSplitGeluParityTest(unittest.TestCase): | ||
def setUp(self): | ||
np.random.seed(0) | ||
|
||
@testutils.skip_if_no_cuda("BiasSplitGelu Kernel unsupported on CPU.") | ||
def test_geglu_stable_diffusion_unet(self): | ||
testutils.test_onnxruntime_rewrite( | ||
"geglu_stable_diffusion_unet", 4, {("com.microsoft", "BiasSplitGelu", "")} | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains 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
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_0/geglu_stable_diffusion_unet_0.onnx
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_0/test_data_set_0/input_0.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_0/test_data_set_0/input_1.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_0/test_data_set_0/input_2.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_0/test_data_set_0/output_0.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_1/geglu_stable_diffusion_unet_1.onnx
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_1/test_data_set_0/input_0.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_1/test_data_set_0/input_1.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_1/test_data_set_0/input_2.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_1/test_data_set_0/output_0.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_2/geglu_stable_diffusion_unet_2.onnx
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_2/test_data_set_0/input_0.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_2/test_data_set_0/input_1.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_2/test_data_set_0/input_2.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_2/test_data_set_0/output_0.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_3/geglu_stable_diffusion_unet_3.onnx
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_3/test_data_set_0/input_0.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_3/test_data_set_0/input_1.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_3/test_data_set_0/input_2.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
testdata/unittest_models/geglu_stable_diffusion_unet_3/test_data_set_0/output_0.pb
Git LFS file not shown
Oops, something went wrong.