Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
39565147
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看板
未验证
提交
39565147
编写于
8月 30, 2021
作者:
X
xiaoxiaohehe001
提交者:
GitHub
8月 30, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Paddle Inference-TRT]Adding six op unittest codes of TRT INT8 (#35130)
* add_op_unittest
上级
b94d7ff3
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
722 addition
and
50 deletion
+722
-50
python/paddle/fluid/tests/unittests/ir/inference/CMakeLists.txt
.../paddle/fluid/tests/unittests/ir/inference/CMakeLists.txt
+3
-0
python/paddle/fluid/tests/unittests/ir/inference/test_trt_conv_quant_dequant_pass.py
...nittests/ir/inference/test_trt_conv_quant_dequant_pass.py
+308
-0
python/paddle/fluid/tests/unittests/ir/inference/test_trt_fc_fuse_pass.py
...uid/tests/unittests/ir/inference/test_trt_fc_fuse_pass.py
+0
-50
python/paddle/fluid/tests/unittests/ir/inference/test_trt_fc_fuse_quant_dequant_pass.py
...tests/ir/inference/test_trt_fc_fuse_quant_dequant_pass.py
+207
-0
python/paddle/fluid/tests/unittests/ir/inference/test_trt_matmul_quant_dequant.py
...s/unittests/ir/inference/test_trt_matmul_quant_dequant.py
+204
-0
未找到文件。
python/paddle/fluid/tests/unittests/ir/inference/CMakeLists.txt
浏览文件 @
39565147
...
...
@@ -39,4 +39,7 @@ set_tests_properties(test_trt_pool_op PROPERTIES ENVIRONMENT FLAGS_fraction_of_g
set_tests_properties
(
test_trt_reduce_mean_op PROPERTIES TIMEOUT 60
)
set_tests_properties
(
test_trt_tile_op PROPERTIES TIMEOUT 60
)
set_tests_properties
(
test_trt_convert_conv2d PROPERTIES TIMEOUT 100
)
set_tests_properties
(
test_trt_fc_fuse_quant_dequant_pass PROPERTIES TIMEOUT 100
)
set_tests_properties
(
test_trt_conv_quant_dequant_pass PROPERTIES TIMEOUT 100
)
set_tests_properties
(
test_trt_matmul_quant_dequant PROPERTIES TIMEOUT 100
)
endif
()
python/paddle/fluid/tests/unittests/ir/inference/test_trt_conv_quant_dequant_pass.py
0 → 100644
浏览文件 @
39565147
# 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
os
import
shutil
import
unittest
import
numpy
as
np
from
inference_pass_test
import
InferencePassTest
from
quant_dequant_test
import
QuantDequantTest
import
paddle.fluid
as
fluid
import
paddle.fluid.core
as
core
from
paddle.fluid.core
import
PassVersionChecker
from
paddle.fluid.core
import
AnalysisConfig
class
QuantDequantTensorRTSubgraphPassConvTest
(
QuantDequantTest
):
def
setUp
(
self
):
self
.
set_params
()
def
network
():
self
.
data
=
fluid
.
data
(
name
=
'data'
,
shape
=
[
1
,
28
,
28
],
dtype
=
'float32'
)
data_reshape
=
fluid
.
layers
.
reshape
(
self
.
data
,
shape
=
[
1
,
4
,
14
,
14
])
self
.
label
=
fluid
.
data
(
name
=
'label'
,
shape
=
[
1
,
1
],
dtype
=
'int64'
)
label_shape
=
fluid
.
layers
.
reshape
(
self
.
label
,
shape
=
[
1
,
1
,
1
])
conv_out
=
fluid
.
layers
.
conv2d
(
input
=
data_reshape
,
num_filters
=
self
.
conv_num_filters
,
filter_size
=
self
.
conv_filter_size
,
groups
=
self
.
conv_groups
,
padding
=
self
.
conv_padding
,
bias_attr
=
False
,
use_cudnn
=
self
.
use_cudnn
,
act
=
None
)
if
self
.
conv_padding
==
[
1
,
1
]:
cout
=
fluid
.
layers
.
reshape
(
conv_out
,
shape
=
[
1
,
1
,
10816
])
elif
self
.
conv_padding
==
'VALID'
:
cout
=
fluid
.
layers
.
reshape
(
conv_out
,
shape
=
[
1
,
1
,
7744
])
elif
self
.
conv_padding
==
'SAME'
:
cout
=
fluid
.
layers
.
reshape
(
conv_out
,
shape
=
[
1
,
1
,
12544
])
elif
self
.
conv_groups
==
4
:
cout
=
fluid
.
layers
.
reshape
(
conv_out
,
shape
=
[
1
,
1
,
10816
])
result
=
fluid
.
layers
.
relu
(
cout
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
result
,
label
=
label_shape
)
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
return
avg_loss
,
result
self
.
main_program
.
random_seed
=
2
self
.
startup_program
.
random_seed
=
2
self
.
test_main_program
.
random_seed
=
2
#self.test_startup_program.random_seed = 2
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
main_program
,
self
.
startup_program
):
self
.
loss
,
result
=
network
()
opt
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.0001
)
opt
.
minimize
(
self
.
loss
)
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
test_main_program
,
self
.
startup_program
):
network
()
self
.
feeds
=
{
"data"
:
np
.
random
.
random
([
1
,
28
,
28
]).
astype
(
"float32"
)}
self
.
fetch_list
=
[
result
]
self
.
enable_trt
=
True
self
.
trt_parameters
=
QuantDequantTensorRTSubgraphPassConvTest
.
TensorRTParam
(
1
<<
30
,
32
,
0
,
AnalysisConfig
.
Precision
.
Int8
,
False
,
False
)
self
.
activation_quantize_type
=
'moving_average_abs_max'
self
.
weight_quantize_type
=
'channel_wise_abs_max'
def
set_params
(
self
):
self
.
conv_num_filters
=
64
self
.
conv_filter_size
=
4
self
.
conv_groups
=
1
self
.
conv_padding
=
[
1
,
1
]
self
.
use_cudnn
=
True
def
test_check_output
(
self
):
if
core
.
is_compiled_with_cuda
():
use_gpu
=
True
self
.
check_output_with_option
(
use_gpu
,
atol
=
1e-1
,
flatten
=
False
,
rtol
=
1e-1
)
self
.
assertTrue
(
PassVersionChecker
.
IsCompatible
(
'tensorrt_subgraph_pass'
))
class
QuantDequantTensorRTSubgraphPassConvValidPaddingTest
(
QuantDequantTensorRTSubgraphPassConvTest
):
def
set_params
(
self
):
self
.
conv_num_filters
=
64
self
.
conv_filter_size
=
4
self
.
conv_groups
=
1
self
.
conv_padding
=
'VALID'
self
.
use_cudnn
=
True
class
QuantDequantTensorRTSubgraphPassConvSamePaddingTest
(
QuantDequantTensorRTSubgraphPassConvTest
):
def
set_params
(
self
):
self
.
conv_num_filters
=
64
self
.
conv_filter_size
=
4
self
.
conv_groups
=
1
self
.
conv_padding
=
'SAME'
self
.
use_cudnn
=
True
class
QuantDequantTensorRTSubgraphPassDWConvTest
(
QuantDequantTensorRTSubgraphPassConvTest
):
def
set_params
(
self
):
self
.
conv_num_filters
=
64
self
.
conv_filter_size
=
4
self
.
conv_groups
=
4
self
.
conv_padding
=
[
1
,
1
]
self
.
use_cudnn
=
True
class
DynamicShapeQuantDequantTensorRTSubgraphPassConvTest
(
QuantDequantTest
):
def
setUp
(
self
):
self
.
set_params
()
def
network
():
self
.
data
=
fluid
.
data
(
name
=
'data'
,
shape
=
[
1
,
28
,
28
],
dtype
=
'float32'
)
data_reshape
=
fluid
.
layers
.
reshape
(
self
.
data
,
shape
=
[
1
,
4
,
14
,
14
])
self
.
label
=
fluid
.
data
(
name
=
'label'
,
shape
=
[
1
,
1
],
dtype
=
'int64'
)
label_shape
=
fluid
.
layers
.
reshape
(
self
.
label
,
shape
=
[
1
,
1
,
1
])
conv_out
=
fluid
.
layers
.
conv2d
(
input
=
data_reshape
,
num_filters
=
self
.
conv_num_filters
,
filter_size
=
self
.
conv_filter_size
,
groups
=
self
.
conv_groups
,
padding
=
self
.
conv_padding
,
bias_attr
=
False
,
use_cudnn
=
self
.
use_cudnn
,
act
=
None
)
cout
=
fluid
.
layers
.
reshape
(
conv_out
,
shape
=
[
1
,
1
,
10816
])
result
=
fluid
.
layers
.
relu
(
cout
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
result
,
label
=
label_shape
)
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
return
avg_loss
,
result
self
.
main_program
.
random_seed
=
2
self
.
startup_program
.
random_seed
=
2
self
.
test_main_program
.
random_seed
=
2
#self.test_startup_program.random_seed = 2
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
main_program
,
self
.
startup_program
):
self
.
loss
,
result
=
network
()
opt
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.0001
)
opt
.
minimize
(
self
.
loss
)
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
test_main_program
,
self
.
startup_program
):
network
()
self
.
feeds
=
{
"data"
:
np
.
random
.
random
([
1
,
28
,
28
]).
astype
(
"float32"
)}
self
.
fetch_list
=
[
result
]
self
.
enable_trt
=
True
self
.
trt_parameters
=
DynamicShapeQuantDequantTensorRTSubgraphPassConvTest
.
TensorRTParam
(
1
<<
30
,
32
,
0
,
AnalysisConfig
.
Precision
.
Int8
,
False
,
False
)
self
.
dynamic_shape_params
=
DynamicShapeQuantDequantTensorRTSubgraphPassConvTest
.
DynamicShapeParam
(
{
"conv2d_0.tmp_0"
:
[
1
,
4
,
14
,
14
],
"data"
:
[
1
,
28
,
28
],
"depthwise_conv2d_0.tmp_0"
:
[
1
,
4
,
14
,
14
],
"reshape2_0.tmp_0"
:
[
1
,
4
,
14
,
14
],
"reshape2_2.tmp_0"
:
[
1
,
1
,
10816
]
},
{
"conv2d_0.tmp_0"
:
[
4
,
4
,
14
,
14
],
"data"
:
[
4
,
28
,
28
],
"depthwise_conv2d_0.tmp_0"
:
[
4
,
4
,
14
,
14
],
"reshape2_0.tmp_0"
:
[
4
,
4
,
14
,
14
],
"reshape2_2.tmp_0"
:
[
1
,
1
,
43264
]
},
{
"conv2d_0.tmp_0"
:
[
1
,
4
,
14
,
14
],
"data"
:
[
1
,
28
,
28
],
"depthwise_conv2d_0.tmp_0"
:
[
1
,
4
,
14
,
14
],
"reshape2_0.tmp_0"
:
[
1
,
4
,
14
,
14
],
"reshape2_2.tmp_0"
:
[
1
,
1
,
10816
]
},
False
)
self
.
activation_quantize_type
=
'moving_average_abs_max'
self
.
weight_quantize_type
=
'channel_wise_abs_max'
def
set_params
(
self
):
self
.
conv_num_filters
=
64
self
.
conv_filter_size
=
4
self
.
conv_groups
=
1
self
.
conv_padding
=
[
1
,
1
]
self
.
use_cudnn
=
True
def
test_check_output
(
self
):
if
core
.
is_compiled_with_cuda
():
use_gpu
=
True
self
.
check_output_with_option
(
use_gpu
,
atol
=
1e-1
,
flatten
=
False
,
rtol
=
1e-1
)
self
.
assertTrue
(
PassVersionChecker
.
IsCompatible
(
'tensorrt_subgraph_pass'
))
class
QuantDequantTensorRTSubgraphPassConvTransposeTest
(
QuantDequantTest
):
def
setUp
(
self
):
self
.
set_params
()
def
network
():
self
.
data
=
fluid
.
data
(
name
=
'data'
,
shape
=
[
1
,
28
,
28
],
dtype
=
'float32'
)
data_reshape
=
fluid
.
layers
.
reshape
(
self
.
data
,
shape
=
[
1
,
4
,
14
,
14
])
self
.
label
=
fluid
.
data
(
name
=
'label'
,
shape
=
[
1
,
1
],
dtype
=
'int64'
)
label_shape
=
fluid
.
layers
.
reshape
(
self
.
label
,
shape
=
[
1
,
1
,
1
])
conv_out
=
fluid
.
layers
.
conv2d_transpose
(
input
=
data_reshape
,
num_filters
=
self
.
conv_num_filters
,
filter_size
=
self
.
conv_filter_size
,
groups
=
self
.
conv_groups
,
padding
=
self
.
conv_padding
,
bias_attr
=
False
,
use_cudnn
=
self
.
use_cudnn
,
act
=
None
)
if
self
.
conv_padding
==
[
1
,
1
]:
cout
=
fluid
.
layers
.
reshape
(
conv_out
,
shape
=
[
1
,
1
,
14400
])
elif
self
.
conv_padding
==
'VALID'
:
cout
=
fluid
.
layers
.
reshape
(
conv_out
,
shape
=
[
1
,
1
,
18496
])
elif
self
.
conv_padding
==
'SAME'
:
cout
=
fluid
.
layers
.
reshape
(
conv_out
,
shape
=
[
1
,
1
,
12544
])
elif
self
.
conv_groups
==
4
:
cout
=
fluid
.
layers
.
reshape
(
conv_out
,
shape
=
[
1
,
1
,
10816
])
result
=
fluid
.
layers
.
relu
(
cout
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
result
,
label
=
label_shape
)
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
return
avg_loss
,
result
self
.
main_program
.
random_seed
=
2
self
.
startup_program
.
random_seed
=
2
self
.
test_main_program
.
random_seed
=
2
#self.test_startup_program.random_seed = 2
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
main_program
,
self
.
startup_program
):
self
.
loss
,
result
=
network
()
opt
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.0001
)
opt
.
minimize
(
self
.
loss
)
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
test_main_program
,
self
.
startup_program
):
network
()
self
.
feeds
=
{
"data"
:
np
.
random
.
random
([
1
,
28
,
28
]).
astype
(
"float32"
)}
self
.
fetch_list
=
[
result
]
self
.
enable_trt
=
True
self
.
trt_parameters
=
QuantDequantTensorRTSubgraphPassConvTransposeTest
.
TensorRTParam
(
1
<<
30
,
32
,
0
,
AnalysisConfig
.
Precision
.
Int8
,
False
,
False
)
self
.
activation_quantize_type
=
'moving_average_abs_max'
self
.
weight_quantize_type
=
'channel_wise_abs_max'
def
set_params
(
self
):
self
.
conv_num_filters
=
64
self
.
conv_filter_size
=
4
self
.
conv_groups
=
1
self
.
conv_padding
=
[
1
,
1
]
self
.
use_cudnn
=
True
def
test_check_output
(
self
):
if
core
.
is_compiled_with_cuda
():
use_gpu
=
True
self
.
check_output_with_option
(
use_gpu
,
atol
=
1e-1
,
flatten
=
False
,
rtol
=
1e-1
)
self
.
assertTrue
(
PassVersionChecker
.
IsCompatible
(
'tensorrt_subgraph_pass'
))
class
QuantDequantTensorRTSubgraphPassConvTransValidPaddingTest
(
QuantDequantTensorRTSubgraphPassConvTransposeTest
):
def
set_params
(
self
):
self
.
conv_num_filters
=
64
self
.
conv_filter_size
=
4
self
.
conv_groups
=
1
self
.
conv_padding
=
'VALID'
self
.
use_cudnn
=
True
class
QuantDequantTensorRTSubgraphPassConvTransSamePaddingTest
(
QuantDequantTensorRTSubgraphPassConvTransposeTest
):
def
set_params
(
self
):
self
.
conv_num_filters
=
64
self
.
conv_filter_size
=
4
self
.
conv_groups
=
1
self
.
conv_padding
=
'SAME'
self
.
use_cudnn
=
True
class
QuantDequantTensorRTSubgraphPassTransDWConvTest
(
QuantDequantTensorRTSubgraphPassConvTransposeTest
):
def
set_params
(
self
):
self
.
conv_num_filters
=
64
self
.
conv_filter_size
=
4
self
.
conv_groups
=
4
self
.
conv_padding
=
[
1
,
1
]
self
.
use_cudnn
=
True
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/ir/inference/test_trt_fc_fuse_pass.py
浏览文件 @
39565147
...
...
@@ -285,55 +285,5 @@ class FCFusePassTRTDynamicDims4Cols3Test(InferencePassTest):
self
.
check_output_with_option
(
use_gpu
[
i
])
class
FcQuantDequantFusePassTRTTest
(
QuantDequantTest
):
def
setUp
(
self
):
def
network
():
self
.
data
=
fluid
.
data
(
name
=
'data'
,
shape
=
[
1
,
28
,
28
],
dtype
=
'float32'
)
self
.
label
=
fluid
.
data
(
name
=
'label'
,
shape
=
[
1
,
1
],
dtype
=
'int64'
)
fc_out
=
fluid
.
layers
.
fc
(
input
=
self
.
data
,
size
=
10
,
num_flatten_dims
=
1
,
bias_attr
=
False
,
act
=
None
)
result
=
fluid
.
layers
.
relu
(
fc_out
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
result
,
label
=
self
.
label
)
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
return
avg_loss
,
result
self
.
main_program
.
random_seed
=
2
self
.
startup_program
.
random_seed
=
2
self
.
test_main_program
.
random_seed
=
2
#self.test_startup_program.random_seed = 2
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
main_program
,
self
.
startup_program
):
self
.
loss
,
result
=
network
()
opt
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.0001
)
opt
.
minimize
(
self
.
loss
)
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
test_main_program
,
self
.
startup_program
):
network
()
self
.
feeds
=
{
"data"
:
np
.
random
.
random
((
1
,
28
,
28
)).
astype
(
"float32"
)}
self
.
fetch_list
=
[
result
]
self
.
enable_trt
=
True
self
.
trt_parameters
=
FcQuantDequantFusePassTRTTest
.
TensorRTParam
(
1
<<
30
,
32
,
0
,
AnalysisConfig
.
Precision
.
Int8
,
False
,
False
)
self
.
activation_quantize_type
=
'moving_average_abs_max'
self
.
weight_quantize_type
=
'channel_wise_abs_max'
def
test_check_output
(
self
):
#self.quant_dequant()
if
core
.
is_compiled_with_cuda
():
use_gpu
=
True
self
.
check_output_with_option
(
use_gpu
,
atol
=
1e-2
,
flatten
=
False
,
rtol
=
1e-2
)
self
.
assertTrue
(
PassVersionChecker
.
IsCompatible
(
'quant_conv2d_dequant_fuse_pass'
))
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/ir/inference/test_trt_fc_fuse_quant_dequant_pass.py
0 → 100644
浏览文件 @
39565147
# 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.
from
__future__
import
print_function
import
unittest
import
numpy
as
np
from
inference_pass_test
import
InferencePassTest
from
quant_dequant_test
import
QuantDequantTest
import
paddle.fluid
as
fluid
import
paddle.fluid.core
as
core
from
paddle.fluid.core
import
AnalysisConfig
from
paddle.fluid.core
import
PassVersionChecker
class
FCQuantDequantFusePassTRTDims3Cols1Test
(
QuantDequantTest
):
def
setUp
(
self
):
def
network
():
self
.
data
=
fluid
.
data
(
name
=
'data'
,
shape
=
[
1
,
28
,
28
],
dtype
=
'float32'
)
self
.
label
=
fluid
.
data
(
name
=
'label'
,
shape
=
[
1
,
1
],
dtype
=
'int64'
)
fc_out
=
fluid
.
layers
.
fc
(
input
=
self
.
data
,
size
=
10
,
num_flatten_dims
=
1
,
bias_attr
=
False
,
act
=
"relu"
)
result
=
fluid
.
layers
.
relu
(
fc_out
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
result
,
label
=
self
.
label
)
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
return
avg_loss
,
result
self
.
main_program
.
random_seed
=
2
self
.
startup_program
.
random_seed
=
2
self
.
test_main_program
.
random_seed
=
2
#self.test_startup_program.random_seed = 2
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
main_program
,
self
.
startup_program
):
self
.
loss
,
result
=
network
()
opt
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.0001
)
opt
.
minimize
(
self
.
loss
)
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
test_main_program
,
self
.
startup_program
):
network
()
self
.
feeds
=
{
"data"
:
np
.
random
.
random
((
1
,
28
,
28
)).
astype
(
"float32"
)}
self
.
fetch_list
=
[
result
]
self
.
enable_trt
=
True
self
.
trt_parameters
=
FCQuantDequantFusePassTRTDims3Cols1Test
.
TensorRTParam
(
1
<<
30
,
32
,
0
,
AnalysisConfig
.
Precision
.
Int8
,
False
,
False
)
self
.
dynamic_shape_params
=
FCQuantDequantFusePassTRTDims3Cols1Test
.
DynamicShapeParam
(
{
'data'
:
[
1
,
28
,
28
],
'reshape2_1.tmp_0'
:
[
1
,
1
,
10
]
},
{
'data'
:
[
2
,
28
,
28
],
'reshape2_1.tmp_0'
:
[
2
,
1
,
10
]},
{
'data'
:
[
1
,
28
,
28
],
'reshape2_1.tmp_0'
:
[
1
,
1
,
10
]},
False
)
self
.
activation_quantize_type
=
'moving_average_abs_max'
self
.
weight_quantize_type
=
'channel_wise_abs_max'
def
test_check_output
(
self
):
#self.quant_dequant()
if
core
.
is_compiled_with_cuda
():
use_gpu
=
True
self
.
check_output_with_option
(
use_gpu
,
atol
=
1e-2
,
flatten
=
False
,
rtol
=
1e-2
)
self
.
assertTrue
(
PassVersionChecker
.
IsCompatible
(
'quant_conv2d_dequant_fuse_pass'
))
class
FCQuantDequantFusePassTRTDims3Cols2Test
(
QuantDequantTest
):
def
setUp
(
self
):
def
network
():
self
.
data
=
fluid
.
data
(
name
=
'data'
,
shape
=
[
1
,
28
,
28
],
dtype
=
'float32'
)
self
.
label
=
fluid
.
data
(
name
=
'label'
,
shape
=
[
1
,
1
],
dtype
=
'int64'
)
label_shape
=
fluid
.
layers
.
reshape
(
self
.
label
,
shape
=
[
1
,
1
,
1
])
fc_out
=
fluid
.
layers
.
fc
(
input
=
self
.
data
,
size
=
28
,
num_flatten_dims
=
2
,
bias_attr
=
False
,
act
=
None
)
c_out
=
fluid
.
layers
.
reshape
(
fc_out
,
shape
=
[
1
,
1
,
784
])
result
=
fluid
.
layers
.
relu
(
c_out
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
result
,
label
=
label_shape
)
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
return
avg_loss
,
result
self
.
main_program
.
random_seed
=
2
self
.
startup_program
.
random_seed
=
2
self
.
test_main_program
.
random_seed
=
2
#self.test_startup_program.random_seed = 2
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
main_program
,
self
.
startup_program
):
self
.
loss
,
result
=
network
()
opt
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.0001
)
opt
.
minimize
(
self
.
loss
)
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
test_main_program
,
self
.
startup_program
):
network
()
self
.
feeds
=
{
"data"
:
np
.
random
.
random
((
1
,
28
,
28
)).
astype
(
"float32"
)}
self
.
fetch_list
=
[
result
]
self
.
enable_trt
=
True
self
.
trt_parameters
=
FCQuantDequantFusePassTRTDims3Cols2Test
.
TensorRTParam
(
1
<<
30
,
32
,
0
,
AnalysisConfig
.
Precision
.
Int8
,
False
,
False
)
self
.
dynamic_shape_params
=
FCQuantDequantFusePassTRTDims3Cols2Test
.
DynamicShapeParam
(
{
'data'
:
[
1
,
28
,
28
],
'reshape2_1.tmp_0'
:
[
1
,
1
,
784
]
},
{
'data'
:
[
4
,
28
,
28
],
'reshape2_1.tmp_0'
:
[
4
,
1
,
784
]},
{
'data'
:
[
1
,
28
,
28
],
'reshape2_1.tmp_0'
:
[
1
,
1
,
784
]},
False
)
self
.
activation_quantize_type
=
'moving_average_abs_max'
self
.
weight_quantize_type
=
'channel_wise_abs_max'
def
test_check_output
(
self
):
#self.quant_dequant()
if
core
.
is_compiled_with_cuda
():
use_gpu
=
True
self
.
check_output_with_option
(
use_gpu
,
atol
=
1e-1
,
flatten
=
False
,
rtol
=
1e-1
)
self
.
assertTrue
(
PassVersionChecker
.
IsCompatible
(
'quant_conv2d_dequant_fuse_pass'
))
class
FCQuantDequantFusePassTRTDims3Cols3Test
(
QuantDequantTest
):
def
setUp
(
self
):
def
network
():
self
.
data
=
fluid
.
data
(
name
=
'data'
,
shape
=
[
1
,
28
,
28
],
dtype
=
'float32'
)
self
.
label
=
fluid
.
data
(
name
=
'label'
,
shape
=
[
1
,
1
],
dtype
=
'int64'
)
label_shape
=
fluid
.
layers
.
reshape
(
self
.
label
,
shape
=
[
1
,
1
,
1
])
reshape_out
=
fluid
.
layers
.
reshape
(
self
.
data
,
shape
=
[
1
,
14
,
14
,
4
])
fc_out
=
fluid
.
layers
.
fc
(
input
=
reshape_out
,
size
=
14
,
num_flatten_dims
=
3
,
bias_attr
=
False
,
act
=
None
)
c_out
=
fluid
.
layers
.
reshape
(
fc_out
,
shape
=
[
1
,
1
,
2744
])
result
=
fluid
.
layers
.
relu
(
c_out
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
result
,
label
=
label_shape
)
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
return
avg_loss
,
result
self
.
main_program
.
random_seed
=
2
self
.
startup_program
.
random_seed
=
2
self
.
test_main_program
.
random_seed
=
2
#self.test_startup_program.random_seed = 2
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
main_program
,
self
.
startup_program
):
self
.
loss
,
result
=
network
()
opt
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.0001
)
opt
.
minimize
(
self
.
loss
)
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
test_main_program
,
self
.
startup_program
):
network
()
self
.
feeds
=
{
"data"
:
np
.
random
.
random
((
1
,
28
,
28
)).
astype
(
"float32"
)}
self
.
fetch_list
=
[
result
]
self
.
enable_trt
=
True
self
.
trt_parameters
=
FCQuantDequantFusePassTRTDims3Cols3Test
.
TensorRTParam
(
1
<<
30
,
32
,
0
,
AnalysisConfig
.
Precision
.
Int8
,
False
,
False
)
self
.
dynamic_shape_params
=
FCQuantDequantFusePassTRTDims3Cols3Test
.
DynamicShapeParam
(
{
'data'
:
[
1
,
28
,
28
],
"reshape2_1.tmp_0"
:
[
1
,
14
,
14
,
4
],
"reshape2_2.tmp_0"
:
[
1
,
1
,
2744
]
},
{
'data'
:
[
4
,
28
,
28
],
"reshape2_1.tmp_0"
:
[
4
,
14
,
14
,
4
],
"reshape2_2.tmp_0"
:
[
4
,
1
,
2744
]
},
{
'data'
:
[
1
,
28
,
28
],
"reshape2_1.tmp_0"
:
[
1
,
14
,
14
,
4
],
"reshape2_2.tmp_0"
:
[
1
,
1
,
2744
]
},
False
)
self
.
activation_quantize_type
=
'moving_average_abs_max'
self
.
weight_quantize_type
=
'channel_wise_abs_max'
def
test_check_output
(
self
):
#self.quant_dequant()
if
core
.
is_compiled_with_cuda
():
use_gpu
=
True
self
.
check_output_with_option
(
use_gpu
,
atol
=
1e0
,
flatten
=
False
,
rtol
=
1e0
)
self
.
assertTrue
(
PassVersionChecker
.
IsCompatible
(
'quant_conv2d_dequant_fuse_pass'
))
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/ir/inference/test_trt_matmul_quant_dequant.py
0 → 100644
浏览文件 @
39565147
# 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
from
quant_dequant_test
import
QuantDequantTest
import
paddle.fluid
as
fluid
import
paddle.fluid.core
as
core
from
paddle.fluid.core
import
PassVersionChecker
from
paddle.fluid.core
import
AnalysisConfig
class
TensorRTMatMulQuantDequantDims3Test
(
QuantDequantTest
):
def
setUp
(
self
):
self
.
set_params
()
def
network
():
self
.
data
=
fluid
.
data
(
name
=
'data'
,
shape
=
[
1
,
28
,
28
],
dtype
=
'float32'
)
self
.
label
=
fluid
.
data
(
name
=
'label'
,
shape
=
[
1
,
1
],
dtype
=
'int64'
)
matmul_out
=
fluid
.
layers
.
matmul
(
x
=
self
.
data
,
y
=
self
.
data
,
transpose_x
=
self
.
transpose_x
,
transpose_y
=
self
.
transpose_y
,
alpha
=
self
.
alpha
)
fc_out
=
fluid
.
layers
.
fc
(
input
=
matmul_out
,
size
=
10
,
num_flatten_dims
=
1
,
bias_attr
=
False
,
act
=
None
)
result
=
fluid
.
layers
.
relu
(
fc_out
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
result
,
label
=
self
.
label
)
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
return
avg_loss
,
result
self
.
main_program
.
random_seed
=
2
self
.
startup_program
.
random_seed
=
2
self
.
test_main_program
.
random_seed
=
2
#self.test_startup_program.random_seed = 2
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
main_program
,
self
.
startup_program
):
self
.
loss
,
result
=
network
()
opt
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.0001
)
opt
.
minimize
(
self
.
loss
)
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
test_main_program
,
self
.
startup_program
):
network
()
self
.
feeds
=
{
"data"
:
np
.
random
.
random
([
1
,
28
,
28
]).
astype
(
"float32"
)}
self
.
fetch_list
=
[
result
]
self
.
enable_trt
=
True
self
.
trt_parameters
=
TensorRTMatMulQuantDequantDims3Test
.
TensorRTParam
(
1
<<
30
,
32
,
0
,
AnalysisConfig
.
Precision
.
Int8
,
False
,
False
)
self
.
activation_quantize_type
=
'moving_average_abs_max'
self
.
weight_quantize_type
=
'channel_wise_abs_max'
def
set_params
(
self
):
self
.
transpose_x
=
False
self
.
transpose_y
=
False
self
.
alpha
=
1.0
def
test_check_output
(
self
):
#self.quant_dequant()
if
core
.
is_compiled_with_cuda
():
use_gpu
=
True
self
.
check_output_with_option
(
use_gpu
,
atol
=
1e-1
,
flatten
=
False
,
rtol
=
1e-1
)
self
.
assertTrue
(
PassVersionChecker
.
IsCompatible
(
'tensorrt_subgraph_pass'
))
class
TensorRTMatMulQuantDequantDims3TransposeXTest
(
TensorRTMatMulQuantDequantDims3Test
):
def
set_params
(
self
):
self
.
transpose_x
=
True
self
.
transpose_y
=
False
self
.
alpha
=
1.0
class
TensorRTMatMulQuantDequantDims3TransposeYTest
(
TensorRTMatMulQuantDequantDims3Test
):
def
set_params
(
self
):
self
.
transpose_x
=
False
self
.
transpose_y
=
True
self
.
alpha
=
1.0
class
TensorRTMatMulQuantDequantDims3TransposeXYTest
(
TensorRTMatMulQuantDequantDims3Test
):
def
set_params
(
self
):
self
.
transpose_x
=
True
self
.
transpose_y
=
True
self
.
alpha
=
1.0
class
TensorRTMatMulQuantDequantDims4Test
(
QuantDequantTest
):
def
setUp
(
self
):
self
.
set_params
()
def
network
():
self
.
data
=
fluid
.
data
(
name
=
'data'
,
shape
=
[
1
,
28
,
28
],
dtype
=
'float32'
)
self
.
label
=
fluid
.
data
(
name
=
'label'
,
shape
=
[
1
,
1
],
dtype
=
'int64'
)
reshape_out
=
fluid
.
layers
.
reshape
(
self
.
data
,
shape
=
[
1
,
4
,
14
,
14
])
matmul_out
=
fluid
.
layers
.
matmul
(
x
=
reshape_out
,
y
=
reshape_out
,
transpose_x
=
self
.
transpose_x
,
transpose_y
=
self
.
transpose_y
,
alpha
=
self
.
alpha
)
out
=
fluid
.
layers
.
batch_norm
(
matmul_out
,
is_test
=
True
)
fc_out
=
fluid
.
layers
.
fc
(
input
=
matmul_out
,
size
=
10
,
num_flatten_dims
=
1
,
bias_attr
=
False
,
act
=
None
)
result
=
fluid
.
layers
.
relu
(
fc_out
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
result
,
label
=
self
.
label
)
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
return
avg_loss
,
result
self
.
main_program
.
random_seed
=
2
self
.
startup_program
.
random_seed
=
2
self
.
test_main_program
.
random_seed
=
2
#self.test_startup_program.random_seed = 2
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
main_program
,
self
.
startup_program
):
self
.
loss
,
result
=
network
()
opt
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.0001
)
opt
.
minimize
(
self
.
loss
)
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
self
.
test_main_program
,
self
.
startup_program
):
network
()
self
.
feeds
=
{
"data"
:
np
.
random
.
random
([
1
,
28
,
28
]).
astype
(
"float32"
)}
self
.
fetch_list
=
[
result
]
self
.
enable_trt
=
True
self
.
trt_parameters
=
TensorRTMatMulQuantDequantDims4Test
.
TensorRTParam
(
1
<<
30
,
32
,
0
,
AnalysisConfig
.
Precision
.
Int8
,
False
,
False
)
self
.
activation_quantize_type
=
'moving_average_abs_max'
self
.
weight_quantize_type
=
'channel_wise_abs_max'
def
set_params
(
self
):
self
.
transpose_x
=
False
self
.
transpose_y
=
False
self
.
alpha
=
1.0
def
test_check_output
(
self
):
#self.quant_dequant()
if
core
.
is_compiled_with_cuda
():
use_gpu
=
True
self
.
check_output_with_option
(
use_gpu
,
atol
=
1e-1
,
flatten
=
False
,
rtol
=
1e-1
)
self
.
assertTrue
(
PassVersionChecker
.
IsCompatible
(
'tensorrt_subgraph_pass'
))
class
TensorRTMatMulQuantDequantDims4TransposeXTest
(
TensorRTMatMulQuantDequantDims4Test
):
def
set_params
(
self
):
self
.
transpose_x
=
True
self
.
transpose_y
=
False
self
.
alpha
=
1.0
class
TensorRTMatMulQuantDequantDims4TransposeYTest
(
TensorRTMatMulQuantDequantDims4Test
):
def
set_params
(
self
):
self
.
transpose_x
=
False
self
.
transpose_y
=
True
self
.
alpha
=
1.0
class
TensorRTMatMulQuantDequantDims4TransposeXYTest
(
TensorRTMatMulQuantDequantDims4Test
):
def
set_params
(
self
):
self
.
transpose_x
=
True
self
.
transpose_y
=
True
self
.
alpha
=
1.0
class
TensorRTMatMulQuantDequantDims4ScaleTest
(
TensorRTMatMulQuantDequantDims4Test
):
def
set_params
(
self
):
self
.
transpose_x
=
False
self
.
transpose_y
=
False
self
.
alpha
=
2.0
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录