Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
c9c0e83f
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看板
未验证
提交
c9c0e83f
编写于
7月 26, 2021
作者:
C
cc
提交者:
GitHub
7月 26, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add int16 quantization for embedding (#857)
上级
4d75cb9c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
80 addition
and
16 deletion
+80
-16
docs/zh_cn/api_cn/static/quant/quantization_api.rst
docs/zh_cn/api_cn/static/quant/quantization_api.rst
+18
-3
docs/zh_cn/tutorials/quant/static/embedding_quant_tutorial.md
.../zh_cn/tutorials/quant/static/embedding_quant_tutorial.md
+20
-3
paddleslim/quant/quant_embedding.py
paddleslim/quant/quant_embedding.py
+13
-5
tests/test_quant_embedding.py
tests/test_quant_embedding.py
+29
-5
未找到文件。
docs/zh_cn/api_cn/static/quant/quantization_api.rst
浏览文件 @
c9c0e83f
...
...
@@ -461,12 +461,27 @@ fluid.Program
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
#
量化为
8
比特,
Embedding
参数的体积减小
4
倍,精度有轻微损失
config
=
{
'quantize_op_types'
:
[
'lookup_table'
],
'quantize_op_types'
:
[
'lookup_table'
],
'lookup_table'
:
{
'quantize_type'
:
'abs_max'
}
'quantize_type'
:
'abs_max'
,
'quantize_bits'
:
8
,
'dtype'
:
'int8'
}
}
'''
# 量化为16比特,Embedding参数的体积减小2倍,精度损失很小
config = {
'
quantize_op_types
': ['
lookup_table
'],
'
lookup_table
': {
'
quantize_type
': '
abs_max
',
'
quantize_bits
': 16,
'
dtype
': '
int16
'
}
}
'''
quant_program
=
quant
.
quant_embedding
(
infer_program
,
place
,
config
)
更详细的用法请参考
`
Embedding
量化
demo
<
https
://
github
.
com
/
PaddlePaddle
/
PaddleSlim
/
tree
/
develop
/
demo
/
quant
/
quant_embedding
>`
_
...
...
docs/zh_cn/tutorials/quant/static/embedding_quant_tutorial.md
浏览文件 @
c9c0e83f
# Embedding量化
Embedding量化将网络中的Embedding参数从
`float32`
类型量化到
`8-bit`
整数类型,在几乎不损失模型精度的情况下减少模型的存储空间和显存占用。
Embedding量化将网络中的Embedding参数从
`float32`
类型量化到
`8-bit`
或者
`16-bit`
整数类型,在几乎不损失模型精度的情况下减少模型的存储空间和显存占用。
Embedding量化仅能减少模型参数的体积,加快加载Embedding参数的速度,并不能显著提升模型预测速度。
Embedding量化仅能减少模型参数的体积,并不能显著提升模型预测速度。
## 使用方法
在预测时调用paddleslim
`quant_embedding`
接口,主要实现代码如下:
...
...
@@ -29,12 +30,28 @@ place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
# 量化为8比特,Embedding参数的体积减小4倍,精度有轻微损失
config
=
{
'quantize_op_types'
:
[
'lookup_table'
],
'lookup_table'
:
{
'quantize_type'
:
'abs_max'
,
'quantize_bits'
:
8
,
'dtype'
:
'int8'
}
}
'''
# 量化为16比特,Embedding参数的体积减小2倍,精度损失很小
config = {
'quantize_op_types': ['lookup_table'],
'lookup_table': {
'quantize_type'
:
'abs_max'
'quantize_type': 'abs_max',
'quantize_bits': 16,
'dtype': 'int16'
}
}
'''
quant_program
=
quant
.
quant_embedding
(
infer_program
,
place
,
config
)
```
...
...
paddleslim/quant/quant_embedding.py
浏览文件 @
c9c0e83f
...
...
@@ -35,10 +35,13 @@ _default_single_config = {
"quantize_bits"
:
8
,
"dtype"
:
"int8"
}
SUPPORT_OP_TYPES
=
[
'lookup_table'
,
'fused_embedding_seq_pool'
,
'pyramid_hash'
]
SUPPORT_OP_TYPES
=
[
'lookup_table'
,
'lookup_table_v2'
,
'fused_embedding_seq_pool'
,
'pyramid_hash'
]
SUPPORT_QUANTIZE_TYPES
=
[
'abs_max'
,
'log'
]
SUPPORT_QUANTIZE_BITS
=
[
8
]
SUPPORT_DTYPE
=
[
'int8'
]
SUPPORT_QUANTIZE_BITS
=
[
8
,
16
]
SUPPORT_DTYPE
=
[
'int8'
,
'int16'
]
_default_config
=
{
"quantize_op_types"
:
SUPPORT_OP_TYPES
,
}
...
...
@@ -125,7 +128,7 @@ def _get_quant_var_name(var_name):
"""
get quantized var name
"""
return
var_name
+
'.int
8
'
return
var_name
+
'.int'
def
_get_dequant_var_name
(
var_name
):
...
...
@@ -151,6 +154,11 @@ def _clear_var(var_name, scope):
tensor
.
_clear
()
def
_get_var_dtype
(
config
):
return
core
.
VarDesc
.
VarType
.
INT8
if
config
[
'dtype'
]
==
'int8'
\
else
core
.
VarDesc
.
VarType
.
INT16
def
_quant_embedding_abs_max
(
graph
,
scope
,
place
,
config
,
var_name
,
embedding_node
):
"""
...
...
@@ -230,7 +238,7 @@ def _quant_embedding_abs_max(graph, scope, place, config, var_name,
_get_quant_var_name
(
var_name
),
var_type
=
embedding_node
.
type
(),
shape
=
embedding_node
.
shape
(),
var_dtype
=
core
.
VarDesc
.
VarType
.
INT8
)
var_dtype
=
_get_var_dtype
(
config
)
)
# create var in scope
scope
.
var
(
_get_quant_var_name
(
var_name
))
scope
.
var
(
_get_scale_var_name
(
var_name
))
...
...
tests/test_quant_embedding.py
浏览文件 @
c9c0e83f
...
...
@@ -21,15 +21,28 @@ from static_case import StaticCase
class
TestQuantEmbedding
(
StaticCase
):
def
set_config
(
self
):
self
.
config
=
{
'quantize_op_types'
:
[
'lookup_table_v2'
],
'lookup_table'
:
{
'quantize_type'
:
'abs_max'
,
'quantize_bits'
:
8
,
'dtype'
:
'int8'
}
}
def
test_quant_embedding
(
self
):
self
.
set_config
()
train_program
=
paddle
.
static
.
Program
()
with
paddle
.
static
.
program_guard
(
train_program
):
startup_program
=
paddle
.
static
.
Program
()
with
paddle
.
static
.
program_guard
(
train_program
,
startup_program
):
input_word
=
paddle
.
static
.
data
(
name
=
"input_word"
,
shape
=
[
None
,
1
],
dtype
=
'int64'
)
param_attr
=
paddle
.
ParamAttr
(
name
=
'emb'
,
initializer
=
paddle
.
nn
.
initializer
.
Uniform
(
-
0.005
,
0.005
))
weight
=
train_program
.
global_block
()
.
create_parameter
(
weight
=
paddle
.
static
.
create_parameter
(
(
100
,
128
),
attr
=
param_attr
,
dtype
=
"float32"
)
input_emb
=
paddle
.
nn
.
functional
.
embedding
(
...
...
@@ -37,13 +50,24 @@ class TestQuantEmbedding(StaticCase):
infer_program
=
train_program
.
clone
(
for_test
=
True
)
use_gpu
=
True
place
=
paddle
.
CUDAPlace
(
0
)
if
use_gpu
else
paddle
.
CPUPlace
()
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
paddle
.
static
.
default_startup_program
()
)
exe
.
run
(
startup_program
)
quant_program
=
quant
.
quant_embedding
(
infer_program
,
place
)
class
TestQuantEmbeddingInt16
(
TestQuantEmbedding
):
def
set_config
(
self
):
self
.
config
=
{
'quantize_op_types'
:
[
'lookup_table'
],
'lookup_table'
:
{
'quantize_type'
:
'abs_max'
,
'quantize_bits'
:
16
,
'dtype'
:
'int16'
}
}
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录