Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
d95da67b
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看板
提交
d95da67b
编写于
1月 17, 2020
作者:
Q
qingqing01
提交者:
whs
1月 17, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove redundant code in paddleslim/prune (#44)
上级
405a4686
变更
8
显示空白变更内容
内联
并排
Showing
8 changed file
with
5 addition
and
243 deletion
+5
-243
paddleslim/common/__init__.py
paddleslim/common/__init__.py
+1
-1
paddleslim/common/controller_server.py
paddleslim/common/controller_server.py
+1
-1
paddleslim/common/lock.py
paddleslim/common/lock.py
+0
-0
paddleslim/common/lock_utils.py
paddleslim/common/lock_utils.py
+0
-38
paddleslim/prune/__init__.py
paddleslim/prune/__init__.py
+0
-6
paddleslim/prune/auto_pruner.py
paddleslim/prune/auto_pruner.py
+3
-3
paddleslim/prune/controller_client.py
paddleslim/prune/controller_client.py
+0
-66
paddleslim/prune/controller_server.py
paddleslim/prune/controller_server.py
+0
-128
未找到文件。
paddleslim/common/__init__.py
浏览文件 @
d95da67b
...
@@ -16,7 +16,7 @@ from .sa_controller import SAController
...
@@ -16,7 +16,7 @@ from .sa_controller import SAController
from
.log_helper
import
get_logger
from
.log_helper
import
get_logger
from
.controller_server
import
ControllerServer
from
.controller_server
import
ControllerServer
from
.controller_client
import
ControllerClient
from
.controller_client
import
ControllerClient
from
.lock
_utils
import
lock
,
unlock
from
.lock
import
lock
,
unlock
from
.cached_reader
import
cached_reader
from
.cached_reader
import
cached_reader
__all__
=
[
__all__
=
[
...
...
paddleslim/common/controller_server.py
浏览文件 @
d95da67b
...
@@ -18,7 +18,7 @@ import socket
...
@@ -18,7 +18,7 @@ import socket
import
time
import
time
from
.log_helper
import
get_logger
from
.log_helper
import
get_logger
from
threading
import
Thread
from
threading
import
Thread
from
.lock
_utils
import
lock
,
unlock
from
.lock
import
lock
,
unlock
__all__
=
[
'ControllerServer'
]
__all__
=
[
'ControllerServer'
]
...
...
paddleslim/
prune
/lock.py
→
paddleslim/
common
/lock.py
浏览文件 @
d95da67b
文件已移动
paddleslim/common/lock_utils.py
已删除
100644 → 0
浏览文件 @
405a4686
# 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
os
__all__
=
[
'lock'
,
'unlock'
]
if
os
.
name
==
'nt'
:
def
lock
(
file
):
raise
NotImplementedError
(
'Windows is not supported.'
)
def
unlock
(
file
):
raise
NotImplementedError
(
'Windows is not supported.'
)
elif
os
.
name
==
'posix'
:
from
fcntl
import
flock
,
LOCK_EX
,
LOCK_UN
def
lock
(
file
):
"""Lock the file in local file system."""
flock
(
file
.
fileno
(),
LOCK_EX
)
def
unlock
(
file
):
"""Unlock the file in local file system."""
flock
(
file
.
fileno
(),
LOCK_UN
)
else
:
raise
RuntimeError
(
"File Locker only support NT and Posix platforms!"
)
paddleslim/prune/__init__.py
浏览文件 @
d95da67b
...
@@ -15,10 +15,6 @@ from .pruner import *
...
@@ -15,10 +15,6 @@ from .pruner import *
import
pruner
import
pruner
from
.auto_pruner
import
*
from
.auto_pruner
import
*
import
auto_pruner
import
auto_pruner
from
.controller_server
import
*
import
controller_server
from
.controller_client
import
*
import
controller_client
from
.sensitive_pruner
import
*
from
.sensitive_pruner
import
*
import
sensitive_pruner
import
sensitive_pruner
from
.sensitive
import
*
from
.sensitive
import
*
...
@@ -32,8 +28,6 @@ __all__ = []
...
@@ -32,8 +28,6 @@ __all__ = []
__all__
+=
pruner
.
__all__
__all__
+=
pruner
.
__all__
__all__
+=
auto_pruner
.
__all__
__all__
+=
auto_pruner
.
__all__
__all__
+=
controller_server
.
__all__
__all__
+=
controller_client
.
__all__
__all__
+=
sensitive_pruner
.
__all__
__all__
+=
sensitive_pruner
.
__all__
__all__
+=
sensitive
.
__all__
__all__
+=
sensitive
.
__all__
__all__
+=
prune_walker
.
__all__
__all__
+=
prune_walker
.
__all__
...
...
paddleslim/prune/auto_pruner.py
浏览文件 @
d95da67b
...
@@ -157,7 +157,7 @@ class AutoPruner(object):
...
@@ -157,7 +157,7 @@ class AutoPruner(object):
def
_constrain_func
(
self
,
tokens
):
def
_constrain_func
(
self
,
tokens
):
ratios
=
self
.
_tokens2ratios
(
tokens
)
ratios
=
self
.
_tokens2ratios
(
tokens
)
pruned_program
=
self
.
_pruner
.
prune
(
pruned_program
,
_
,
_
=
self
.
_pruner
.
prune
(
self
.
_program
,
self
.
_program
,
self
.
_scope
,
self
.
_scope
,
self
.
_params
,
self
.
_params
,
...
@@ -183,7 +183,7 @@ class AutoPruner(object):
...
@@ -183,7 +183,7 @@ class AutoPruner(object):
Program: The pruned program.
Program: The pruned program.
"""
"""
self
.
_current_ratios
=
self
.
_next_ratios
()
self
.
_current_ratios
=
self
.
_next_ratios
()
pruned_program
=
self
.
_pruner
.
prune
(
pruned_program
,
_
,
_
=
self
.
_pruner
.
prune
(
program
,
program
,
self
.
_scope
,
self
.
_scope
,
self
.
_params
,
self
.
_params
,
...
@@ -193,7 +193,7 @@ class AutoPruner(object):
...
@@ -193,7 +193,7 @@ class AutoPruner(object):
param_backup
=
self
.
_param_backup
)
param_backup
=
self
.
_param_backup
)
pruned_val_program
=
None
pruned_val_program
=
None
if
eval_program
is
not
None
:
if
eval_program
is
not
None
:
pruned_val_program
=
self
.
_pruner
.
prune
(
pruned_val_program
,
_
,
_
=
self
.
_pruner
.
prune
(
program
,
program
,
self
.
_scope
,
self
.
_scope
,
self
.
_params
,
self
.
_params
,
...
...
paddleslim/prune/controller_client.py
已删除
100644 → 0
浏览文件 @
405a4686
# 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
logging
import
socket
from
..common
import
get_logger
__all__
=
[
'ControllerClient'
]
_logger
=
get_logger
(
__name__
,
level
=
logging
.
INFO
)
class
ControllerClient
(
object
):
"""
Controller client.
"""
def
__init__
(
self
,
server_ip
=
None
,
server_port
=
None
,
key
=
None
):
"""
Args:
server_ip(str): The ip that controller server listens on. None means getting the ip automatically. Default: None.
server_port(int): The port that controller server listens on. 0 means getting usable port automatically. Default: 0.
key(str): The key used to identify legal agent for controller server. Default: "light-nas"
"""
self
.
server_ip
=
server_ip
self
.
server_port
=
server_port
self
.
socket_client
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
_key
=
key
def
update
(
self
,
tokens
,
reward
):
"""
Update the controller according to latest tokens and reward.
Args:
tokens(list<int>): The tokens generated in last step.
reward(float): The reward of tokens.
"""
socket_client
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
socket_client
.
connect
((
self
.
server_ip
,
self
.
server_port
))
tokens
=
","
.
join
([
str
(
token
)
for
token
in
tokens
])
socket_client
.
send
(
"{}
\t
{}
\t
{}"
.
format
(
self
.
_key
,
tokens
,
reward
)
.
encode
())
tokens
=
socket_client
.
recv
(
1024
).
decode
()
tokens
=
[
int
(
token
)
for
token
in
tokens
.
strip
(
"
\n
"
).
split
(
","
)]
return
tokens
def
next_tokens
(
self
):
"""
Get next tokens.
"""
socket_client
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
socket_client
.
connect
((
self
.
server_ip
,
self
.
server_port
))
socket_client
.
send
(
"next_tokens"
.
encode
())
tokens
=
socket_client
.
recv
(
1024
).
decode
()
tokens
=
[
int
(
token
)
for
token
in
tokens
.
strip
(
"
\n
"
).
split
(
","
)]
return
tokens
paddleslim/prune/controller_server.py
已删除
100644 → 0
浏览文件 @
405a4686
# 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
os
import
logging
import
socket
from
..common
import
get_logger
from
threading
import
Thread
from
.lock
import
lock
,
unlock
__all__
=
[
'ControllerServer'
]
_logger
=
get_logger
(
__name__
,
level
=
logging
.
INFO
)
class
ControllerServer
(
object
):
"""
The controller wrapper with a socket server to handle the request of search agent.
"""
def
__init__
(
self
,
controller
=
None
,
address
=
(
''
,
0
),
max_client_num
=
100
,
search_steps
=
None
,
key
=
None
):
"""
Args:
controller(slim.searcher.Controller): The controller used to generate tokens.
address(tuple): The address of current server binding with format (ip, port). Default: ('', 0).
which means setting ip automatically
max_client_num(int): The maximum number of clients connecting to current server simultaneously. Default: 100.
search_steps(int): The total steps of searching. None means never stopping. Default: None
"""
self
.
_controller
=
controller
self
.
_address
=
address
self
.
_max_client_num
=
max_client_num
self
.
_search_steps
=
search_steps
self
.
_closed
=
False
self
.
_port
=
address
[
1
]
self
.
_ip
=
address
[
0
]
self
.
_key
=
key
self
.
_socket_file
=
"./controller_server.socket"
def
start
(
self
):
open
(
self
.
_socket_file
,
'a'
).
close
()
socket_file
=
open
(
self
.
_socket_file
,
'r+'
)
lock
(
socket_file
)
tid
=
socket_file
.
readline
()
if
tid
==
''
:
_logger
.
info
(
"start controller server..."
)
tid
=
self
.
_start
()
socket_file
.
write
(
"tid: {}
\n
ip: {}
\n
port: {}
\n
"
.
format
(
tid
,
self
.
_ip
,
self
.
_port
))
_logger
.
info
(
"started controller server..."
)
unlock
(
socket_file
)
socket_file
.
close
()
def
_start
(
self
):
self
.
_socket_server
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
_socket_server
.
bind
(
self
.
_address
)
self
.
_socket_server
.
listen
(
self
.
_max_client_num
)
self
.
_port
=
self
.
_socket_server
.
getsockname
()[
1
]
self
.
_ip
=
self
.
_socket_server
.
getsockname
()[
0
]
_logger
.
info
(
"ControllerServer - listen on: [{}:{}]"
.
format
(
self
.
_ip
,
self
.
_port
))
thread
=
Thread
(
target
=
self
.
run
)
thread
.
start
()
return
str
(
thread
)
def
close
(
self
):
"""Close the server."""
self
.
_closed
=
True
os
.
remove
(
self
.
_socket_file
)
_logger
.
info
(
"server closed!"
)
def
port
(
self
):
"""Get the port."""
return
self
.
_port
def
ip
(
self
):
"""Get the ip."""
return
self
.
_ip
def
run
(
self
):
_logger
.
info
(
"Controller Server run..."
)
try
:
while
((
self
.
_search_steps
is
None
)
or
(
self
.
_controller
.
_iter
<
(
self
.
_search_steps
)))
and
not
self
.
_closed
:
conn
,
addr
=
self
.
_socket_server
.
accept
()
message
=
conn
.
recv
(
1024
).
decode
()
if
message
.
strip
(
"
\n
"
)
==
"next_tokens"
:
tokens
=
self
.
_controller
.
next_tokens
()
tokens
=
","
.
join
([
str
(
token
)
for
token
in
tokens
])
conn
.
send
(
tokens
.
encode
())
else
:
_logger
.
debug
(
"recv message from {}: [{}]"
.
format
(
addr
,
message
))
messages
=
message
.
strip
(
'
\n
'
).
split
(
"
\t
"
)
if
(
len
(
messages
)
<
3
)
or
(
messages
[
0
]
!=
self
.
_key
):
_logger
.
debug
(
"recv noise from {}: [{}]"
.
format
(
addr
,
message
))
continue
tokens
=
messages
[
1
]
reward
=
messages
[
2
]
tokens
=
[
int
(
token
)
for
token
in
tokens
.
split
(
","
)]
self
.
_controller
.
update
(
tokens
,
float
(
reward
))
tokens
=
self
.
_controller
.
next_tokens
()
tokens
=
","
.
join
([
str
(
token
)
for
token
in
tokens
])
conn
.
send
(
tokens
.
encode
())
_logger
.
debug
(
"send message to {}: [{}]"
.
format
(
addr
,
tokens
))
conn
.
close
()
finally
:
self
.
_socket_server
.
close
()
self
.
close
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录