fedsim.fl package#
Subpackages#
Submodules#
fedsim.fl.aggregators module#
fedsim.fl.evaluation module#
- fedsim.fl.evaluation.inference(model, data_loader, metric_fn_dict, link_fn=functools.partial(<built-in method argmax of type object>, dim=1), device='cpu', transform_y=None)[source]#
to test the performance of a model on a test set.
- Parameters
model – model to get the predictions from
loader – data loader
metric_fn_dict – a dict of {name: fn}, fn gets (inputs, targets)
link_fn – to be applied on top of model output (e.g. softmax)
device – device (e.g., ‘cuda’, ‘<gpu number> or ‘cpu’)
fedsim.fl.fl_algorithm module#
- class fedsim.fl.fl_algorithm.FLAlgorithm(data_manager, metric_logger, num_clients, sample_scheme, sample_rate, model_class, epochs, loss_fn, batch_size, test_batch_size, local_weight_decay, slr, clr, clr_decay, clr_decay_type, min_clr, clr_step_size, device, log_freq, *args, **kwargs)[source]#
Bases:
object
- deploy() Optional[Mapping[Hashable, Any]] [source]#
return Mapping of name -> parameters_set to test the model
- Raises
NotImplementedError – abstract class to be implemented by child
- optimize(aggregator: Any) Mapping[Hashable, Any] [source]#
optimize server mdoel(s) and return metrics to be reported
- Parameters
aggregator (Any) – Aggregator instance
- Raises
NotImplementedError – abstract class to be implemented by child
- Returns
Mapping[Hashable, Any] – context to be reported
- receive_from_client(client_id: int, client_msg: Mapping[Hashable, Any], aggregator: Any)[source]#
receive and aggregate info from selected clients
- Parameters
client_id (int) – id of the sender (client)
client_msg (Mapping[Hashable, Any]) – client context that is sent
aggregator (Any) – aggregator instance to collect info
- Raises
NotImplementedError – abstract class to be implemented by child
- report(dataloaders: Dict[str, Any], metric_logger: Any, device: str, optimize_reports: Mapping[Hashable, Any], deployment_points: Optional[Mapping[Hashable, torch.Tensor]] = None) None [source]#
test on global data and report info
- Parameters
dataloaders (Any) – dict of data loaders to test the global model(s)
metric_logger (Any) – the logging object (e.g., SummaryWriter)
device (str) – ‘cuda’, ‘cpu’ or gpu number
optimize_reports (Mapping[Hashable, Any]) – dict returned by optimzier
deployment_points (Mapping[Hashable, torch.Tensor], optional) – output of deploy method
- Raises
NotImplementedError – abstract class to be implemented by child
- send_to_client(client_id: int) Mapping[Hashable, Any] [source]#
- returns context to send to the client corresponding to client_id.
Do not send shared objects like server model if you made any before you deepcopy it.
- Parameters
client_id (int) – id of the receiving client
- Raises
NotImplementedError – abstract class to be implemented by child
- Returns
Mapping[Hashable, Any] – the context to be sent in form of a Mapping
- send_to_server(client_id: int, datasets: Dict[str, Iterable], epochs: int, loss_fn: torch.nn.modules.module.Module, batch_size: int, lr: float, weight_decay: float = 0, device: Union[int, str] = 'cuda', ctx: Optional[Dict[Hashable, Any]] = None, *args, **kwargs) Mapping[str, Any] [source]#
client operation on the recieved information.
- Parameters
client_id (int) – id of the client
datasets (Dict[str, Iterable]) – this comes from Data Manager
epochs (int) – number of epochs to train
loss_fn (nn.Module) – either ‘ce’ (for cross-entropy) or ‘mse’
batch_size (int) – training batch_size
lr (float) – client learning rate
weight_decay (float, optional) – weight decay for SGD. Defaults to 0.
device (Union[int, str], optional) – Defaults to ‘cuda’.
ctx (Optional[Dict[Hashable, Any]], optional) – context reveived from server. Defaults to None.
- Raises
NotImplementedError – abstract class to be implemented by child
- Returns
Mapping[str, Any] – client context to be sent to the server
fedsim.fl.utils module#
- class fedsim.fl.utils.ModelReconstructor(feature_extractor, classifier, connection_fn=None)[source]#
Bases:
torch.nn.modules.module.Module
- forward(input)[source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool#
- fedsim.fl.utils.default_closure(x, y, model, loss_fn, optimizer, metric_fn_dict, max_grad_norm=1000, link_fn=functools.partial(<built-in method argmax of type object>, dim=1), device='cpu', transform_grads=None, transform_y=None, **kwargs)[source]#
- fedsim.fl.utils.vector_to_parameters_like(vec, parameters_like)[source]#
Convert one vector to new parameters like the ones provided
- Parameters
vec (Tensor) – a single vector represents the parameters of a model.
parameters (Iterable[Tensor]) – an iterator of Tensors that are the parameters of a model. This is only used to get the sizes. New parametere are defined.