def backward(self, epoch, batch): encrypted_guest_weight_gradient = self.get_guest_encrypted_weight_gradient_from_guest( epoch, batch) LOGGER.info("decrypt weight gradient of epoch {} batch {}".format( epoch, batch)) decrypted_guest_weight_gradient = self.encrypter.recursive_decrypt( encrypted_guest_weight_gradient) noise_weight_gradient = self.rng_generator.generate_random_number( (self.input_shape, self.output_unit)) decrypted_guest_weight_gradient += noise_weight_gradient / self.learning_rate self.send_guest_decrypted_weight_gradient_to_guest( decrypted_guest_weight_gradient, epoch, batch) LOGGER.info("encrypt acc_noise of epoch {} batch {}".format( epoch, batch)) encrypted_acc_noise = self.encrypter.recursive_encrypt(self.acc_noise) self.send_encrypted_acc_noise_to_guest(encrypted_acc_noise, epoch, batch) self.acc_noise += noise_weight_gradient host_input_gradient = PaillierTensor( tb_obj=self.get_host_backward_from_guest(epoch, batch)) host_input_gradient = host_input_gradient.decrypt( self.encrypter).numpy() return host_input_gradient
def forward(self, host_input, epoch=0, batch=0): if batch >= len(self.train_encrypted_calculator): self.train_encrypted_calculator.append( self.generated_encrypted_calculator()) LOGGER.info( "forward propagation: encrypt host_bottom_output of epoch {} batch {}" .format(epoch, batch)) host_input = PaillierTensor(ori_data=host_input, partitions=self.partitions) encrypted_host_input = host_input.encrypt( self.train_encrypted_calculator[batch]) self.send_host_encrypted_forward_to_guest( encrypted_host_input.get_obj(), epoch, batch) encrypted_guest_forward = PaillierTensor( tb_obj=self.get_guest_encrypted_forwrad_from_guest(epoch, batch)) decrypted_guest_forward = encrypted_guest_forward.decrypt( self.encrypter) if self.acc_noise is None: self.input_shape = host_input.shape[1] self.output_unit = encrypted_guest_forward.shape[1] self.acc_noise = np.zeros((self.input_shape, self.output_unit)) """some bugs here""" decrypted_guest_forward_with_noise = decrypted_guest_forward + host_input * self.acc_noise self.send_decrypted_guest_forward_with_noise_to_guest( decrypted_guest_forward_with_noise.get_obj(), epoch, batch)
def decrypt_guest_data(self, epoch_idx, local_round=-1): encrypted_consts = self.transfer_variable.guest_side_const.get(suffix=( epoch_idx, local_round, ), idx=0) grad_table = self.transfer_variable.guest_side_gradients.get(suffix=( epoch_idx, local_round, ), idx=0) inter_grad = PaillierTensor(tb_obj=grad_table, partitions=self.partitions) decrpyted_grad = inter_grad.decrypt(self.encrypter) decrypted_const = self.encrypter.recursive_decrypt(encrypted_consts) self.transfer_variable.decrypted_guest_const.remote(decrypted_const, suffix=( epoch_idx, local_round, )) self.transfer_variable.decrypted_guest_gradients.remote( decrpyted_grad.get_obj(), suffix=( epoch_idx, local_round, ))
def decrypt_host_data(self, epoch_idx, local_round=-1): inter_grad = self.transfer_variable.host_side_gradients.get( suffix=(epoch_idx, local_round, 'host_de_send'), idx=0) inter_grad_pt = PaillierTensor(tb_obj=inter_grad, partitions=self.partitions) self.transfer_variable.decrypted_host_gradients.remote( inter_grad_pt.decrypt(self.encrypter).get_obj(), suffix=(epoch_idx, local_round, 'host_de_get'))
def backward(self, epoch, batch): do_backward = True selective_ids = [] if self.do_backward_select_strategy: selective_ids, do_backward = self.sync_backward_select_info( epoch, batch) if not do_backward: return [], selective_ids encrypted_guest_weight_gradient = self.get_guest_encrypted_weight_gradient_from_guest( epoch, batch) LOGGER.info("decrypt weight gradient of epoch {} batch {}".format( epoch, batch)) decrypted_guest_weight_gradient = self.encrypter.recursive_decrypt( encrypted_guest_weight_gradient) noise_weight_gradient = self.rng_generator.generate_random_number( (self.input_shape, self.output_unit)) decrypted_guest_weight_gradient += noise_weight_gradient / self.learning_rate self.send_guest_decrypted_weight_gradient_to_guest( decrypted_guest_weight_gradient, epoch, batch) LOGGER.info("encrypt acc_noise of epoch {} batch {}".format( epoch, batch)) encrypted_acc_noise = self.encrypter.recursive_encrypt(self.acc_noise) self.send_encrypted_acc_noise_to_guest(encrypted_acc_noise, epoch, batch) self.acc_noise += noise_weight_gradient host_input_gradient = PaillierTensor( tb_obj=self.get_host_backward_from_guest(epoch, batch)) host_input_gradient = host_input_gradient.decrypt(self.encrypter) if self.fixed_point_encoder: host_input_gradient = host_input_gradient.decode( self.fixed_point_encoder).numpy() else: host_input_gradient = host_input_gradient.numpy() return host_input_gradient, selective_ids
def forward(self, host_input, epoch=0, batch=0, train=True): if batch >= len(self.train_encrypted_calculator): self.train_encrypted_calculator.append( self.generated_encrypted_calculator()) LOGGER.info( "forward propagation: encrypt host_bottom_output of epoch {} batch {}" .format(epoch, batch)) host_input = PaillierTensor(ori_data=host_input, partitions=self.partitions) encrypted_host_input = host_input.encrypt( self.train_encrypted_calculator[batch]) self.send_host_encrypted_forward_to_guest( encrypted_host_input.get_obj(), epoch, batch) encrypted_guest_forward = PaillierTensor( tb_obj=self.get_guest_encrypted_forwrad_from_guest(epoch, batch)) decrypted_guest_forward = encrypted_guest_forward.decrypt( self.encrypter) if self.fixed_point_encoder: decrypted_guest_forward = decrypted_guest_forward.decode( self.fixed_point_encoder) if self.acc_noise is None: self.input_shape = host_input.shape[1] self.output_unit = self.get_interactive_layer_output_unit() self.acc_noise = np.zeros((self.input_shape, self.output_unit)) mask_table = None if train and self.drop_out_keep_rate and self.drop_out_keep_rate < 1: mask_table = self.get_interactive_layer_drop_out_table( epoch, batch) if mask_table: decrypted_guest_forward_with_noise = decrypted_guest_forward + ( host_input * self.acc_noise).select_columns(mask_table) self.mask_table = mask_table else: decrypted_guest_forward_with_noise = decrypted_guest_forward + ( host_input * self.acc_noise) self.send_decrypted_guest_forward_with_noise_to_guest( decrypted_guest_forward_with_noise.get_obj(), epoch, batch)