Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
bb02b103
P
PaddleSlim
项目概览
PaddlePaddle
/
PaddleSlim
1 年多 前同步成功
通知
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看板
未验证
提交
bb02b103
编写于
8月 25, 2023
作者:
C
Chang Xu
提交者:
GitHub
8月 25, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix Bugs (#1792)
上级
73e15291
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
10 addition
and
7 deletion
+10
-7
paddleslim/quant/advanced/layerwise_quant_error.py
paddleslim/quant/advanced/layerwise_quant_error.py
+5
-4
paddleslim/quant/advanced/utils.py
paddleslim/quant/advanced/utils.py
+4
-2
paddleslim/quant/advanced/utils_layers.py
paddleslim/quant/advanced/utils_layers.py
+1
-1
未找到文件。
paddleslim/quant/advanced/layerwise_quant_error.py
浏览文件 @
bb02b103
...
...
@@ -54,6 +54,7 @@ class LayerWiseQuantError(nn.Layer):
if type(cur_layer) == LayerWiseQuantError:
print(cur_name, cur_layer.losses.mean())
'''
super
(
LayerWiseQuantError
,
self
).
__init__
()
self
.
layer
=
layer
self
.
weight
=
layer
.
weight
self
.
weight_bits
=
weight_bits
...
...
@@ -62,14 +63,13 @@ class LayerWiseQuantError(nn.Layer):
self
.
act_method
=
act_quant_method
self
.
loss_function
=
loss_function
self
.
losses
=
[]
self
.
loss
=
None
def
forward
(
self
,
input
):
act
=
input
[
0
]
if
type
(
input
)
==
tuple
else
input
origin_out
=
paddle
.
matmul
(
act
,
self
.
weight
)
bnt
=
(
1
<<
(
self
.
weight_bits
-
1
))
-
1
quant_scale
=
compute_scales
(
self
.
weight
.
cast
(
'float32'
),
method
=
self
.
weight_method
).
cast
(
self
.
weight
.
dtype
)
quant_scale
=
compute_scales
(
self
.
weight
,
method
=
self
.
weight_method
)
quant_weight
=
paddle
.
clip
(
paddle
.
round
(
self
.
weight
/
quant_scale
*
bnt
),
-
bnt
-
1
,
bnt
)
quant_dequant_weight
=
quant_weight
/
bnt
*
quant_scale
...
...
@@ -80,6 +80,7 @@ class LayerWiseQuantError(nn.Layer):
paddle
.
round
(
act
/
quant_scale
*
bnt
),
-
bnt
-
1
,
bnt
)
quant_dequant_act
=
quant_act
/
bnt
*
quant_scale
quant_out
=
paddle
.
matmul
(
quant_dequant_act
,
quant_dequant_weight
)
loss
=
self
.
loss_function
(
origin_out
,
quant_out
)
loss
=
self
.
loss_function
(
origin_out
,
quant_out
)
.
cast
(
'float32'
)
self
.
losses
.
append
(
loss
)
self
.
loss
=
paddle
.
to_tensor
(
self
.
losses
,
dtype
=
'float32'
).
mean
()
return
self
.
layer
(
input
)
paddleslim/quant/advanced/utils.py
浏览文件 @
bb02b103
...
...
@@ -48,8 +48,10 @@ def compute_scales(x, method='abs_max'):
elif
method
==
'abs_max_channel_wise'
:
reduce_axis
=
tuple
([
i
for
i
in
range
(
len
(
x
.
shape
))
if
i
!=
1
])
quant_scale
=
paddle
.
max
(
paddle
.
abs
(
x
),
axis
=
reduce_axis
)
quant_scale
=
paddle
.
where
(
quant_scale
==
np
.
float32
(
0.0
),
np
.
float32
(
1e-8
),
quant_scale
)
quant_scale
=
paddle
.
where
(
quant_scale
==
paddle
.
to_tensor
(
0
,
dtype
=
x
.
dtype
),
paddle
.
to_tensor
(
1e-8
,
dtype
=
x
.
dtype
),
quant_scale
)
return
quant_scale
...
...
paddleslim/quant/advanced/utils_layers.py
浏览文件 @
bb02b103
...
...
@@ -26,7 +26,7 @@ class ShiftSmoothHelpLayer(nn.Layer):
super
(
ShiftSmoothHelpLayer
,
self
).
__init__
()
self
.
weight
=
layer
.
weight
shift_shape
=
self
.
weight
.
shape
[
0
]
if
hasattr
(
layer
,
"bias"
)
or
layer
.
bias
is
None
:
if
not
hasattr
(
layer
,
"bias"
)
or
layer
.
bias
is
None
:
self
.
bias
=
paddle
.
create_parameter
(
shape
=
[
self
.
weight
.
shape
[
1
]],
dtype
=
self
.
weight
.
dtype
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录