Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
db4ae6e6
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
db4ae6e6
编写于
8月 21, 2023
作者:
Z
zhanglirong1999
提交者:
GitHub
8月 21, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[OneDNN] cpu_quantize_pass fix (#56303)
* add cpu quantize pass unit test
上级
1f987a75
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
128 addition
and
10 deletion
+128
-10
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.cc
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.cc
+0
-10
test/cpp/fluid/mkldnn/CMakeLists.txt
test/cpp/fluid/mkldnn/CMakeLists.txt
+12
-0
test/cpp/fluid/mkldnn/test_mkldnn_cpu_quantize_pass.cc
test/cpp/fluid/mkldnn/test_mkldnn_cpu_quantize_pass.cc
+116
-0
未找到文件。
paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.cc
浏览文件 @
db4ae6e6
...
...
@@ -548,16 +548,6 @@ void CPUQuantizePass::QuantizeConv(Graph* graph,
conv_op
->
Op
()
->
SetAttr
(
"force_fp32_output"
,
true
);
}
// change threshold in bounded ReLu
if
(
conv_op
->
Op
()
->
GetAttrIfExists
<
std
::
string
>
(
"fuse_activation"
)
==
"relu6"
)
{
float
scale_out
=
PADDLE_GET_CONST
(
float
,
conv_op
->
Op
()
->
GetAttr
(
"Scale_out"
));
float
threshold
=
PADDLE_GET_CONST
(
float
,
conv_op
->
Op
()
->
GetAttr
(
"fuse_alpha"
));
conv_op
->
Op
()
->
SetAttr
(
"fuse_alpha"
,
scale_out
*
threshold
);
}
++
quantize_conv_count
;
};
...
...
test/cpp/fluid/mkldnn/CMakeLists.txt
浏览文件 @
db4ae6e6
...
...
@@ -10,6 +10,18 @@ cc_test(
device_context
enforce
generated_static_op
)
cc_test
(
test_mkldnn_cpu_quantize_pass
SRCS test_mkldnn_cpu_quantize_pass.cc
DEPS executor
op_registry
activation_op
conv_activation_mkldnn_fuse_pass
cpu_quantize_placement_pass
cpu_quantize_pass
phi
scope
device_context
)
set
(
TEST_MKLDNN_CACHING_DEPS
op_registry
...
...
test/cpp/fluid/mkldnn/test_mkldnn_cpu_quantize_pass.cc
0 → 100644
浏览文件 @
db4ae6e6
/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <fstream>
#include <iostream>
#include <unordered_map>
#include "paddle/fluid/framework/ir/fuse_pass_base.h"
#include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/ir/pass.h"
#include "paddle/fluid/framework/ir/pass_tester_helper.h"
#include "paddle/fluid/framework/op_desc.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/platform/enforce.h"
using
std
::
pair
;
using
std
::
string
;
using
std
::
unordered_map
;
DEFINE_bool
(
enable_mkldnn
,
true
,
"Enable MKLDNN"
);
namespace
paddle
{
namespace
pass
{
using
VarQuantScale
=
std
::
unordered_map
<
std
::
string
,
std
::
pair
<
bool
,
phi
::
DenseTensor
>>
;
static
float
const
SCALE
=
2.
f
;
const
std
::
vector
<
std
::
string
>
PreGraphPasses
({
"conv_activation_mkldnn_fuse_pass"
,
"cpu_quantize_placement_pass"
,
"cpu_quantize_pass"
,
});
TEST
(
cpuQuantizePass
,
ConvReLU6
)
{
paddle
::
framework
::
ProgramDesc
prog
;
auto
*
block
=
prog
.
MutableBlock
(
0
);
auto
*
conv2d_op
=
block
->
AppendOp
();
conv2d_op
->
SetType
(
"conv2d"
);
conv2d_op
->
SetInput
(
"Input"
,
{
"conv2d-X"
});
conv2d_op
->
SetInput
(
"Filter"
,
{
"conv2d-Y"
});
conv2d_op
->
SetOutput
(
"Output"
,
{
"conv2d-Out"
});
const
std
::
vector
<
int
>
strides
({
1
,
1
});
const
std
::
vector
<
int
>
paddings
({
1
,
1
});
const
std
::
vector
<
int
>
dilations
({
1
,
1
});
const
int
groups
=
1
;
conv2d_op
->
SetAttr
(
"strides"
,
strides
);
conv2d_op
->
SetAttr
(
"paddings"
,
paddings
);
conv2d_op
->
SetAttr
(
"dilations"
,
dilations
);
conv2d_op
->
SetAttr
(
"groups"
,
groups
);
auto
*
relu6_op
=
block
->
AppendOp
();
relu6_op
->
SetType
(
"relu6"
);
relu6_op
->
SetAttr
(
"threshold"
,
6.
f
);
relu6_op
->
SetInput
(
"X"
,
{
"conv2d-Out"
});
relu6_op
->
SetOutput
(
"Out"
,
{
"relu-Out"
});
auto
place
=
phi
::
CPUPlace
();
VarQuantScale
*
scales
=
new
VarQuantScale
();
phi
::
DenseTensor
scale_input_tensor
;
phi
::
DenseTensor
scale_weight_tensor
;
scale_input_tensor
.
Resize
({
1
});
scale_weight_tensor
.
Resize
({
1
});
auto
*
ptr_scale_input
=
scale_input_tensor
.
mutable_data
<
double
>
(
place
);
auto
*
ptr_scale_weight
=
scale_weight_tensor
.
mutable_data
<
double
>
(
place
);
ptr_scale_input
[
0
]
=
SCALE
;
ptr_scale_weight
[
0
]
=
SCALE
;
(
*
scales
)[
"conv2d-X"
]
=
std
::
make_pair
(
false
,
std
::
move
(
scale_input_tensor
));
(
*
scales
)[
"conv2d-Y"
]
=
std
::
make_pair
(
false
,
std
::
move
(
scale_weight_tensor
));
paddle
::
framework
::
Scope
scope
;
std
::
unique_ptr
<
paddle
::
framework
::
ir
::
Graph
>
graph
(
new
paddle
::
framework
::
ir
::
Graph
(
prog
));
(
graph
)
->
SetNotOwned
(
paddle
::
framework
::
ir
::
kParamScopeAttr
,
&
scope
);
for
(
const
auto
&
pass
:
PreGraphPasses
)
{
auto
pass_
=
paddle
::
framework
::
ir
::
PassRegistry
::
Instance
().
Get
(
pass
);
if
(
pass
==
"cpu_quantize_pass"
)
{
pass_
->
Set
(
"quant_var_scales"
,
scales
);
}
graph
.
reset
(
pass_
->
Apply
(
graph
.
release
()));
}
int
fused_conv2d_num
=
0
;
for
(
auto
*
node
:
graph
->
Nodes
())
{
if
(
node
->
IsOp
()
&&
node
->
Op
()
&&
node
->
Op
()
->
Type
()
==
"fused_conv2d"
)
{
CHECK_EQ
(
node
->
Op
()
->
GetAttrIfExists
<
float
>
(
"fuse_beta"
),
6
)
<<
"Attr fuse_beta must equal to 6."
;
fused_conv2d_num
++
;
}
}
CHECK_GT
(
fused_conv2d_num
,
0
)
<<
"Graph must contain fused_conv2d"
;
}
}
// namespace pass
}
// namespace paddle
USE_PASS
(
conv_activation_mkldnn_fuse_pass
);
USE_PASS
(
cpu_quantize_placement_pass
);
USE_PASS
(
cpu_quantize_pass
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录