Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
96138ca8
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看板
提交
96138ca8
编写于
9月 20, 2018
作者:
D
Dang Qingqing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix unit test.
上级
9e580631
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
49 addition
and
42 deletion
+49
-42
python/paddle/fluid/contrib/tests/test_quantize_transpiler.py
...on/paddle/fluid/contrib/tests/test_quantize_transpiler.py
+49
-42
未找到文件。
python/paddle/fluid/contrib/tests/test_quantize_transpiler.py
浏览文件 @
96138ca8
...
@@ -208,53 +208,60 @@ class TestQuantizeTranspiler(unittest.TestCase):
...
@@ -208,53 +208,60 @@ class TestQuantizeTranspiler(unittest.TestCase):
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
batch_size
)
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
batch_size
)
feeder
=
fluid
.
DataFeeder
(
feed_list
=
[
img
,
label
],
place
=
place
)
feeder
=
fluid
.
DataFeeder
(
feed_list
=
[
img
,
label
],
place
=
place
)
for
_
in
range
(
iter
):
with
fluid
.
program_guard
(
main
):
data
=
train_reader
().
next
()
for
_
in
range
(
iter
):
loss_v
=
exe
.
run
(
program
=
main
,
data
=
train_reader
().
next
()
feed
=
feeder
.
feed
(
data
),
loss_v
=
exe
.
run
(
program
=
main
,
fetch_list
=
[
loss
])
feed
=
feeder
.
feed
(
data
),
test_data
=
test_reader
().
next
()
fetch_list
=
[
loss
])
f_var
=
fluid
.
framework
.
get_var
(
'conv2d_1.tmp_0'
,
test_program
)
with
fluid
.
program_guard
(
test_program
):
w_var
=
fluid
.
framework
.
get_var
(
'conv2d_1.w_0.quantized'
,
test_program
)
test_data
=
test_reader
().
next
()
# Testing during training
f_var
=
fluid
.
framework
.
get_var
(
'conv2d_1.tmp_0'
,
test_program
)
test_loss1
,
f_v1
,
w_quant
=
exe
.
run
(
program
=
test_program
,
w_var
=
fluid
.
framework
.
get_var
(
'conv2d_1.w_0.quantized'
,
feed
=
feeder
.
feed
(
test_data
),
test_program
)
fetch_list
=
[
loss
,
f_var
,
w_var
])
# Testing during training
test_loss1
,
f_v1
,
w_quant
=
exe
.
run
(
# Freeze program for inference, but the weight of fc/conv is still float type.
program
=
test_program
,
quant_transpiler
.
freeze_program
(
test_program
,
place
)
feed
=
feeder
.
feed
(
test_data
),
fv2
=
fluid
.
framework
.
get_var
(
'conv2d_1.tmp_0.dequantized'
,
fetch_list
=
[
loss
,
f_var
,
w_var
])
test_program
)
test_loss2
,
f_v2
=
exe
.
run
(
program
=
test_program
,
# Freeze program for inference, but the weight of fc/conv is still float type.
feed
=
feeder
.
feed
(
test_data
),
quant_transpiler
.
freeze_program
(
test_program
,
place
)
fetch_list
=
[
loss
,
fv2
])
fv2
=
fluid
.
framework
.
get_var
(
'conv2d_1.tmp_0.dequantized'
,
self
.
assertAlmostEqual
(
test_loss1
,
test_loss2
,
delta
=
1e-5
)
test_program
)
self
.
assertAlmostEqual
(
f_v1
.
all
(),
f_v2
.
all
(),
delta
=
1e-5
)
test_loss2
,
f_v2
=
exe
.
run
(
program
=
test_program
,
w_freeze
=
np
.
array
(
fluid
.
global_scope
().
find_var
(
'conv2d_1.w_0'
)
feed
=
feeder
.
feed
(
test_data
),
.
get_tensor
())
fetch_list
=
[
loss
,
fv2
])
self
.
assertEqual
(
np
.
sum
(
w_freeze
),
np
.
sum
(
w_quant
))
self
.
assertAlmostEqual
(
test_loss1
,
test_loss2
,
delta
=
1e-3
)
self
.
assertTrue
(
np
.
allclose
(
f_v1
,
f_v2
,
rtol
=
1e-05
,
atol
=
1e-05
))
# Convert parameter to 8-bit.
w_freeze
=
np
.
array
(
fluid
.
global_scope
().
find_var
(
'conv2d_1.w_0'
)
quant_transpiler
.
convert_to_int8
(
test_program
,
place
)
.
get_tensor
())
# Save the 8-bit parameter and model file.
self
.
assertEqual
(
np
.
sum
(
w_freeze
),
np
.
sum
(
w_quant
))
fluid
.
io
.
save_inference_model
(
'model_8bit'
,
[
'image'
,
'label'
],
[
loss
],
exe
,
test_program
)
# Convert parameter to 8-bit.
# Test whether the 8-bit parameter and model file can be loaded successfully.
quant_transpiler
.
convert_to_int8
(
test_program
,
place
)
[
infer
,
feed
,
fetch
]
=
fluid
.
io
.
load_inference_model
(
'model_8bit'
,
exe
)
# Save the 8-bit parameter and model file.
# Check the loaded 8-bit weight.
fluid
.
io
.
save_inference_model
(
'model_8bit'
,
[
'image'
,
'label'
],
w_8bit
=
np
.
array
(
fluid
.
global_scope
().
find_var
(
'conv2d_1.w_0.int8'
)
[
loss
],
exe
,
test_program
)
.
get_tensor
())
# Test whether the 8-bit parameter and model file can be loaded successfully.
[
infer
,
feed
,
fetch
]
=
fluid
.
io
.
load_inference_model
(
'model_8bit'
,
self
.
assertEqual
(
w_8bit
.
dtype
,
np
.
int8
)
exe
)
self
.
assertEqual
(
np
.
sum
(
w_8bit
),
np
.
sum
(
w_freeze
))
# Check the loaded 8-bit weight.
w_8bit
=
np
.
array
(
fluid
.
global_scope
().
find_var
(
'conv2d_1.w_0.int8'
)
.
get_tensor
())
self
.
assertEqual
(
w_8bit
.
dtype
,
np
.
int8
)
self
.
assertEqual
(
np
.
sum
(
w_8bit
),
np
.
sum
(
w_freeze
))
def
test_freeze_program_cuda
(
self
):
def
test_freeze_program_cuda
(
self
):
if
fluid
.
core
.
is_compiled_with_cuda
():
if
fluid
.
core
.
is_compiled_with_cuda
():
self
.
freeze_program
(
True
)
with
fluid
.
unique_name
.
guard
():
self
.
freeze_program
(
True
)
def
test_freeze_program_cpu
(
self
):
def
test_freeze_program_cpu
(
self
):
self
.
freeze_program
(
False
)
with
fluid
.
unique_name
.
guard
():
self
.
freeze_program
(
False
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录