def serialize_model_params(params): """Serializes list of tensors into State/protobuf.""" model_params_state = State(state_placeholders=[ PlaceHolder().instantiate(param) for param in params ]) # make fake local worker for serialization worker = sy.VirtualWorker(hook=None) pb = protobuf.serde._bufferize(worker, model_params_state) serialized_state = pb.SerializeToString() return serialized_state
def report(self, updated_model_params: list): # Calc params diff orig_params = self.model.tensors() diff_params = [orig_params[i] - updated_model_params[i] for i in range(len(orig_params))] # Wrap diff in State diff_ph = [PlaceHolder().instantiate(t) for t in diff_params] diff = State(state_placeholders=diff_ph) response = self.grid_client.report( worker_id=self.fl_client.worker_id, request_key=self.cycle_params["request_key"], diff=diff, ) return response
def copy(self): # TODO not the cleanest method ever placeholders = {} old_ids_2_new_ids = {} for ph in self.placeholders.values(): copy = ph.copy() old_ids_2_new_ids[ph.id.value] = copy.id.value placeholders[copy.id.value] = copy new_input_placeholder_ids = tuple( old_ids_2_new_ids[self.placeholders[input_id].id.value] for input_id in self.input_placeholder_ids) new_output_placeholder_ids = tuple( old_ids_2_new_ids[self.placeholders[output_id].id.value] for output_id in self.output_placeholder_ids) state_placeholders = [] for ph in self.state.state_placeholders: new_ph = PlaceHolder( id=old_ids_2_new_ids[ph.id.value]).instantiate(ph.child) state_placeholders.append(new_ph) state = State(state_placeholders) _replace_placeholder_ids = lambda obj: Role.nested_object_traversal( obj, lambda x: PlaceholderId(old_ids_2_new_ids[x.value]), PlaceholderId) new_actions = [] for action in self.actions: action_type = type(action) target = _replace_placeholder_ids(action.target) args_ = _replace_placeholder_ids(action.args) kwargs_ = _replace_placeholder_ids(action.kwargs) return_ids = _replace_placeholder_ids(action.return_ids) new_actions.append( action_type(action.name, target, args_, kwargs_, return_ids)) return Role( state=state, actions=new_actions, placeholders=placeholders, input_placeholder_ids=new_input_placeholder_ids, output_placeholder_ids=new_output_placeholder_ids, id=sy.ID_PROVIDER.pop(), )
def __init__( self, state: State = None, actions: List[Action] = None, placeholders: Dict[Union[str, int], PlaceHolder] = None, input_placeholder_ids: Tuple[int, str] = None, output_placeholder_ids: Tuple[int, str] = None, # General kwargs id: Union[str, int] = None, ): self.id = id or sy.ID_PROVIDER.pop() self.actions = actions or [] # All placeholders self.placeholders = placeholders or {} # Input placeholders, stored by id self.input_placeholder_ids = input_placeholder_ids or () # Output placeholders self.output_placeholder_ids = output_placeholder_ids or () self.state = state or State()
def tensors_to_state(tensors): return State( state_placeholders=[PlaceHolder().instantiate(t) for t in tensors])
"pool_selection": "random", "do_not_reuse_workers_until_cycle": 6, "cycle_length": 28800, # max cycle length in seconds "num_cycles": 5, # max number of cycles "max_diffs": 1, # number of diffs to collect before avg "minimum_upload_speed": 0, "minimum_download_speed": 0, "iterative_plan": True, # tells PyGrid that avg plan is executed per diff #"authentication": { # "type": "jwt", # "pub_key": public_key, #} } model_params_state = State(state_placeholders=[ PlaceHolder().instantiate(param) for param in model_params ]) print("Hosting plan...") try: response = grid.host_federated_training(model=model_params_state, client_plans={ 'training_plan': training_plan, 'eval_plan': eval_plan }, client_protocols={}, server_averaging_plan=avg_plan, client_config=client_config, server_config=server_config) print("Host response:", response)