Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
74bdbd72
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看板
提交
74bdbd72
编写于
4月 13, 2020
作者:
W
wanghaoshuang
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'develop' of
https://github.com/PaddlePaddle/PaddleSlim
into bert
上级
752745e4
502f7612
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
31 addition
and
13 deletion
+31
-13
paddleslim/core/registry.py
paddleslim/core/registry.py
+0
-3
paddleslim/prune/idx_selector.py
paddleslim/prune/idx_selector.py
+1
-3
paddleslim/prune/prune_walker.py
paddleslim/prune/prune_walker.py
+27
-2
paddleslim/prune/pruner.py
paddleslim/prune/pruner.py
+3
-5
未找到文件。
paddleslim/core/registry.py
浏览文件 @
74bdbd72
...
...
@@ -25,9 +25,6 @@ class Registry(object):
return
self
.
_module_dict
.
get
(
key
,
None
)
def
_register_module
(
self
,
module_class
):
if
not
inspect
.
isclass
(
module_class
):
raise
TypeError
(
'module must be a class, but receive {}.'
.
format
(
type
(
module_class
)))
module_name
=
module_class
.
__name__
if
module_name
in
self
.
_module_dict
:
raise
KeyError
(
'{} is already registered in {}.'
.
format
(
...
...
paddleslim/prune/idx_selector.py
浏览文件 @
74bdbd72
...
...
@@ -52,12 +52,11 @@ def default_idx_selector(group, ratio):
list: pruned indexes
"""
assert
(
isinstance
(
graph
,
GraphWrapper
))
name
,
axis
,
score
=
group
[
0
]
# sort channels by the first convolution's score
sorted_idx
=
score
.
argsort
()
pruned_num
=
len
(
sorted_idx
)
*
ratio
pruned_num
=
int
(
round
(
len
(
sorted_idx
)
*
ratio
))
pruned_idx
=
sorted_idx
[:
pruned_num
]
idxs
=
[]
...
...
@@ -94,7 +93,6 @@ def optimal_threshold(group, ratio):
list: pruned indexes
"""
assert
(
isinstance
(
graph
,
GraphWrapper
))
name
,
axis
,
score
=
group
[
0
]
# sort channels by the first convolution's score
...
...
paddleslim/prune/prune_walker.py
浏览文件 @
74bdbd72
...
...
@@ -71,8 +71,11 @@ class PruneWorker(object):
if
visited
is
not
None
:
self
.
visited
=
visited
cls
=
PRUNE_WORKER
.
get
(
op
.
type
())
assert
cls
is
not
None
,
"The walker of {} is not registered."
.
format
(
op
.
type
())
if
cls
is
None
:
_logger
.
warn
(
"{} op will be pruned by default walker to keep the shapes of input and output being same because its walker is not registered."
.
format
(
op
.
type
()))
cls
=
PRUNE_WORKER
.
get
(
"default_walker"
)
_logger
.
debug
(
"
\n
from: {}
\n
to: {}
\n
pruned_axis: {}; var: {}"
.
format
(
self
.
op
,
op
,
pruned_axis
,
var
.
name
()))
walker
=
cls
(
op
,
...
...
@@ -236,6 +239,7 @@ class elementwise_mul(elementwise_op):
super
(
elementwise_mul
,
self
).
__init__
(
op
,
pruned_params
,
visited
)
@
PRUNE_WORKER
.
register
class
activation
(
PruneWorker
):
def
__init__
(
self
,
op
,
pruned_params
,
visited
):
super
(
activation
,
self
).
__init__
(
op
,
pruned_params
,
visited
)
...
...
@@ -256,6 +260,27 @@ class activation(PruneWorker):
self
.
_prune_op
(
op
,
out_var
,
pruned_axis
,
pruned_idx
)
@
PRUNE_WORKER
.
register
class
default_walker
(
PruneWorker
):
def
__init__
(
self
,
op
,
pruned_params
,
visited
):
super
(
default_walker
,
self
).
__init__
(
op
,
pruned_params
,
visited
)
def
_prune
(
self
,
var
,
pruned_axis
,
pruned_idx
):
if
var
in
self
.
op
.
all_outputs
():
for
in_var
in
self
.
op
.
inputs
():
if
len
(
in_var
.
shape
())
==
len
(
var
.
shape
()):
pre_ops
=
in_var
.
inputs
()
for
op
in
pre_ops
:
self
.
_prune_op
(
op
,
in_var
,
pruned_axis
,
pruned_idx
)
for
out_var
in
self
.
op
.
all_outputs
():
if
len
(
out_var
.
shape
())
==
len
(
var
.
shape
()):
self
.
_visit
(
out_var
,
pruned_axis
)
next_ops
=
out_var
.
outputs
()
for
op
in
next_ops
:
self
.
_prune_op
(
op
,
out_var
,
pruned_axis
,
pruned_idx
)
@
PRUNE_WORKER
.
register
class
uniform_random_batch_size_like
(
activation
):
def
__init__
(
self
,
op
,
pruned_params
,
visited
):
...
...
paddleslim/prune/pruner.py
浏览文件 @
74bdbd72
...
...
@@ -41,8 +41,6 @@ class Pruner():
def
__init__
(
self
,
criterion
=
"l1_norm"
,
idx_selector
=
"default_idx_selector"
):
self
.
criterion
=
criterion
self
.
channel_sortor
=
channel_sortor
if
isinstance
(
criterion
,
str
):
self
.
criterion
=
CRITERION
.
get
(
criterion
)
else
:
...
...
@@ -98,7 +96,7 @@ class Pruner():
param_v
=
graph
.
var
(
param
)
pruned_num
=
int
(
round
(
param_v
.
shape
()[
0
]
*
ratio
))
pruned_idx
=
[
0
]
*
pruned_num
for
name
,
a
ix
s
in
group
:
for
name
,
a
xi
s
in
group
:
pruned_params
.
append
((
name
,
axis
,
pruned_idx
))
else
:
...
...
@@ -109,10 +107,10 @@ class Pruner():
values
=
np
.
array
(
scope
.
find_var
(
name
).
get_tensor
())
group_values
.
append
((
name
,
values
,
axis
))
scores
=
self
.
criterion
(
group_
with_
values
,
scores
=
self
.
criterion
(
group_values
,
graph
)
# [(name, axis, score)]
pruned_params
=
self
.
idx_selector
(
scores
)
pruned_params
.
extend
(
self
.
idx_selector
(
scores
,
ratio
)
)
merge_pruned_params
=
{}
for
param
,
pruned_axis
,
pruned_idx
in
pruned_params
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录