def receive_blocks(self, package: IPack):
     self.__global_weights -= package.content
     reply = NormalPack(
         Parameter_Server,
         self.__global_weights.astype(
             GlobalSettings.get_params(Low_Precision_Server)))
     return netEncapsulation(package.node_id, reply)
 def update_blocks(
         self, block_weight: BlockWeight) -> netEncapsulation[NormalPack]:
     package = NormalPack(
         self.node_id,
         block_weight.content.astype(
             GlobalSettings.get_params(Full_Precision_Client)))
     return netEncapsulation(Parameter_Server, package)
Exemple #3
0
 def receive_blocks(self, content: dict):
     """
         SGQ receive a quantized matrix and returns the grad diff for the client
     :param content:
     :return:
     """
     # decode data
     data = SGQPackage.decode(content)
     # get working node state
     last_state = self.Weights_Last_Received[data.node_id]
     # update global state
     self.Global_State = self.Global_State + data.content()
     # get grad diff
     grad_diff = self.Global_State - last_state
     # encode data
     data_rtn = SGQPackage(grad_diff)
     # update state buffer
     self.Weights_Last_Received[
         data.node_id] = last_state + data_rtn.content()
     # for DEBUG
     error = np.sum(self.Weights_Last_Received[data.node_id] -
                    self.Global_State)
     if error > SGQServer.__max_error:
         SGQServer.__max_error = error
         print("Error:", error)
     # return
     return netEncapsulation(data.node_id, data_rtn.encode())
 def update_blocks(
         self,
         block_weight: BlockWeight) -> netEncapsulation[QuantizedPack]:
     package = QuantizedPack(
         self.node_id, *quantize(block_weight.content,
                                 QuantizedClient.codex))
     return netEncapsulation(Parameter_Server, package)
Exemple #5
0
 def update_blocks(self, block_weight: BlockWeight):
     """
         Update weights to parameter server
     :param block_weight:
     :return:
     """
     content = block_weight.content
     pkg = SGQPackage(content, self.node_id)
     return netEncapsulation(Parameter_Server, pkg.encode())
Exemple #6
0
 def update_blocks(
     self, block_weight: BlockWeight
 ) -> Union[Iterable[netEncapsulation], netEncapsulation, None]:
     self.__local_turn += 1
     if self.__local_turn >= self.__TURN:
         return netEncapsulation(Parameter_Server,
                                 (self.node_id, block_weight.content))
     else:
         self.set_result(block_weight.content)
 def receive_blocks(self, package: IPack) -> netEncapsulation[IPack]:
     delta_w = package.decode(DC_QSGDServer.codex)
     ggw_t = np.multiply(np.multiply(self.__gw_t, self.__gw_t), delta_w)
     gw_t_tau = self.__gw_t + self.__lambda * ggw_t
     self.set_result(gw_t_tau - delta_w, lambda x, y: x + y
                     if x is not None else y)
     return netEncapsulation(
         Parameter_Server,
         QuantizedPack(self.node_id, gw_t_tau, DC_QSGDClient.codex))
Exemple #8
0
 def receive_blocks(self, package: IPack):
     grad = package.content
     self.__global_weights -= grad
     self.__vt = self.__beta * self.__vt + np.square(grad) * (1 -
                                                              self.__beta)
     reply = QuantizedPack(
         Parameter_Server,
         *Q_w(self.__global_weights, QuantizedParaServer.q_space,
              np.sqrt(self.__vt)))
     return netEncapsulation(package.node_id, reply)
Exemple #9
0
    def receive_blocks(
        self, content: Tuple[int, ndarray]
    ) -> Union[Iterable[netEncapsulation], netEncapsulation, None]:
        """
            PA Server receive a json_dict and send back a request
        :param content:
        :return:
        """
        # update global current state
        self.Current_Weights = self.Current_Weights - content[1]

        return netEncapsulation(content[0], (-1, self.Current_Weights))
Exemple #10
0
 def update_blocks(
         self, block_weight: BlockWeight
 ) -> netEncapsulation[Tuple[int, ndarray]]:
     """
         Try collect all blocks.
     """
     self.BlockWeights[block_weight.block_id] = block_weight.content
     self.check_for_combine()
     send_to = GlobalSettings.get_default().get_adversary(
         block_weight.block_id)
     return netEncapsulation(send_to,
                             (block_weight.block_id, block_weight.content))
Exemple #11
0
 def receive_blocks(self, package: IPack):
     if isinstance(package, QuantizedPack):
         self.__global_weights -= package.decode(DC_QSGDClient.codex)
         self.__weights_states[package.node_id] = self.__global_weights
     elif isinstance(package, SignalPack):
         # returns Q(w_t+\tau - w_t)
         if not isinstance(self.__global_weights, np.ndarray):
             reply = SignalPack(Parameter_Server)
         else:
             reply = QuantizedPack(
                 Parameter_Server, self.__global_weights -
                 self.__weights_states.get(package.node_id, 0),
                 DC_QSGDServer.codex)
         return netEncapsulation(package.node_id, reply)
Exemple #12
0
 def receive_blocks(
     self, content: Tuple[int, ndarray]
 ) -> Union[Iterable[netEncapsulation], netEncapsulation, None]:
     """
         PA Server receive a json_dict and send back a request
     :param content:
     :return:
     """
     # update global current state
     self.Bak_Weights_Node[content[0]] = content[1]
     if len(self.Bak_Weights_Node) == GlobalSettings.get_default(
     ).node_count:
         global_weight = np.mean(list(self.Bak_Weights_Node.values()),
                                 axis=0)
         self.dispose()
         return netEncapsulation(GlobalSettings.get_default().nodes,
                                 (Parameter_Server, global_weight))
Exemple #13
0
    def receive_blocks(
        self, content: Tuple[int, ndarray]
    ) -> Union[Iterable[netEncapsulation], netEncapsulation, None]:
        """
            PA Server receive a json_dict and send back a request
        :param content:
        :return:
        """
        # get last state of working node
        last_state = self.Bak_Weights_Node.get(content[0], 0)
        # update global current state
        self.Current_Weights = self.Current_Weights + content[1]
        # get difference
        grad_diff = self.Current_Weights - last_state
        # update last state of working node
        self.Bak_Weights_Node[content[0]] = self.Current_Weights

        return netEncapsulation(content[0], (-1, grad_diff))
Exemple #14
0
    def update_blocks(
            self, block_weight: BlockWeight
    ) -> netEncapsulation[Dict[str, np.ndarray]]:
        print('Weights delta received.')
        print('from block: {}'.format(block_weight.block_id))
        print('It has a content with shape: {}'.format(
            block_weight.content.shape))

        # 获取没有该数据的节点
        send_to = GlobalSettings.get_default().get_adversary(
            block_weight.block_id)
        # 我们使用 'data' 字符串来标记我们的梯度内容
        pkg = {'data': block_weight.content}
        # 记录本机梯度
        self.__global_weights += block_weight.content
        self.__current_recv += 1
        # 检查是否接受完所有数据
        self.__do_grad_average()
        # 发送梯度
        return netEncapsulation(send_to, pkg)
Exemple #15
0
 def receive_blocks(
     self, content: Tuple[int, np.ndarray]
 ) -> Union[Iterable[netEncapsulation], netEncapsulation]:
     """
         Adaptive DC-ASGD algorithm.
     """
     content_square = np.multiply(content[1], content[1])
     # Update weights with delay-compensation
     delay = np.multiply(
         content_square,
         self.Weights_init - self.Bak_Weights_Node[content[0]])
     self.Weights_init = self.Weights_init - self.Learn_Rate * (
         content[1] + self.Lambda_T * delay)
     self.Mean_Square = self.Mean_Square_M * self.Mean_Square + (
         1 - self.Mean_Square_M) * content_square
     self.Lambda_T = self.Lambda_T / np.sqrt(self.Mean_Square +
                                             self.Mean_Square_Epsilon)
     # Send back updated weights
     self.Bak_Weights_Node[content[0]] = self.Weights_init
     return netEncapsulation(content[0], (-1, self.Weights_init))
Exemple #16
0
 def receive_blocks(self, content: dict):
     pkg = TQNPackage.decode(content)
     self.__global_weights -= pkg.content()
     return netEncapsulation(
         pkg.node_id,
         TQNPackage(self.__global_weights, Parameter_Server).encode())
 def receive_blocks(self, package: IPack):
     self.__global_weights -= package.content
     reply = NormalPack(Parameter_Server, self.__global_weights)
     return netEncapsulation(package.node_id, reply)
Exemple #18
0
 def receive_blocks(self, package: IPack):
     self.__global_weights -= package.decode(QuantizedClient.codex)
     reply = QuantizedPack(Parameter_Server, self.__global_weights,
                           QuantizedParaServer.codex)
     return netEncapsulation(package.node_id, reply)
Exemple #19
0
 def update_blocks(
     self, block_weight: BlockWeight
 ) -> Union[Iterable[netEncapsulation], netEncapsulation, None]:
     return netEncapsulation(Parameter_Server,
                             (self.node_id, block_weight.content))
 def receive_blocks(self, package: IPack):
     self.__global_weights -= package.content
     reply = QuantizedPack(Parameter_Server,
                           *binarize(self.__global_weights))
     return netEncapsulation(package.node_id, reply)
Exemple #21
0
 def update_blocks(self, block_weight: BlockWeight):
     return netEncapsulation(
         Parameter_Server,
         TQNPackage(block_weight.content, self.node_id).encode())
Exemple #22
0
 def update_blocks(self,
                   block_weight: BlockWeight) -> netEncapsulation[IPack]:
     self.__gw_t = block_weight.content
     return netEncapsulation(Parameter_Server, SignalPack(self.node_id))