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
e46a6a52
Unverified
Commit
e46a6a52
authored
Aug 03, 2023
by
Umang Yadav
Committed by
GitHub
Aug 03, 2023
Browse files
Add use of implicit deps inside sorting function (#2005)
* use implicit deps for the sorting * use BFS for sorting program
parent
01d4ae09
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
42 additions
and
6 deletions
+42
-6
src/include/migraphx/module.hpp
src/include/migraphx/module.hpp
+10
-0
src/module.cpp
src/module.cpp
+9
-1
src/program.cpp
src/program.cpp
+13
-4
test/module_test.cpp
test/module_test.cpp
+10
-1
No files found.
src/include/migraphx/module.hpp
View file @
e46a6a52
...
...
@@ -222,7 +222,17 @@ struct MIGRAPHX_EXPORT module
void
annotate
(
std
::
ostream
&
os
,
std
::
function
<
void
(
instruction_ref
)
>
a
)
const
;
std
::
vector
<
module_ref
>
get_sub_modules
(
bool
shallow
=
false
)
const
;
/* sorts the module in topological order aka reverse-post order (RPO) DFS order
it takes last instruction or @return as the root and walks back the graph and moves inputs
of the each instruction such that it appears before the instruction itself.
*/
module
&
sort
();
/* Any instruction "X" can have module arguments and those modules inside them can use any other
* instruction "Y" from predecessor modules of the instruction "X". Such instruction "Y" inside
* module args are not listed as input instructions to "X". But those instructions "Y" must be
* evaluted before the instruction "X" can. Therefore such "Y" instructions are considered
* implicit dependency to "X".
*/
ins_dep_map
calc_implicit_deps
()
const
;
MIGRAPHX_EXPORT
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
module
&
m
);
...
...
src/module.cpp
View file @
e46a6a52
...
...
@@ -1011,9 +1011,17 @@ std::vector<module_ref> module::get_sub_modules(bool shallow) const
module
&
module
::
sort
()
{
auto
implicit_deps
=
calc_implicit_deps
();
fix
([
&
](
auto
self
,
auto
ins
)
{
this
->
move_instruction
(
ins
,
this
->
begin
());
for
(
auto
child
:
ins
->
inputs
())
auto
ins_inputs
=
ins
->
inputs
();
if
(
implicit_deps
.
find
(
ins
)
!=
implicit_deps
.
end
())
{
auto
ins_implict_inputs
=
implicit_deps
.
at
(
ins
);
ins_inputs
.
insert
(
ins_inputs
.
end
(),
ins_implict_inputs
.
begin
(),
ins_implict_inputs
.
end
());
}
for
(
auto
child
:
ins_inputs
)
{
if
(
not
contains
(
this
->
impl
->
instructions
,
child
))
{
...
...
src/program.cpp
View file @
e46a6a52
...
...
@@ -40,13 +40,14 @@
#include <migraphx/make_op.hpp>
#include <migraphx/marker.hpp>
#include <migraphx/supported_segments.hpp>
#include <iostream>
#include <queue>
#include <sstream>
#include <algorithm>
#include <set>
#include <unordered_map>
#include <utility>
#include <unordered_set>
#include <map>
#include <cassert>
...
...
@@ -1191,11 +1192,19 @@ void program::remove_unused_modules()
program
&
program
::
sort
()
{
for
(
auto
&
pp
:
this
->
impl
->
modules
)
std
::
queue
<
migraphx
::
module_ref
>
mqueue
;
mqueue
.
push
(
get_main_module
());
while
(
not
mqueue
.
empty
())
{
pp
.
second
.
sort
();
module_ref
current_mod
=
mqueue
.
front
();
current_mod
->
sort
();
mqueue
.
pop
();
auto
child_mods
=
current_mod
->
get_sub_modules
(
true
);
for
(
auto
&
sub_mod
:
child_mods
)
{
mqueue
.
push
(
sub_mod
);
}
}
return
*
this
;
}
...
...
test/module_test.cpp
View file @
e46a6a52
...
...
@@ -83,7 +83,7 @@ TEST_CASE(calc_implict_deps)
auto
*
else_mod
=
p
.
create_module
(
"If_5_else"
);
auto
l2
=
else_mod
->
add_literal
(
migraphx
::
literal
(
ys
,
datay
));
auto
a2
=
else_mod
->
add_instruction
(
migraphx
::
make_op
(
"if"
),
{
cond
},
{
then_mod1
,
else_mod1
});
auto
a3
=
mm
->
add_instruction
(
migraphx
::
make_op
(
"get_tuple_elem"
,
{{
"index"
,
0
}}),
a2
);
auto
a3
=
else_mod
->
add_instruction
(
migraphx
::
make_op
(
"get_tuple_elem"
,
{{
"index"
,
0
}}),
a2
);
else_mod
->
add_return
({
a3
,
l2
});
auto
ret
=
mm
->
add_instruction
(
migraphx
::
make_op
(
"if"
),
{
cond
},
{
then_mod
,
else_mod
});
...
...
@@ -95,6 +95,15 @@ TEST_CASE(calc_implict_deps)
EXPECT
(
migraphx
::
contains
(
implicit_deps
.
at
(
ret
),
x1
));
EXPECT
(
migraphx
::
contains
(
implicit_deps
.
at
(
ret
),
x2
));
EXPECT
(
migraphx
::
contains
(
implicit_deps
.
at
(
ret
),
y2
));
EXPECT
(
migraphx
::
contains
(
implicit_deps
.
at
(
ret
),
lx
));
EXPECT
(
migraphx
::
contains
(
implicit_deps
.
at
(
ret
),
ly
));
// test for sorting
p
.
sort
();
auto
ret_inputs
=
ret
->
inputs
();
ret_inputs
.
insert
(
ret_inputs
.
end
(),
implicit_deps
.
at
(
ret
).
begin
(),
implicit_deps
.
at
(
ret
).
end
());
EXPECT
(
std
::
all_of
(
ret_inputs
.
begin
(),
ret_inputs
.
end
(),
[
&
](
const
auto
i
)
{
return
std
::
distance
(
mm
->
begin
(),
i
)
<
std
::
distance
(
mm
->
begin
(),
ret
);
}));
}
TEST_CASE
(
module_annotate
)
...
...
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