Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
2def79bc
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 2 年 前同步成功
通知
2325
Star
20933
Fork
5424
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
2def79bc
编写于
3月 16, 2022
作者:
Z
Zuza
提交者:
GitHub
3月 16, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Quantize elementwise mul (#40546)
* Quantize elementwise mul op * Parametrize elementwise functions * Fix code formatting
上级
dec2b1ca
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
163 addition
and
128 deletion
+163
-128
paddle/fluid/framework/ir/graph_pattern_detector.cc
paddle/fluid/framework/ir/graph_pattern_detector.cc
+11
-10
paddle/fluid/framework/ir/graph_pattern_detector.h
paddle/fluid/framework/ir/graph_pattern_detector.h
+14
-14
paddle/fluid/framework/ir/mkldnn/conv_elementwise_add_mkldnn_fuse_pass.cc
...mework/ir/mkldnn/conv_elementwise_add_mkldnn_fuse_pass.cc
+45
-45
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.cc
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.cc
+36
-35
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.h
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.h
+2
-1
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass_tester.cc
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass_tester.cc
+51
-19
paddle/fluid/framework/ir/mkldnn/cpu_quantize_placement_pass.cc
.../fluid/framework/ir/mkldnn/cpu_quantize_placement_pass.cc
+4
-4
未找到文件。
paddle/fluid/framework/ir/graph_pattern_detector.cc
浏览文件 @
2def79bc
...
...
@@ -2052,18 +2052,19 @@ PDNode *patterns::Pool::operator()() {
return
output_var
;
}
PDNode
*
patterns
::
ElementwiseAdd
::
operator
()(
PDNode
*
x_var
,
PDNode
*
y_var
)
{
auto
elementwise_add_op
=
pattern
->
NewNode
(
elementwise_add_op_repr
())
->
assert_is_op
(
"elementwise_add"
);
x_var
->
AsInput
()
->
assert_is_op_input
(
"elementwise_add"
,
"X"
);
y_var
->
AsInput
()
->
assert_is_op_input
(
"elementwise_add"
,
"Y"
);
auto
out_var
=
pattern
->
NewNode
(
elementwise_add_out_repr
())
PDNode
*
patterns
::
Elementwise
::
operator
()(
PDNode
*
x_var
,
PDNode
*
y_var
,
const
std
::
string
elementwise_type
)
{
auto
elementwise_op
=
pattern
->
NewNode
(
elementwise_op_repr
())
->
assert_is_op
(
elementwise_type
);
x_var
->
AsInput
()
->
assert_is_op_input
(
elementwise_type
,
"X"
);
y_var
->
AsInput
()
->
assert_is_op_input
(
elementwise_type
,
"Y"
);
auto
out_var
=
pattern
->
NewNode
(
elementwise_out_repr
())
->
AsOutput
()
->
assert_is_op_output
(
"elementwise_add"
,
"Out"
);
->
assert_is_op_output
(
elementwise_type
,
"Out"
);
elementwise_
add_
op
->
LinksFrom
({
x_var
,
y_var
});
elementwise_
add_
op
->
LinksTo
({
out_var
});
elementwise_op
->
LinksFrom
({
x_var
,
y_var
});
elementwise_op
->
LinksTo
({
out_var
});
return
out_var
;
}
...
...
paddle/fluid/framework/ir/graph_pattern_detector.h
浏览文件 @
2def79bc
...
...
@@ -1016,20 +1016,20 @@ struct Pool : public PatternBase {
PATTERN_DECL_NODE
(
pool_output
);
};
// Elementwise
Add used in residual connections.
//
y_var is used and convolution output.
//
The operator is removed, when residual
// connection fusion is on.
struct
ElementwiseAdd
:
public
PatternBase
{
ElementwiseAdd
(
PDPattern
*
pattern
,
const
std
::
string
&
name_scope
)
:
PatternBase
(
pattern
,
name_scope
,
"elementwise_add"
)
{}
PDNode
*
operator
()(
PDNode
*
x_var
,
PDNode
*
y_var
);
PATTERN_DECL_NODE
(
elementwise_
add_
op
);
PATTERN_DECL_NODE
(
elementwise_
add_
x
);
PATTERN_DECL_NODE
(
elementwise_
add_
y
);
PATTERN_DECL_NODE
(
elementwise_
add_
out
);
// Elementwise
ops
//
Forward pass for element-wise operators (add, mul)
//
elementwise_mul_out is the result of the operator
struct
Elementwise
:
public
PatternBase
{
Elementwise
(
PDPattern
*
pattern
,
const
std
::
string
&
name_scope
)
:
PatternBase
(
pattern
,
name_scope
,
"elementwise"
)
{}
PDNode
*
operator
()(
PDNode
*
x_var
,
PDNode
*
y_var
,
const
std
::
string
elementwise_type
);
PATTERN_DECL_NODE
(
elementwise_op
);
PATTERN_DECL_NODE
(
elementwise_x
);
PATTERN_DECL_NODE
(
elementwise_y
);
PATTERN_DECL_NODE
(
elementwise_out
);
};
// Transpose op
...
...
paddle/fluid/framework/ir/mkldnn/conv_elementwise_add_mkldnn_fuse_pass.cc
浏览文件 @
2def79bc
...
...
@@ -145,10 +145,10 @@ GraphWithStats ResidualConnectionMKLDNNFusePass::FuseConvAsX(
patterns
::
Conv
conv_pattern
{
pattern
,
name_scope
};
auto
conv_output
=
conv_pattern
();
patterns
::
Elementwise
Add
elementwise_add
_pattern
{
pattern
,
name_scope
};
elementwise_
add_
pattern
(
conv_output
,
pattern
->
NewNode
(
elementwise_add_pattern
.
elementwise_add_y_repr
())
);
patterns
::
Elementwise
elementwise
_pattern
{
pattern
,
name_scope
};
elementwise_pattern
(
conv_output
,
pattern
->
NewNode
(
elementwise_pattern
.
elementwise_y_repr
()),
"elementwise_add"
);
conv_output
->
AsIntermediate
();
int
found_conv_as_x_count
=
0
;
...
...
@@ -160,16 +160,16 @@ GraphWithStats ResidualConnectionMKLDNNFusePass::FuseConvAsX(
GET_IR_NODE_FROM_SUBGRAPH
(
conv_filter
,
conv_filter
,
conv_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
conv_output
,
conv_output
,
conv_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
add_op
,
elementwise_add
_op
,
elementwise_
add_
pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
add_identity
,
elementwise_add
_y
,
elementwise_
add_
pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
add_out
,
elementwise_add
_out
,
elementwise_
add_
pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
op
,
elementwise
_op
,
elementwise_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
identity
,
elementwise
_y
,
elementwise_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
out
,
elementwise
_out
,
elementwise_pattern
);
if
(
FindFuseOption
(
*
conv_op
,
*
elementwise_
add_
op
)
!=
FUSE_MKLDNN
)
return
;
if
(
FindFuseOption
(
*
conv_op
,
*
elementwise_op
)
!=
FUSE_MKLDNN
)
return
;
if
(
!
IsReachable
(
g
,
elementwise_
add_
identity
,
conv_output
))
return
;
if
(
!
IsReachable
(
g
,
elementwise_identity
,
conv_output
))
return
;
if
(
HasFusedActivation
(
conv_op
))
return
;
...
...
@@ -179,14 +179,14 @@ GraphWithStats ResidualConnectionMKLDNNFusePass::FuseConvAsX(
return
;
}
conv_op
->
Op
()
->
SetInput
(
"ResidualData"
,
{
elementwise_
add_
identity
->
Name
()});
conv_op
->
Op
()
->
SetOutput
(
"Output"
,
{
elementwise_
add_
out
->
Name
()});
conv_op
->
Op
()
->
SetInput
(
"ResidualData"
,
{
elementwise_identity
->
Name
()});
conv_op
->
Op
()
->
SetOutput
(
"Output"
,
{
elementwise_out
->
Name
()});
conv_op
->
Op
()
->
SetAttr
(
"fuse_residual_connection"
,
true
);
GraphSafeRemoveNodes
(
g
,
{
conv_output
,
elementwise_
add_
op
});
GraphSafeRemoveNodes
(
g
,
{
conv_output
,
elementwise_op
});
IR_NODE_LINK_TO
(
elementwise_
add_
identity
,
conv_op
);
IR_NODE_LINK_TO
(
conv_op
,
elementwise_
add_
out
);
IR_NODE_LINK_TO
(
elementwise_identity
,
conv_op
);
IR_NODE_LINK_TO
(
conv_op
,
elementwise_out
);
found_conv_as_x_count
++
;
};
...
...
@@ -212,10 +212,10 @@ GraphWithStats ResidualConnectionMKLDNNFusePass::FuseConvAsY(
patterns
::
Conv
conv_pattern
{
pattern
,
name_scope
};
auto
conv_output
=
conv_pattern
();
patterns
::
Elementwise
Add
elementwise_add
_pattern
{
pattern
,
name_scope
};
elementwise_
add_
pattern
(
pattern
->
NewNode
(
elementwise_
add_pattern
.
elementwise_add_x_repr
())
,
conv_output
);
patterns
::
Elementwise
elementwise
_pattern
{
pattern
,
name_scope
};
elementwise_pattern
(
pattern
->
NewNode
(
elementwise_
pattern
.
elementwise_x_repr
()),
conv_output
,
"elementwise_add"
);
conv_output
->
AsIntermediate
();
int
found_conv_as_y_count
=
0
;
...
...
@@ -227,16 +227,16 @@ GraphWithStats ResidualConnectionMKLDNNFusePass::FuseConvAsY(
GET_IR_NODE_FROM_SUBGRAPH
(
conv_filter
,
conv_filter
,
conv_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
conv_output
,
conv_output
,
conv_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
add_op
,
elementwise_add
_op
,
elementwise_
add_
pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
add_x
,
elementwise_add
_x
,
elementwise_
add_
pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
add_out
,
elementwise_add
_out
,
elementwise_
add_
pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
op
,
elementwise
_op
,
elementwise_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
x
,
elementwise
_x
,
elementwise_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
out
,
elementwise
_out
,
elementwise_pattern
);
if
(
FindFuseOption
(
*
conv_op
,
*
elementwise_
add_
op
)
!=
FUSE_MKLDNN
)
return
;
if
(
FindFuseOption
(
*
conv_op
,
*
elementwise_op
)
!=
FUSE_MKLDNN
)
return
;
if
(
!
IsReachable
(
g
,
elementwise_
add_
x
,
conv_output
))
return
;
if
(
!
IsReachable
(
g
,
elementwise_x
,
conv_output
))
return
;
if
(
HasFusedActivation
(
conv_op
))
return
;
...
...
@@ -246,14 +246,14 @@ GraphWithStats ResidualConnectionMKLDNNFusePass::FuseConvAsY(
return
;
}
conv_op
->
Op
()
->
SetInput
(
"ResidualData"
,
{
elementwise_
add_
x
->
Name
()});
conv_op
->
Op
()
->
SetOutput
(
"Output"
,
{
elementwise_
add_
out
->
Name
()});
conv_op
->
Op
()
->
SetInput
(
"ResidualData"
,
{
elementwise_x
->
Name
()});
conv_op
->
Op
()
->
SetOutput
(
"Output"
,
{
elementwise_out
->
Name
()});
conv_op
->
Op
()
->
SetAttr
(
"fuse_residual_connection"
,
true
);
GraphSafeRemoveNodes
(
g
,
{
conv_output
,
elementwise_
add_
op
});
GraphSafeRemoveNodes
(
g
,
{
conv_output
,
elementwise_op
});
IR_NODE_LINK_TO
(
elementwise_
add_
x
,
conv_op
);
IR_NODE_LINK_TO
(
conv_op
,
elementwise_
add_
out
);
IR_NODE_LINK_TO
(
elementwise_x
,
conv_op
);
IR_NODE_LINK_TO
(
conv_op
,
elementwise_out
);
found_conv_as_y_count
++
;
};
...
...
@@ -282,8 +282,8 @@ GraphWithStats ResidualConnectionMKLDNNFusePass::FuseProjectionConv(
patterns
::
Conv
conv_y_pattern
{
pattern
,
name_scope
};
auto
conv_y_output
=
conv_y_pattern
();
patterns
::
Elementwise
Add
elementwise_add
_pattern
{
pattern
,
name_scope
};
elementwise_
add_pattern
(
conv_x_output
,
conv_y_output
);
patterns
::
Elementwise
elementwise
_pattern
{
pattern
,
name_scope
};
elementwise_
pattern
(
conv_x_output
,
conv_y_output
,
"elementwise_add"
);
conv_x_output
->
AsIntermediate
();
conv_y_output
->
AsIntermediate
();
...
...
@@ -301,10 +301,10 @@ GraphWithStats ResidualConnectionMKLDNNFusePass::FuseProjectionConv(
GET_IR_NODE_FROM_SUBGRAPH
(
conv_y_filter
,
conv_filter
,
conv_y_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
conv_y_output
,
conv_output
,
conv_y_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
add_op
,
elementwise_add
_op
,
elementwise_
add_
pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
add_out
,
elementwise_add
_out
,
elementwise_
add_
pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
op
,
elementwise
_op
,
elementwise_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
out
,
elementwise
_out
,
elementwise_pattern
);
if
(
!
IsCompat
(
subgraph
,
g
))
{
LOG
(
WARNING
)
...
...
@@ -312,8 +312,8 @@ GraphWithStats ResidualConnectionMKLDNNFusePass::FuseProjectionConv(
return
;
}
if
(
FindFuseOption
(
*
conv_x_op
,
*
elementwise_
add_
op
)
!=
FUSE_MKLDNN
)
return
;
if
(
FindFuseOption
(
*
conv_y_op
,
*
elementwise_
add_
op
)
!=
FUSE_MKLDNN
)
return
;
if
(
FindFuseOption
(
*
conv_x_op
,
*
elementwise_op
)
!=
FUSE_MKLDNN
)
return
;
if
(
FindFuseOption
(
*
conv_y_op
,
*
elementwise_op
)
!=
FUSE_MKLDNN
)
return
;
Node
*
projection_node
;
Node
*
residual_conv_op
;
...
...
@@ -333,14 +333,14 @@ GraphWithStats ResidualConnectionMKLDNNFusePass::FuseProjectionConv(
if
(
HasFusedActivation
(
residual_conv_op
))
return
;
residual_conv_op
->
Op
()
->
SetInput
(
"ResidualData"
,
{
projection_node
->
Name
()});
residual_conv_op
->
Op
()
->
SetOutput
(
"Output"
,
{
elementwise_
add_
out
->
Name
()});
residual_conv_op
->
Op
()
->
SetOutput
(
"Output"
,
{
elementwise_out
->
Name
()});
residual_conv_op
->
Op
()
->
SetAttr
(
"fuse_residual_connection"
,
true
);
GraphSafeRemoveNodes
(
g
,
{
residual_conv_output
,
elementwise_
add_
op
});
GraphSafeRemoveNodes
(
g
,
{
residual_conv_output
,
elementwise_op
});
IR_NODE_LINK_TO
(
projection_node
,
residual_conv_op
);
IR_NODE_LINK_TO
(
residual_conv_op
,
elementwise_
add_
out
);
IR_NODE_LINK_TO
(
residual_conv_op
,
elementwise_out
);
found_projection_conv_count
++
;
};
...
...
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.cc
浏览文件 @
2def79bc
...
...
@@ -807,74 +807,74 @@ void CPUQuantizePass::QuantizeMatmul(Graph* graph) const {
PrettyLogDetail
(
"--- quantized %d matmul ops"
,
quantize_matmul_count
);
}
void
CPUQuantizePass
::
QuantizeElementwiseAdd
(
Graph
*
graph
)
const
{
void
CPUQuantizePass
::
QuantizeElementwise
(
Graph
*
graph
,
const
std
::
string
elementwise_type
)
const
{
GraphPatternDetector
gpd
;
auto
pattern
=
gpd
.
mutable_pattern
();
patterns
::
Elementwise
Add
elementwise_add
_pattern
{
pattern
,
name_scope_
};
patterns
::
Elementwise
elementwise
_pattern
{
pattern
,
name_scope_
};
elementwise_add_pattern
(
pattern
->
NewNode
(
elementwise_add_pattern
.
elementwise_add_x_repr
()),
pattern
->
NewNode
(
elementwise_add_pattern
.
elementwise_add_y_repr
()));
elementwise_pattern
(
pattern
->
NewNode
(
elementwise_pattern
.
elementwise_x_repr
()),
pattern
->
NewNode
(
elementwise_pattern
.
elementwise_y_repr
()),
elementwise_type
);
int
quantize_elementwise_
add_
count
=
0
;
int
quantize_elementwise_count
=
0
;
auto
handler
=
[
&
](
const
GraphPatternDetector
::
subgraph_t
&
subgraph
,
Graph
*
g
)
{
VLOG
(
4
)
<<
"Quantize
elementwise_add
op"
;
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
add_op
,
elementwise_add
_op
,
elementwise_
add_
pattern
);
VLOG
(
4
)
<<
"Quantize
"
+
elementwise_type
+
"
op"
;
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
op
,
elementwise
_op
,
elementwise_pattern
);
// skip if should not be quantized
if
(
!
platform
::
HasOpINT8DataType
(
elementwise_
add_
op
->
Op
()))
{
LogQuantizationDisabled
(
elementwise_
add_
op
);
if
(
!
platform
::
HasOpINT8DataType
(
elementwise_op
->
Op
()))
{
LogQuantizationDisabled
(
elementwise_op
);
return
;
}
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
add_x
,
elementwise_add
_x
,
elementwise_
add_
pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
add_y
,
elementwise_add
_y
,
elementwise_
add_
pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
add_out
,
elementwise_add
_out
,
elementwise_
add_
pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
x
,
elementwise
_x
,
elementwise_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
y
,
elementwise
_y
,
elementwise_pattern
);
GET_IR_NODE_FROM_SUBGRAPH
(
elementwise_
out
,
elementwise
_out
,
elementwise_pattern
);
if
(
!
AreScalesPresentForNodes
(
{
elementwise_
add_x
,
elementwise_add_y
,
elementwise_add
_out
}))
{
LogCannotQuantizeOp
(
elementwise_
add_
op
,
{
elementwise_
x
,
elementwise_y
,
elementwise
_out
}))
{
LogCannotQuantizeOp
(
elementwise_op
,
"No scale available for the operator"
);
return
;
}
bool
is_x_unsigned
{
false
},
is_y_unsigned
{
false
};
auto
input_x_scale
=
GetScaleValueForNode
(
elementwise_add_x
,
&
is_x_unsigned
);
auto
input_y_scale
=
GetScaleValueForNode
(
elementwise_add_y
,
&
is_y_unsigned
);
auto
input_x_scale
=
GetScaleValueForNode
(
elementwise_x
,
&
is_x_unsigned
);
auto
input_y_scale
=
GetScaleValueForNode
(
elementwise_y
,
&
is_y_unsigned
);
// TODO(sfraczek): add support for different signness
if
(
is_x_unsigned
!=
is_y_unsigned
)
{
LogCannotQuantizeOp
(
elementwise_
add_
op
,
"Elementwise
Add
inputs must be of the same type."
);
LogCannotQuantizeOp
(
elementwise_op
,
"Elementwise inputs must be of the same type."
);
return
;
}
QuantizeInput
(
g
,
elementwise_
add_op
,
elementwise_add
_x
,
"X"
,
input_x_scale
,
QuantizeInput
(
g
,
elementwise_
op
,
elementwise
_x
,
"X"
,
input_x_scale
,
is_x_unsigned
,
"Scale_x"
);
QuantizeInput
(
g
,
elementwise_
add_op
,
elementwise_add
_y
,
"Y"
,
input_y_scale
,
QuantizeInput
(
g
,
elementwise_
op
,
elementwise
_y
,
"Y"
,
input_y_scale
,
is_y_unsigned
,
"Scale_y"
);
bool
is_output_unsigned
{
false
};
auto
output_scale
=
GetScaleValueForNode
(
elementwise_
add_
out
,
&
is_output_unsigned
);
GetScaleValueForNode
(
elementwise_out
,
&
is_output_unsigned
);
DequantizeOutput
(
g
,
elementwise_
add_op
,
elementwise_add_out
,
"Out"
,
output_scale
,
is_output_unsigned
,
"Scale_out"
);
DequantizeOutput
(
g
,
elementwise_
op
,
elementwise_out
,
"Out"
,
output_scale
,
is_output_unsigned
,
"Scale_out"
);
++
quantize_elementwise_
add_
count
;
++
quantize_elementwise_count
;
};
gpd
(
graph
,
handler
);
AddStatis
(
quantize_elementwise_
add_
count
);
AddStatis
(
quantize_elementwise_count
);
PrettyLogDetail
(
"--- quantized %d
elementwise_add ops"
,
quantize_elementwise_add_count
);
PrettyLogDetail
(
"--- quantized %d
%s ops"
,
quantize_elementwise_count
,
elementwise_type
);
}
void
CPUQuantizePass
::
QuantizeFusionGru
(
Graph
*
graph
)
const
{
...
...
@@ -1146,7 +1146,8 @@ void CPUQuantizePass::ApplyImpl(ir::Graph* graph) const {
QuantizeFc
(
graph
);
QuantizeReshape
(
graph
);
QuantizeMatmul
(
graph
);
QuantizeElementwiseAdd
(
graph
);
QuantizeElementwise
(
graph
,
"elementwise_add"
);
QuantizeElementwise
(
graph
,
"elementwise_mul"
);
QuantizeFusionGru
(
graph
);
QuantizeMultiGru
(
graph
);
QuantizeFusionLSTM
(
graph
);
...
...
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.h
浏览文件 @
2def79bc
...
...
@@ -57,7 +57,8 @@ class CPUQuantizePass : public FusePassBase {
void
QuantizeTranspose
(
Graph
*
graph
)
const
;
void
QuantizeReshape
(
Graph
*
graph
)
const
;
void
QuantizeMatmul
(
Graph
*
graph
)
const
;
void
QuantizeElementwiseAdd
(
Graph
*
graph
)
const
;
void
QuantizeElementwise
(
Graph
*
graph
,
const
std
::
string
elementwise_type
)
const
;
void
QuantizeFusionGru
(
Graph
*
graph
)
const
;
void
QuantizeMultiGru
(
Graph
*
graph
)
const
;
void
QuantizeFusionLSTM
(
Graph
*
graph
)
const
;
...
...
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass_tester.cc
浏览文件 @
2def79bc
...
...
@@ -90,7 +90,7 @@ void SetOp(ProgramDesc* prog, const std::string& type, const std::string& name,
op
->
SetAttr
(
"Scale_x"
,
1.0
f
);
op
->
SetAttr
(
"Scale_y"
,
1.0
f
);
op
->
SetAttr
(
"Scale_out"
,
1.0
f
);
}
else
if
(
type
==
"elementwise_add"
)
{
}
else
if
(
type
==
"elementwise_add"
||
type
==
"elementwise_mul"
)
{
op
->
SetInput
(
"X"
,
{
inputs
[
0
]});
if
(
inputs
.
size
()
>
1
)
op
->
SetInput
(
"Y"
,
{
inputs
[
1
]});
op
->
SetOutput
(
"Out"
,
{
outputs
[
0
]});
...
...
@@ -167,7 +167,8 @@ void CheckScales(const OpDesc* op, float scale, float shift) {
scale
);
scale_names
.
push_back
(
"Scale_in"
);
scale_names
.
push_back
(
"Scale_out"
);
}
else
if
(
type
==
"matmul"
||
type
==
"elementwise_add"
)
{
}
else
if
(
type
==
"matmul"
||
type
==
"elementwise_add"
||
type
==
"elementwise_mul"
)
{
scale_names
.
push_back
(
"Scale_x"
);
scale_names
.
push_back
(
"Scale_y"
);
scale_names
.
push_back
(
"Scale_out"
);
...
...
@@ -546,46 +547,77 @@ TEST(CpuQuantizePass, matmul_not_quantized) {
expected_operators
,
added_nodes
,
1.0
f
);
}
static
const
std
::
initializer_list
<
std
::
string
>
variable_names_elementwise
_add
=
{
"a"
,
"b"
,
"c"
,
"d"
,
"e"
,
"f"
};
static
const
std
::
initializer_list
<
std
::
string
>
variable_names_elementwise
=
{
"a"
,
"b"
,
"c"
,
"d"
,
"e"
,
"f"
};
ProgramDesc
BuildProgramDescElementwiseAdd
()
{
ProgramDesc
BuildProgramDescElementwise
(
const
std
::
string
elementwise_type
,
const
std
::
string
elementwise_name
)
{
ProgramDesc
prog
;
for
(
auto
&
v
:
variable_names_elementwise
_add
)
{
for
(
auto
&
v
:
variable_names_elementwise
)
{
prog
.
MutableBlock
(
0
)
->
Var
(
v
);
}
SetOp
(
&
prog
,
"dequantize"
,
"Dequantize1"
,
{
"a"
},
{
"b"
},
true
);
SetOp
(
&
prog
,
"dequantize"
,
"Dequantize2"
,
{
"c"
},
{
"d"
},
true
);
SetOp
(
&
prog
,
"elementwise_add"
,
"ElementwiseAdd"
,
{
"b"
,
"d"
},
{
"e"
},
true
,
SetOp
(
&
prog
,
elementwise_type
,
elementwise_name
,
{
"b"
,
"d"
},
{
"e"
},
true
,
"int8"
);
SetOp
(
&
prog
,
"dropout"
,
"Dropout"
,
{
"e"
},
{
"f"
},
true
,
"float32"
);
return
prog
;
}
TEST
(
CpuQuantizePass
,
elementwise_add
)
{
void
TestElementwise
(
const
std
::
string
elementwise_type
,
const
std
::
string
elementwise_name
)
{
// 2 Quant + 2 IN + 1 DeQuant + 1 OUT
int
added_nodes
=
6
;
std
::
unordered_map
<
std
::
string
,
int
>
expected_operators
=
{
{
"elementwise_add"
,
1
},
{
"quantize"
,
2
},
{
"dequantize"
,
3
}};
MainTest
(
BuildProgramDescElementwiseAdd
(),
variable_names_elementwise_add
,
expected_operators
,
added_nodes
,
SCALE
*
S8_MAX
);
{
elementwise_type
,
1
},
{
"quantize"
,
2
},
{
"dequantize"
,
3
}};
MainTest
(
BuildProgramDescElementwise
(
elementwise_type
,
elementwise_name
),
variable_names_elementwise
,
expected_operators
,
added_nodes
,
SCALE
*
S8_MAX
);
}
TEST
(
CpuQuantizePass
,
elementwise_add_output_scale_missing
)
{
void
TestElementwiseOutputScaleMissing
(
const
std
::
string
elementwise_type
,
const
std
::
string
elementwise_name
)
{
int
added_nodes
=
0
;
std
::
unordered_map
<
std
::
string
,
int
>
expected_operators
=
{
{
"elementwise_add"
,
1
},
{
"quantize"
,
0
},
{
"dequantize"
,
2
}};
MainTest
(
BuildProgramDescElementwiseAdd
(),
variable_names_elementwise_add
,
expected_operators
,
added_nodes
,
1.
f
,
1.
f
,
"e"
);
{
elementwise_type
,
1
},
{
"quantize"
,
0
},
{
"dequantize"
,
2
}};
MainTest
(
BuildProgramDescElementwise
(
elementwise_type
,
elementwise_name
),
variable_names_elementwise
,
expected_operators
,
added_nodes
,
1.
f
,
1.
f
,
"e"
);
}
TEST
(
CpuQuantizePass
,
elementwise_add_unsigned_and_signed_input
)
{
void
TestElementwiseUnsignedAndSignedInput
(
const
std
::
string
elementwise_type
,
const
std
::
string
elementwise_name
)
{
int
added_nodes
=
0
;
std
::
unordered_map
<
std
::
string
,
int
>
expected_operators
=
{
{
"elementwise_add"
,
1
},
{
"quantize"
,
0
},
{
"dequantize"
,
2
}};
MainTest
(
BuildProgramDescElementwiseAdd
(),
variable_names_elementwise_add
,
expected_operators
,
added_nodes
,
1.
f
,
1.
f
,
""
,
"b"
);
{
elementwise_type
,
1
},
{
"quantize"
,
0
},
{
"dequantize"
,
2
}};
MainTest
(
BuildProgramDescElementwise
(
elementwise_type
,
elementwise_name
),
variable_names_elementwise
,
expected_operators
,
added_nodes
,
1.
f
,
1.
f
,
""
,
"b"
);
}
TEST
(
CpuQuantizePass
,
elementwise_add
)
{
TestElementwise
(
"elementwise_add"
,
"ElementwiseAdd"
);
}
TEST
(
CpuQuantizePass
,
elementwise_add_output_scale_missing
)
{
TestElementwiseOutputScaleMissing
(
"elementwise_add"
,
"ElementwiseAdd"
);
}
TEST
(
CpuQuantizePass
,
elementwise_add_unsigned_and_signed_input
)
{
TestElementwiseUnsignedAndSignedInput
(
"elementwise_add"
,
"ElementwiseAdd"
);
}
TEST
(
CpuQuantizePass
,
elementwise_mul
)
{
TestElementwise
(
"elementwise_mul"
,
"ElementwiseMul"
);
}
TEST
(
CpuQuantizePass
,
elementwise_mul_output_scale_missing
)
{
TestElementwiseOutputScaleMissing
(
"elementwise_mul"
,
"ElementwiseMul"
);
}
TEST
(
CpuQuantizePass
,
elementwise_mul_unsigned_and_signed_input
)
{
TestElementwiseUnsignedAndSignedInput
(
"elementwise_mul"
,
"ElementwiseMul"
);
}
const
std
::
vector
<
std
::
string
>
churn_out_vars
(
ProgramDesc
*
prog
,
...
...
paddle/fluid/framework/ir/mkldnn/cpu_quantize_placement_pass.cc
浏览文件 @
2def79bc
...
...
@@ -26,10 +26,10 @@ void CPUQuantizePlacementPass::ApplyImpl(ir::Graph* graph) const {
VLOG
(
3
)
<<
"Marks operators which are to be quantized."
;
std
::
unordered_set
<
std
::
string
>
supported_op_types
=
std
::
unordered_set
<
std
::
string
>
(
{
"concat"
,
"conv2d"
,
"depthwise_conv2d"
,
"elementwise_add"
,
"fc"
,
"
matmul"
,
"nearest_interp"
,
"nearest_interp_v2"
,
"pool2d
"
,
"
prior_box"
,
"reshape2"
,
"transpose2"
,
"fusion_gru"
,
"fusion_lstm
"
,
"multi_gru"
,
"slice"
});
{
"concat"
,
"conv2d"
,
"depthwise_conv2d"
,
"elementwise_add"
,
"
elementwise_mul"
,
"fc"
,
"matmul"
,
"nearest_interp
"
,
"
nearest_interp_v2"
,
"pool2d"
,
"prior_box"
,
"reshape2"
,
"transpose2
"
,
"
fusion_gru"
,
"fusion_lstm"
,
"
multi_gru"
,
"slice"
});
const
auto
&
excluded_ids_list
=
Get
<
std
::
unordered_set
<
int
>>
(
"quantize_excluded_op_ids"
);
const
auto
&
op_types_list
=
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录