Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
ed7e33e7
P
PaddleSlim
项目概览
PaddlePaddle
/
PaddleSlim
大约 2 年 前同步成功
通知
51
Star
1434
Fork
344
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
53
列表
看板
标记
里程碑
合并请求
16
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleSlim
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
53
Issue
53
列表
看板
标记
里程碑
合并请求
16
合并请求
16
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
ed7e33e7
编写于
11月 10, 2019
作者:
I
itminner
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add some params check
上级
cd738054
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
27 addition
and
16 deletion
+27
-16
paddleslim/quant/__init__.py
paddleslim/quant/__init__.py
+1
-1
paddleslim/quant/quanter.py
paddleslim/quant/quanter.py
+26
-15
未找到文件。
paddleslim/quant/__init__.py
浏览文件 @
ed7e33e7
...
@@ -12,5 +12,5 @@
...
@@ -12,5 +12,5 @@
# See the License for the specific language governing permissions and
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.
from
quanter
import
quant_aware
,
quant_post
,
convert
from
.
quanter
import
quant_aware
,
quant_post
,
convert
from
.quant_embedding
import
quant_embedding
from
.quant_embedding
import
quant_embedding
paddleslim/quant/quanter.py
浏览文件 @
ed7e33e7
...
@@ -22,7 +22,9 @@ from paddle.fluid.contrib.slim.quantization import ConvertToInt8Pass
...
@@ -22,7 +22,9 @@ from paddle.fluid.contrib.slim.quantization import ConvertToInt8Pass
from
paddle.fluid.contrib.slim.quantization
import
TransformForMobilePass
from
paddle.fluid.contrib.slim.quantization
import
TransformForMobilePass
from
paddle.fluid
import
core
from
paddle.fluid
import
core
QUANTIZATION_TYPES
=
[
'abs_max'
,
'channel_wise_abs_max'
,
'range_abs_max'
,
'moving_average_abs_max'
]
WEIGHT_QUANTIZATION_TYPES
=
[
'abs_max'
,
'channel_wise_abs_max'
]
ACTIVATION_QUANTIZATION_TYPES
=
[
'abs_max'
,
'range_abs_max'
,
'moving_average_abs_max'
]
VALID_DTYPES
=
[
'int8'
]
quant_config_default
=
{
quant_config_default
=
{
# weight quantize type, default is 'abs_max'
# weight quantize type, default is 'abs_max'
...
@@ -61,19 +63,23 @@ def _parse_configs(user_config):
...
@@ -61,19 +63,23 @@ def _parse_configs(user_config):
configs
.
update
(
user_config
)
configs
.
update
(
user_config
)
# check configs is valid
# check configs is valid
assert
configs
[
'weight_quantize_type'
]
in
QUANTIZATION_TYPES
,
\
assert
configs
[
'weight_quantize_type'
]
in
WEIGHT_QUANTIZATION_TYPES
,
\
"Unknown weight_quantize_type: '%s'. It can only be "
\
"Unknown weight_quantize_type: '%s'. It can only be "
+
" "
.
join
(
WEIGHT_QUANTIZATION_TYPES
)
"'abs_max' or 'channel_wise_abs_max' or 'range_abs_max' or 'moving_average_abs_max'."
assert
configs
[
'activation_quantize_type'
]
in
QUANTIZATION_TYPES
,
\
assert
configs
[
'activation_quantize_type'
]
in
ACTIVATION_QUANTIZATION_TYPES
,
\
"Unknown activation_quantize_type: '%s'. It can only be "
\
"Unknown activation_quantize_type: '%s'. It can only be "
+
" "
.
join
(
ACTIVATION_QUANTIZATION_TYPES
)
"'abs_max' or 'channel_wise_abs_max' or 'range_abs_max' or 'moving_average_abs_max'."
assert
isinstance
(
configs
[
'weight_bits'
],
int
),
\
assert
isinstance
(
configs
[
'weight_bits'
],
int
),
\
"weight_bits must be int value, such as 8, 16, 32, etc"
"weight_bits must be int value."
assert
isinstance
(
configs
[
'weight_bits'
]
>=
1
and
configs
[
'weight_bits'
]
<=
16
),
\
"weight_bits should be between 1 and 16."
assert
isinstance
(
configs
[
'activation_bits'
],
int
),
\
assert
isinstance
(
configs
[
'activation_bits'
],
int
),
\
"activation_bits must be int value, such as 8, 16, 32, etc"
"activation_bits must be int value."
assert
isinstance
(
configs
[
'activation_bits'
]
>=
1
and
configs
[
'activation_bits'
]
<=
16
),
\
"activation_bits should be between 1 and 16."
assert
isinstance
(
configs
[
'not_quant_pattern'
],
list
),
\
assert
isinstance
(
configs
[
'not_quant_pattern'
],
list
),
\
"not_quant_pattern must be a list"
"not_quant_pattern must be a list"
...
@@ -82,7 +88,10 @@ def _parse_configs(user_config):
...
@@ -82,7 +88,10 @@ def _parse_configs(user_config):
"quantize_op_types must be a list"
"quantize_op_types must be a list"
assert
isinstance
(
configs
[
'dtype'
],
str
),
\
assert
isinstance
(
configs
[
'dtype'
],
str
),
\
"dtype must be a str, it can be config as 'int8', 'uint8', 'int16', etc."
"dtype must be a str."
assert
isinstance
(
configs
[
'dtype'
]
in
VALID_DTYPES
),
\
"dtype can only be "
+
" "
.
join
(
VALID_DTYPES
)
assert
isinstance
(
configs
[
'window_size'
],
int
),
\
assert
isinstance
(
configs
[
'window_size'
],
int
),
\
"window_size must be int value, window size for 'range_abs_max' quantization, default is 10000."
"window_size must be int value, window size for 'range_abs_max' quantization, default is 10000."
...
@@ -104,10 +113,10 @@ def quant_aware(program, scope, place, config, for_test=False):
...
@@ -104,10 +113,10 @@ def quant_aware(program, scope, place, config, for_test=False):
add trainable quantization ops in program.
add trainable quantization ops in program.
Args:
Args:
program(fluid.Program): program
program(fluid.Program): program
scope(fluid.Scope): the scope to store var,
when is None will use fluid.global_scope()
scope(fluid.Scope): the scope to store var,
it's should be the value of program's scope, usually it's fluid.global_scope().
place(fluid.CPUPlace or fluid.CUDAPlace): place
place(fluid.CPUPlace or fluid.CUDAPlace): place
config(dict): configs for quantization, default values are in quant_config_default dict.
config(dict): configs for quantization, default values are in quant_config_default dict.
for_test: i
s for test program
.
for_test: i
f program is test program, for_test should be set True, else False
.
Return:
Return:
fluid.Program: user can finetune this quantization program to enhance the accuracy.
fluid.Program: user can finetune this quantization program to enhance the accuracy.
"""
"""
...
@@ -122,17 +131,18 @@ def quant_aware(program, scope, place, config, for_test=False):
...
@@ -122,17 +131,18 @@ def quant_aware(program, scope, place, config, for_test=False):
main_graph
=
IrGraph
(
core
.
Graph
(
program
.
desc
),
for_test
=
for_test
)
main_graph
=
IrGraph
(
core
.
Graph
(
program
.
desc
),
for_test
=
for_test
)
transform_pass
=
QuantizationTransformPass
(
transform_pass
=
QuantizationTransformPass
(
scope
=
scope
,
place
=
place
,
scope
=
scope
,
place
=
place
,
weight_bits
=
config
[
'weight_bits'
],
weight_bits
=
config
[
'weight_bits'
],
activation_bits
=
config
[
'activation_bits'
],
activation_bits
=
config
[
'activation_bits'
],
activation_quantize_type
=
config
[
'activation_quantize_type'
],
activation_quantize_type
=
config
[
'activation_quantize_type'
],
weight_quantize_type
=
config
[
'weight_quantize_type'
],
weight_quantize_type
=
config
[
'weight_quantize_type'
],
window_size
=
config
[
'window_size'
],
window_size
=
config
[
'window_size'
],
moving_rate
=
config
[
'moving_rate'
],
moving_rate
=
config
[
'moving_rate'
],
quantizable_op_type
=
config
[
'quantize_op_types'
],
skip_pattern
=
''
#not_quant_pattern
skip_pattern
=
''
#not_quant_pattern
)
)
transform_pass
.
apply
(
main_graph
)
transform_pass
.
apply
(
main_graph
)
if
for_test
:
if
for_test
:
...
@@ -146,7 +156,7 @@ def quant_post(program, scope, place, config):
...
@@ -146,7 +156,7 @@ def quant_post(program, scope, place, config):
add quantization ops in program. the program returned is not trainable.
add quantization ops in program. the program returned is not trainable.
Args:
Args:
program(fluid.Program): program
program(fluid.Program): program
scope(fluid.Scope): the scope to store var,
when is None will use fluid.global_scope()
scope(fluid.Scope): the scope to store var,
it's should be the value of program's scope, usually it's fluid.global_scope().
place(fluid.CPUPlace or fluid.CUDAPlace): place
place(fluid.CPUPlace or fluid.CUDAPlace): place
config(dict): configs for quantization, default values are in quant_config_default dict.
config(dict): configs for quantization, default values are in quant_config_default dict.
for_test: is for test program.
for_test: is for test program.
...
@@ -186,6 +196,7 @@ def convert(program, scope, place, config, save_int8=False):
...
@@ -186,6 +196,7 @@ def convert(program, scope, place, config, save_int8=False):
fluid.Program: freezed program which can be used for inference.
fluid.Program: freezed program which can be used for inference.
parameters is float32 type, but it's value in int8 range.
parameters is float32 type, but it's value in int8 range.
fluid.Program: freezed int8 program which can be used for inference.
fluid.Program: freezed int8 program which can be used for inference.
if save_int8 is False, this value is None.
"""
"""
test_graph
=
IrGraph
(
core
.
Graph
(
program
.
desc
),
for_test
=
True
)
test_graph
=
IrGraph
(
core
.
Graph
(
program
.
desc
),
for_test
=
True
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录