Skip to content

declearn.main.config.PrivacyConfig

Dataclass wrapping parameters to set up local differential privacy.

The parameters wrapped by this class specify the DP-SGD algorithm [1], providing with a budget, an accountant method, a sensitivity clipping threshold, and RNG-related parameters for the noise-addition module.

Accountants supported by Opacus 1.2.0 include:

  • rdp : Renyi-DP accountant, see [1]
  • gdp : Gaussian-DP, see [2]
  • prv : Privacy loss Random Variables privacy accountant, see [3]

Note: for more details, refer to the Opacus source code and the doctrings of each accountant. See https://github.com/pytorch/opacus/tree/main/opacus/accountants

Attributes:

Name Type Description
budget float, float

Target total privacy budget per client, expressed in terms of (epsilon-delta)-DP over the full training schedule.

accountant str

Accounting mechanism used to estimate epsilon by Opacus.

sclip_norm float

Clipping threshold of sample-wise gradients' euclidean norm. This parameter binds the sensitivity of sample-wise gradients.

use_csprng bool

Whether to use cryptographically-secure pseudo-random numbers (CSPRNG) rather than the default numpy generator. This is significantly slower than using the default numpy RNG.

seed int or None

Optional seed to the noise-addition module's RNG. Unused if safe_mode=True.

References

  • [1] Abadi et al, 2016. Deep Learning with Differential Privacy. https://arxiv.org/abs/1607.00133
  • [2] Dong et al, 2019. Gaussian Differential Privacy. https://arxiv.org/abs/1905.02383
  • [3] Gopi et al, 2021. Numerical Composition of Differential Privacy. https://arxiv.org/abs/2106.02848
Source code in declearn/main/config/_dataclasses.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
@dataclasses.dataclass
class PrivacyConfig:
    """Dataclass wrapping parameters to set up local differential privacy.

    The parameters wrapped by this class specify the DP-SGD algorithm [1],
    providing with a budget, an accountant method, a sensitivity clipping
    threshold, and RNG-related parameters for the noise-addition module.

    Accountants supported by Opacus 1.2.0 include:

    * rdp : Renyi-DP accountant, see [1]
    * gdp : Gaussian-DP, see [2]
    * prv : Privacy loss Random Variables privacy accountant, see [3]

    Note: for more details, refer to the Opacus source code and the
    doctrings of each accountant. See
    https://github.com/pytorch/opacus/tree/main/opacus/accountants

    Attributes
    ----------
    budget: (float, float)
        Target total privacy budget per client, expressed in terms of
        (epsilon-delta)-DP over the full training schedule.
    accountant: str
        Accounting mechanism used to estimate epsilon by Opacus.
    sclip_norm: float
        Clipping threshold of sample-wise gradients' euclidean norm.
        This parameter binds the sensitivity of sample-wise gradients.
    use_csprng: bool
        Whether to use cryptographically-secure pseudo-random numbers
        (CSPRNG) rather than the default numpy generator.
        This is significantly slower than using the default numpy RNG.
    seed: int or None
        Optional seed to the noise-addition module's RNG.
        Unused if `safe_mode=True`.

    References
    ----------
    - [1]
        Abadi et al, 2016.
        Deep Learning with Differential Privacy.
        https://arxiv.org/abs/1607.00133
    - [2]
        Dong et al, 2019.
        Gaussian Differential Privacy.
        https://arxiv.org/abs/1905.02383
    - [3]
        Gopi et al, 2021.
        Numerical Composition of Differential Privacy.
        https://arxiv.org/abs/2106.02848
    """

    budget: Tuple[float, float]
    sclip_norm: float
    accountant: str = "rdp"
    use_csprng: bool = False
    seed: Optional[int] = None

    def __post_init__(self):
        """Type- and value-check (some of) the wrapped parameters."""
        # Verify budget validity.
        if isinstance(self.budget, list):
            self.budget = tuple(self.budget)
        if not (
            isinstance(self.budget, tuple)
            and len(self.budget) == 2
            and isinstance(self.budget[0], (float, int))
            and self.budget[0] > 0
            and isinstance(self.budget[1], (float, int))
            and self.budget[1] >= 0
        ):
            raise TypeError("'budget' should be a tuple of positive floats.")
        # Verify max_norm validity.
        if not (
            isinstance(self.sclip_norm, (float, int)) and self.sclip_norm > 0
        ):
            raise TypeError("'sclip_norm' should be a positive float.")
        # Verify accountant validity.
        accountants = ("rdp", "gdp", "prv")
        if self.accountant not in accountants:
            raise TypeError(f"'accountant' should be one of {accountants}")

__post_init__()

Type- and value-check (some of) the wrapped parameters.

Source code in declearn/main/config/_dataclasses.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
def __post_init__(self):
    """Type- and value-check (some of) the wrapped parameters."""
    # Verify budget validity.
    if isinstance(self.budget, list):
        self.budget = tuple(self.budget)
    if not (
        isinstance(self.budget, tuple)
        and len(self.budget) == 2
        and isinstance(self.budget[0], (float, int))
        and self.budget[0] > 0
        and isinstance(self.budget[1], (float, int))
        and self.budget[1] >= 0
    ):
        raise TypeError("'budget' should be a tuple of positive floats.")
    # Verify max_norm validity.
    if not (
        isinstance(self.sclip_norm, (float, int)) and self.sclip_norm > 0
    ):
        raise TypeError("'sclip_norm' should be a positive float.")
    # Verify accountant validity.
    accountants = ("rdp", "gdp", "prv")
    if self.accountant not in accountants:
        raise TypeError(f"'accountant' should be one of {accountants}")