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
a855a3ad
Commit
a855a3ad
authored
Mar 22, 2018
by
Nick Korovaiko
Committed by
adstraw
Mar 22, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make MatMulBias aware of addition commutativity (#713)
* make matmulbias callback aware that addition is commutative
parent
d73f92c4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
2 deletions
+44
-2
matcher.hpp
src/ngraph/pattern/matcher.hpp
+21
-0
cpu_fusion.cpp
src/ngraph/runtime/cpu/pass/cpu_fusion.cpp
+2
-2
cpu_fusion.cpp
test/cpu_fusion.cpp
+21
-0
No files found.
src/ngraph/pattern/matcher.hpp
View file @
a855a3ad
...
...
@@ -63,6 +63,27 @@ namespace ngraph
/// \param graph_node is an input graph to be matched against
bool
match
(
const
std
::
shared_ptr
<
Node
>&
graph_node
);
template
<
typename
T
>
static
std
::
shared_ptr
<
T
>
unique_match
(
std
::
shared_ptr
<
Node
>
node
)
{
std
::
shared_ptr
<
T
>
matched
;
for
(
auto
arg
:
node
->
get_input_ops
())
{
if
(
auto
t_casted
=
std
::
dynamic_pointer_cast
<
T
>
(
arg
))
{
if
(
matched
)
{
throw
ngraph_error
(
"There's more than two arguments of the same type"
);
}
else
{
matched
=
t_casted
;
}
}
}
return
matched
;
}
bool
process_match
(
gr_callback_fn
callback
=
nullptr
);
void
reset
()
{}
...
...
src/ngraph/runtime/cpu/pass/cpu_fusion.cpp
View file @
a855a3ad
...
...
@@ -138,8 +138,8 @@ void ngraph::runtime::cpu::pass::CPUFusion::construct_matmulbias_pattern()
<<
m
.
match_root
()
->
get_name
();
auto
mpattern
=
m
.
match_root
();
//add
auto
m_matmul
=
std
::
dynamic_pointer_cast
<
op
::
MatmulBias
>
(
mpattern
->
get_input_op
(
0
)
);
auto
m_broadcast
=
std
::
dynamic_pointer_cast
<
op
::
Broadcast
>
(
mpattern
->
get_input_op
(
1
)
);
auto
m_matmul
=
ngraph
::
pattern
::
Matcher
::
unique_match
<
op
::
MatmulBias
>
(
mpattern
);
auto
m_broadcast
=
ngraph
::
pattern
::
Matcher
::
unique_match
<
op
::
Broadcast
>
(
mpattern
);
auto
m_bias
=
m_broadcast
->
get_input_op
(
0
);
auto
pattern_map
=
m
.
get_pattern_map
();
...
...
test/cpu_fusion.cpp
View file @
a855a3ad
...
...
@@ -269,6 +269,27 @@ TEST(cpu_fusion, cpu_fusion_pass_basic)
ASSERT_NE
(
std
::
dynamic_pointer_cast
<
op
::
MatmulBias
>
(
graph
->
get_input_op
(
0
)),
nullptr
);
}
TEST
(
cpu_fusion
,
commutative_matmul_bias
)
{
Shape
shape
{};
Shape
shape_w
{
2
,
4
};
Shape
shape_x
{
4
,
1
};
Shape
shape_b
{
1
};
auto
A
=
make_shared
<
op
::
Parameter
>
(
element
::
f32
,
shape_w
);
auto
B
=
make_shared
<
op
::
Parameter
>
(
element
::
f32
,
shape_x
);
auto
C
=
make_shared
<
op
::
Parameter
>
(
element
::
f32
,
shape_b
);
auto
dot
=
make_shared
<
op
::
Dot
>
(
A
,
B
);
auto
broadcast
=
make_shared
<
op
::
Broadcast
>
(
C
,
dot
->
get_shape
(),
AxisSet
{
0
});
auto
add
=
broadcast
+
dot
;
auto
graph
=
make_shared
<
op
::
Abs
>
(
add
);
pass
::
Manager
pass_manager
;
pass_manager
.
register_pass
<
runtime
::
cpu
::
pass
::
CPUFusion
>
();
auto
func
=
make_shared
<
Function
>
(
graph
,
op
::
ParameterVector
{
A
,
B
,
C
});
pass_manager
.
run_passes
(
func
);
ASSERT_NE
(
std
::
dynamic_pointer_cast
<
op
::
MatmulBias
>
(
graph
->
get_input_op
(
0
)),
nullptr
);
}
TEST
(
cpu_fusion
,
cpu_fusion_pass_matmul_bias
)
{
Shape
shape_w
{
2
,
4
};
...
...
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