Server#

class fl_sim.nodes.Server(model: Module, dataset: FedDataset, config: ServerConfig, client_config: ClientConfig, lazy: bool = False, is_subprocess: bool = False)[源代码]#

基类:Node, CitationMixin

The class to simulate the server node.

The server node is responsible for communicating with clients, and perform the aggregation of the local model parameters (and/or gradients), and update the global model parameters.

参数:
  • model (torch.nn.Module) -- The model to be trained (optimized).

  • dataset (FedDataset) -- The dataset to be used for training.

  • config (ServerConfig) -- The configs for the server.

  • client_config (ClientConfig) -- The configs for the clients.

  • lazy (bool, default False) -- Whether to use lazy initialization for the client nodes. This is useful when one wants to do centralized training for verification.

TODO

  1. Run clients training in parallel.

  2. Use the attribute _is_convergent to control the termination of the training. This perhaps can be achieved by comparing part of the items in self._cached_models

add_parameters(params: Iterable[Parameter], ratio: float) → None[源代码]#

Update the server's parameters with the given parameters.

参数:
返回类型:

None

aggregate_client_metrics(ignore: Sequence[str] | None = None) → None[源代码]#

Aggregate the metrics transmitted from the clients.

参数:

ignore (Sequence[str], optional) -- The metrics to ignore.

返回类型:

None

avg_parameters(size_aware: bool = False, inertia: float = 0.0) → None[源代码]#

Update the server's parameters via averaging the parameters received from the clients.

参数:
  • size_aware (bool, default False) -- Whether to use the size-aware averaging, which is the weighted average of the parameters, where the weight is the number of training samples. From the view of optimization theory, this is recommended to be set False.

  • inertia (float, default 0.0) -- The weight of the previous parameters, should be in the range [0, 1).

返回类型:

None

abstract property client_cls: type#

Class of the client node.

communicate(target: Client) → None[源代码]#

Broadcast the current model parameters to the target client.

This is the default (typed-API) implementation; subclasses override it for algorithm-specific payloads. The typed-API way to send additional data is building a ServerToClient and calling Client.receive() on the target; the legacy way (overriding communicate and writing target._received_messages directly) keeps working.

参数:

target (Client) -- The client to broadcast to.

返回类型:

None

abstract property config_cls: Dict[str, type]#

Class of the client node config and server node config.

Keys are "client" and "server".

evaluate_centralized(dataloader: DataLoader) → Dict[str, float][源代码]#

Evaluate the model on the given dataloader on the server node.

参数:

dataloader (DataLoader) -- The dataloader for evaluation.

返回:

metrics -- The metrics of the model on the given dataloader.

返回类型:

dict

extra_repr_keys() → List[str][源代码]#

Extra keys for __repr__() and __str__().

get_cached_metrics(client_idx: int | None = None) → List[Dict[str, float]][源代码]#

Get the cached metrics of the given client, or the cached aggregated metrics stored on the server.

参数:

client_idx (int, optional) -- The index of the client. If None, returns the cached aggregated metrics stored on the server.

返回:

The cached metrics of the given client, or the cached aggregated metrics stored on the server.

返回类型:

List[Dict[str, float]]

get_client_data(client_idx: int) → Tuple[Tensor, Tensor][源代码]#

Get all the data of the given client.

This method is a helper function for fast access to the data of the given client.

参数:

client_idx (int) -- The index of the client.

返回:

Input data and labels of the given client.

返回类型:

Tuple[Tensor, Tensor]

get_client_model(client_idx: int) → Module[源代码]#

Get the model of the given client.

This method is a helper function for fast access to the model of the given client.

参数:

client_idx (int) -- The index of the client.

返回:

The model of the given client.

返回类型:

torch.nn.Module

property is_convergent: bool#

Whether the training process is convergent.

pre_iter() → None[源代码]#

Preprocess before each iteration, after evaluation. Can be used to update the model, e.g. update the local model.

返回类型:

None

receive_one(message: ClientToServer) → None[源代码]#

Receive a typed message uploaded from a client.

The message is converted to the legacy ClientMessage and appended to self._received_messages.

参数:

message (ClientToServer) -- The typed message uploaded from a client.

返回类型:

None

train(mode: str = 'federated', extra_configs: dict | None = None) → None[源代码]#

The main training loop.

参数:
  • mode ({"federated", "centralized", "local"}, optional) -- The mode of training, by default "federated", case-insensitive.

  • extra_configs (dict, optional) -- The extra configs for the training mode.

返回类型:

None

train_centralized(extra_configs: dict | None = None) → None[源代码]#

Centralized training, conducted only on the server node.

This is used as a baseline for comparison.

参数:

extra_configs (dict, optional) -- The extra configs for centralized training.

返回类型:

None

train_federated(extra_configs: dict | None = None) → None[源代码]#

Federated (distributed) training, conducted on the clients and the server.

参数:

extra_configs (dict, optional) -- The extra configs for federated training.

返回类型:

None

TODO

Run clients training in parallel.

train_local(extra_configs: dict | None = None) → None[源代码]#

Local training, conducted on the clients, without any communication with the server. Used for comparison.

参数:

extra_configs (dict, optional) -- The extra configs for local training.

返回类型:

None

update_gradients() → None[源代码]#

Update the server's gradients.