Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
8a4f85fe
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
8a4f85fe
编写于
9月 29, 2020
作者:
P
Pei Yang
提交者:
GitHub
9月 29, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add unittests and OP version registry for quant_conv2d_dequant_fuse_pass (#27689)
上级
dec53a9c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
172 addition
and
19 deletion
+172
-19
paddle/fluid/framework/ir/quant_conv2d_dequant_fuse_pass.cc
paddle/fluid/framework/ir/quant_conv2d_dequant_fuse_pass.cc
+13
-0
python/paddle/fluid/tests/unittests/ir/inference/inference_pass_test.py
...fluid/tests/unittests/ir/inference/inference_pass_test.py
+71
-19
python/paddle/fluid/tests/unittests/ir/inference/test_trt_quant_conv2d_dequant_fuse_pass.py
...s/ir/inference/test_trt_quant_conv2d_dequant_fuse_pass.py
+88
-0
未找到文件。
paddle/fluid/framework/ir/quant_conv2d_dequant_fuse_pass.cc
浏览文件 @
8a4f85fe
...
@@ -19,6 +19,7 @@
...
@@ -19,6 +19,7 @@
#include "paddle/fluid/framework/ir/graph_viz_pass.h"
#include "paddle/fluid/framework/ir/graph_viz_pass.h"
#include "paddle/fluid/framework/ir/quant_conv2d_dequant_fuse_pass.h"
#include "paddle/fluid/framework/ir/quant_conv2d_dequant_fuse_pass.h"
#include "paddle/fluid/framework/op_version_registry.h"
namespace
paddle
{
namespace
paddle
{
namespace
framework
{
namespace
framework
{
...
@@ -284,3 +285,15 @@ void QuantDequantFusePass::ApplyImpl(ir::Graph* graph) const {
...
@@ -284,3 +285,15 @@ void QuantDequantFusePass::ApplyImpl(ir::Graph* graph) const {
REGISTER_PASS
(
quant_conv2d_dequant_fuse_pass
,
REGISTER_PASS
(
quant_conv2d_dequant_fuse_pass
,
paddle
::
framework
::
ir
::
QuantDequantFusePass
);
paddle
::
framework
::
ir
::
QuantDequantFusePass
);
REGISTER_PASS_CAPABILITY
(
tensorrt_subgraph_pass
)
.
AddCombination
(
paddle
::
framework
::
compatible
::
OpVersionComparatorCombination
()
.
EQ
(
"conv2d"
,
0
)
.
EQ
(
"fc"
,
0
)
.
LE
(
"conv2d_transpose"
,
1
)
.
EQ
(
"fake_quantize_abs_max"
,
0
)
.
EQ
(
"fake_quantize_range_abs_max"
,
0
)
.
EQ
(
"fake_quantize_moving_average_abs_max"
,
0
)
.
EQ
(
"fake_channel_wise_quantize_abs_max"
,
0
)
.
EQ
(
"fake_dequantize_max_abs"
,
0
));
python/paddle/fluid/tests/unittests/ir/inference/inference_pass_test.py
浏览文件 @
8a4f85fe
...
@@ -27,6 +27,10 @@ from paddle.fluid.core import PaddleDType
...
@@ -27,6 +27,10 @@ from paddle.fluid.core import PaddleDType
from
paddle.fluid.core
import
AnalysisConfig
from
paddle.fluid.core
import
AnalysisConfig
from
paddle.fluid.core
import
create_paddle_predictor
from
paddle.fluid.core
import
create_paddle_predictor
from
paddle.fluid.framework
import
IrGraph
from
paddle.fluid.contrib.slim.quantization
import
QuantizationTransformPass
from
paddle.fluid.contrib.slim.quantization
import
QuantizationFreezePass
class
InferencePassTest
(
unittest
.
TestCase
):
class
InferencePassTest
(
unittest
.
TestCase
):
def
__init__
(
self
,
methodName
=
'runTest'
):
def
__init__
(
self
,
methodName
=
'runTest'
):
...
@@ -48,22 +52,23 @@ class InferencePassTest(unittest.TestCase):
...
@@ -48,22 +52,23 @@ class InferencePassTest(unittest.TestCase):
def
_get_place
(
self
):
def
_get_place
(
self
):
return
set
([
False
,
core
.
is_compiled_with_cuda
()])
return
set
([
False
,
core
.
is_compiled_with_cuda
()])
def
_save_models
(
self
,
executor
,
program
):
def
_save_models
(
self
,
executor
,
program
,
scope
):
outs
=
executor
.
run
(
program
=
program
,
with
fluid
.
scope_guard
(
scope
):
feed
=
self
.
feeds
,
outs
=
executor
.
run
(
program
=
program
,
fetch_list
=
self
.
fetch_list
,
feed
=
self
.
feeds
,
return_numpy
=
False
)
fetch_list
=
self
.
fetch_list
,
# save models as combined to ensure that
return_numpy
=
False
)
# there won't be too many useless files
# save models as combined to ensure that
# after finishing a couple of tests.
# there won't be too many useless files
fluid
.
io
.
save_inference_model
(
# after finishing a couple of tests.
dirname
=
self
.
path
,
fluid
.
io
.
save_inference_model
(
feeded_var_names
=
list
(
self
.
feeds
.
keys
()),
dirname
=
self
.
path
,
target_vars
=
self
.
fetch_list
,
feeded_var_names
=
list
(
self
.
feeds
.
keys
()),
executor
=
executor
,
target_vars
=
self
.
fetch_list
,
main_program
=
program
,
executor
=
executor
,
model_filename
=
"model"
,
main_program
=
program
,
params_filename
=
"params"
)
model_filename
=
"model"
,
params_filename
=
"params"
)
return
outs
return
outs
...
@@ -133,7 +138,11 @@ class InferencePassTest(unittest.TestCase):
...
@@ -133,7 +138,11 @@ class InferencePassTest(unittest.TestCase):
for
place_
in
use_gpu
:
for
place_
in
use_gpu
:
self
.
check_output_with_option
(
place_
,
atol
)
self
.
check_output_with_option
(
place_
,
atol
)
def
check_output_with_option
(
self
,
use_gpu
,
atol
=
1e-5
,
flatten
=
False
):
def
check_output_with_option
(
self
,
use_gpu
,
atol
=
1e-5
,
flatten
=
False
,
quant
=
False
):
'''
'''
Check whether calculating on CPU and GPU, enable TensorRT
Check whether calculating on CPU and GPU, enable TensorRT
or disable TensorRT, enable MKLDNN or disable MKLDNN
or disable TensorRT, enable MKLDNN or disable MKLDNN
...
@@ -141,9 +150,52 @@ class InferencePassTest(unittest.TestCase):
...
@@ -141,9 +150,52 @@ class InferencePassTest(unittest.TestCase):
'''
'''
place
=
fluid
.
CUDAPlace
(
0
)
if
use_gpu
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_gpu
else
fluid
.
CPUPlace
()
executor
=
fluid
.
Executor
(
place
)
executor
=
fluid
.
Executor
(
place
)
scope
=
fluid
.
Scope
()
device
=
"GPU"
if
use_gpu
else
"CPU"
device
=
"GPU"
if
use_gpu
else
"CPU"
executor
.
run
(
self
.
startup_program
)
with
fluid
.
scope_guard
(
scope
):
outs
=
self
.
_save_models
(
executor
,
self
.
main_program
)
executor
.
run
(
self
.
startup_program
)
if
quant
:
main_graph
=
IrGraph
(
core
.
Graph
(
self
.
main_program
.
desc
),
for_test
=
True
)
transform_pass
=
QuantizationTransformPass
(
scope
=
scope
,
place
=
place
,
activation_quantize_type
=
self
.
activation_quant_type
,
weight_quantize_type
=
self
.
weight_quant_type
,
quantizable_op_type
=
[
'conv2d'
,
'mul'
,
'depthwise_conv2d'
,
'conv2d_transpose'
])
transform_pass
.
apply
(
main_graph
)
weight_scale_map
=
{
"conv2d"
:
"conv2d_0.w_0.scale"
,
"mul"
:
"fc_0.w_0.scale"
}
weight_scale_tensor
=
scope
.
var
(
weight_scale_map
[
self
.
quantized_op_type
]).
get_tensor
()
weight_scale
=
np
.
ones
(
self
.
channels
).
astype
(
"float32"
)
weight_scale_tensor
.
set
(
weight_scale
,
place
)
op_nodes
=
main_graph
.
all_op_nodes
()
for
op_node
in
op_nodes
:
if
op_node
.
name
()
in
[
self
.
quantized_op_type
,
"relu"
]:
op_node
.
op
().
_set_attr
(
"out_threshold"
,
0.5
)
with
fluid
.
scope_guard
(
scope
):
executor
.
run
(
program
=
self
.
main_program
,
feed
=
self
.
feeds
,
fetch_list
=
self
.
fetch_list
)
freeze_pass
=
QuantizationFreezePass
(
scope
=
scope
,
place
=
place
,
weight_quantize_type
=
self
.
weight_quant_type
)
freeze_pass
.
apply
(
main_graph
)
self
.
main_program
=
main_graph
.
to_program
()
outs
=
self
.
_save_models
(
executor
,
self
.
main_program
,
scope
)
analysis_outputs
=
self
.
_get_analysis_outputs
(
analysis_outputs
=
self
.
_get_analysis_outputs
(
self
.
_get_analysis_config
(
use_gpu
=
use_gpu
))
self
.
_get_analysis_config
(
use_gpu
=
use_gpu
))
...
...
python/paddle/fluid/tests/unittests/ir/inference/test_trt_quant_conv2d_dequant_fuse_pass.py
0 → 100644
浏览文件 @
8a4f85fe
# Copyright (c) 2020 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.
import
unittest
import
numpy
as
np
from
inference_pass_test
import
InferencePassTest
import
paddle.fluid
as
fluid
import
paddle.fluid.core
as
core
from
paddle.fluid.framework
import
IrGraph
from
paddle.fluid.core
import
PassVersionChecker
from
paddle.fluid.contrib.slim.quantization
import
QuantizationTransformPass
from
paddle.fluid.contrib.slim.quantization
import
QuantizationFreezePass
from
paddle.fluid.core
import
AnalysisConfig
class
QuantDequantTest
(
InferencePassTest
):
def
setUp
(
self
):
with
fluid
.
program_guard
(
self
.
main_program
,
self
.
startup_program
):
data
=
fluid
.
data
(
name
=
"data"
,
shape
=
[
-
1
,
3
,
32
,
32
],
dtype
=
"float32"
)
param_attr
=
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Constant
(
value
=
0.0
),
trainable
=
False
)
quantized_op_out
=
self
.
append_quantized_op
(
data
,
param_attr
)
relu_out
=
fluid
.
layers
.
relu
(
quantized_op_out
)
self
.
set_quant_pattern
()
self
.
feeds
=
{
"data"
:
np
.
random
.
random
([
1
,
3
,
32
,
32
]).
astype
(
"float32"
),
}
self
.
enable_trt
=
True
self
.
trt_parameters
=
QuantDequantTest
.
TensorRTParam
(
1
<<
30
,
32
,
0
,
AnalysisConfig
.
Precision
.
Int8
,
False
,
False
)
self
.
fetch_list
=
[
relu_out
]
def
append_quantized_op
(
self
,
x
,
param_attr
):
return
fluid
.
layers
.
conv2d
(
input
=
x
,
num_filters
=
3
,
filter_size
=
3
,
param_attr
=
param_attr
,
bias_attr
=
False
,
act
=
None
)
def
set_quant_pattern
(
self
):
self
.
activation_quant_type
=
'moving_average_abs_max'
self
.
weight_quant_type
=
'channel_wise_abs_max'
self
.
quantized_op_type
=
'conv2d'
self
.
channels
=
3
def
test_check_output
(
self
):
if
core
.
is_compiled_with_cuda
():
use_gpu
=
True
self
.
check_output_with_option
(
use_gpu
,
flatten
=
True
,
quant
=
True
)
self
.
assertTrue
(
PassVersionChecker
.
IsCompatible
(
'quant_conv2d_dequant_fuse_pass'
))
class
QuantFcDequantTest
(
QuantDequantTest
):
def
append_quantized_op
(
self
,
x
,
param_attr
):
return
fluid
.
layers
.
fc
(
x
,
size
=
100
,
num_flatten_dims
=
1
,
param_attr
=
param_attr
,
bias_attr
=
False
,
act
=
None
)
def
set_quant_pattern
(
self
):
self
.
activation_quant_type
=
'moving_average_abs_max'
self
.
weight_quant_type
=
'abs_max'
self
.
quantized_op_type
=
'mul'
self
.
channels
=
1
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录