server.py 8.7 KB
Newer Older
C
ceci3 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# Copyright (c) 2020 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 zmq
import socket
import signal
import six
import os
if six.PY2:
    import cPickle as pickle
else:
    import pickle
import logging
import time
import threading
from .log_helper import get_logger
C
ceci3 已提交
28
from .rl_controller.utils import add_grad, ConnectMessage
C
ceci3 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

_logger = get_logger(__name__, level=logging.INFO)


class Server(object):
    def __init__(self,
                 controller,
                 address,
                 is_sync=False,
                 load_controller=None,
                 save_controller=None):
        self._controller = controller
        self._address = address
        self._ip = self._address[0]
        self._port = self._address[1]
        self._is_sync = is_sync
        self._done = False
        self._load_controller = load_controller
        self._save_controller = save_controller
        ### key-value : client_name-update_times
        self._client_dict = dict()
        self._client = list()
        self._lock = threading.Lock()
        self._server_alive = True
        self._max_update_times = 0

    def close(self):
        self._server_alive = False
        _logger.info("server closed")
        pid = os.getpid()
        os.kill(pid, signal.SIGTERM)

    def start(self):
        self._ctx = zmq.Context()
        ### main socket
        self._server_socket = self._ctx.socket(zmq.REP)
        server_address = "{}:{}".format(self._ip, self._port)
        self._server_socket.bind("tcp://{}".format(server_address))
        self._server_socket.linger = 0
C
ceci3 已提交
68 69
        _logger.info("ControllerServer Start!!!")
        _logger.debug("ControllerServer - listen on: [{}]".format(
C
ceci3 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
            server_address))
        thread = threading.Thread(target=self.run, args=())
        thread.setDaemon(True)
        thread.start()

        if self._load_controller:
            assert os.path.exists(
                self._load_controller
            ), "controller checkpoint is not exist, please check your directory: {}".format(
                self._load_controller)

            with open(
                    os.path.join(self._load_controller, 'rlnas.params'),
                    'rb') as f:
                self._params_dict = pickle.load(f)
            _logger.info("Load params done")

        else:
            self._params_dict = self._controller.param_dict

        if self._is_sync:
            self._wait_socket = self._ctx.socket(zmq.REP)
            self._wait_port = self._wait_socket.bind_to_random_port(
                addr="tcp://*")
            self._wait_socket_linger = 0
            wait_thread = threading.Thread(
                target=self._wait_for_params, args=())
            wait_thread.setDaemon(True)
            wait_thread.start()

    def _wait_for_params(self):
        try:
            while self._server_alive:
                message = self._wait_socket.recv_multipart()
C
ceci3 已提交
104 105
                cmd = pickle.loads(message[0])
                client_name = pickle.loads(message[1])
C
ceci3 已提交
106 107 108 109
                if cmd == ConnectMessage.WAIT_PARAMS:
                    _logger.debug("Server: wait for params")
                    self._lock.acquire()
                    self._wait_socket.send_multipart([
C
ceci3 已提交
110 111
                        pickle.dumps(ConnectMessage.OK)
                        if self._done else pickle.dumps(ConnectMessage.WAIT)
C
ceci3 已提交
112 113 114 115
                    ])
                    if self._done and client_name in self._client:
                        self._client.remove(client_name)
                    if len(self._client) == 0:
C
ceci3 已提交
116 117
                        if self._save_controller != False:
                            self.save_params()
C
ceci3 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131
                        self._done = False
                    self._lock.release()
                else:
                    _logger.error("Error message {}".format(message))
                    raise NotImplementedError
        except Exception as err:
            logger.error(err)

    def run(self):
        try:
            while self._server_alive:
                try:
                    sum_params_dict = dict()
                    message = self._server_socket.recv_multipart()
C
ceci3 已提交
132 133
                    cmd = pickle.loads(message[0])
                    client_name = pickle.loads(message[1])
C
ceci3 已提交
134 135
                    if cmd == ConnectMessage.INIT:
                        self._server_socket.send_multipart(
C
ceci3 已提交
136
                            [pickle.dumps(ConnectMessage.INIT_DONE)])
C
ceci3 已提交
137 138 139 140 141 142 143 144
                        _logger.debug("Server: init client {}".format(
                            client_name))
                        self._client_dict[client_name] = 0
                    elif cmd == ConnectMessage.GET_WEIGHT:
                        self._lock.acquire()
                        _logger.debug("Server: get weight {}".format(
                            client_name))
                        self._server_socket.send_multipart(
C
ceci3 已提交
145
                            [pickle.dumps(self._params_dict)])
C
ceci3 已提交
146 147 148 149 150
                        _logger.debug("Server: send params done {}".format(
                            client_name))
                        self._lock.release()
                    elif cmd == ConnectMessage.UPDATE_WEIGHT:
                        _logger.info("Server: update {}".format(client_name))
C
ceci3 已提交
151
                        params_dict_grad = pickle.loads(message[2])
C
ceci3 已提交
152 153 154 155 156 157 158 159 160 161 162 163
                        if self._is_sync:
                            if not sum_params_dict:
                                sum_params_dict = self._params_dict
                            self._lock.acquire()
                            sum_params_dict = add_grad(sum_params_dict,
                                                       params_dict_grad)
                            self._client.append(client_name)
                            self._lock.release()

                            if len(self._client) == len(
                                    self._client_dict.items()):
                                self._done = True
C
ceci3 已提交
164 165
                                self._params_dict = sum_params_dict
                                del sum_params_dict
C
ceci3 已提交
166 167

                            self._server_socket.send_multipart([
C
ceci3 已提交
168
                                pickle.dumps(ConnectMessage.WAIT),
C
ceci3 已提交
169
                                pickle.dumps(self._wait_port)
C
ceci3 已提交
170 171 172 173 174 175 176 177 178 179 180
                            ])
                        else:
                            self._lock.acquire()
                            self._params_dict = add_grad(self._params_dict,
                                                         params_dict_grad)
                            self._client_dict[client_name] += 1
                            if self._client_dict[
                                    client_name] > self._max_update_times:
                                self._max_update_times = self._client_dict[
                                    client_name]
                            self._lock.release()
C
ceci3 已提交
181 182
                            if self._save_controller != False:
                                self.save_params()
C
ceci3 已提交
183
                            self._server_socket.send_multipart(
C
ceci3 已提交
184
                                [pickle.dumps(ConnectMessage.OK)])
C
ceci3 已提交
185 186 187 188 189 190

                    elif cmd == ConnectMessage.EXIT:
                        self._client_dict.pop(client_name)
                        if client_name in self._client:
                            self._client.remove(client_name)
                        self._server_socket.send_multipart(
C
ceci3 已提交
191
                            [pickle.dumps(ConnectMessage.EXIT)])
C
ceci3 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
                except zmq.error.Again as e:
                    _logger.error(e)
            self.close()

        except Exception as err:
            _logger.error(err)
        finally:
            self._server_socket.close(0)
            if self._is_sync:
                self._wait_socket.close(0)
            self.close()

    def save_params(self):
        if self._save_controller:
            if not os.path.exists(self._save_controller):
                os.makedirs(self._save_controller)
            output_dir = self._save_controller
        else:
C
ceci3 已提交
210 211
            if not os.path.exists('./.rlnas_controller'):
                os.makedirs('./.rlnas_controller')
C
ceci3 已提交
212 213 214 215
            output_dir = './.rlnas_controller'

        with open(os.path.join(output_dir, 'rlnas.params'), 'wb') as f:
            pickle.dump(self._params_dict, f)
C
ceci3 已提交
216
        _logger.debug("Save params done")