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
aa0b6230
Commit
aa0b6230
authored
Aug 24, 2018
by
Paul
Browse files
Enable bn rewrite and check for literals
parent
5ff89419
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
47 deletions
+58
-47
src/fwd_conv_batchnorm_rewrite.cpp
src/fwd_conv_batchnorm_rewrite.cpp
+50
-47
src/include/migraph/instruction.hpp
src/include/migraph/instruction.hpp
+5
-0
src/targets/gpu/target.cpp
src/targets/gpu/target.cpp
+3
-0
No files found.
src/fwd_conv_batchnorm_rewrite.cpp
View file @
aa0b6230
...
@@ -10,54 +10,57 @@ void fwd_conv_batchnorm_rewrite::apply(program& p) const
...
@@ -10,54 +10,57 @@ void fwd_conv_batchnorm_rewrite::apply(program& p) const
{
{
for
(
auto
ins
:
iterator_for
(
p
))
for
(
auto
ins
:
iterator_for
(
p
))
{
{
if
(
ins
->
op
.
name
()
==
"batch_norm_inference"
)
if
(
ins
->
op
.
name
()
!=
"batch_norm_inference"
)
{
continue
;
auto
ins_prev
=
ins
->
arguments
[
0
];
if
(
not
std
::
all_of
(
ins
->
arguments
.
begin
()
+
1
,
ins
->
arguments
.
end
(),
[](
auto
arg
)
{
return
arg
->
op
.
name
()
==
"@literal"
;
}))
if
(
ins_prev
->
op
.
name
()
==
"convolution"
)
continue
;
{
// Get scale, bias, mean, variance from instruction_ref
auto
conv_ins
=
ins
->
arguments
[
0
];
auto
gamma
=
ins
->
arguments
[
1
]
->
lit
;
if
(
conv_ins
->
op
.
name
()
!=
"convolution"
)
auto
bias
=
ins
->
arguments
[
2
]
->
lit
;
continue
;
auto
mean
=
ins
->
arguments
[
3
]
->
lit
;
if
(
conv_ins
->
arguments
[
1
]
->
op
.
name
()
!=
"@literal"
)
auto
variance
=
ins
->
arguments
[
4
]
->
lit
;
continue
;
// Get epsilon
// Get scale, bias, mean, variance from instruction_ref
auto
bn_op
=
any_cast
<
batch_norm_inference
>
(
ins
->
op
);
const
auto
&
gamma
=
ins
->
arguments
[
1
]
->
get_literal
();
auto
epsilon
=
bn_op
.
epsilon
;
const
auto
&
bias
=
ins
->
arguments
[
2
]
->
get_literal
();
// Get convolution weights
const
auto
&
mean
=
ins
->
arguments
[
3
]
->
get_literal
();
auto
weights
=
ins_prev
->
arguments
[
1
]
->
lit
;
const
auto
&
variance
=
ins
->
arguments
[
4
]
->
get_literal
();
// Get convolution op
// Get epsilon
auto
conv_op
=
ins_prev
->
op
;
auto
bn_op
=
any_cast
<
batch_norm_inference
>
(
ins
->
op
);
auto
out_channels
=
weights
.
get_shape
().
lens
()[
0
];
auto
epsilon
=
bn_op
.
epsilon
;
auto
in_channels
=
weights
.
get_shape
().
lens
()[
1
];
// Get convolution weights
auto
height
=
weights
.
get_shape
().
lens
()[
2
];
const
auto
&
weights
=
conv_ins
->
arguments
[
1
]
->
get_literal
();
auto
width
=
weights
.
get_shape
().
lens
()[
3
];
// Get convolution op
argument
new_weights
{
weights
.
get_shape
()};
auto
conv_op
=
conv_ins
->
op
;
argument
new_bias
{
bias
.
get_shape
()};
auto
out_channels
=
weights
.
get_shape
().
lens
()[
0
];
visit_all
(
weights
,
gamma
,
bias
,
mean
,
variance
,
new_weights
,
new_bias
)(
auto
in_channels
=
weights
.
get_shape
().
lens
()[
1
];
[
&
](
auto
weights2
,
auto
height
=
weights
.
get_shape
().
lens
()[
2
];
auto
gamma2
,
auto
width
=
weights
.
get_shape
().
lens
()[
3
];
auto
bias2
,
argument
new_weights
{
weights
.
get_shape
()};
auto
mean2
,
argument
new_bias
{
bias
.
get_shape
()};
auto
variance2
,
visit_all
(
weights
,
gamma
,
bias
,
mean
,
variance
,
new_weights
,
new_bias
)(
auto
new_weights2
,
[
&
](
auto
weights2
,
auto
new_bias2
)
{
auto
gamma2
,
dfor
(
out_channels
,
in_channels
,
height
,
width
)(
auto
bias2
,
[
&
](
std
::
size_t
k
,
std
::
size_t
c
,
std
::
size_t
h
,
std
::
size_t
w
)
{
auto
mean2
,
new_weights2
(
k
,
c
,
h
,
w
)
=
gamma2
(
k
)
/
auto
variance2
,
std
::
sqrt
(
variance2
(
k
)
+
epsilon
)
*
auto
new_weights2
,
weights2
(
k
,
c
,
h
,
w
);
auto
new_bias2
)
{
new_bias2
(
k
,
c
,
h
,
w
)
=
dfor
(
out_channels
,
in_channels
,
height
,
width
)(
bias2
(
k
)
-
(
mean2
(
k
)
/
std
::
sqrt
(
variance2
(
k
)
+
epsilon
));
[
&
](
std
::
size_t
k
,
std
::
size_t
c
,
std
::
size_t
h
,
std
::
size_t
w
)
{
});
new_weights2
(
k
,
c
,
h
,
w
)
=
gamma2
(
k
)
/
std
::
sqrt
(
variance2
(
k
)
+
epsilon
)
*
weights2
(
k
,
c
,
h
,
w
);
new_bias2
(
k
,
c
,
h
,
w
)
=
bias2
(
k
)
-
(
mean2
(
k
)
/
std
::
sqrt
(
variance2
(
k
)
+
epsilon
));
});
});
// Replace convolution instruction with updated weights
});
auto
l_weights
=
p
.
add_literal
({
weights
.
get_shape
(),
new_weights
.
data
()});
// Replace convolution instruction with updated weights
auto
l_bias
=
p
.
add_literal
({
bias
.
get_shape
(),
new_bias
.
data
()});
auto
l_weights
=
p
.
add_literal
({
weights
.
get_shape
(),
new_weights
.
data
()});
auto
c
=
auto
l_bias
=
p
.
add_literal
({
bias
.
get_shape
(),
new_bias
.
data
()});
p
.
replace_instruction
(
ins_prev
,
conv_op
,
{
ins_prev
->
arguments
[
0
],
l_weights
});
auto
c
=
p
.
replace_instruction
(
ins
,
add
{},
{
c
,
l_bias
});
p
.
replace_instruction
(
conv_ins
,
conv_op
,
{
conv_ins
->
arguments
[
0
],
l_weights
});
}
p
.
replace_instruction
(
ins
,
add
{},
{
c
,
l_bias
});
}
}
}
}
}
}
// namespace migraph
}
// namespace migraph
src/include/migraph/instruction.hpp
View file @
aa0b6230
...
@@ -115,6 +115,11 @@ struct instruction
...
@@ -115,6 +115,11 @@ struct instruction
}
}
shape
get_shape
()
const
{
return
result
;
}
shape
get_shape
()
const
{
return
result
;
}
const
literal
&
get_literal
()
const
{
assert
(
op
.
name
()
==
"@literal"
);
return
lit
;
}
friend
bool
operator
==
(
instruction_ref
ref
,
const
instruction
&
i
)
{
return
i
==
ref
;
}
friend
bool
operator
==
(
instruction_ref
ref
,
const
instruction
&
i
)
{
return
i
==
ref
;
}
...
...
src/targets/gpu/target.cpp
View file @
aa0b6230
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
#include <migraph/dead_code_elimination.hpp>
#include <migraph/dead_code_elimination.hpp>
#include <migraph/simplify_reshapes.hpp>
#include <migraph/simplify_reshapes.hpp>
#include <migraph/eliminate_contiguous.hpp>
#include <migraph/eliminate_contiguous.hpp>
#include <migraph/fwd_conv_batchnorm_rewrite.hpp>
namespace
migraph
{
namespace
migraph
{
namespace
gpu
{
namespace
gpu
{
...
@@ -24,6 +25,8 @@ std::vector<pass> target::get_passes(migraph::context& gctx) const
...
@@ -24,6 +25,8 @@ std::vector<pass> target::get_passes(migraph::context& gctx) const
auto_contiguous
{},
auto_contiguous
{},
simplify_reshapes
{},
simplify_reshapes
{},
dead_code_elimination
{},
dead_code_elimination
{},
fwd_conv_batchnorm_rewrite
{},
dead_code_elimination
{},
lowering
{
ctx
},
lowering
{
ctx
},
fuse_ops
{},
fuse_ops
{},
dead_code_elimination
{},
dead_code_elimination
{},
...
...
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