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
df661763
Unverified
Commit
df661763
authored
Aug 01, 2019
by
Scott Cyphers
Committed by
GitHub
Aug 01, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' into aprocter/cf-dyn-broadcast
parents
e9e410aa
e57c0f0f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
158 additions
and
138 deletions
+158
-138
constant_folding.cpp
src/ngraph/pass/constant_folding.cpp
+158
-138
No files found.
src/ngraph/pass/constant_folding.cpp
View file @
df661763
...
...
@@ -142,6 +142,92 @@ shared_ptr<op::Constant> fold_constant_reshape(shared_ptr<op::Constant> constant
return
make_shared
<
op
::
Constant
>
(
constant
->
get_element_type
(),
out_shape
,
out_vec
);
}
void
pass
::
ConstantFolding
::
construct_constant_reshape
()
{
auto
constant_label
=
make_shared
<
pattern
::
op
::
Label
>
(
element
::
f32
,
Shape
{
2
,
4
},
pattern
::
has_class
<
op
::
Constant
>
());
auto
reshape
=
make_shared
<
op
::
Reshape
>
(
constant_label
,
AxisVector
{
0
,
1
},
Shape
{
2
,
4
,
1
});
auto
constant_reshape_callback
=
[
&
,
constant_label
](
pattern
::
Matcher
&
m
)
{
NGRAPH_DEBUG
<<
"In callback for constant_reshape_callback against node = "
<<
m
.
get_match_root
()
->
get_name
();
auto
pattern_map
=
m
.
get_pattern_map
();
auto
constant_match
=
static_pointer_cast
<
op
::
Constant
>
(
pattern_map
[
constant_label
]);
auto
reshape_match
=
static_pointer_cast
<
op
::
Reshape
>
(
m
.
get_match_root
());
NodeExecutorTy
func
=
nullptr
;
if
(
!
m_cfmap
.
empty
())
{
auto
handler
=
m_cfmap
.
find
(
type_index
(
typeid
(
ngraph
::
op
::
Reshape
)));
NGRAPH_CHECK
(
handler
!=
m_cfmap
.
end
(),
"constant folding map should have reshape entry"
);
func
=
handler
->
second
(
reshape_match
.
get
());
}
std
::
shared_ptr
<
Node
>
replacement
;
auto
type
=
constant_match
->
get_element_type
();
switch
(
type
.
get_type_enum
())
{
case
element
:
:
Type_t
::
undefined
:
NGRAPH_CHECK
(
false
,
"Encountered 'undefined' element type in constant_reshape_callback"
);
break
;
case
element
:
:
Type_t
::
dynamic
:
NGRAPH_CHECK
(
false
,
"Encountered 'dynamic' element type in constant_reshape_callback"
);
break
;
case
element
:
:
Type_t
::
boolean
:
replacement
=
fold_constant_reshape
<
char
>
(
constant_match
,
reshape_match
,
func
);
break
;
case
element
:
:
Type_t
::
bf16
:
replacement
=
fold_constant_reshape
<
bfloat16
>
(
constant_match
,
reshape_match
,
func
);
break
;
case
element
:
:
Type_t
::
f16
:
replacement
=
fold_constant_reshape
<
float16
>
(
constant_match
,
reshape_match
,
func
);
break
;
case
element
:
:
Type_t
::
f32
:
replacement
=
fold_constant_reshape
<
float
>
(
constant_match
,
reshape_match
,
func
);
break
;
case
element
:
:
Type_t
::
f64
:
replacement
=
fold_constant_reshape
<
double
>
(
constant_match
,
reshape_match
,
func
);
break
;
case
element
:
:
Type_t
::
i8
:
replacement
=
fold_constant_reshape
<
int8_t
>
(
constant_match
,
reshape_match
,
func
);
break
;
case
element
:
:
Type_t
::
i16
:
replacement
=
fold_constant_reshape
<
int16_t
>
(
constant_match
,
reshape_match
,
func
);
break
;
case
element
:
:
Type_t
::
i32
:
replacement
=
fold_constant_reshape
<
int32_t
>
(
constant_match
,
reshape_match
,
func
);
break
;
case
element
:
:
Type_t
::
i64
:
replacement
=
fold_constant_reshape
<
int64_t
>
(
constant_match
,
reshape_match
,
func
);
break
;
case
element
:
:
Type_t
::
u8
:
replacement
=
fold_constant_reshape
<
uint8_t
>
(
constant_match
,
reshape_match
,
func
);
break
;
case
element
:
:
Type_t
::
u16
:
replacement
=
fold_constant_reshape
<
uint16_t
>
(
constant_match
,
reshape_match
,
func
);
break
;
case
element
:
:
Type_t
::
u32
:
replacement
=
fold_constant_reshape
<
uint32_t
>
(
constant_match
,
reshape_match
,
func
);
break
;
case
element
:
:
Type_t
::
u64
:
replacement
=
fold_constant_reshape
<
uint64_t
>
(
constant_match
,
reshape_match
,
func
);
break
;
}
replace_node
(
m
.
get_match_root
(),
replacement
);
return
true
;
};
auto
reshape_matcher
=
make_shared
<
pattern
::
Matcher
>
(
reshape
,
"ConstantFolding.ConstantReshape"
);
this
->
add_matcher
(
reshape_matcher
,
constant_reshape_callback
,
PassProperty
::
REQUIRE_STATIC_SHAPE
);
}
template
<
class
T
>
shared_ptr
<
op
::
Constant
>
fold_constant_pad
(
shared_ptr
<
op
::
Constant
>
constant
,
shared_ptr
<
op
::
Pad
>
pad
,
...
...
@@ -208,123 +294,63 @@ void pass::ConstantFolding::construct_constant_pad()
func
=
handler
->
second
(
pad_match
.
get
());
}
auto
type
=
constant_match
->
get_element_type
();
if
(
type
==
element
::
i32
)
{
replace_node
(
m
.
get_match_root
(),
fold_constant_pad
<
int
>
(
constant_match
,
pad_match
,
func
));
return
true
;
}
else
if
(
type
==
element
::
i8
)
{
replace_node
(
m
.
get_match_root
(),
fold_constant_pad
<
int8_t
>
(
constant_match
,
pad_match
,
func
));
return
true
;
}
else
if
(
type
==
element
::
f32
)
{
replace_node
(
m
.
get_match_root
(),
fold_constant_pad
<
float
>
(
constant_match
,
pad_match
,
func
));
return
true
;
}
else
if
(
type
==
element
::
f64
)
{
replace_node
(
m
.
get_match_root
(),
fold_constant_pad
<
double
>
(
constant_match
,
pad_match
,
func
));
return
true
;
}
return
false
;
};
auto
pad_matcher
=
make_shared
<
pattern
::
Matcher
>
(
pad
,
"ConstantFolding.ConstantPad"
);
this
->
add_matcher
(
pad_matcher
,
constant_pad_callback
,
PassProperty
::
REQUIRE_STATIC_SHAPE
);
}
void
pass
::
ConstantFolding
::
construct_constant_reshape
()
{
auto
constant_label
=
make_shared
<
pattern
::
op
::
Label
>
(
element
::
f32
,
Shape
{
2
,
4
},
pattern
::
has_class
<
op
::
Constant
>
());
auto
reshape
=
make_shared
<
op
::
Reshape
>
(
constant_label
,
AxisVector
{
0
,
1
},
Shape
{
2
,
4
,
1
});
auto
constant_reshape_callback
=
[
&
,
constant_label
](
pattern
::
Matcher
&
m
)
{
NGRAPH_DEBUG
<<
"In callback for constant_reshape_callback against node = "
<<
m
.
get_match_root
()
->
get_name
();
auto
pattern_map
=
m
.
get_pattern_map
();
auto
constant_match
=
static_pointer_cast
<
op
::
Constant
>
(
pattern_map
[
constant_label
]);
auto
reshape_match
=
static_pointer_cast
<
op
::
Reshape
>
(
m
.
get_match_root
());
NodeExecutorTy
func
=
nullptr
;
if
(
!
m_cfmap
.
empty
())
{
auto
handler
=
m_cfmap
.
find
(
type_index
(
typeid
(
ngraph
::
op
::
Reshape
)));
NGRAPH_CHECK
(
handler
!=
m_cfmap
.
end
(),
"constant folding map should have reshape entry"
);
func
=
handler
->
second
(
reshape_match
.
get
());
}
std
::
shared_ptr
<
Node
>
replacement
;
auto
type
=
constant_match
->
get_element_type
();
switch
(
type
.
get_type_enum
())
{
case
element
:
:
Type_t
::
undefined
:
NGRAPH_CHECK
(
false
,
"Encountered 'undefined' element type in constant_reshape_callback"
);
NGRAPH_CHECK
(
false
,
"Encountered 'undefined' element type in constant_pad_callback"
);
break
;
case
element
:
:
Type_t
::
dynamic
:
NGRAPH_CHECK
(
false
,
"Encountered 'dynamic' element type in constant_
reshape
_callback"
);
NGRAPH_CHECK
(
false
,
"Encountered 'dynamic' element type in constant_
pad
_callback"
);
break
;
case
element
:
:
Type_t
::
boolean
:
replacement
=
fold_constant_
reshape
<
char
>
(
constant_match
,
reshape
_match
,
func
);
replacement
=
fold_constant_
pad
<
char
>
(
constant_match
,
pad
_match
,
func
);
break
;
case
element
:
:
Type_t
::
bf16
:
replacement
=
fold_constant_
reshape
<
bfloat16
>
(
constant_match
,
reshape
_match
,
func
);
replacement
=
fold_constant_
pad
<
bfloat16
>
(
constant_match
,
pad
_match
,
func
);
break
;
case
element
:
:
Type_t
::
f16
:
replacement
=
fold_constant_
reshape
<
float16
>
(
constant_match
,
reshape
_match
,
func
);
replacement
=
fold_constant_
pad
<
float16
>
(
constant_match
,
pad
_match
,
func
);
break
;
case
element
:
:
Type_t
::
f32
:
replacement
=
fold_constant_
reshape
<
float
>
(
constant_match
,
reshape
_match
,
func
);
replacement
=
fold_constant_
pad
<
float
>
(
constant_match
,
pad
_match
,
func
);
break
;
case
element
:
:
Type_t
::
f64
:
replacement
=
fold_constant_
reshape
<
double
>
(
constant_match
,
reshape
_match
,
func
);
replacement
=
fold_constant_
pad
<
double
>
(
constant_match
,
pad
_match
,
func
);
break
;
case
element
:
:
Type_t
::
i8
:
replacement
=
fold_constant_
reshape
<
int8_t
>
(
constant_match
,
reshape
_match
,
func
);
replacement
=
fold_constant_
pad
<
int8_t
>
(
constant_match
,
pad
_match
,
func
);
break
;
case
element
:
:
Type_t
::
i16
:
replacement
=
fold_constant_
reshape
<
int16_t
>
(
constant_match
,
reshape
_match
,
func
);
replacement
=
fold_constant_
pad
<
int16_t
>
(
constant_match
,
pad
_match
,
func
);
break
;
case
element
:
:
Type_t
::
i32
:
replacement
=
fold_constant_
reshape
<
int32_t
>
(
constant_match
,
reshape
_match
,
func
);
replacement
=
fold_constant_
pad
<
int32_t
>
(
constant_match
,
pad
_match
,
func
);
break
;
case
element
:
:
Type_t
::
i64
:
replacement
=
fold_constant_
reshape
<
int64_t
>
(
constant_match
,
reshape
_match
,
func
);
replacement
=
fold_constant_
pad
<
int64_t
>
(
constant_match
,
pad
_match
,
func
);
break
;
case
element
:
:
Type_t
::
u8
:
replacement
=
fold_constant_
reshape
<
uint8_t
>
(
constant_match
,
reshape
_match
,
func
);
replacement
=
fold_constant_
pad
<
uint8_t
>
(
constant_match
,
pad
_match
,
func
);
break
;
case
element
:
:
Type_t
::
u16
:
replacement
=
fold_constant_
reshape
<
uint16_t
>
(
constant_match
,
reshape
_match
,
func
);
replacement
=
fold_constant_
pad
<
uint16_t
>
(
constant_match
,
pad
_match
,
func
);
break
;
case
element
:
:
Type_t
::
u32
:
replacement
=
fold_constant_
reshape
<
uint32_t
>
(
constant_match
,
reshape
_match
,
func
);
replacement
=
fold_constant_
pad
<
uint32_t
>
(
constant_match
,
pad
_match
,
func
);
break
;
case
element
:
:
Type_t
::
u64
:
replacement
=
fold_constant_
reshape
<
uint64_t
>
(
constant_match
,
reshape
_match
,
func
);
replacement
=
fold_constant_
pad
<
uint64_t
>
(
constant_match
,
pad
_match
,
func
);
break
;
}
replace_node
(
m
.
get_match_root
(),
replacement
);
return
fals
e
;
return
tru
e
;
};
auto
reshape_matcher
=
make_shared
<
pattern
::
Matcher
>
(
reshape
,
"ConstantFolding.ConstantReshape"
);
this
->
add_matcher
(
reshape_matcher
,
constant_reshape_callback
,
PassProperty
::
REQUIRE_STATIC_SHAPE
);
auto
pad_matcher
=
make_shared
<
pattern
::
Matcher
>
(
pad
,
"ConstantFolding.ConstantPad"
);
this
->
add_matcher
(
pad_matcher
,
constant_pad_callback
,
PassProperty
::
REQUIRE_STATIC_SHAPE
);
}
template
<
class
T
>
...
...
@@ -431,7 +457,7 @@ void pass::ConstantFolding::construct_constant_dyn_reshape()
}
replace_node
(
m
.
get_match_root
(),
replacement
);
return
fals
e
;
return
tru
e
;
};
auto
dyn_reshape_matcher
=
...
...
@@ -547,7 +573,7 @@ void pass::ConstantFolding::construct_constant_transpose()
}
replace_node
(
m
.
get_match_root
(),
replacement
);
return
fals
e
;
return
tru
e
;
};
auto
transpose_matcher
=
...
...
@@ -610,40 +636,61 @@ void pass::ConstantFolding::construct_constant_broadcast()
func
=
handler
->
second
(
broadcast_match
.
get
());
}
auto
type
=
constant_match
->
get_element_type
();
if
(
type
==
element
::
i32
)
{
replace_node
(
m
.
get_match_root
(),
fold_constant_broadcast
<
int
>
(
constant_match
,
broadcast_match
,
func
));
return
true
;
}
else
if
(
type
==
element
::
i8
)
{
replace_node
(
m
.
get_match_root
(),
fold_constant_broadcast
<
int8_t
>
(
constant_match
,
broadcast_match
,
func
));
return
true
;
}
else
if
(
type
==
element
::
f32
)
{
replace_node
(
m
.
get_match_root
(),
fold_constant_broadcast
<
float
>
(
constant_match
,
broadcast_match
,
func
));
return
true
;
}
else
if
(
type
==
element
::
f64
)
{
replace_node
(
m
.
get_match_root
(),
fold_constant_broadcast
<
double
>
(
constant_match
,
broadcast_match
,
func
));
return
true
;
}
else
if
(
type
==
element
::
bf16
)
std
::
shared_ptr
<
Node
>
replacement
;
auto
type
=
broadcast_match
->
get_element_type
();
switch
(
type
.
get_type_enum
())
{
replace_node
(
m
.
get_match_root
(),
fold_constant_broadcast
<
ngraph
::
bfloat16
>
(
constant_match
,
broadcast_match
,
func
));
return
true
;
case
element
:
:
Type_t
::
undefined
:
NGRAPH_CHECK
(
false
,
"Encountered 'undefined' element type in constant_broadcast_callback"
);
break
;
case
element
:
:
Type_t
::
dynamic
:
NGRAPH_CHECK
(
false
,
"Encountered 'dynamic' element type in constant_broadcast_callback"
);
break
;
case
element
:
:
Type_t
::
boolean
:
replacement
=
fold_constant_broadcast
<
char
>
(
constant_match
,
broadcast_match
,
func
);
break
;
case
element
:
:
Type_t
::
bf16
:
replacement
=
fold_constant_broadcast
<
bfloat16
>
(
constant_match
,
broadcast_match
,
func
);
break
;
case
element
:
:
Type_t
::
f16
:
replacement
=
fold_constant_broadcast
<
float16
>
(
constant_match
,
broadcast_match
,
func
);
break
;
case
element
:
:
Type_t
::
f32
:
replacement
=
fold_constant_broadcast
<
float
>
(
constant_match
,
broadcast_match
,
func
);
break
;
case
element
:
:
Type_t
::
f64
:
replacement
=
fold_constant_broadcast
<
double
>
(
constant_match
,
broadcast_match
,
func
);
break
;
case
element
:
:
Type_t
::
i8
:
replacement
=
fold_constant_broadcast
<
int8_t
>
(
constant_match
,
broadcast_match
,
func
);
break
;
case
element
:
:
Type_t
::
i16
:
replacement
=
fold_constant_broadcast
<
int16_t
>
(
constant_match
,
broadcast_match
,
func
);
break
;
case
element
:
:
Type_t
::
i32
:
replacement
=
fold_constant_broadcast
<
int32_t
>
(
constant_match
,
broadcast_match
,
func
);
break
;
case
element
:
:
Type_t
::
i64
:
replacement
=
fold_constant_broadcast
<
int64_t
>
(
constant_match
,
broadcast_match
,
func
);
break
;
case
element
:
:
Type_t
::
u8
:
replacement
=
fold_constant_broadcast
<
uint8_t
>
(
constant_match
,
broadcast_match
,
func
);
break
;
case
element
:
:
Type_t
::
u16
:
replacement
=
fold_constant_broadcast
<
uint16_t
>
(
constant_match
,
broadcast_match
,
func
);
break
;
case
element
:
:
Type_t
::
u32
:
replacement
=
fold_constant_broadcast
<
uint32_t
>
(
constant_match
,
broadcast_match
,
func
);
break
;
case
element
:
:
Type_t
::
u64
:
replacement
=
fold_constant_broadcast
<
uint64_t
>
(
constant_match
,
broadcast_match
,
func
);
break
;
}
return
false
;
replace_node
(
m
.
get_match_root
(),
replacement
);
return
true
;
};
auto
broadcast_matcher
=
...
...
@@ -1462,11 +1509,6 @@ template <typename TI>
shared_ptr
<
op
::
Constant
>
fold_constant_convert_helper0
(
shared_ptr
<
op
::
Constant
>
constant
,
const
element
::
Type
&
output_element_type
)
{
#if !(defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ == 8))
#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wswitch"
#pragma GCC diagnostic error "-Wswitch-enum"
#endif
switch
(
output_element_type
.
get_type_enum
())
{
case
element
:
:
Type_t
::
undefined
:
...
...
@@ -1504,10 +1546,6 @@ shared_ptr<op::Constant> fold_constant_convert_helper0(shared_ptr<op::Constant>
}
NGRAPH_UNREACHABLE
(
"Unexpected switch case"
);
#if !(defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ == 8))
#pragma GCC diagnostic pop
#endif
}
static
shared_ptr
<
op
::
Constant
>
fold_constant_convert
(
shared_ptr
<
op
::
Constant
>
constant
,
...
...
@@ -1520,11 +1558,6 @@ static shared_ptr<op::Constant> fold_constant_convert(shared_ptr<op::Constant> c
return
constant
;
}
#if !(defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ == 8))
#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wswitch"
#pragma GCC diagnostic error "-Wswitch-enum"
#endif
switch
(
input_element_type
.
get_type_enum
())
{
case
element
:
:
Type_t
::
undefined
:
...
...
@@ -1562,10 +1595,6 @@ static shared_ptr<op::Constant> fold_constant_convert(shared_ptr<op::Constant> c
}
NGRAPH_UNREACHABLE
(
"Unexpected switch case"
);
#if !(defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ == 8))
#pragma GCC diagnostic pop
#endif
}
void
pass
::
ConstantFolding
::
construct_constant_convert
()
...
...
@@ -1648,11 +1677,6 @@ static shared_ptr<op::Constant> fold_constant_reverse(shared_ptr<op::Constant> c
{
auto
&
input_element_type
=
constant
->
get_output_element_type
(
0
);
#if !(defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ == 8))
#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wswitch"
#pragma GCC diagnostic error "-Wswitch-enum"
#endif
switch
(
input_element_type
.
get_type_enum
())
{
case
element
:
:
Type_t
::
undefined
:
...
...
@@ -1686,10 +1710,6 @@ static shared_ptr<op::Constant> fold_constant_reverse(shared_ptr<op::Constant> c
}
NGRAPH_UNREACHABLE
(
"Unexpected switch case"
);
#if !(defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ == 8))
#pragma GCC diagnostic pop
#endif
}
void
pass
::
ConstantFolding
::
construct_constant_reverse
()
...
...
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