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.

Parameters:

learning_rate (float) – Learning rate to use in the config

Returns:

A function that creates client configs with this learning rate

Return type:

Callable[[int], Dict[str, Scalar]]

icos_fl.server.server.on_evaluate_config(server_round)

Return evaluation configuration dict for each round.

Parameters:

server_round (int) – Current federated learning round

Returns:

Configuration dictionary with server round

Return type:

Dict[str, Scalar]

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_fn function 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.

Parameters:
  • server_round (int) – Current federated learning round

  • metrics_dict (Dict[str, Scalar]) – Dictionary containing metrics to log

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:

Tuple[Optional[float], Dict[str, Scalar]]

evaluate(server_round, parameters)

Evaluate the global model parameters on the server.

Parameters:
  • server_round (int) – Current round of federated learning

  • parameters (Parameters) – Current global model parameters

Returns:

Tuple of (loss, metrics) or None

Return type:

Optional[Tuple[float, Dict[str, Scalar]]]

icos_fl.server.strategy.train_metrics_aggregation(metrics)

Aggregate training metrics from multiple clients.

Parameters:

metrics (List[Tuple[int, Metrics]]) – List of tuples (num_examples, metrics_dict)

Returns:

Aggregated metrics dictionary

Return type:

Metrics

icos_fl.server.strategy.evaluate_metrics_aggregation(metrics)

Aggregate evaluation metrics from multiple clients.

Parameters:

metrics (List[Tuple[int, Metrics]]) – List of tuples (num_examples, metrics_dict)

Returns:

Aggregated metrics dictionary

Return type:

Metrics

Server Workflow

The server workflow in ICOS-FL follows these steps:

  1. Initialization: The server is initialized with a strategy and configuration

  2. Client Selection: The server selects a subset of available clients for the current round

  3. Global Model Distribution: The server sends the current global model to selected clients

  4. Client Update Collection: The server receives model updates from clients

  5. Update Aggregation: The server aggregates client updates using the FedAvg strategy

  6. Global Model Update: The server updates the global model

  7. Model Evaluation: The server evaluates the global model

  8. Model Checkpointing: The server saves the model checkpoint

  9. 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()