Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
a78c4b8a
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看板
提交
a78c4b8a
编写于
11月 08, 2019
作者:
W
wanghaoshuang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add auto pruner base sa search strategy.
上级
efd51ec4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
130 addition
and
1 deletion
+130
-1
paddleslim/prune/__init__.py
paddleslim/prune/__init__.py
+7
-1
paddleslim/prune/auto_pruner.py
paddleslim/prune/auto_pruner.py
+123
-0
未找到文件。
paddleslim/prune/__init__.py
浏览文件 @
a78c4b8a
...
@@ -11,4 +11,10 @@
...
@@ -11,4 +11,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# 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
pruner
import
Pruner
import
pruner
from
pruner
import
*
import
auto_pruner
from
auto_pruner
import
*
__all__
=
[]
__all__
+=
pruner
.
__all__
__all__
+=
auto_pruner
.
__all__
paddleslim/prune/auto_pruner.py
0 → 100644
浏览文件 @
a78c4b8a
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
numpy
as
np
import
paddle.fluid
as
fluid
from
.pruner
import
Pruner
from
..core
import
VarWrapper
,
OpWrapper
,
GraphWrapper
from
..search
import
ControllerServer
__all__
=
[
"AutoPruner"
]
class
AutoPruner
(
object
):
def
__init__
(
self
,
params
=
[],
init_ratios
=
None
,
pruned_flops
=
0.5
,
pruned_latency
=
None
,
server_addr
=
(
""
,
""
),
search_strategy
=
"sa"
):
"""
Search a group of ratios used to prune program.
Args:
params(list<str>): The names of parameters to be pruned.
init_ratios(list<float>|float): Init ratios used to pruned parameters in `params`.
List means ratios used for pruning each parameter in `params`.
The length of `init_ratios` should be equal to length of params when `init_ratios` is a list.
If it is a scalar, all the parameters in `params` will be pruned by uniform ratio.
None means get a group of init ratios by `pruned_flops` of `pruned_latency`. Default: None.
pruned_flops(float): The percent of FLOPS to be pruned. Default: None.
pruned_latency(float): The percent of latency to be pruned. Default: None.
server_addr(tuple): A tuple of server ip and server port for controller server.
search_strategy(str): The search strategy. Default: 'sa'.
"""
# step1: Create controller server. And start server if current host match server_ip.
self
.
_controller_server
=
ControllerServer
(
addr
=
(
server_ip
,
server_port
),
search_strategy
=
"sa"
)
self
.
_params
=
params
self
.
_init_ratios
=
init_ratios
self
.
_pruned_flops
=
pruned_flops
self
.
_pruned_latency
=
pruned_latency
self
.
_pruner
=
Pruner
()
self
.
_controller_agent
=
None
self
.
_base_flops
=
None
self
.
_base_latency
=
None
def
prune
(
self
,
program
,
scope
,
place
):
if
self
.
_controller_agent
is
None
:
self
.
_controller_agent
=
PrunerAgent
(
addr
=
self
.
_controller_server
.
addr
,
self
.
_range_table
)
if
self
.
_init_ratios
is
None
:
self
.
_init_ratios
=
self
.
_get_init_ratios
(
program
,
self
.
_params
,
self
.
_pruned_flops
,
self
.
_pruned_latency
)
self
.
_current_ratios
=
self
.
_init_ratios
else
:
self
.
_current_ratios
=
self
.
_controller_agent
.
next_ratios
()
if
self
.
_base_flops
==
None
:
self
.
_base_flops
=
flops
(
program
)
for
i
in
range
(
self
.
_max_try_num
):
pruned_program
=
self
.
_pruner
.
prune
(
program
,
scope
,
self
.
_params
,
self
.
_current_ratios
,
only_graph
=
True
)
if
flops
(
pruned_program
)
<
self
.
_base_flops
*
(
1
-
self
.
_pruned_flops
):
break
self
.
_current_ratios
=
self
.
_controller_agent
.
illegal_ratios
(
self
.
_current_ratios
)
pruned_program
=
self
.
_pruner
.
prune
(
program
,
scope
,
self
.
_params
,
self
.
_current_ratios
)
return
pruned_program
def
reward
(
self
,
score
):
self
.
_controller_agent
.
reward
(
self
.
_current_ratios
,
score
)
class
PrunerAgent
(
object
):
"""
The agent used to talk with controller server.
"""
def
__init__
(
self
,
server_attr
=
(
""
,
""
),
range_table
):
self
.
_range_table
=
range_table
self
.
_controller_client
=
ControllerClient
(
server_attr
)
self
.
_controller_client
.
send_range_table
(
range_table
)
def
next_ratios
(
self
):
tokens
=
self
.
_controller_client
.
next_tokens
()
self
.
_tokens2ratios
(
tokens
)
def
illegal_ratios
(
self
,
ratios
):
tokens
=
self
.
_ratios2tokens
(
ratios
)
tokens
=
self
.
_controller_client
.
illegal_tokens
(
tokens
)
return
self
.
_tokens2ratios
(
tokens
)
def
_ratios2tokens
(
self
,
ratios
):
"""Convert pruned ratios to tokens.
"""
return
[
int
(
ratio
/
0.01
)
for
ratio
in
ratios
]
def
_tokens2_ratios
(
self
,
tokens
):
"""Convert tokens to pruned ratios.
"""
return
[
token
*
0.01
for
token
in
tokens
]
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录