-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Layer norm fusion deepspeed stage3 changes (#17614)
### Description <!-- Describe your changes. --> Layer norm fusion changes required for deepspeed stage 3, also includes test case. ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> It helps fusing layer norm for Deepspeed Stage 3. Added a test case scenario which ensures that the fusion is working properly for the scenario.
- Loading branch information
Showing
4 changed files
with
136 additions
and
21 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
Binary file added
BIN
+854 Bytes
onnxruntime/test/testdata/transform/fusion/layer_norm_fusion_scale_bias.onnx
Binary file not shown.
81 changes: 81 additions & 0 deletions
81
onnxruntime/test/testdata/transform/fusion/layer_norm_fusion_scale_bias.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,81 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
import onnx | ||
from onnx import OperatorSetIdProto, TensorProto, helper | ||
|
||
|
||
def GenerateModel(model_name, has_casts=False, has_identity=False): # noqa: N802 | ||
nodes = [ # LayerNorm subgraph | ||
helper.make_node("ReduceMean", ["A"], ["rd_out"], "reduce1", axes=[-1], keepdims=1), | ||
helper.make_node("Sub", ["A", "rd_out"], ["sub_out"], "sub"), | ||
helper.make_node("Pow", ["cast_sub_out" if has_casts else "sub_out", "pow_in_2"], ["pow_out"], "pow"), | ||
helper.make_node("ReduceMean", ["pow_out"], ["rd2_out"], "reduce2", axes=[-1], keepdims=1), | ||
helper.make_node("Add", ["rd2_out", "const_e12_f32"], ["add1_out"], "add1"), | ||
helper.make_node("Sqrt", ["add1_out"], ["sqrt_out"], "sqrt"), | ||
helper.make_node("Div", ["cast_sub_out" if has_casts else "sub_out", "sqrt_out"], ["div_out"], "div"), | ||
helper.make_node( | ||
"Mul", | ||
["gamma_id_out" if has_identity else "gamma", "cast_div_out" if has_casts else "div_out"], | ||
["mul_out"], | ||
"mul", | ||
), | ||
helper.make_node("Add", ["mul_out", "const_e6_f16_out" if has_identity else "const_e6_f16"], ["C"], "add2"), | ||
] | ||
|
||
if has_casts: | ||
nodes.extend( | ||
[ | ||
helper.make_node("Cast", ["sub_out"], ["cast_sub_out"], "cast_sub", to=1), | ||
helper.make_node("Cast", ["div_out"], ["cast_div_out"], "cast_2", to=10), | ||
] | ||
) | ||
|
||
if has_identity: | ||
nodes.extend( | ||
[ | ||
helper.make_node("Identity", ["gamma"], ["gamma_id_out"], "gamma_identity"), | ||
helper.make_node("Identity", ["const_e6_f16"], ["const_e6_f16_out"], "const_e6_f16_identity"), | ||
] | ||
) | ||
|
||
initializers = [ # initializers | ||
helper.make_tensor("pow_in_2", TensorProto.FLOAT, [], [2]), | ||
helper.make_tensor("const_e12_f32", TensorProto.FLOAT, [], [1e-12]), | ||
helper.make_tensor("const_e6_f16", TensorProto.FLOAT16, [4], [1e-6, 1e-6, 1e-6, 1e-6]), | ||
helper.make_tensor( | ||
"gamma", | ||
TensorProto.FLOAT16 if has_casts else TensorProto.FLOAT, | ||
[4], | ||
[1, 2, 3, 4], | ||
), | ||
] | ||
|
||
input_type = TensorProto.FLOAT16 if has_casts else TensorProto.FLOAT | ||
output_type = TensorProto.FLOAT16 if has_casts else TensorProto.FLOAT | ||
|
||
graph = helper.make_graph( | ||
nodes, | ||
"LayerNorm", # name | ||
[ # inputs | ||
helper.make_tensor_value_info("A", input_type, [16, 32, 4]), | ||
], | ||
[ # outputs | ||
helper.make_tensor_value_info("C", output_type, [16, 32, 4]), | ||
], | ||
initializers, | ||
) | ||
|
||
onnxdomain = OperatorSetIdProto() | ||
onnxdomain.version = 12 | ||
# The empty string ("") or absence of this field implies the operator set that is defined as part of the ONNX specification. | ||
onnxdomain.domain = "" | ||
msdomain = OperatorSetIdProto() | ||
msdomain.version = 1 | ||
msdomain.domain = "com.microsoft" | ||
opsets = [onnxdomain, msdomain] | ||
|
||
model = helper.make_model(graph, opset_imports=opsets) | ||
onnx.save(model, model_name) | ||
|
||
|
||
GenerateModel("layer_norm_fusion_scale_bias.onnx", True, True) |