Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
gaoqiong
MIGraphX
Commits
00df057a
Commit
00df057a
authored
Jun 06, 2022
by
Paul
Browse files
Rewrite dot add for better effeciency
parent
7271ddbc
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
58 additions
and
11 deletions
+58
-11
src/simplify_algebra.cpp
src/simplify_algebra.cpp
+58
-11
No files found.
src/simplify_algebra.cpp
View file @
00df057a
...
@@ -185,6 +185,43 @@ struct find_mul_add
...
@@ -185,6 +185,43 @@ struct find_mul_add
}
}
};
};
struct
find_dot_add
{
auto
matcher
()
const
{
return
match
::
name
(
"dot"
)(
match
::
either_arg
(
0
,
1
)(
match
::
name
(
"add"
)(
match
::
either_arg
(
0
,
1
)(
match
::
any
().
bind
(
"x"
),
match
::
any_of
(
match
::
is_constant
()).
bind
(
"b"
)),
match
::
none_of
(
match
::
args
(
match
::
is_constant
(),
match
::
is_constant
())),
match
::
used_once
()),
match
::
is_constant
().
bind
(
"a"
)));
}
void
apply
(
module
&
m
,
const
match
::
matcher_result
&
r
)
const
{
auto
ins
=
r
.
result
;
auto
a_ins
=
r
.
instructions
[
"a"
];
auto
b_ins
=
r
.
instructions
[
"b"
];
auto
x_ins
=
r
.
instructions
[
"x"
];
assert
(
x_ins
!=
b_ins
);
const
bool
flipped
=
a_ins
==
ins
->
inputs
().
back
();
auto
insert_dot
=
[
&
](
auto
x
,
auto
y
)
{
if
(
flipped
)
return
m
.
insert_instruction
(
ins
,
make_op
(
"dot"
),
y
,
x
);
else
return
m
.
insert_instruction
(
ins
,
make_op
(
"dot"
),
x
,
y
);
};
auto
ax_ins
=
insert_dot
(
a_ins
,
x_ins
);
auto
ab_ins
=
insert_dot
(
a_ins
,
b_ins
);
m
.
replace_instruction
(
ins
,
make_op
(
"add"
),
ax_ins
,
ab_ins
);
}
};
struct
find_add_lit_broadcast
struct
find_add_lit_broadcast
{
{
auto
matcher
()
const
auto
matcher
()
const
...
@@ -247,25 +284,34 @@ struct find_inner_broadcast
...
@@ -247,25 +284,34 @@ struct find_inner_broadcast
auto
matcher
()
const
auto
matcher
()
const
{
{
return
pointwise
(
return
pointwise
(
match
::
nargs
(
2
),
match
::
all_of
[
match
::
inputs
()](
match
::
broadcast_shape
(),
match
::
name
(
"broadcast"
,
"multibroadcast"
)));
match
::
args
(
match
::
name
(
"broadcast"
).
bind
(
"x"
),
match
::
name
(
"broadcast"
).
bind
(
"y"
)));
}
}
void
apply
(
module
&
m
,
const
match
::
matcher_result
&
r
)
const
void
apply
(
module
&
m
,
const
match
::
matcher_result
&
r
)
const
{
{
auto
ins
=
r
.
result
;
auto
ins
=
r
.
result
;
auto
x_ins
=
r
.
instructions
[
"x"
];
auto
inputs
=
ins
->
inputs
();
auto
y_ins
=
r
.
instructions
[
"y"
];
if
(
inputs
.
empty
())
return
;
auto
xbroadcast
=
any_cast
<
op
::
broadcast
>
(
x_ins
->
get_operator
());
std
::
transform
(
inputs
.
begin
(),
inputs
.
end
(),
inputs
.
begin
(),
[
&
](
auto
i
)
{
auto
ybroadcast
=
any_cast
<
op
::
broadcast
>
(
y_ins
->
get_operator
());
if
(
contains
({
"broadcast"
,
"multibroadcast"
},
i
->
name
()))
return
i
->
inputs
().
front
();
else
return
i
;
});
if
(
xbroadcast
.
axis
!=
ybroadcast
.
axis
)
if
(
not
std
::
all_of
(
inputs
.
begin
(),
inputs
.
end
(),
[
&
](
auto
&
x
)
{
return
x
->
get_shape
()
==
inputs
.
front
()
->
get_shape
();
}))
return
;
return
;
auto
op
=
m
.
insert_instruction
(
auto
op
=
m
.
insert_instruction
(
ins
,
ins
->
get_operator
(),
inputs
);
ins
,
ins
->
get_operator
(),
x_ins
->
inputs
().
front
(),
y_ins
->
inputs
().
front
());
auto
bop
=
std
::
find_if
(
ins
->
inputs
().
begin
(),
ins
->
inputs
().
end
(),
[
&
](
auto
i
)
{
m
.
replace_instruction
(
ins
,
xbroadcast
,
op
);
return
contains
({
"broadcast"
,
"multibroadcast"
},
i
->
name
());
});
assert
(
bop
!=
ins
->
inputs
().
end
());
m
.
replace_instruction
(
ins
,
(
*
bop
)
->
get_operator
(),
op
);
}
}
};
};
...
@@ -1025,6 +1071,7 @@ void simplify_algebra::apply(module& m) const
...
@@ -1025,6 +1071,7 @@ void simplify_algebra::apply(module& m) const
find_mul_conv
{},
find_mul_conv
{},
find_mul_slice_conv
{},
find_mul_slice_conv
{},
find_mul_add
{},
find_mul_add
{},
find_dot_add
{},
find_div_const
{},
find_div_const
{},
find_sub_const
{},
find_sub_const
{},
find_rsqrt
{},
find_rsqrt
{},
...
...
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