Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
31f81685
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
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看板
未验证
提交
31f81685
编写于
3月 22, 2023
作者:
J
joanna.wozna.intel
提交者:
GitHub
3月 22, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Correct lstm qat test (#51499)
上级
6ba0507d
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
46 addition
and
54 deletion
+46
-54
paddle/fluid/framework/ir/mkldnn/compute_propagate_scales_mkldnn_pass.cc
...amework/ir/mkldnn/compute_propagate_scales_mkldnn_pass.cc
+3
-3
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.cc
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.cc
+5
-1
paddle/fluid/framework/ir/mkldnn/mkldnn_pass_util.h
paddle/fluid/framework/ir/mkldnn/mkldnn_pass_util.h
+33
-44
paddle/fluid/framework/ir/mkldnn/quant_dequant_mkldnn_pass.cc
...le/fluid/framework/ir/mkldnn/quant_dequant_mkldnn_pass.cc
+2
-2
paddle/fluid/inference/api/paddle_pass_builder.cc
paddle/fluid/inference/api/paddle_pass_builder.cc
+1
-1
python/paddle/static/quantization/tests/quant2_int8_lstm_model.py
...addle/static/quantization/tests/quant2_int8_lstm_model.py
+2
-3
未找到文件。
paddle/fluid/framework/ir/mkldnn/compute_propagate_scales_mkldnn_pass.cc
浏览文件 @
31f81685
...
...
@@ -37,7 +37,7 @@ void ComputePropagateScalesMkldnnPass::GetTensorFromVector(
void
ComputePropagateScalesMkldnnPass
::
GetQuantInfo
(
ir
::
Graph
*
graph
,
StringPairMap
*
var_quant_scales
)
const
{
std
::
unordered_map
<
std
::
string
,
std
::
vector
<
float
>>
info_map
{};
GetInfoFromThe
First
Op
(
graph
,
"has_quant_info"
,
"var_quant_scales"
,
&
info_map
);
GetInfoFromThe
Tmp
Op
(
graph
,
"has_quant_info"
,
"var_quant_scales"
,
&
info_map
);
for
(
auto
iter
=
info_map
.
begin
();
iter
!=
info_map
.
end
();
iter
++
)
{
phi
::
DenseTensor
tensor
;
...
...
@@ -510,9 +510,9 @@ void ComputePropagateScalesMkldnnPass::ApplyImpl(ir::Graph* graph) const {
UpdateReluOutputScales
(
graph
,
&
var_quant_scales
);
PropagateScales
(
graph
,
&
var_quant_scales
,
scale_immutable_ops
);
// save var_quant_scales in the
first
op's attr
// save var_quant_scales in the
temporary save
op's attr
// for cpu_quantize_pass
SaveInfoInThe
First
Op
(
SaveInfoInThe
Tmp
Op
(
graph
,
"has_quant_info"
,
"var_quant_scales"
,
var_quant_scales
);
}
...
...
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.cc
浏览文件 @
31f81685
...
...
@@ -435,7 +435,7 @@ bool CPUQuantizePass::IsOpQuantized(const Node* node) const {
}
void
CPUQuantizePass
::
GetQuantInfo
(
Graph
*
graph
)
const
{
GetInfoFromThe
First
Op
(
GetInfoFromThe
Tmp
Op
(
graph
,
"has_quant_info"
,
"var_quant_scales"
,
var_quant_scales_
);
}
...
...
@@ -1250,6 +1250,10 @@ void CPUQuantizePass::QuantizeFusionLSTM(Graph* graph) const {
bool
is_x_unsigned
{
false
};
auto
input_x_scale
=
GetScaleValueForNode
(
x
,
&
is_x_unsigned
);
// In the QAT process scales are prepared for only int8 data type,
// lstm scales should behave as input is int8 to get correct accuracy
is_x_unsigned
=
false
;
double
input_x_shift
{
128.
};
if
(
is_x_unsigned
)
input_x_shift
=
0.
;
...
...
paddle/fluid/framework/ir/mkldnn/mkldnn_pass_util.h
浏览文件 @
31f81685
...
...
@@ -25,7 +25,7 @@ namespace ir {
using
StringPairMap
=
std
::
unordered_map
<
std
::
string
,
std
::
pair
<
bool
,
phi
::
DenseTensor
>>
;
static
void
SaveInfoInThe
First
Op
(
static
void
SaveInfoInThe
Tmp
Op
(
ir
::
Graph
*
graph
,
const
std
::
string
&
flag
,
const
std
::
string
&
key_suffix
,
...
...
@@ -33,48 +33,39 @@ static void SaveInfoInTheFirstOp(
VLOG
(
3
)
<<
"save variables in the first op's attr"
;
const
std
::
string
suffix
=
"_"
+
key_suffix
+
"_"
+
flag
;
for
(
auto
*
op_node
:
ir
::
TopologyVarientSort
(
*
graph
,
static_cast
<
ir
::
SortKind
>
(
0
)))
{
if
(
!
op_node
->
IsOp
()
||
op_node
->
Op
()
->
Type
()
==
"feed"
||
op_node
->
Op
()
->
Type
()
==
"fetch"
||
op_node
->
Op
()
->
Type
()
==
"fill_constant"
)
continue
;
op_node
->
Op
()
->
SetAttr
(
flag
,
true
);
for
(
auto
iter
=
info_map
.
begin
();
iter
!=
info_map
.
end
();
++
iter
)
{
op_node
->
Op
()
->
SetAttr
(
iter
->
first
+
suffix
,
iter
->
second
);
}
break
;
OpDesc
op_desc
;
op_desc
.
SetType
(
"save"
);
auto
*
op_node
=
graph
->
CreateOpNode
(
&
op_desc
);
op_node
->
Op
()
->
SetAttr
(
flag
,
true
);
for
(
auto
iter
=
info_map
.
begin
();
iter
!=
info_map
.
end
();
++
iter
)
{
op_node
->
Op
()
->
SetAttr
(
iter
->
first
+
suffix
,
iter
->
second
);
}
}
static
void
SaveInfoInThe
First
Op
(
ir
::
Graph
*
graph
,
const
std
::
string
&
flag
,
const
std
::
string
&
key_suffix
,
const
StringPairMap
&
info_map
)
{
static
void
SaveInfoInThe
Tmp
Op
(
ir
::
Graph
*
graph
,
const
std
::
string
&
flag
,
const
std
::
string
&
key_suffix
,
const
StringPairMap
&
info_map
)
{
VLOG
(
3
)
<<
"save variables in the first op's attr"
;
const
std
::
string
suffix
=
"_"
+
key_suffix
+
"_"
+
flag
;
for
(
auto
*
op_node
:
ir
::
TopologyVarientSort
(
*
graph
,
static_cast
<
ir
::
SortKind
>
(
0
)))
{
if
(
!
op_node
->
IsOp
()
||
op_node
->
Op
()
->
Type
()
==
"feed"
||
op_node
->
Op
()
->
Type
()
==
"fetch"
||
op_node
->
Op
()
->
Type
()
==
"fill_constant"
)
continue
;
op_node
->
Op
()
->
SetAttr
(
flag
,
true
);
for
(
auto
iter
=
info_map
.
begin
();
iter
!=
info_map
.
end
();
++
iter
)
{
auto
*
data
=
iter
->
second
.
second
.
data
<
float
>
();
std
::
vector
<
float
>
data_v
(
data
,
data
+
iter
->
second
.
second
.
numel
());
op_node
->
Op
()
->
SetAttr
(
iter
->
first
+
suffix
+
"_unsigned"
,
iter
->
second
.
first
);
op_node
->
Op
()
->
SetAttr
(
iter
->
first
+
suffix
,
data_v
);
}
break
;
OpDesc
op_desc
;
op_desc
.
SetType
(
"save"
);
auto
*
op_node
=
graph
->
CreateOpNode
(
&
op_desc
);
op_node
->
Op
()
->
SetAttr
(
flag
,
true
);
for
(
auto
iter
=
info_map
.
begin
();
iter
!=
info_map
.
end
();
++
iter
)
{
auto
*
data
=
iter
->
second
.
second
.
data
<
float
>
();
std
::
vector
<
float
>
data_v
(
data
,
data
+
iter
->
second
.
second
.
numel
());
op_node
->
Op
()
->
SetAttr
(
iter
->
first
+
suffix
+
"_unsigned"
,
iter
->
second
.
first
);
op_node
->
Op
()
->
SetAttr
(
iter
->
first
+
suffix
,
data_v
);
}
}
static
void
GetInfoFromThe
First
Op
(
static
void
GetInfoFromThe
Tmp
Op
(
ir
::
Graph
*
graph
,
const
std
::
string
&
flag
,
const
std
::
string
&
key_suffix
,
...
...
@@ -84,9 +75,7 @@ static void GetInfoFromTheFirstOp(
const
std
::
string
suffix
=
"_"
+
key_suffix
+
"_"
+
flag
;
for
(
auto
*
op_node
:
ir
::
TopologyVarientSort
(
*
graph
,
static_cast
<
ir
::
SortKind
>
(
0
)))
{
if
(
!
op_node
->
IsOp
()
||
op_node
->
Op
()
->
Type
()
==
"feed"
||
op_node
->
Op
()
->
Type
()
==
"fetch"
)
continue
;
if
(
!
op_node
->
IsOp
()
||
op_node
->
Op
()
->
Type
()
!=
"save"
)
continue
;
auto
*
op_desc
=
op_node
->
Op
();
if
(
op_desc
->
GetAttrIfExists
<
bool
>
(
flag
))
{
...
...
@@ -102,24 +91,23 @@ static void GetInfoFromTheFirstOp(
op_desc
->
RemoveAttr
(
fake_name
);
}
}
graph
->
RemoveNode
(
op_node
);
break
;
}
}
}
static
void
GetInfoFromThe
First
Op
(
ir
::
Graph
*
graph
,
const
std
::
string
&
flag
,
const
std
::
string
&
key_suffix
,
StringPairMap
*
info_map
)
{
static
void
GetInfoFromThe
Tmp
Op
(
ir
::
Graph
*
graph
,
const
std
::
string
&
flag
,
const
std
::
string
&
key_suffix
,
StringPairMap
*
info_map
)
{
VLOG
(
3
)
<<
"get variables from the first op's attr"
;
const
std
::
string
unsigned_flag
=
"_unsigned"
;
const
std
::
string
suffix
=
"_"
+
key_suffix
+
"_"
+
flag
;
const
std
::
string
suffix_is_unsigned
=
suffix
+
unsigned_flag
;
for
(
auto
*
op_node
:
ir
::
TopologyVarientSort
(
*
graph
,
static_cast
<
ir
::
SortKind
>
(
0
)))
{
if
(
!
op_node
->
IsOp
()
||
op_node
->
Op
()
->
Type
()
==
"feed"
||
op_node
->
Op
()
->
Type
()
==
"fetch"
)
continue
;
if
(
!
op_node
->
IsOp
()
||
op_node
->
Op
()
->
Type
()
!=
"save"
)
continue
;
auto
*
op_desc
=
op_node
->
Op
();
if
(
op_desc
->
GetAttrIfExists
<
bool
>
(
flag
))
{
...
...
@@ -150,6 +138,7 @@ static void GetInfoFromTheFirstOp(ir::Graph* graph,
op_desc
->
RemoveAttr
(
vector_name
);
}
}
graph
->
RemoveNode
(
op_node
);
break
;
}
}
...
...
paddle/fluid/framework/ir/mkldnn/quant_dequant_mkldnn_pass.cc
浏览文件 @
31f81685
...
...
@@ -754,9 +754,9 @@ void QuantDequantMkldnnPass::ApplyImpl(ir::Graph* graph) const {
UpdateActivations
(
graph
);
RemoveCtrlVars
(
graph
);
// save var_quant_scales in the
first
op's attr
// save var_quant_scales in the
temporary save
op's attr
// for compute_propagate_scales_mkldnn_pass
SaveInfoInThe
First
Op
(
SaveInfoInThe
Tmp
Op
(
graph
,
"has_quant_info"
,
"var_quant_scales"
,
var_quant_scales
);
}
...
...
paddle/fluid/inference/api/paddle_pass_builder.cc
浏览文件 @
31f81685
...
...
@@ -430,7 +430,6 @@ void CpuPassStrategy::EnableMkldnnInt8() {
passes_
.
push_back
(
"simplify_with_basic_ops_pass"
);
passes_
.
push_back
(
"quant_dequant_mkldnn_pass"
);
passes_
.
push_back
(
"mkldnn_placement_pass"
);
passes_
.
push_back
(
"constant_folding_pass"
);
passes_
.
push_back
(
"squeeze2_transpose2_onednn_fuse_pass"
);
passes_
.
push_back
(
"layer_norm_fuse_pass"
);
passes_
.
push_back
(
"attention_lstm_fuse_pass"
);
...
...
@@ -485,6 +484,7 @@ void CpuPassStrategy::EnableMkldnnInt8() {
passes_
.
push_back
(
"quant_transpose2_dequant_onednn_fuse_pass"
);
passes_
.
push_back
(
"int8_scale_calculation_mkldnn_pass"
);
passes_
.
push_back
(
"params_quantization_mkldnn_pass"
);
passes_
.
push_back
(
"constant_folding_pass"
);
}
use_mkldnn_int8_
=
true
;
#else
...
...
python/paddle/static/quantization/tests/quant2_int8_lstm_model.py
浏览文件 @
31f81685
...
...
@@ -116,10 +116,9 @@ class TestLstmModelPTQ(unittest.TestCase):
config
.
switch_ir_optim
(
True
)
config
.
enable_mkldnn
()
config
.
disable_mkldnn_fc_passes
()
# fc passes caused dnnl error
config
.
pass_builder
().
insert_pass
(
5
,
"fc_lstm_fuse_pass"
)
config
.
set_mkldnn_cache_capacity
(
mkldnn_cache_capacity
)
if
mode
==
"ptq"
:
# This pass to work properly, must be added before fc_fuse_pass
config
.
pass_builder
().
insert_pass
(
5
,
"fc_lstm_fuse_pass"
)
config
.
enable_quantizer
()
config
.
quantizer_config
().
set_quant_data
(
warmup_data
)
config
.
quantizer_config
().
set_quant_batch_size
(
1
)
...
...
@@ -244,7 +243,7 @@ class TestLstmModelPTQ(unittest.TestCase):
)
(
quant_hx_acc
,
quant_ctc_acc
,
quant_fps
)
=
self
.
run_program
(
quant_model
+
"_int8"
,
quant_model
,
infer_data
,
num_threads
,
mkldnn_cache_capacity
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录