def __init__( self, id: Union[str, int] = None, worker: AbstractWorker = None, 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, ): self.id = id or sy.ID_PROVIDER.pop() self.worker = worker or sy.local_worker 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() self.tracing = False for name, package in framework_packages.items(): tracing_wrapper = FrameworkWrapper(package=package, role=self) setattr(self, name, tracing_wrapper)
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, state_tensors=None, # General kwargs id: Union[str, int] = None, owner: "sy.workers.BaseWorker" = None, tags: List[str] = None, description: str = None, ): owner = owner or sy.local_worker AbstractObject.__init__(self, id, owner, tags, description, child=None) self.owner = owner self.actions = actions or [] self.state = state or State(owner=owner) # 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 () # state_tensors are provided when plans are created using func2plan if state_tensors: # we want to make sure in that case that the state is empty assert state is None for tensor in state_tensors: self.register_state_tensor(tensor)
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], owner=self.owner).instantiate(ph.child) state_placeholders.append(new_ph) state = State(owner=self.owner, state_placeholders=state_placeholders) def _replace_placeholder_ids(obj): if isinstance(obj, (tuple, list)): r = [_replace_placeholder_ids(o) for o in obj] return type(obj)(r) elif isinstance(obj, dict): return { key: _replace_placeholder_ids(value) for key, value in obj.items() } elif isinstance(obj, PlaceholderId): return PlaceholderId(old_ids_2_new_ids[obj.value]) else: return obj 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(), owner=self.owner, tags=self.tags, description=self.description, )
def __init__( self, name: str = None, state: State = None, include_state: bool = False, is_built: bool = False, actions: List[ComputationAction] = None, placeholders: Dict[Union[str, int], PlaceHolder] = None, forward_func=None, state_tensors=None, # General kwargs id: Union[str, int] = None, owner: "sy.workers.BaseWorker" = None, tags: List[str] = None, description: str = None, ): owner = owner or sy.local_worker AbstractObject.__init__(self, id, owner, tags, description, child=None) ObjectStorage.__init__(self) # Plan instance info self.name = name or self.__class__.__name__ self.owner = owner self.actions = actions or [] # Keep a local reference to all placeholders, stored by id self.placeholders = placeholders or {} # Incremental value to tag all placeholders with different tags self.var_count = 0 self.state = state or State(owner=owner) # state_tensors are provided when plans are created using func2plan if state_tensors is not None: # we want to make sure in that case that the state is empty assert state is None for tensor in state_tensors: placeholder = sy.PlaceHolder( tags={"#state", f"#{self.var_count + 1}"}, id=tensor.id, owner=self.owner) self.var_count += 1 placeholder.instantiate(tensor) self.state.state_placeholders.append(placeholder) self.placeholders[tensor.id] = placeholder self.include_state = include_state self.is_built = is_built # The plan has not been sent so it has no reference to remote locations self.pointers = dict() if not hasattr(self, "forward"): self.forward = forward_func or None self.__name__ = self.__repr__( ) # For PyTorch jit tracing compatibility
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)