Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
0cb50bb9
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看板
提交
0cb50bb9
编写于
3月 06, 2019
作者:
Z
Zhen Wang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
avoid ce fails on windows.
上级
f5a37518
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
89 addition
and
72 deletion
+89
-72
python/paddle/fluid/contrib/slim/tests/test_quantization_pass.py
...paddle/fluid/contrib/slim/tests/test_quantization_pass.py
+89
-72
未找到文件。
python/paddle/fluid/contrib/slim/tests/test_quantization_pass.py
浏览文件 @
0cb50bb9
...
@@ -123,7 +123,7 @@ class TestQuantizationTransformPass(unittest.TestCase):
...
@@ -123,7 +123,7 @@ class TestQuantizationTransformPass(unittest.TestCase):
arg_name
.
endswith
(
'.quantized.dequantized'
))
arg_name
.
endswith
(
'.quantized.dequantized'
))
self
.
assertTrue
(
arg_name
in
quantized_ops
)
self
.
assertTrue
(
arg_name
in
quantized_ops
)
def
linear_fc_quant
(
self
,
quant_type
):
def
linear_fc_quant
(
self
,
quant_type
,
enable_ce
=
False
):
main
=
fluid
.
Program
()
main
=
fluid
.
Program
()
startup
=
fluid
.
Program
()
startup
=
fluid
.
Program
()
with
fluid
.
program_guard
(
main
,
startup
):
with
fluid
.
program_guard
(
main
,
startup
):
...
@@ -138,29 +138,29 @@ class TestQuantizationTransformPass(unittest.TestCase):
...
@@ -138,29 +138,29 @@ class TestQuantizationTransformPass(unittest.TestCase):
place
=
place
,
place
=
place
,
activation_quantize_type
=
quant_type
)
activation_quantize_type
=
quant_type
)
transform_pass
.
apply
(
graph
)
transform_pass
.
apply
(
graph
)
marked_nodes
=
set
()
if
not
enable_ce
:
for
op
in
graph
.
all_op_nodes
():
marked_nodes
=
set
()
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
for
op
in
graph
.
all_op_nodes
():
marked_nodes
.
add
(
op
)
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
graph
.
draw
(
'.'
,
'quantize_fc_'
+
quant_type
,
marked_nodes
)
marked_nodes
.
add
(
op
)
graph
.
draw
(
'.'
,
'quantize_fc_'
+
quant_type
,
marked_nodes
)
program
=
graph
.
to_program
()
program
=
graph
.
to_program
()
self
.
check_program
(
transform_pass
,
program
)
self
.
check_program
(
transform_pass
,
program
)
val_graph
=
IrGraph
(
core
.
Graph
(
program
.
desc
),
for_test
=
False
)
val_graph
=
IrGraph
(
core
.
Graph
(
program
.
desc
),
for_test
=
False
)
val_marked_nodes
=
set
()
if
not
enable_ce
:
for
op
in
val_graph
.
all_op_nodes
():
val_marked_nodes
=
set
()
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
for
op
in
val_graph
.
all_op_nodes
():
val_marked_nodes
.
add
(
op
)
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
val_graph
.
draw
(
'.'
,
'val_fc_'
+
quant_type
,
val_marked_nodes
)
val_marked_nodes
.
add
(
op
)
val_graph
.
draw
(
'.'
,
'val_fc_'
+
quant_type
,
val_marked_nodes
)
def
test_linear_fc_quant_abs_max
(
self
):
def
test_linear_fc_quant_abs_max
(
self
):
self
.
act_quant_op_type
=
'fake_quantize_abs_max'
self
.
linear_fc_quant
(
'abs_max'
,
enable_ce
=
True
)
self
.
linear_fc_quant
(
'abs_max'
)
def
test_linear_fc_quant_range_abs_max
(
self
):
def
test_linear_fc_quant_range_abs_max
(
self
):
self
.
act_quant_op_type
=
'fake_quantize_range_abs_max'
self
.
linear_fc_quant
(
'range_abs_max'
,
enable_ce
=
True
)
self
.
linear_fc_quant
(
'range_abs_max'
)
def
residual_block_quant
(
self
,
quant_type
):
def
residual_block_quant
(
self
,
quant_type
,
enable_ce
=
False
):
main
=
fluid
.
Program
()
main
=
fluid
.
Program
()
startup
=
fluid
.
Program
()
startup
=
fluid
.
Program
()
with
fluid
.
program_guard
(
main
,
startup
):
with
fluid
.
program_guard
(
main
,
startup
):
...
@@ -175,31 +175,31 @@ class TestQuantizationTransformPass(unittest.TestCase):
...
@@ -175,31 +175,31 @@ class TestQuantizationTransformPass(unittest.TestCase):
place
=
place
,
place
=
place
,
activation_quantize_type
=
quant_type
)
activation_quantize_type
=
quant_type
)
transform_pass
.
apply
(
graph
)
transform_pass
.
apply
(
graph
)
marked_nodes
=
set
()
if
not
enable_ce
:
for
op
in
graph
.
all_op_nodes
():
marked_nodes
=
set
()
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
for
op
in
graph
.
all_op_nodes
():
marked_nodes
.
add
(
op
)
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
graph
.
draw
(
'.'
,
'quantize_residual_'
+
quant_type
,
marked_nodes
)
marked_nodes
.
add
(
op
)
graph
.
draw
(
'.'
,
'quantize_residual_'
+
quant_type
,
marked_nodes
)
program
=
graph
.
to_program
()
program
=
graph
.
to_program
()
self
.
check_program
(
transform_pass
,
program
)
self
.
check_program
(
transform_pass
,
program
)
val_graph
=
IrGraph
(
core
.
Graph
(
program
.
desc
),
for_test
=
False
)
val_graph
=
IrGraph
(
core
.
Graph
(
program
.
desc
),
for_test
=
False
)
val_marked_nodes
=
set
()
if
not
enable_ce
:
for
op
in
val_graph
.
all_op_nodes
():
val_marked_nodes
=
set
()
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
for
op
in
val_graph
.
all_op_nodes
():
val_marked_nodes
.
add
(
op
)
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
val_graph
.
draw
(
'.'
,
'val_residual_'
+
quant_type
,
val_marked_nodes
)
val_marked_nodes
.
add
(
op
)
val_graph
.
draw
(
'.'
,
'val_residual_'
+
quant_type
,
val_marked_nodes
)
def
test_residual_block_abs_max
(
self
):
def
test_residual_block_abs_max
(
self
):
self
.
act_quant_op_type
=
'fake_quantize_abs_max'
self
.
residual_block_quant
(
'abs_max'
,
enable_ce
=
True
)
self
.
residual_block_quant
(
'abs_max'
)
def
test_residual_block_range_abs_max
(
self
):
def
test_residual_block_range_abs_max
(
self
):
self
.
act_quant_op_type
=
'fake_quantize_range_abs_max'
self
.
residual_block_quant
(
'range_abs_max'
,
enable_ce
=
True
)
self
.
residual_block_quant
(
'range_abs_max'
)
class
TestQuantizationFreezePass
(
unittest
.
TestCase
):
class
TestQuantizationFreezePass
(
unittest
.
TestCase
):
def
freeze_graph
(
self
,
use_cuda
,
seed
,
quant_type
):
def
freeze_graph
(
self
,
use_cuda
,
seed
,
quant_type
,
enable_ce
=
False
):
def
build_program
(
main
,
startup
,
is_test
):
def
build_program
(
main
,
startup
,
is_test
):
main
.
random_seed
=
seed
main
.
random_seed
=
seed
startup
.
random_seed
=
seed
startup
.
random_seed
=
seed
...
@@ -237,16 +237,17 @@ class TestQuantizationFreezePass(unittest.TestCase):
...
@@ -237,16 +237,17 @@ class TestQuantizationFreezePass(unittest.TestCase):
transform_pass
.
apply
(
main_graph
)
transform_pass
.
apply
(
main_graph
)
transform_pass
.
apply
(
test_graph
)
transform_pass
.
apply
(
test_graph
)
dev_name
=
'_gpu_'
if
use_cuda
else
'_cpu_'
dev_name
=
'_gpu_'
if
use_cuda
else
'_cpu_'
marked_nodes
=
set
()
if
not
enable_ce
:
for
op
in
main_graph
.
all_op_nodes
():
marked_nodes
=
set
()
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
for
op
in
main_graph
.
all_op_nodes
():
marked_nodes
.
add
(
op
)
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
main_graph
.
draw
(
'.'
,
'main'
+
dev_name
+
quant_type
,
marked_nodes
)
marked_nodes
.
add
(
op
)
marked_nodes
=
set
()
main_graph
.
draw
(
'.'
,
'main'
+
dev_name
+
quant_type
,
marked_nodes
)
for
op
in
test_graph
.
all_op_nodes
():
marked_nodes
=
set
()
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
for
op
in
test_graph
.
all_op_nodes
():
marked_nodes
.
add
(
op
)
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
test_graph
.
draw
(
'.'
,
'test'
+
dev_name
+
quant_type
,
marked_nodes
)
marked_nodes
.
add
(
op
)
test_graph
.
draw
(
'.'
,
'test'
+
dev_name
+
quant_type
,
marked_nodes
)
quantized_main_program
=
main_graph
.
to_program
()
quantized_main_program
=
main_graph
.
to_program
()
quantized_test_program
=
test_graph
.
to_program
()
quantized_test_program
=
test_graph
.
to_program
()
...
@@ -266,7 +267,9 @@ class TestQuantizationFreezePass(unittest.TestCase):
...
@@ -266,7 +267,9 @@ class TestQuantizationFreezePass(unittest.TestCase):
loss_v
=
exe
.
run
(
program
=
quantized_main_program
,
loss_v
=
exe
.
run
(
program
=
quantized_main_program
,
feed
=
feeder
.
feed
(
data
),
feed
=
feeder
.
feed
(
data
),
fetch_list
=
[
loss
])
fetch_list
=
[
loss
])
print
(
'{}: {}'
.
format
(
'loss'
+
dev_name
+
quant_type
,
loss_v
))
if
not
enable_ce
:
print
(
'{}: {}'
.
format
(
'loss'
+
dev_name
+
quant_type
,
loss_v
))
test_data
=
next
(
test_reader
())
test_data
=
next
(
test_reader
())
with
fluid
.
program_guard
(
quantized_test_program
):
with
fluid
.
program_guard
(
quantized_test_program
):
...
@@ -281,12 +284,13 @@ class TestQuantizationFreezePass(unittest.TestCase):
...
@@ -281,12 +284,13 @@ class TestQuantizationFreezePass(unittest.TestCase):
# Freeze graph for inference, but the weight of fc/conv is still float type.
# Freeze graph for inference, but the weight of fc/conv is still float type.
freeze_pass
=
QuantizationFreezePass
(
scope
=
scope
,
place
=
place
)
freeze_pass
=
QuantizationFreezePass
(
scope
=
scope
,
place
=
place
)
freeze_pass
.
apply
(
test_graph
)
freeze_pass
.
apply
(
test_graph
)
marked_nodes
=
set
()
if
not
enable_ce
:
for
op
in
test_graph
.
all_op_nodes
():
marked_nodes
=
set
()
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
for
op
in
test_graph
.
all_op_nodes
():
marked_nodes
.
add
(
op
)
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
test_graph
.
draw
(
'.'
,
'test_freeze'
+
dev_name
+
quant_type
,
marked_nodes
.
add
(
op
)
marked_nodes
)
test_graph
.
draw
(
'.'
,
'test_freeze'
+
dev_name
+
quant_type
,
marked_nodes
)
server_program
=
test_graph
.
to_program
()
server_program
=
test_graph
.
to_program
()
with
fluid
.
scope_guard
(
scope
):
with
fluid
.
scope_guard
(
scope
):
...
@@ -294,24 +298,30 @@ class TestQuantizationFreezePass(unittest.TestCase):
...
@@ -294,24 +298,30 @@ class TestQuantizationFreezePass(unittest.TestCase):
feed
=
feeder
.
feed
(
test_data
),
feed
=
feeder
.
feed
(
test_data
),
fetch_list
=
[
loss
])
fetch_list
=
[
loss
])
self
.
assertAlmostEqual
(
test_loss1
,
test_loss2
,
delta
=
5e-3
)
self
.
assertAlmostEqual
(
test_loss1
,
test_loss2
,
delta
=
5e-3
)
print
(
'{}: {}'
.
format
(
'test_loss1'
+
dev_name
+
quant_type
,
test_loss1
))
if
not
enable_ce
:
print
(
'{}: {}'
.
format
(
'test_loss2'
+
dev_name
+
quant_type
,
test_loss2
))
print
(
'{}: {}'
.
format
(
'test_loss1'
+
dev_name
+
quant_type
,
test_loss1
))
print
(
'{}: {}'
.
format
(
'test_loss2'
+
dev_name
+
quant_type
,
test_loss2
))
w_freeze
=
np
.
array
(
scope
.
find_var
(
'conv2d_1.w_0'
).
get_tensor
())
w_freeze
=
np
.
array
(
scope
.
find_var
(
'conv2d_1.w_0'
).
get_tensor
())
# Maybe failed, this is due to the calculation precision
# Maybe failed, this is due to the calculation precision
# self.assertAlmostEqual(np.sum(w_freeze), np.sum(w_quant))
# self.assertAlmostEqual(np.sum(w_freeze), np.sum(w_quant))
print
(
'{}: {}'
.
format
(
'w_freeze'
+
dev_name
+
quant_type
,
if
not
enable_ce
:
np
.
sum
(
w_freeze
)))
print
(
'{}: {}'
.
format
(
'w_freeze'
+
dev_name
+
quant_type
,
print
(
'{}: {}'
.
format
(
'w_quant'
+
dev_name
+
quant_type
,
np
.
sum
(
w_freeze
)))
np
.
sum
(
w_quant
)))
print
(
'{}: {}'
.
format
(
'w_quant'
+
dev_name
+
quant_type
,
np
.
sum
(
w_quant
)))
# Convert parameter to 8-bit.
# Convert parameter to 8-bit.
convert_int8_pass
=
ConvertToInt8Pass
(
scope
=
scope
,
place
=
place
)
convert_int8_pass
=
ConvertToInt8Pass
(
scope
=
scope
,
place
=
place
)
convert_int8_pass
.
apply
(
test_graph
)
convert_int8_pass
.
apply
(
test_graph
)
marked_nodes
=
set
()
if
not
enable_ce
:
for
op
in
test_graph
.
all_op_nodes
():
marked_nodes
=
set
()
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
for
op
in
test_graph
.
all_op_nodes
():
marked_nodes
.
add
(
op
)
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
test_graph
.
draw
(
'.'
,
'test_int8'
+
dev_name
+
quant_type
,
marked_nodes
)
marked_nodes
.
add
(
op
)
test_graph
.
draw
(
'.'
,
'test_int8'
+
dev_name
+
quant_type
,
marked_nodes
)
server_program_int8
=
test_graph
.
to_program
()
server_program_int8
=
test_graph
.
to_program
()
# Save the 8-bit parameter and model file.
# Save the 8-bit parameter and model file.
with
fluid
.
scope_guard
(
scope
):
with
fluid
.
scope_guard
(
scope
):
...
@@ -325,18 +335,21 @@ class TestQuantizationFreezePass(unittest.TestCase):
...
@@ -325,18 +335,21 @@ class TestQuantizationFreezePass(unittest.TestCase):
w_8bit
=
np
.
array
(
scope
.
find_var
(
'conv2d_1.w_0.int8'
).
get_tensor
())
w_8bit
=
np
.
array
(
scope
.
find_var
(
'conv2d_1.w_0.int8'
).
get_tensor
())
self
.
assertEqual
(
w_8bit
.
dtype
,
np
.
int8
)
self
.
assertEqual
(
w_8bit
.
dtype
,
np
.
int8
)
self
.
assertEqual
(
np
.
sum
(
w_8bit
),
np
.
sum
(
w_freeze
))
self
.
assertEqual
(
np
.
sum
(
w_8bit
),
np
.
sum
(
w_freeze
))
print
(
'{}: {}'
.
format
(
'w_8bit'
+
dev_name
+
quant_type
,
np
.
sum
(
w_8bit
)))
if
not
enable_ce
:
print
(
'{}: {}'
.
format
(
'w_freeze'
+
dev_name
+
quant_type
,
print
(
'{}: {}'
.
format
(
'w_8bit'
+
dev_name
+
quant_type
,
np
.
sum
(
w_freeze
)))
np
.
sum
(
w_8bit
)))
print
(
'{}: {}'
.
format
(
'w_freeze'
+
dev_name
+
quant_type
,
np
.
sum
(
w_freeze
)))
mobile_pass
=
TransformForMobilePass
()
mobile_pass
=
TransformForMobilePass
()
mobile_pass
.
apply
(
test_graph
)
mobile_pass
.
apply
(
test_graph
)
marked_nodes
=
set
()
if
not
enable_ce
:
for
op
in
test_graph
.
all_op_nodes
():
marked_nodes
=
set
()
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
for
op
in
test_graph
.
all_op_nodes
():
marked_nodes
.
add
(
op
)
if
op
.
name
().
find
(
'quantize'
)
>
-
1
:
test_graph
.
draw
(
'.'
,
'test_mobile'
+
dev_name
+
quant_type
,
marked_nodes
.
add
(
op
)
marked_nodes
)
test_graph
.
draw
(
'.'
,
'test_mobile'
+
dev_name
+
quant_type
,
marked_nodes
)
mobile_program
=
test_graph
.
to_program
()
mobile_program
=
test_graph
.
to_program
()
with
fluid
.
scope_guard
(
scope
):
with
fluid
.
scope_guard
(
scope
):
...
@@ -347,20 +360,24 @@ class TestQuantizationFreezePass(unittest.TestCase):
...
@@ -347,20 +360,24 @@ class TestQuantizationFreezePass(unittest.TestCase):
def
test_freeze_graph_cuda_dynamic
(
self
):
def
test_freeze_graph_cuda_dynamic
(
self
):
if
fluid
.
core
.
is_compiled_with_cuda
():
if
fluid
.
core
.
is_compiled_with_cuda
():
with
fluid
.
unique_name
.
guard
():
with
fluid
.
unique_name
.
guard
():
self
.
freeze_graph
(
True
,
seed
=
1
,
quant_type
=
'abs_max'
)
self
.
freeze_graph
(
True
,
seed
=
1
,
quant_type
=
'abs_max'
,
enable_ce
=
True
)
def
test_freeze_graph_cpu_dynamic
(
self
):
def
test_freeze_graph_cpu_dynamic
(
self
):
with
fluid
.
unique_name
.
guard
():
with
fluid
.
unique_name
.
guard
():
self
.
freeze_graph
(
False
,
seed
=
2
,
quant_type
=
'abs_max'
)
self
.
freeze_graph
(
False
,
seed
=
2
,
quant_type
=
'abs_max'
,
enable_ce
=
True
)
def
test_freeze_graph_cuda_static
(
self
):
def
test_freeze_graph_cuda_static
(
self
):
if
fluid
.
core
.
is_compiled_with_cuda
():
if
fluid
.
core
.
is_compiled_with_cuda
():
with
fluid
.
unique_name
.
guard
():
with
fluid
.
unique_name
.
guard
():
self
.
freeze_graph
(
True
,
seed
=
1
,
quant_type
=
'range_abs_max'
)
self
.
freeze_graph
(
True
,
seed
=
1
,
quant_type
=
'range_abs_max'
,
enable_ce
=
True
)
def
test_freeze_graph_cpu_static
(
self
):
def
test_freeze_graph_cpu_static
(
self
):
with
fluid
.
unique_name
.
guard
():
with
fluid
.
unique_name
.
guard
():
self
.
freeze_graph
(
False
,
seed
=
2
,
quant_type
=
'range_abs_max'
)
self
.
freeze_graph
(
False
,
seed
=
2
,
quant_type
=
'range_abs_max'
,
enable_ce
=
True
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录