Server¶
This page documents the server-side components of ICOS-FL, particularly the classes and functions defined in icos_fl.server.server and icos_fl.server.strategy.
Server Components¶
- icos_fl.server.server.server_fn(context)¶
Create and return a Flower server instance.
This function is used by the ServerApp to create server components for the federated learning system.
- Parameters:
context (Context) – Flower server context
- Returns:
Instantiated ServerAppComponents
- Return type:
ServerAppComponents
- icos_fl.server.server.gen_evaluate_fn(model, metric, time_step, batch_size, device)¶
Generate a centralized evaluation function.
This function creates a callable that can evaluate the global model on a centralized test dataset.
- Parameters:
model (LSTMModel) – The LSTM model to evaluate
metric (str) – The metric being predicted
time_step (int) – Window size for time series prediction
batch_size (int) – Batch size for data loaders
device (torch.device) – Device to run evaluation on
- Returns:
Function that takes (round, parameters, config) and returns (loss, metrics)
- Return type:
Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]
- icos_fl.server.server.create_on_fit_config_fn(learning_rate)¶
Create an on_fit_config function with the specified learning rate.
- icos_fl.server.server.on_evaluate_config(server_round)¶
Return evaluation configuration dict for each round.
- icos_fl.server.server.app¶
The Flower ServerApp instance used for federated learning.
This is the main entry point for the server-side federated learning process. It uses the
server_fnfunction to create server components.- Type:
ServerApp
Strategy Components¶
- class icos_fl.server.strategy.CustomFedAvg(*args, run_config, model, metric, use_wandb=True, save_dir='model', **kwargs)¶
Custom FedAvg strategy for ICOS-FL.
Extends the standard FedAvg strategy with additional functionality: - Tracks and logs metrics using Weights & Biases - Saves model checkpoints
- Parameters:
run_config (Dict[str, Any]) – Configuration dictionary from context
model (LSTMModel) – The LSTM model used
metric (str) – The metric being predicted
use_wandb (bool, optional) – Whether to use Weights & Biases for logging, defaults to True
save_dir (str, optional) – Directory for saving model checkpoints, defaults to “model”
args – Variable length argument list for FedAvg parent class
kwargs – Arbitrary keyword arguments for FedAvg parent class
- _init_wandb()¶
Initialize Weights & Biases project.
- log_metrics(server_round, metrics_dict)¶
Log metrics to Weights & Biases and optionally save them.
- aggregate_fit(server_round, results, failures)¶
Aggregate model updates from clients and update the global model.
- Parameters:
server_round (int) – Current round of federated learning
results (List[Tuple[ClientProxy, FitRes]]) – List of tuples of (client, fit result)
failures (List[Union[Tuple[ClientProxy, FitRes], BaseException]]) – List of failures that occurred during fitting
- Returns:
Tuple of (new global parameters, aggregation metrics)
- Return type:
Tuple[Optional[Parameters], Dict[str, Scalar]]
- aggregate_evaluate(server_round, results, failures)¶
Aggregate evaluation results from clients.
- Parameters:
server_round (int) – Current round of federated learning
results (List[Tuple[ClientProxy, FitRes]]) – List of tuples of (client, evaluation result)
failures (List[Union[Tuple[ClientProxy, FitRes], BaseException]]) – List of failures that occurred during evaluation
- Returns:
Tuple of (aggregated loss, aggregation metrics)
- Return type:
- evaluate(server_round, parameters)¶
Evaluate the global model parameters on the server.
Server Workflow¶
The server workflow in ICOS-FL follows these steps:
Initialization: The server is initialized with a strategy and configuration
Client Selection: The server selects a subset of available clients for the current round
Global Model Distribution: The server sends the current global model to selected clients
Client Update Collection: The server receives model updates from clients
Update Aggregation: The server aggregates client updates using the FedAvg strategy
Global Model Update: The server updates the global model
Model Evaluation: The server evaluates the global model
Model Checkpointing: The server saves the model checkpoint
Iteration: The process repeats for a configured number of rounds
Example Usage¶
# Run the server with Flower CLI
$ flwr run . remote-deployment
# Or programmatically
from icos_fl.server.server import app
app.start()