fl4health.utils.privacy_utilities module¶
- convert_model_to_opacus_model(model, grad_sample_mode='hooks', *args, **kwargs)[source]¶
This function converts a standard pytorch model to an Opacus GradSampleModule, which Opacus uses to perform efficient DP-SGD operations. It uses the wrap_model functionality and mimics its defaults.
- Parameters:
model (nn.Module) – Pytorch model to be converted to an Opacus GradSampleModule
grad_sample_mode (str, optional) – This determines how Opacus performs the conversion under the hood. The standard mechanism is indicated by “hooks” but other approaches may be necessary depending on how the pytorch module is defined. Defaults to “hooks”.
- Returns:
The Opacus wrapped GradSampleModule
- Return type:
GradSampleModule
- map_model_to_opacus_model(model, grad_sample_mode='hooks', *args, **kwargs)[source]¶
Performs an validation and modifications necessary to make the provided pytorch model “Opacus Compliant” via the call to privacy_validate_and_fix_modules. The resulting model is then converted to an Opacus GradSampleModule via convert_model_to_opacus_model.
- Parameters:
model (nn.Module) – Pytorch model to be converted to an Opacus compliant GradSampleModule
grad_sample_mode (str, optional) – This determines how Opacus performs the conversion under the hood. The standard mechanism is indicated by “hooks” but other approaches may be necessary depending on how the pytorch module is defined. Defaults to “hooks”.
- Returns:
The Opacus-compliant, wrapped GradSampleModule
- Return type:
GradSampleModule
- privacy_validate_and_fix_modules(model)[source]¶
This function runs Opacus model validation to ensure that the provided models layers are compatible with the privacy mechanisms in Opacus. The function attempts to use Opacus to replace any incompatible layers if possible. For example BatchNormalization layers are not “DP Compliant” and will be replaced by compliant layers such as GroupNormalization with this function. Note that this uses the default “fix” functionality in Opacus. For more custom options, defining your own setup_opacus_objects function is required.
- Parameters:
model (nn.Module) – The model to be validated and potentially modified to be Opacus compliant.
- Returns:
- Returns a (possibly) modified pytorch model and a boolean indicating whether a
reinitialization of any optimizers associated with the model will be required. Reinitialization of the optimizer parameters is required, for example, when the model layers are modified, yielding a mismatch in the optimizer parameters and the new model parameters.
- Return type: