fl4health.clients.clipping_client module

class NumpyClippingClient(data_path, metrics, device, loss_meter_type=LossMeterType.AVERAGE, checkpoint_and_state_module=None, reporters=None, progress_bar=False, client_name=None)[source]

Bases: BasicClient

__init__(data_path, metrics, device, loss_meter_type=LossMeterType.AVERAGE, checkpoint_and_state_module=None, reporters=None, progress_bar=False, client_name=None)[source]

Client that clips updates being sent to the server where noise is added. Used to obtain Client Level Differential Privacy in FL setting.

Parameters:
  • data_path (Path) – path to the data to be used to load the data for client-side training

  • metrics (Sequence[Metric]) – Metrics to be computed based on the labels and predictions of the client model

  • device (torch.device) – Device indicator for where to send the model, batches, labels etc. Often “cpu” or “cuda”

  • loss_meter_type (LossMeterType, optional) – Type of meter used to track and compute the losses over each batch. Defaults to LossMeterType.AVERAGE.

  • checkpoint_and_state_module (ClientCheckpointAndStateModule | None, optional) – A module meant to handle both checkpointing and state saving. The module, and its underlying model and state checkpointing components will determine when and how to do checkpointing during client-side training. No checkpointing (state or model) is done if not provided. Defaults to None.

  • reporters (Sequence[BaseReporter] | None, optional) – A sequence of FL4Health reporters which the client should send data to. Defaults to None.

  • progress_bar (bool, optional) – Whether or not to display a progress bar during client training and validation. Uses tqdm. Defaults to False

  • client_name (str | None, optional) – An optional client name that uniquely identifies a client. If not passed, a hash is randomly generated. Client state will use this as part of its state file name. Defaults to None.

calculate_parameters_norm(parameters)[source]

Given a set of parameters, compute the l2-norm of the parameters. This is a matrix norm: squared sum of all of the weights

Parameters:

parameters (NDArrays) – Tensor to measure with the norm

Returns:

Squared sum of all values in the NDArrays

Return type:

float

clip_parameters(parameters)[source]

Performs “flat clipping” on the parameters according to

\[\text{parameters} \cdot \min \left(1, \frac{C}{\Vert \text{parameters} \Vert_2} \right)\]
Parameters:

parameters (NDArrays) – Parameters to clip

Returns:

Clipped parameters and the associated clipping bit indicating whether the norm was below self.clipping_bound. If self.adaptive_clipping is false, this bit is always 0.0

Return type:

tuple[NDArrays, float]

compute_weight_update_and_clip(parameters)[source]

Compute the weight delta (i.e. new weights - old weights) and clip according to self.clipping_bound

Parameters:

parameters (NDArrays) – Updated parameters to compute the delta from and clip thereafter

Returns:

Clipped weighted updates (weight deltas) and the associated clipping bit

Return type:

tuple[NDArrays, float]

get_parameter_exchanger(config)[source]

Returns Full Parameter Exchangers. Subclasses that require custom Parameter Exchangers can override this.

Parameters:

config (Config) – The config from server.

Returns:

Used to exchange parameters between server and client.

Return type:

ParameterExchanger

get_parameters(config)[source]

This function performs clipping through compute_weight_update_and_clip and stores the clipping bit as the last entry in the NDArrays

Return type:

List[ndarray[Any, dtype[Any]]]

set_parameters(parameters, config, fitting_round)[source]

This function assumes that the parameters being passed contain model parameters followed by the last entry of the list being the new clipping bound. They are unpacked for the clients to use in training. If it is called in the first fitting round, we assume the full model is being initialized and use the FullParameterExchanger() to set all model weights.

Parameters:
  • parameters (NDArrays) – Parameters have information about model state to be added to the relevant client model and also the clipping bound.

  • config (Config) – The config is sent by the FL server to allow for customization in the function if desired.

  • fitting_round (bool) – Boolean that indicates whether the current federated learning round is a fitting round or an evaluation round. This is used to help determine which parameter exchange should be used for pulling parameters. A full parameter exchanger is used if the current federated learning round is the very first fitting round.

Return type:

None

setup_client(config)[source]

Set dataloaders, optimizers, parameter exchangers and other attributes derived from these. Then set initialized attribute to True.

Parameters:

config (Config) – The config from the server.

Return type:

None