Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
N
ngraph
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
ngraph
Commits
0ad2a3dd
Commit
0ad2a3dd
authored
Jul 03, 2019
by
Amy Zhuang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add unit test.
Add shape check in fusion. Rename function.
parent
c95f0f47
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
73 additions
and
3 deletions
+73
-3
cpu_fusion.cpp
src/ngraph/runtime/cpu/pass/cpu_fusion.cpp
+9
-1
cpu_fusion.hpp
src/ngraph/runtime/cpu/pass/cpu_fusion.hpp
+2
-2
cpu_fusion.cpp
test/cpu_fusion.cpp
+62
-0
No files found.
src/ngraph/runtime/cpu/pass/cpu_fusion.cpp
View file @
0ad2a3dd
...
...
@@ -650,7 +650,7 @@ void ngraph::runtime::cpu::pass::CPUFusion::construct_batch_norm_relu_global_sta
this
->
add_matcher
(
m
,
callback
);
}
void
ngraph
::
runtime
::
cpu
::
pass
::
CPUFusion
::
construct_batch_norm_infer_relu_with_multi_add
()
void
ngraph
::
runtime
::
cpu
::
pass
::
CPUFusion
::
construct_batch_norm_infer_relu_with_multi
ply
_add
()
{
auto
input_shape
=
Shape
{
1
,
3
,
2
,
2
};
auto
input
=
std
::
make_shared
<
pattern
::
op
::
Label
>
(
element
::
f32
,
input_shape
);
...
...
@@ -704,6 +704,14 @@ void ngraph::runtime::cpu::pass::CPUFusion::construct_batch_norm_infer_relu_with
NGRAPH_DEBUG
<<
"Add isn't the only user of Multiply's output"
;
return
false
;
}
if
(
pattern_map
[
broadcast1_input
]
->
output
(
0
).
get_shape
()
!=
pattern_map
[
gamma
]
->
output
(
0
).
get_shape
()
||
pattern_map
[
broadcast2_input
]
->
output
(
0
).
get_shape
()
!=
pattern_map
[
gamma
]
->
output
(
0
).
get_shape
())
{
NGRAPH_DEBUG
<<
"shapes of Broadcast input and gamma do not match"
;
return
false
;
}
auto
new_gamma
=
std
::
make_shared
<
ngraph
::
op
::
Multiply
>
(
pattern_map
[
gamma
],
pattern_map
[
broadcast1_input
]);
...
...
src/ngraph/runtime/cpu/pass/cpu_fusion.hpp
View file @
0ad2a3dd
...
...
@@ -78,7 +78,7 @@ public:
construct_deconvolution_affine_folding_relu
();
}
construct_dropout
();
construct_batch_norm_infer_relu_with_multi_add
();
construct_batch_norm_infer_relu_with_multi
ply
_add
();
}
}
...
...
@@ -91,7 +91,7 @@ private:
void
construct_sigmoid_multiply
();
void
construct_batch_norm_relu
();
void
construct_batch_norm_relu_global_stats
();
void
construct_batch_norm_infer_relu_with_multi_add
();
void
construct_batch_norm_infer_relu_with_multi
ply
_add
();
void
construct_conv_relu
();
void
construct_conv_bias_relu
();
void
construct_conv_bias_add
();
...
...
test/cpu_fusion.cpp
View file @
0ad2a3dd
...
...
@@ -560,6 +560,68 @@ TEST(cpu_fusion, conv_bias_bprop)
ASSERT_EQ
(
ccg
,
1
);
}
TEST
(
cpu_fusion
,
batchnorm_multiply_add_relu
)
{
auto
input_shape
=
Shape
{
1
,
3
,
2
,
2
};
auto
make_bn_relu_function
=
[
&
]()
{
auto
c_axis
=
input_shape
[
1
];
auto
input
=
make_shared
<
op
::
Parameter
>
(
element
::
f32
,
input_shape
);
auto
mean_shape
=
Shape
{
c_axis
};
auto
mean
=
std
::
make_shared
<
op
::
Parameter
>
(
element
::
f32
,
mean_shape
);
auto
var_shape
=
Shape
{
c_axis
};
auto
var
=
std
::
make_shared
<
op
::
Parameter
>
(
element
::
f32
,
var_shape
);
auto
gamma_shape
=
Shape
{
c_axis
};
auto
gamma
=
make_shared
<
op
::
Parameter
>
(
element
::
f32
,
gamma_shape
);
auto
beta_shape
=
Shape
{
c_axis
};
auto
beta
=
make_shared
<
op
::
Parameter
>
(
element
::
f32
,
beta_shape
);
double
eps
=
0.001
;
auto
bn
=
std
::
make_shared
<
ngraph
::
op
::
BatchNormInference
>
(
eps
,
gamma
,
beta
,
input
,
mean
,
var
);
std
::
vector
<
size_t
>
vec
{
0
};
for
(
auto
i
=
2
;
i
<
input_shape
.
size
();
i
++
)
{
vec
.
push_back
(
i
);
}
auto
broadcast1_input
=
std
::
make_shared
<
op
::
Parameter
>
(
element
::
f32
,
gamma_shape
);
auto
broadcast1
=
std
::
make_shared
<
ngraph
::
op
::
Broadcast
>
(
broadcast1_input
,
input_shape
,
AxisSet
(
vec
));
auto
multiply
=
std
::
make_shared
<
ngraph
::
op
::
Multiply
>
(
bn
,
broadcast1
);
auto
broadcast2_input
=
std
::
make_shared
<
op
::
Parameter
>
(
element
::
f32
,
gamma_shape
);
auto
broadcast2
=
std
::
make_shared
<
ngraph
::
op
::
Broadcast
>
(
broadcast2_input
,
input_shape
,
AxisSet
(
vec
));
auto
add
=
std
::
make_shared
<
ngraph
::
op
::
Add
>
(
multiply
,
broadcast2
);
auto
relu
=
std
::
make_shared
<
ngraph
::
op
::
Relu
>
(
add
);
auto
f
=
make_shared
<
Function
>
(
relu
,
ParameterVector
{
gamma
,
beta
,
input
,
mean
,
var
,
broadcast1_input
,
broadcast2_input
});
return
f
;
};
auto
cpu_f
=
make_bn_relu_function
();
auto
int_f
=
make_bn_relu_function
();
test
::
Uniform
<
float
>
rng
(
-
10.0
f
,
10.0
f
);
vector
<
vector
<
float
>>
args
;
for
(
shared_ptr
<
op
::
Parameter
>
param
:
int_f
->
get_parameters
())
{
vector
<
float
>
tensor_val
(
shape_size
(
param
->
get_shape
()));
rng
.
initialize
(
tensor_val
);
args
.
push_back
(
tensor_val
);
}
auto
int_results
=
execute
(
int_f
,
args
,
"INTERPRETER"
);
auto
cpu_results
=
execute
(
cpu_f
,
args
,
"CPU"
);
for
(
size_t
i
=
0
;
i
<
cpu_results
.
size
();
i
++
)
{
EXPECT_TRUE
(
test
::
all_close
(
cpu_results
.
at
(
i
),
int_results
.
at
(
i
),
1.0e-4
f
,
1.0e-4
f
));
}
size_t
bn_relu
=
count_ops_of_type
<
op
::
BatchNormInferenceRelu
>
(
cpu_f
);
ASSERT_EQ
(
bn_relu
,
1
);
}
TEST
(
cpu_fusion
,
batchnorm_fprop_relu_b1c2h2w2
)
{
auto
input_shape
=
Shape
{
1
,
2
,
2
,
2
};
...
...
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