diff --git a/src/py/flwr/server/strategy/bulyan.py b/src/py/flwr/server/strategy/bulyan.py index 0243f4e6546f..1e4f97530ab7 100644 --- a/src/py/flwr/server/strategy/bulyan.py +++ b/src/py/flwr/server/strategy/bulyan.py @@ -38,10 +38,43 @@ # flake8: noqa: E501 +# pylint: disable=line-too-long class Bulyan(FedAvg): - """Bulyan strategy implementation.""" - - # pylint: disable=too-many-arguments,too-many-instance-attributes,line-too-long, too-many-locals + """Bulyan strategy. + + Implementation based on https://arxiv.org/abs/1802.07927. + + Parameters + ---------- + fraction_fit : float, optional + Fraction of clients used during training. Defaults to 1.0. + fraction_evaluate : float, optional + Fraction of clients used during validation. Defaults to 1.0. + min_fit_clients : int, optional + Minimum number of clients used during training. Defaults to 2. + min_evaluate_clients : int, optional + Minimum number of clients used during validation. Defaults to 2. + min_available_clients : int, optional + Minimum number of total clients in the system. Defaults to 2. + num_malicious_clients : int, optional + Number of malicious clients in the system. Defaults to 0. + evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] + Optional function used for validation. Defaults to None. + on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure training. Defaults to None. + on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure validation. Defaults to None. + accept_failures : bool, optional + Whether or not accept rounds containing failures. Defaults to True. + initial_parameters : Parameters, optional + Initial global model parameters. + first_aggregation_rule: Callable + Byzantine resilient aggregation rule that is used as the first step of the Bulyan (e.g., Krum) + **aggregation_rule_kwargs: Any + arguments to the first_aggregation rule + """ + + # pylint: disable=too-many-arguments,too-many-instance-attributes,too-many-locals def __init__( self, *, @@ -66,39 +99,6 @@ def __init__( first_aggregation_rule: Callable = aggregate_krum, # type: ignore **aggregation_rule_kwargs: Any, ) -> None: - """Bulyan strategy. - - Implementation based on https://arxiv.org/abs/1802.07927. - - Parameters - ---------- - fraction_fit : float, optional - Fraction of clients used during training. Defaults to 1.0. - fraction_evaluate : float, optional - Fraction of clients used during validation. Defaults to 1.0. - min_fit_clients : int, optional - Minimum number of clients used during training. Defaults to 2. - min_evaluate_clients : int, optional - Minimum number of clients used during validation. Defaults to 2. - min_available_clients : int, optional - Minimum number of total clients in the system. Defaults to 2. - num_malicious_clients : int, optional - Number of malicious clients in the system. Defaults to 0. - evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] - Optional function used for validation. Defaults to None. - on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure training. Defaults to None. - on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure validation. Defaults to None. - accept_failures : bool, optional - Whether or not accept rounds containing failures. Defaults to True. - initial_parameters : Parameters, optional - Initial global model parameters. - first_aggregation_rule: Callable - Byzantine resilient aggregation rule that is used as the first step of the Bulyan (e.g., Krum) - **aggregation_rule_kwargs: Any - arguments to the first_aggregation rule - """ super().__init__( fraction_fit=fraction_fit, fraction_evaluate=fraction_evaluate, diff --git a/src/py/flwr/server/strategy/fedadagrad.py b/src/py/flwr/server/strategy/fedadagrad.py index 085362891d94..4a8f52d98e18 100644 --- a/src/py/flwr/server/strategy/fedadagrad.py +++ b/src/py/flwr/server/strategy/fedadagrad.py @@ -38,13 +38,47 @@ from .fedopt import FedOpt +# pylint: disable=line-too-long class FedAdagrad(FedOpt): """FedAdagrad strategy - Adaptive Federated Optimization using Adagrad. - Paper: https://arxiv.org/abs/2003.00295 + Implementation based on https://arxiv.org/abs/2003.00295v5 + + Parameters + ---------- + fraction_fit : float, optional + Fraction of clients used during training. Defaults to 1.0. + fraction_evaluate : float, optional + Fraction of clients used during validation. Defaults to 1.0. + min_fit_clients : int, optional + Minimum number of clients used during training. Defaults to 2. + min_evaluate_clients : int, optional + Minimum number of clients used during validation. Defaults to 2. + min_available_clients : int, optional + Minimum number of total clients in the system. Defaults to 2. + evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]],Optional[Tuple[float, Dict[str, Scalar]]]]] + Optional function used for validation. Defaults to None. + on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure training. Defaults to None. + on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure validation. Defaults to None. + fit_metrics_aggregation_fn : Optional[MetricsAggregationFn] + Metrics aggregation function, optional. + evaluate_metrics_aggregation_fn: Optional[MetricsAggregationFn] + Metrics aggregation function, optional. + accept_failures : bool, optional + Whether or not accept rounds containing failures. Defaults to True. + initial_parameters : Parameters + Initial global model parameters. + eta : float, optional + Server-side learning rate. Defaults to 1e-1. + eta_l : float, optional + Client-side learning rate. Defaults to 1e-1. + tau : float, optional + Controls the algorithm's degree of adaptability. Defaults to 1e-9. """ - # pylint: disable=too-many-arguments,too-many-locals,too-many-instance-attributes, line-too-long + # pylint: disable=too-many-arguments,too-many-locals,too-many-instance-attributes def __init__( self, *, @@ -69,43 +103,6 @@ def __init__( eta_l: float = 1e-1, tau: float = 1e-9, ) -> None: - """Federated learning strategy using Adagrad on server-side. - - Implementation based on https://arxiv.org/abs/2003.00295v5 - - Parameters - ---------- - fraction_fit : float, optional - Fraction of clients used during training. Defaults to 1.0. - fraction_evaluate : float, optional - Fraction of clients used during validation. Defaults to 1.0. - min_fit_clients : int, optional - Minimum number of clients used during training. Defaults to 2. - min_evaluate_clients : int, optional - Minimum number of clients used during validation. Defaults to 2. - min_available_clients : int, optional - Minimum number of total clients in the system. Defaults to 2. - evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] - Optional function used for validation. Defaults to None. - on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure training. Defaults to None. - on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure validation. Defaults to None. - fit_metrics_aggregation_fn : Optional[MetricsAggregationFn] - Metrics aggregation function, optional. - evaluate_metrics_aggregation_fn: Optional[MetricsAggregationFn] - Metrics aggregation function, optional. - accept_failures : bool, optional - Whether or not accept rounds containing failures. Defaults to True. - initial_parameters : Parameters - Initial global model parameters. - eta : float, optional - Server-side learning rate. Defaults to 1e-1. - eta_l : float, optional - Client-side learning rate. Defaults to 1e-1. - tau : float, optional - Controls the algorithm's degree of adaptability. Defaults to 1e-9. - """ super().__init__( fraction_fit=fraction_fit, fraction_evaluate=fraction_evaluate, diff --git a/src/py/flwr/server/strategy/fedadam.py b/src/py/flwr/server/strategy/fedadam.py index ca6229029376..8a47cf0dd8ac 100644 --- a/src/py/flwr/server/strategy/fedadam.py +++ b/src/py/flwr/server/strategy/fedadam.py @@ -38,13 +38,51 @@ from .fedopt import FedOpt +# pylint: disable=line-too-long class FedAdam(FedOpt): """FedAdam - Adaptive Federated Optimization using Adam. - Paper: https://arxiv.org/abs/2003.00295 + Implementation based on https://arxiv.org/abs/2003.00295v5 + + Parameters + ---------- + fraction_fit : float, optional + Fraction of clients used during training. Defaults to 1.0. + fraction_evaluate : float, optional + Fraction of clients used during validation. Defaults to 1.0. + min_fit_clients : int, optional + Minimum number of clients used during training. Defaults to 2. + min_evaluate_clients : int, optional + Minimum number of clients used during validation. Defaults to 2. + min_available_clients : int, optional + Minimum number of total clients in the system. Defaults to 2. + evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]],Optional[Tuple[float, Dict[str, Scalar]]]]] + Optional function used for validation. Defaults to None. + on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure training. Defaults to None. + on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure validation. Defaults to None. + accept_failures : bool, optional + Whether or not accept rounds containing failures. Defaults to True. + initial_parameters : Parameters + Initial global model parameters. + fit_metrics_aggregation_fn : Optional[MetricsAggregationFn] + Metrics aggregation function, optional. + evaluate_metrics_aggregation_fn: Optional[MetricsAggregationFn] + Metrics aggregation function, optional. + eta : float, optional + Server-side learning rate. Defaults to 1e-1. + eta_l : float, optional + Client-side learning rate. Defaults to 1e-1. + beta_1 : float, optional + Momentum parameter. Defaults to 0.9. + beta_2 : float, optional + Second moment parameter. Defaults to 0.99. + tau : float, optional + Controls the algorithm's degree of adaptability. Defaults to 1e-9. """ - # pylint: disable=too-many-arguments,too-many-instance-attributes,too-many-locals, line-too-long + # pylint: disable=too-many-arguments,too-many-instance-attributes,too-many-locals def __init__( self, *, @@ -71,47 +109,6 @@ def __init__( beta_2: float = 0.99, tau: float = 1e-9, ) -> None: - """Federated learning strategy using Adagrad on server-side. - - Implementation based on https://arxiv.org/abs/2003.00295v5 - - Parameters - ---------- - fraction_fit : float, optional - Fraction of clients used during training. Defaults to 1.0. - fraction_evaluate : float, optional - Fraction of clients used during validation. Defaults to 1.0. - min_fit_clients : int, optional - Minimum number of clients used during training. Defaults to 2. - min_evaluate_clients : int, optional - Minimum number of clients used during validation. Defaults to 2. - min_available_clients : int, optional - Minimum number of total clients in the system. Defaults to 2. - evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] - Optional function used for validation. Defaults to None. - on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure training. Defaults to None. - on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure validation. Defaults to None. - accept_failures : bool, optional - Whether or not accept rounds containing failures. Defaults to True. - initial_parameters : Parameters - Initial global model parameters. - fit_metrics_aggregation_fn : Optional[MetricsAggregationFn] - Metrics aggregation function, optional. - evaluate_metrics_aggregation_fn: Optional[MetricsAggregationFn] - Metrics aggregation function, optional. - eta : float, optional - Server-side learning rate. Defaults to 1e-1. - eta_l : float, optional - Client-side learning rate. Defaults to 1e-1. - beta_1 : float, optional - Momentum parameter. Defaults to 0.9. - beta_2 : float, optional - Second moment parameter. Defaults to 0.99. - tau : float, optional - Controls the algorithm's degree of adaptability. Defaults to 1e-9. - """ super().__init__( fraction_fit=fraction_fit, fraction_evaluate=fraction_evaluate, diff --git a/src/py/flwr/server/strategy/fedavg.py b/src/py/flwr/server/strategy/fedavg.py index 86afd31e2bfb..c93c8cb8b83e 100644 --- a/src/py/flwr/server/strategy/fedavg.py +++ b/src/py/flwr/server/strategy/fedavg.py @@ -48,8 +48,43 @@ """ +# pylint: disable=line-too-long class FedAvg(Strategy): - """Configurable FedAvg strategy implementation.""" + """Federated Averaging strategy. + + Implementation based on https://arxiv.org/abs/1602.05629 + + Parameters + ---------- + fraction_fit : float, optional + Fraction of clients used during training. In case `min_fit_clients` + is larger than `fraction_fit * available_clients`, `min_fit_clients` + will still be sampled. Defaults to 1.0. + fraction_evaluate : float, optional + Fraction of clients used during validation. In case `min_evaluate_clients` + is larger than `fraction_evaluate * available_clients`, + `min_evaluate_clients` will still be sampled. Defaults to 1.0. + min_fit_clients : int, optional + Minimum number of clients used during training. Defaults to 2. + min_evaluate_clients : int, optional + Minimum number of clients used during validation. Defaults to 2. + min_available_clients : int, optional + Minimum number of total clients in the system. Defaults to 2. + evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]],Optional[Tuple[float, Dict[str, Scalar]]]]] + Optional function used for validation. Defaults to None. + on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure training. Defaults to None. + on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure validation. Defaults to None. + accept_failures : bool, optional + Whether or not accept rounds containing failures. Defaults to True. + initial_parameters : Parameters, optional + Initial global model parameters. + fit_metrics_aggregation_fn : Optional[MetricsAggregationFn] + Metrics aggregation function, optional. + evaluate_metrics_aggregation_fn : Optional[MetricsAggregationFn] + Metrics aggregation function, optional. + """ # pylint: disable=too-many-arguments,too-many-instance-attributes, line-too-long def __init__( @@ -73,41 +108,6 @@ def __init__( fit_metrics_aggregation_fn: Optional[MetricsAggregationFn] = None, evaluate_metrics_aggregation_fn: Optional[MetricsAggregationFn] = None, ) -> None: - """Federated Averaging strategy. - - Implementation based on https://arxiv.org/abs/1602.05629 - - Parameters - ---------- - fraction_fit : float, optional - Fraction of clients used during training. In case `min_fit_clients` - is larger than `fraction_fit * available_clients`, `min_fit_clients` - will still be sampled. Defaults to 1.0. - fraction_evaluate : float, optional - Fraction of clients used during validation. In case `min_evaluate_clients` - is larger than `fraction_evaluate * available_clients`, - `min_evaluate_clients` will still be sampled. Defaults to 1.0. - min_fit_clients : int, optional - Minimum number of clients used during training. Defaults to 2. - min_evaluate_clients : int, optional - Minimum number of clients used during validation. Defaults to 2. - min_available_clients : int, optional - Minimum number of total clients in the system. Defaults to 2. - evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] - Optional function used for validation. Defaults to None. - on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure training. Defaults to None. - on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure validation. Defaults to None. - accept_failures : bool, optional - Whether or not accept rounds containing failures. Defaults to True. - initial_parameters : Parameters, optional - Initial global model parameters. - fit_metrics_aggregation_fn : Optional[MetricsAggregationFn] - Metrics aggregation function, optional. - evaluate_metrics_aggregation_fn : Optional[MetricsAggregationFn] - Metrics aggregation function, optional. - """ super().__init__() if ( diff --git a/src/py/flwr/server/strategy/fedavg_android.py b/src/py/flwr/server/strategy/fedavg_android.py index 377397678e38..e890f7216020 100644 --- a/src/py/flwr/server/strategy/fedavg_android.py +++ b/src/py/flwr/server/strategy/fedavg_android.py @@ -39,10 +39,38 @@ from .strategy import Strategy +# pylint: disable=line-too-long class FedAvgAndroid(Strategy): - """Configurable FedAvg strategy implementation.""" - - # pylint: disable=too-many-arguments,too-many-instance-attributes, line-too-long + """Federated Averaging strategy. + + Implementation based on https://arxiv.org/abs/1602.05629 + + Parameters + ---------- + fraction_fit : Optional[float] + Fraction of clients used during training. Defaults to 1.0. + fraction_evaluate : Optional[float] + Fraction of clients used during validation. Defaults to 1.0. + min_fit_clients : Optional[int] + Minimum number of clients used during training. Defaults to 2. + min_evaluate_clients : Optional[int] + Minimum number of clients used during validation. Defaults to 2. + min_available_clients : Optional[int] + Minimum number of total clients in the system. Defaults to 2. + evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] + Optional function used for validation. Defaults to None. + on_fit_config_fn : Optional[Callable[[int], Dict[str, Scalar]]] + Function used to configure training. Defaults to None. + on_evaluate_config_fn : Optional[Callable[[int], Dict[str, Scalar]]] + Function used to configure validation. Defaults to None. + accept_failures : Optional[bool] + Whether or not accept rounds + containing failures. Defaults to True. + initial_parameters : Optional[Parameters] + Initial global model parameters. + """ + + # pylint: disable=too-many-arguments,too-many-instance-attributes def __init__( self, *, @@ -62,34 +90,6 @@ def __init__( accept_failures: bool = True, initial_parameters: Optional[Parameters] = None, ) -> None: - """Federated Averaging strategy. - - Implementation based on https://arxiv.org/abs/1602.05629 - - Parameters - ---------- - fraction_fit : Optional[float] - Fraction of clients used during training. Defaults to 0.1. - fraction_evaluate : Optional[float] - Fraction of clients used during validation. Defaults to 0.1. - min_fit_clients : Optional[int] - Minimum number of clients used during training. Defaults to 2. - min_evaluate_clients : Optional[int] - Minimum number of clients used during validation. Defaults to 2. - min_available_clients : Optional[int] - Minimum number of total clients in the system. Defaults to 2. - evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] - Optional function used for validation. Defaults to None. - on_fit_config_fn : Optional[Callable[[int], Dict[str, Scalar]]] - Function used to configure training. Defaults to None. - on_evaluate_config_fn : Optional[Callable[[int], Dict[str, Scalar]]] - Function used to configure validation. Defaults to None. - accept_failures : Optional[bool] - Whether or not accept rounds - containing failures. Defaults to True. - initial_parameters : Optional[Parameters] - Initial global model parameters. - """ super().__init__() self.min_fit_clients = min_fit_clients self.min_evaluate_clients = min_evaluate_clients diff --git a/src/py/flwr/server/strategy/fedavgm.py b/src/py/flwr/server/strategy/fedavgm.py index 37cccd01479c..fb9261abe89d 100644 --- a/src/py/flwr/server/strategy/fedavgm.py +++ b/src/py/flwr/server/strategy/fedavgm.py @@ -38,8 +38,40 @@ from .fedavg import FedAvg +# pylint: disable=line-too-long class FedAvgM(FedAvg): - """Configurable FedAvg with Momentum strategy implementation.""" + """Federated Averaging with Momentum strategy. + + Implementation based on https://arxiv.org/abs/1909.06335 + + Parameters + ---------- + fraction_fit : float, optional + Fraction of clients used during training. Defaults to 1.0. + fraction_evaluate : float, optional + Fraction of clients used during validation. Defaults to 1.0. + min_fit_clients : int, optional + Minimum number of clients used during training. Defaults to 2. + min_evaluate_clients : int, optional + Minimum number of clients used during validation. Defaults to 2. + min_available_clients : int, optional + Minimum number of total clients in the system. Defaults to 2. + evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] + Optional function used for validation. Defaults to None. + on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure training. Defaults to None. + on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure validation. Defaults to None. + accept_failures : bool, optional + Whether or not accept rounds containing failures. Defaults to True. + initial_parameters : Parameters, optional + Initial global model parameters. + server_learning_rate: float + Server-side learning rate used in server-side optimization. + Defaults to 1.0. + server_momentum: float + Server-side momentum factor used for FedAvgM. Defaults to 0.0. + """ # pylint: disable=too-many-arguments,too-many-instance-attributes, line-too-long def __init__( @@ -65,38 +97,6 @@ def __init__( server_learning_rate: float = 1.0, server_momentum: float = 0.0, ) -> None: - """Federated Averaging with Momentum strategy. - - Implementation based on https://arxiv.org/pdf/1909.06335.pdf - - Parameters - ---------- - fraction_fit : float, optional - Fraction of clients used during training. Defaults to 0.1. - fraction_evaluate : float, optional - Fraction of clients used during validation. Defaults to 0.1. - min_fit_clients : int, optional - Minimum number of clients used during training. Defaults to 2. - min_evaluate_clients : int, optional - Minimum number of clients used during validation. Defaults to 2. - min_available_clients : int, optional - Minimum number of total clients in the system. Defaults to 2. - evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] - Optional function used for validation. Defaults to None. - on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure training. Defaults to None. - on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure validation. Defaults to None. - accept_failures : bool, optional - Whether or not accept rounds containing failures. Defaults to True. - initial_parameters : Parameters, optional - Initial global model parameters. - server_learning_rate: float - Server-side learning rate used in server-side optimization. - Defaults to 1.0. - server_momentum: float - Server-side momentum factor used for FedAvgM. Defaults to 0.0. - """ super().__init__( fraction_fit=fraction_fit, fraction_evaluate=fraction_evaluate, diff --git a/src/py/flwr/server/strategy/fedopt.py b/src/py/flwr/server/strategy/fedopt.py index 78dd92061c07..be5f260d96fa 100644 --- a/src/py/flwr/server/strategy/fedopt.py +++ b/src/py/flwr/server/strategy/fedopt.py @@ -31,8 +31,49 @@ from .fedavg import FedAvg +# pylint: disable=line-too-long class FedOpt(FedAvg): - """Configurable FedAdagrad strategy implementation.""" + """Federated Optim strategy. + + Implementation based on https://arxiv.org/abs/2003.00295v5 + + Parameters + ---------- + fraction_fit : float, optional + Fraction of clients used during training. Defaults to 1.0. + fraction_evaluate : float, optional + Fraction of clients used during validation. Defaults to 1.0. + min_fit_clients : int, optional + Minimum number of clients used during training. Defaults to 2. + min_evaluate_clients : int, optional + Minimum number of clients used during validation. Defaults to 2. + min_available_clients : int, optional + Minimum number of total clients in the system. Defaults to 2. + evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] + Optional function used for validation. Defaults to None. + on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure training. Defaults to None. + on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure validation. Defaults to None. + accept_failures : bool, optional + Whether or not accept rounds containing failures. Defaults to True. + initial_parameters : Parameters + Initial global model parameters. + fit_metrics_aggregation_fn : Optional[MetricsAggregationFn] + Metrics aggregation function, optional. + evaluate_metrics_aggregation_fn: Optional[MetricsAggregationFn] + Metrics aggregation function, optional. + eta : float, optional + Server-side learning rate. Defaults to 1e-1. + eta_l : float, optional + Client-side learning rate. Defaults to 1e-1. + beta_1 : float, optional + Momentum parameter. Defaults to 0.0. + beta_2 : float, optional + Second moment parameter. Defaults to 0.0. + tau : float, optional + Controls the algorithm's degree of adaptability. Defaults to 1e-9. + """ # pylint: disable=too-many-arguments,too-many-instance-attributes,too-many-locals, line-too-long def __init__( @@ -61,47 +102,6 @@ def __init__( beta_2: float = 0.0, tau: float = 1e-9, ) -> None: - """Federated Optim strategy interface. - - Implementation based on https://arxiv.org/abs/2003.00295v5 - - Parameters - ---------- - fraction_fit : float, optional - Fraction of clients used during training. Defaults to 1.0. - fraction_evaluate : float, optional - Fraction of clients used during validation. Defaults to 1.0. - min_fit_clients : int, optional - Minimum number of clients used during training. Defaults to 2. - min_evaluate_clients : int, optional - Minimum number of clients used during validation. Defaults to 2. - min_available_clients : int, optional - Minimum number of total clients in the system. Defaults to 2. - evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] - Optional function used for validation. Defaults to None. - on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure training. Defaults to None. - on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure validation. Defaults to None. - accept_failures : bool, optional - Whether or not accept rounds containing failures. Defaults to True. - initial_parameters : Parameters - Initial global model parameters. - fit_metrics_aggregation_fn : Optional[MetricsAggregationFn] - Metrics aggregation function, optional. - evaluate_metrics_aggregation_fn: Optional[MetricsAggregationFn] - Metrics aggregation function, optional. - eta : float, optional - Server-side learning rate. Defaults to 1e-1. - eta_l : float, optional - Client-side learning rate. Defaults to 1e-1. - beta_1 : float, optional - Momentum parameter. Defaults to 0.0. - beta_2 : float, optional - Second moment parameter. Defaults to 0.0. - tau : float, optional - Controls the algorithm's degree of adaptability. Defaults to 1e-9. - """ super().__init__( fraction_fit=fraction_fit, fraction_evaluate=fraction_evaluate, diff --git a/src/py/flwr/server/strategy/fedprox.py b/src/py/flwr/server/strategy/fedprox.py index b2e3db7c31f4..d20f578b193d 100644 --- a/src/py/flwr/server/strategy/fedprox.py +++ b/src/py/flwr/server/strategy/fedprox.py @@ -27,10 +27,82 @@ from .fedavg import FedAvg +# pylint: disable=line-too-long class FedProx(FedAvg): - """Configurable FedProx strategy implementation.""" - - # pylint: disable=too-many-arguments,too-many-instance-attributes, line-too-long + r"""Federated Optimization strategy. + + Implementation based on https://arxiv.org/abs/1812.06127 + + The strategy in itself will not be different than FedAvg, the client needs to + be adjusted. + A proximal term needs to be added to the loss function during the training: + + .. math:: + \\frac{\\mu}{2} || w - w^t ||^2 + + Where $w^t$ are the global parameters and $w$ are the local weights the function + will be optimized with. + + In PyTorch, for example, the loss would go from: + + .. code:: python + + loss = criterion(net(inputs), labels) + + To: + + .. code:: python + + for local_weights, global_weights in zip(net.parameters(), global_params): + proximal_term += (local_weights - global_weights).norm(2) + loss = criterion(net(inputs), labels) + (config["proximal_mu"] / 2) * + proximal_term + + With `global_params` being a copy of the parameters before the training takes + place. + + .. code:: python + + global_params = copy.deepcopy(net).parameters() + + Parameters + ---------- + fraction_fit : float, optional + Fraction of clients used during training. In case `min_fit_clients` + is larger than `fraction_fit * available_clients`, `min_fit_clients` + will still be sampled. Defaults to 1.0. + fraction_evaluate : float, optional + Fraction of clients used during validation. In case `min_evaluate_clients` + is larger than `fraction_evaluate * available_clients`, + `min_evaluate_clients` will still be sampled. Defaults to 1.0. + min_fit_clients : int, optional + Minimum number of clients used during training. Defaults to 2. + min_evaluate_clients : int, optional + Minimum number of clients used during validation. Defaults to 2. + min_available_clients : int, optional + Minimum number of total clients in the system. Defaults to 2. + evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] + Optional function used for validation. Defaults to None. + on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure training. Defaults to None. + on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure validation. Defaults to None. + accept_failures : bool, optional + Whether or not accept rounds containing failures. Defaults to True. + initial_parameters : Parameters, optional + Initial global model parameters. + fit_metrics_aggregation_fn : Optional[MetricsAggregationFn] + Metrics aggregation function, optional. + evaluate_metrics_aggregation_fn : Optional[MetricsAggregationFn] + Metrics aggregation function, optional. + proximal_mu : float + The weight of the proximal term used in the optimization. 0.0 makes + this strategy equivalent to FedAvg, and the higher the coefficient, the more + regularization will be used (that is, the client parameters will need to be + closer to the server parameters during training). + """ + + # pylint: disable=too-many-arguments,too-many-instance-attributes def __init__( self, *, @@ -53,78 +125,6 @@ def __init__( evaluate_metrics_aggregation_fn: Optional[MetricsAggregationFn] = None, proximal_mu: float, ) -> None: - r"""Federated Optimization strategy. - - Implementation based on https://arxiv.org/abs/1812.06127 - - The strategy in itself will not be different than FedAvg, the client needs to - be adjusted. - A proximal term needs to be added to the loss function during the training: - - .. math:: - \\frac{\\mu}{2} || w - w^t ||^2 - - Where $w^t$ are the global parameters and $w$ are the local weights the function - will be optimized with. - - In PyTorch, for example, the loss would go from: - - .. code:: python - - loss = criterion(net(inputs), labels) - - To: - - .. code:: python - - for local_weights, global_weights in zip(net.parameters(), global_params): - proximal_term += (local_weights - global_weights).norm(2) - loss = criterion(net(inputs), labels) + (config["proximal_mu"] / 2) * - proximal_term - - With `global_params` being a copy of the parameters before the training takes - place. - - .. code:: python - - global_params = copy.deepcopy(net).parameters() - - Parameters - ---------- - fraction_fit : float, optional - Fraction of clients used during training. In case `min_fit_clients` - is larger than `fraction_fit * available_clients`, `min_fit_clients` - will still be sampled. Defaults to 1.0. - fraction_evaluate : float, optional - Fraction of clients used during validation. In case `min_evaluate_clients` - is larger than `fraction_evaluate * available_clients`, - `min_evaluate_clients` will still be sampled. Defaults to 1.0. - min_fit_clients : int, optional - Minimum number of clients used during training. Defaults to 2. - min_evaluate_clients : int, optional - Minimum number of clients used during validation. Defaults to 2. - min_available_clients : int, optional - Minimum number of total clients in the system. Defaults to 2. - evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] - Optional function used for validation. Defaults to None. - on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure training. Defaults to None. - on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure validation. Defaults to None. - accept_failures : bool, optional - Whether or not accept rounds containing failures. Defaults to True. - initial_parameters : Parameters, optional - Initial global model parameters. - fit_metrics_aggregation_fn : Optional[MetricsAggregationFn] - Metrics aggregation function, optional. - evaluate_metrics_aggregation_fn : Optional[MetricsAggregationFn] - Metrics aggregation function, optional. - proximal_mu : float - The weight of the proximal term used in the optimization. 0.0 makes - this strategy equivalent to FedAvg, and the higher the coefficient, the more - regularization will be used (that is, the client parameters will need to be - closer to the server parameters during training). - """ super().__init__( fraction_fit=fraction_fit, fraction_evaluate=fraction_evaluate, diff --git a/src/py/flwr/server/strategy/fedtrimmedavg.py b/src/py/flwr/server/strategy/fedtrimmedavg.py index 8ec3ce89e98e..96b0d35e7a61 100644 --- a/src/py/flwr/server/strategy/fedtrimmedavg.py +++ b/src/py/flwr/server/strategy/fedtrimmedavg.py @@ -35,10 +35,36 @@ from .fedavg import FedAvg +# pylint: disable=line-too-long class FedTrimmedAvg(FedAvg): """Federated Averaging with Trimmed Mean [Dong Yin, et al., 2021]. - Paper: https://arxiv.org/abs/1803.01498 + Implemented based on: https://arxiv.org/abs/1803.01498 + + Parameters + ---------- + fraction_fit : float, optional + Fraction of clients used during training. Defaults to 1.0. + fraction_evaluate : float, optional + Fraction of clients used during validation. Defaults to 1.0. + min_fit_clients : int, optional + Minimum number of clients used during training. Defaults to 2. + min_evaluate_clients : int, optional + Minimum number of clients used during validation. Defaults to 2. + min_available_clients : int, optional + Minimum number of total clients in the system. Defaults to 2. + evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] + Optional function used for validation. Defaults to None. + on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure training. Defaults to None. + on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure validation. Defaults to None. + accept_failures : bool, optional + Whether or not accept rounds containing failures. Defaults to True. + initial_parameters : Parameters, optional + Initial global model parameters. + beta : float, optional + Fraction to cut off of both tails of the distribution. Defaults to 0.2. """ # pylint: disable=too-many-arguments,too-many-instance-attributes, line-too-long @@ -64,33 +90,6 @@ def __init__( evaluate_metrics_aggregation_fn: Optional[MetricsAggregationFn] = None, beta: float = 0.2, ) -> None: - """Federated Averaging with Trimmed Mean [Dong Yin, et al., 2021]. - - Parameters - ---------- - fraction_fit : float, optional - Fraction of clients used during training. Defaults to 0.1. - fraction_evaluate : float, optional - Fraction of clients used during validation. Defaults to 0.1. - min_fit_clients : int, optional - Minimum number of clients used during training. Defaults to 2. - min_evaluate_clients : int, optional - Minimum number of clients used during validation. Defaults to 2. - min_available_clients : int, optional - Minimum number of total clients in the system. Defaults to 2. - evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] - Optional function used for validation. Defaults to None. - on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure training. Defaults to None. - on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure validation. Defaults to None. - accept_failures : bool, optional - Whether or not accept rounds containing failures. Defaults to True. - initial_parameters : Parameters, optional - Initial global model parameters. - beta : float, optional - Fraction to cut off of both tails of the distribution. Defaults to 0.2. - """ super().__init__( fraction_fit=fraction_fit, fraction_evaluate=fraction_evaluate, diff --git a/src/py/flwr/server/strategy/fedyogi.py b/src/py/flwr/server/strategy/fedyogi.py index 245090534c7c..7c77aab7ae73 100644 --- a/src/py/flwr/server/strategy/fedyogi.py +++ b/src/py/flwr/server/strategy/fedyogi.py @@ -36,12 +36,50 @@ from .fedopt import FedOpt +# pylint: disable=line-too-long class FedYogi(FedOpt): """FedYogi [Reddi et al., 2020] strategy. - Adaptive Federated Optimization using Yogi. - - Paper: https://arxiv.org/abs/2003.00295 + Implementation based on https://arxiv.org/abs/2003.00295v5 + + Parameters + ---------- + fraction_fit : float, optional + Fraction of clients used during training. Defaults to 1.0. + fraction_evaluate : float, optional + Fraction of clients used during validation. Defaults to 1.0. + min_fit_clients : int, optional + Minimum number of clients used during training. Defaults to 2. + min_evaluate_clients : int, optional + Minimum number of clients used during validation. Defaults to 2. + min_available_clients : int, optional + Minimum number of total clients in the system. Defaults to 2. + evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[ + Tuple[float, Dict[str, Scalar]]]]] + Optional function used for validation. Defaults to None. + on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure training. Defaults to None. + on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure validation. Defaults to None. + accept_failures : bool, optional + Whether or not accept rounds containing failures. Defaults to True. + initial_parameters : Parameters + Initial global model parameters. + fit_metrics_aggregation_fn : Optional[MetricsAggregationFn] + Metrics aggregation function, optional. + evaluate_metrics_aggregation_fn: Optional[MetricsAggregationFn] + Metrics aggregation function, optional. + eta : float, optional + Server-side learning rate. Defaults to 1e-2. + eta_l : float, optional + Client-side learning rate. Defaults to 0.0316. + beta_1 : float, optional + Momentum parameter. Defaults to 0.9. + beta_2 : float, optional + Second moment parameter. Defaults to 0.99. + tau : float, optional + Controls the algorithm's degree of adaptability. + Defaults to 1e-3. """ # pylint: disable=too-many-arguments,too-many-instance-attributes,too-many-locals, line-too-long @@ -71,48 +109,6 @@ def __init__( beta_2: float = 0.99, tau: float = 1e-3, ) -> None: - """Federated learning strategy using Yogi on server-side. - - Implementation based on https://arxiv.org/abs/2003.00295v5 - - Parameters - ---------- - fraction_fit : float, optional - Fraction of clients used during training. Defaults to 1.0. - fraction_evaluate : float, optional - Fraction of clients used during validation. Defaults to 1.0. - min_fit_clients : int, optional - Minimum number of clients used during training. Defaults to 2. - min_evaluate_clients : int, optional - Minimum number of clients used during validation. Defaults to 2. - min_available_clients : int, optional - Minimum number of total clients in the system. Defaults to 2. - evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] - Optional function used for validation. Defaults to None. - on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure training. Defaults to None. - on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure validation. Defaults to None. - accept_failures : bool, optional - Whether or not accept rounds containing failures. Defaults to True. - initial_parameters : Parameters - Initial global model parameters. - fit_metrics_aggregation_fn : Optional[MetricsAggregationFn] - Metrics aggregation function, optional. - evaluate_metrics_aggregation_fn: Optional[MetricsAggregationFn] - Metrics aggregation function, optional. - eta : float, optional - Server-side learning rate. Defaults to 1e-1. - eta_l : float, optional - Client-side learning rate. Defaults to 1e-1. - beta_1 : float, optional - Momentum parameter. Defaults to 0.9. - beta_2 : float, optional - Second moment parameter. Defaults to 0.99. - tau : float, optional - Controls the algorithm's degree of adaptability. - Defaults to 1e-9. - """ super().__init__( fraction_fit=fraction_fit, fraction_evaluate=fraction_evaluate, diff --git a/src/py/flwr/server/strategy/krum.py b/src/py/flwr/server/strategy/krum.py index d7f15531902f..16eb5212940e 100644 --- a/src/py/flwr/server/strategy/krum.py +++ b/src/py/flwr/server/strategy/krum.py @@ -39,10 +39,42 @@ from .fedavg import FedAvg +# pylint: disable=line-too-long class Krum(FedAvg): - """Configurable Krum strategy implementation.""" + """Krum [Blanchard et al., 2017] strategy. - # pylint: disable=too-many-arguments,too-many-instance-attributes, line-too-long + Implementation based on https://arxiv.org/abs/1703.02757 + + Parameters + ---------- + fraction_fit : float, optional + Fraction of clients used during training. Defaults to 1.0. + fraction_evaluate : float, optional + Fraction of clients used during validation. Defaults to 1.0. + min_fit_clients : int, optional + Minimum number of clients used during training. Defaults to 2. + min_evaluate_clients : int, optional + Minimum number of clients used during validation. Defaults to 2. + min_available_clients : int, optional + Minimum number of total clients in the system. Defaults to 2. + num_malicious_clients : int, optional + Number of malicious clients in the system. Defaults to 0. + num_clients_to_keep : int, optional + Number of clients to keep before averaging (MultiKrum). Defaults to 0, in + that case classical Krum is applied. + evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] + Optional function used for validation. Defaults to None. + on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure training. Defaults to None. + on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional + Function used to configure validation. Defaults to None. + accept_failures : bool, optional + Whether or not accept rounds containing failures. Defaults to True. + initial_parameters : Parameters, optional + Initial global model parameters. + """ + + # pylint: disable=too-many-arguments,too-many-instance-attributes def __init__( self, *, @@ -66,36 +98,6 @@ def __init__( fit_metrics_aggregation_fn: Optional[MetricsAggregationFn] = None, evaluate_metrics_aggregation_fn: Optional[MetricsAggregationFn] = None, ) -> None: - """Krum strategy. - - Parameters - ---------- - fraction_fit : float, optional - Fraction of clients used during training. Defaults to 0.1. - fraction_evaluate : float, optional - Fraction of clients used during validation. Defaults to 0.1. - min_fit_clients : int, optional - Minimum number of clients used during training. Defaults to 2. - min_evaluate_clients : int, optional - Minimum number of clients used during validation. Defaults to 2. - min_available_clients : int, optional - Minimum number of total clients in the system. Defaults to 2. - num_malicious_clients : int, optional - Number of malicious clients in the system. Defaults to 0. - num_clients_to_keep : int, optional - Number of clients to keep before averaging (MultiKrum). Defaults to 0, in - that case classical Krum is applied. - evaluate_fn : Optional[Callable[[int, NDArrays, Dict[str, Scalar]], Optional[Tuple[float, Dict[str, Scalar]]]]] - Optional function used for validation. Defaults to None. - on_fit_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure training. Defaults to None. - on_evaluate_config_fn : Callable[[int], Dict[str, Scalar]], optional - Function used to configure validation. Defaults to None. - accept_failures : bool, optional - Whether or not accept rounds containing failures. Defaults to True. - initial_parameters : Parameters, optional - Initial global model parameters. - """ super().__init__( fraction_fit=fraction_fit, fraction_evaluate=fraction_evaluate,