Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
MIGraphX
Commits
6ca16d98
Commit
6ca16d98
authored
May 26, 2022
by
turneram
Browse files
Use parse_layernorm to un-fuse LayerNormalization op
parent
503188d5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
11 deletions
+25
-11
src/onnx/parse_attention.cpp
src/onnx/parse_attention.cpp
+0
-1
src/onnx/parse_layernorm.cpp
src/onnx/parse_layernorm.cpp
+25
-10
No files found.
src/onnx/parse_attention.cpp
View file @
6ca16d98
...
...
@@ -31,7 +31,6 @@ struct parse_attention : op_parser<parse_attention>
auto
input_lens
=
input
->
get_shape
().
lens
();
auto
batch_size
=
input_lens
.
at
(
0
);
auto
sequence_length
=
input_lens
.
at
(
1
);
auto
input_hidden_size
=
input_lens
.
at
(
2
);
// bias shape= (3 * hidden_size)
auto
bias_lens
=
bias
->
get_shape
().
lens
();
...
...
src/onnx/parse_layernorm.cpp
View file @
6ca16d98
...
...
@@ -18,7 +18,14 @@ struct parse_layernorm : op_parser<parse_layernorm>
onnx_parser
::
node_info
info
,
const
std
::
vector
<
instruction_ref
>&
args
)
const
{
float
epsilon
=
1e-3
f
;
// un-fuse layernorm op so migraphx can handle fusion instead
auto
x
=
args
.
front
();
auto
x_type
=
x
->
get_shape
().
type
();
auto
weights
=
args
.
at
(
1
);
auto
bias
=
args
.
at
(
2
);
float
epsilon
=
1e-12
f
;
int64_t
axis
=
-
1
;
if
(
contains
(
info
.
attributes
,
"epsilon"
))
{
...
...
@@ -28,17 +35,25 @@ struct parse_layernorm : op_parser<parse_layernorm>
{
epsilon
=
parser
.
parse_value
(
info
.
attributes
.
at
(
"axis"
)).
at
<
int64_t
>
();
}
auto
epsilon_lit
=
info
.
add_literal
(
literal
{
shape
{
x_type
,
{
1
}},
{
epsilon
}});
auto
exponent
=
info
.
add_literal
(
literal
{
shape
{
x_type
,
{
1
}},
{
2.0
}});
auto
dims
=
x
->
get_shape
().
lens
();
auto
layernorm
=
info
.
add_instruction
(
make_op
(
"layernorm"
,
{{
"epsilon"
,
epsilon
},
{
"axis"
,
axis
}}),
args
.
front
());
if
(
args
.
size
()
==
3
)
{
layernorm
=
info
.
add_broadcastable_binary_op
(
"mul"
,
layernorm
,
args
.
at
(
1
));
layernorm
=
info
.
add_broadcastable_binary_op
(
"add"
,
layernorm
,
args
.
at
(
2
));
}
auto
mean
=
info
.
add_instruction
(
migraphx
::
make_op
(
"reduce_mean"
,
{{
"axes"
,
{
axis
}}}),
x
);
auto
mean_mbcast
=
info
.
add_instruction
(
migraphx
::
make_op
(
"multibroadcast"
,
{{
"out_lens"
,
dims
}}),
mean
);
auto
sub
=
info
.
add_instruction
(
migraphx
::
make_op
(
"sub"
),
x
,
mean_mbcast
);
auto
exponent_mbcast
=
info
.
add_instruction
(
migraphx
::
make_op
(
"multibroadcast"
,
{{
"out_lens"
,
dims
}}),
exponent
);
auto
pow
=
info
.
add_instruction
(
migraphx
::
make_op
(
"pow"
),
sub
,
exponent_mbcast
);
auto
var
=
info
.
add_instruction
(
migraphx
::
make_op
(
"reduce_mean"
,
{{
"axes"
,
{
axis
}}}),
pow
);
auto
add_epsilon
=
info
.
add_broadcastable_binary_op
(
"add"
,
var
,
epsilon_lit
);
auto
sqrt
=
info
.
add_instruction
(
migraphx
::
make_op
(
"sqrt"
),
add_epsilon
);
auto
div
=
info
.
add_broadcastable_binary_op
(
"div"
,
sub
,
sqrt
);
auto
mul
=
info
.
add_broadcastable_binary_op
(
"mul"
,
div
,
weights
);
return
layernorm
;
return
info
.
add_broadcastable_binary_op
(
"add"
,
mul
,
bias
)
;
}
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment