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
94ef1ad0
"...composable_kernel_rocm.git" did not exist on "24d996aae11c45430571ebc1ee428dc67fd2d91b"
Commit
94ef1ad0
authored
Oct 10, 2018
by
Paul
Browse files
Add const-prop pass
parent
32c19efa
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
133 additions
and
0 deletions
+133
-0
src/CMakeLists.txt
src/CMakeLists.txt
+1
-0
src/constant_propagate.cpp
src/constant_propagate.cpp
+31
-0
src/include/migraph/constant_propagate.hpp
src/include/migraph/constant_propagate.hpp
+18
-0
src/include/migraph/literal.hpp
src/include/migraph/literal.hpp
+14
-0
test/constant_propagate_test.cpp
test/constant_propagate_test.cpp
+69
-0
No files found.
src/CMakeLists.txt
View file @
94ef1ad0
add_library
(
migraph
add_library
(
migraph
auto_contiguous.cpp
auto_contiguous.cpp
constant_propagate.cpp
dead_code_elimination.cpp
dead_code_elimination.cpp
eliminate_allocation.cpp
eliminate_allocation.cpp
eliminate_contiguous.cpp
eliminate_contiguous.cpp
...
...
src/constant_propagate.cpp
0 → 100644
View file @
94ef1ad0
#include <migraph/constant_propagate.hpp>
#include <migraph/program.hpp>
#include <migraph/matcher.hpp>
#include <migraph/literal.hpp>
namespace
migraph
{
struct
match_const_add
{
auto
matcher
()
const
{
return
match
::
name
(
"add"
)(
match
::
args
(
match
::
name
(
"@literal"
),
match
::
name
(
"@literal"
)));
}
void
apply
(
program
&
p
,
match
::
matcher_result
r
)
const
{
auto
ins
=
r
.
result
;
auto
arg1
=
ins
->
inputs
().
at
(
0
)
->
get_literal
();
auto
arg2
=
ins
->
inputs
().
at
(
1
)
->
get_literal
();
auto
sum
=
p
.
add_literal
(
transform
(
arg1
,
arg2
,
[](
auto
x
,
auto
y
)
{
return
x
+
y
;
}));
p
.
replace_instruction
(
ins
,
sum
);
}
};
void
constant_propagate
::
apply
(
program
&
p
)
const
{
match
::
find_matches
(
p
,
match_const_add
{});
}
}
// namespace migraph
src/include/migraph/constant_propagate.hpp
0 → 100644
View file @
94ef1ad0
#ifndef MIGRAPH_GUARD_RTGLIB_CONSTANT_PROPAGATE_HPP
#define MIGRAPH_GUARD_RTGLIB_CONSTANT_PROPAGATE_HPP
#include <string>
namespace
migraph
{
struct
program
;
struct
constant_propagate
{
std
::
string
name
()
const
{
return
"constant_propagate"
;
}
void
apply
(
program
&
p
)
const
;
};
}
// namespace migraph
#endif
src/include/migraph/literal.hpp
View file @
94ef1ad0
...
@@ -108,6 +108,20 @@ literal transform(literal l, F f)
...
@@ -108,6 +108,20 @@ literal transform(literal l, F f)
return
result
;
return
result
;
}
}
template
<
class
F
>
literal
transform
(
literal
l1
,
literal
l2
,
F
f
)
{
assert
(
l1
.
get_shape
()
==
l2
.
get_shape
());
literal
result
;
visit_all
(
l1
,
l2
)([
&
](
auto
x
,
auto
y
)
{
using
type
=
std
::
remove_cv_t
<
typename
decltype
(
x
)
::
value_type
>
;
std
::
vector
<
type
>
output
(
x
.
size
(),
0.0
);
std
::
transform
(
x
.
begin
(),
x
.
end
(),
y
.
begin
(),
output
.
begin
(),
f
);
result
=
literal
{
l1
.
get_shape
(),
output
};
});
return
result
;
}
}
// namespace migraph
}
// namespace migraph
#endif
#endif
test/constant_propagate_test.cpp
0 → 100644
View file @
94ef1ad0
#include <migraph/constant_propagate.hpp>
#include <migraph/dead_code_elimination.hpp>
#include <migraph/operators.hpp>
#include <basic_ops.hpp>
#include <test.hpp>
struct
const_prop_target
{
std
::
string
name
()
const
{
return
"const_prop"
;
}
std
::
vector
<
migraph
::
pass
>
get_passes
(
migraph
::
context
&
)
const
{
return
{
migraph
::
constant_propagate
{},
migraph
::
dead_code_elimination
{}};
}
migraph
::
context
get_context
()
const
{
return
{};
}
};
void
const_add1
()
{
migraph
::
program
p1
;
auto
one
=
p1
.
add_literal
(
1
);
auto
two
=
p1
.
add_literal
(
2
);
auto
sum
=
p1
.
add_instruction
(
migraph
::
op
::
add
{},
one
,
two
);
p1
.
add_instruction
(
pass_op
{},
sum
);
p1
.
compile
(
const_prop_target
{});
migraph
::
program
p2
;
auto
total
=
p2
.
add_literal
(
3
);
p2
.
add_instruction
(
pass_op
{},
total
);
EXPECT
(
p1
==
p2
);
}
void
const_add2
()
{
migraph
::
program
p1
;
auto
one
=
p1
.
add_parameter
(
"one"
,
{
migraph
::
shape
::
int32_type
,
{
1
}});
auto
two
=
p1
.
add_literal
(
2
);
auto
sum
=
p1
.
add_instruction
(
migraph
::
op
::
add
{},
one
,
two
);
p1
.
add_instruction
(
pass_op
{},
sum
);
p1
.
compile
(
const_prop_target
{});
migraph
::
program
p2
;
auto
total
=
p2
.
add_literal
(
3
);
p2
.
add_instruction
(
pass_op
{},
total
);
EXPECT
(
p1
!=
p2
);
}
void
const_add3
()
{
migraph
::
program
p1
;
auto
one
=
p1
.
add_literal
(
1
);
auto
two
=
p1
.
add_literal
(
2
);
auto
sum1
=
p1
.
add_instruction
(
migraph
::
op
::
add
{},
one
,
two
);
auto
sum2
=
p1
.
add_instruction
(
migraph
::
op
::
add
{},
sum1
,
two
);
p1
.
add_instruction
(
pass_op
{},
sum2
);
p1
.
compile
(
const_prop_target
{});
migraph
::
program
p2
;
auto
total
=
p2
.
add_literal
(
5
);
p2
.
add_instruction
(
pass_op
{},
total
);
EXPECT
(
p1
==
p2
);
}
int
main
()
{
const_add1
();
const_add2
();
const_add3
();
}
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