StrategyΒΆ

class Strategy[source]ΒΆ

Bases: ABC

Abstract base class for server strategy implementations.

Methods

aggregate_evaluate(server_round, replies)

Aggregate evaluation metrics from client nodes.

aggregate_train(server_round, replies)

Aggregate training results from client nodes.

configure_evaluate(server_round, arrays, ...)

Configure the next round of evaluation.

configure_train(server_round, arrays, ...)

Configure the next round of training.

start(grid, initial_arrays[, num_rounds, ...])

Execute the federated learning strategy.

summary()

Log summary configuration of the strategy.

abstract aggregate_evaluate(server_round: int, replies: Iterable[Message]) → MetricRecord | None[source]ΒΆ

Aggregate evaluation metrics from client nodes.

Parameters:
  • server_round (int) – The current round of federated learning.

  • replies (Iterable[Message]) – Iterable of reply messages received from client nodes after evaluation. MetricRecords in the messages are aggregated.

Returns:

Aggregated evaluation metrics from all participating clients, or None if aggregation failed.

Return type:

Optional[MetricRecord]

abstract aggregate_train(server_round: int, replies: Iterable[Message]) → tuple[ArrayRecord | None, MetricRecord | None][source]ΒΆ

Aggregate training results from client nodes.

Parameters:
  • server_round (int) – The current round of federated learning, starting from 1.

  • replies (Iterable[Message]) – Iterable of reply messages received from client nodes after training. Each message contains ArrayRecords and MetricRecords that get aggregated.

Returns:

A tuple containing: - ArrayRecord: Aggregated ArrayRecord, or None if aggregation failed - MetricRecord: Aggregated MetricRecord, or None if aggregation failed

Return type:

tuple[Optional[ArrayRecord], Optional[MetricRecord]]

abstract configure_evaluate(server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid) → Iterable[Message][source]ΒΆ

Configure the next round of evaluation.

Parameters:
  • server_round (int) – The current round of federated learning.

  • arrays (ArrayRecord) – Current global ArrayRecord (e.g. global model) to be sent to client nodes for evaluation.

  • config (ConfigRecord) – Configuration to be sent to clients nodes for evaluation.

  • grid (Grid) – The Grid instance used for node sampling and communication.

Returns:

An iterable of messages to be sent to selected client nodes for evaluation.

Return type:

Iterable[Message]

abstract configure_train(server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid) → Iterable[Message][source]ΒΆ

Configure the next round of training.

Parameters:
  • server_round (int) – The current round of federated learning.

  • arrays (ArrayRecord) – Current global ArrayRecord (e.g. global model) to be sent to client nodes for training.

  • config (ConfigRecord) – Configuration to be sent to clients nodes for training.

  • grid (Grid) – The Grid instance used for node sampling and communication.

Returns:

An iterable of messages to be sent to selected client nodes for training.

Return type:

Iterable[Message]

start(grid: Grid, initial_arrays: ArrayRecord, num_rounds: int = 3, timeout: float = 3600, train_config: ConfigRecord | None = None, evaluate_config: ConfigRecord | None = None, evaluate_fn: Callable[[int, ArrayRecord], MetricRecord | None] | None = None, should_stop: Callable[[int, Result], bool] | None = None) → Result[source]ΒΆ

Execute the federated learning strategy.

Runs the complete federated learning workflow for the specified number of rounds, including training, evaluation, and optional centralized evaluation.

Parameters:
  • grid (Grid) – The Grid instance used to send/receive Messages from nodes executing a ClientApp.

  • initial_arrays (ArrayRecord) – Initial model parameters (arrays) to be used for federated learning.

  • num_rounds (int (default: 3)) – Maximum number of federated learning rounds to execute.

  • timeout (float (default: 3600)) – Timeout in seconds for waiting for node responses. Outstanding responses from nodes that become unavailable are replaced with error replies.

  • train_config (ConfigRecord, optional) – Configuration to be sent to nodes during training rounds. If unset, an empty ConfigRecord will be used.

  • evaluate_config (ConfigRecord, optional) – Configuration to be sent to nodes during evaluation rounds. If unset, an empty ConfigRecord will be used.

  • evaluate_fn (Callable[[int, ArrayRecord], Optional[MetricRecord]], optional) – Optional function for centralized evaluation of the global model. Takes server round number and array record, returns a MetricRecord or None. If provided, will be called before the first round and after each round. Defaults to None.

  • should_stop (Callable[[int, Result], bool], optional) – Optional predicate to determine whether to stop training early. Called at the end of each round with the round number and accumulated results. Return True to stop and return the model and metrics collected so far. Not called for round zero. Results must not be modified. Defaults to None (no early stopping).

Returns:

Results containing final model arrays and also training metrics, evaluation metrics and global evaluation metrics (if provided) from all rounds.

Return type:

Results

abstract summary() → None[source]ΒΆ

Log summary configuration of the strategy.