jax_privacy.clipping.clipped_fun

Contents

jax_privacy.clipping.clipped_fun#

jax_privacy.clipping.clipped_fun(fun, has_aux=False, *, batch_argnums=0, keep_batch_dim=True, l2_clip_norm=1.0, rescale_to_unit_norm=False, normalize_by=1.0, return_norms=False, microbatch_size=None, nan_safe=True, dtype=None, prng_argnum=None, spmd_axis_name=None, grid_scale=None, slack=None)[source]#

Transforms a function to clip its output and sum across a batch.

Example Usage:
>>> data = jnp.array([0, 1, 2, 3, 4, 5])
>>> clipped_mean = clipped_fun(jnp.mean, l2_clip_norm=1.0)
>>> clipped_mean(data)
Array(5., dtype=float32)
>>> is_padding_example = jnp.array([False, False, False, False, True, True])
>>> clipped_mean(data, is_padding_example=is_padding_example)
Array(3., dtype=float32)
Formal Guarantees:
For the first function output:

The L2 sensitivity of the returned callable is a complex function of the input parameters (l2_clip_norm, rescale_to_unit_norm, normalize_by, grid_scale, per-layer clipping settings, etc.) and may change as new features are added. Rather than reasoning about the sensitivity from the parameters, callers should query the returned callable’s .sensitivity() method (or equivalently its .l2_norm_bound attribute for add-or-remove-one / zero-out DP) directly. Under replace-one DP, .sensitivity() doubles the add/remove bound.

Extra auxiliary outputs (aux, norms) are per-example.

This function guarantees that per-example outputs only depend on the data for the same example. This allows maximum flexibility for the caller to aggregate these as desired (possibly with a DP mean, median, quantile, or histogram mechanism).

Parameters:
  • fun (Callable) – The function to be clipped.

  • has_aux (bool) – If True, fun is expected to return a tuple (value, aux). Only the value will be clipped + aggregated, aux will be returned on a per-example basis. Exercise caution when using this as the sensitivity guarantees of the returned Callable are only provided w.r.t. value.

  • batch_argnums (int | Sequence[int]) – Specifies which argument(s) of fun contain the batch dimension. All arguments specified here must have the same size along the 0th axis.

  • keep_batch_dim (bool) – If True, batch inputs will be passed to fun with a leading batch axis of size 1. If False, this size 1 axis will be dropped (reducing the rank of the batch args by 1 before passing to fun).

  • l2_clip_norm (Union[float, Array, ndarray, bool, number, bool, int, complex, Iterable[optax.ArrayTree], Mapping[Any, optax.ArrayTree]]) – The maximum L2 norm allowed. Can be a single float or a PyTree; depending on whether Global / “Per-Layer” clipping is to be done.

  • rescale_to_unit_norm (bool) – If True, the output PyTree’s norm is rescaled by 1.0 / clip_norm after potential clipping. If False, the output PyTree has norm at most clip_norm.

  • normalize_by (float) – Divide the clipped output by this value before returning.

  • return_norms (bool) – If True, the returned Callable will return the l2_norms of the per-example values before clipping. These values should be handled with care, see the formal guarantees above.

  • microbatch_size (int | None) – If set, the batch is split up into microbatches of this size. These microbatches are then processed sequentially, with operations on the groups within each microbatch being vectorized using jax.vmap(). This can be used to reduce peak memory usage at the cost of increased sequential computation.

  • nan_safe (bool) – If True, per-example outputs with non-finite L2 norms (NaN or inf) are zeroed out before aggregation, preserving the formal guarantees.

  • dtype (Union[str, type[Any], dtype, SupportsDType, None]) – Optional dtype for the clipped+aggregated PyTree. If None, the dtype will be the same as the dtypes of the function output. Can be useful to avoid overflow issues when using low-precision dtypes as the transformed function computes a sum over a potentially large batch.

  • prng_argnum (int | None) – If set, specifies which argument of fun is a PRNG key. The PRNG will be split to have a batch dimension and vmapped over.

  • spmd_axis_name (str | None) – See jax.vmap().

  • grid_scale (int | None) – If set, per-example outputs are additionally scaled and rounded to an integer grid after clipping. Specifically, each clipped output is multiplied by grid_scale / l2_clip_norm, rounded to the nearest integer, and cast to jnp.int64. The clipping norm is tightened automatically so that the integer L2 norm of each rounded output is at most grid_scale. This option is designed for use with the discrete Gaussian mechanism. Incompatible with rescale_to_unit_norm=True and normalize_by != 1.0. When set, dtype is ignored (output is always jnp.int64).

  • slack (int | None) – Optional number of SlaClip slack coordinates. Must be a positive Python integer. When set, the returned value is a ClippedGradOutput whose fields must be privatized together. Slack coordinates have float32 dtype. Only scalar global clipping is currently supported.

Return type:

BoundedSensitivityCallable

Returns:

A BoundedSensitivityCallable wrapping a new function clip_fn that clips the output of fun and sums across the batch. clip_fn takes the same positional arguments as fun, and additionally accepts an optional keyword argument is_padding_example (see __call__() for details). The exact output signature depends on has_aux and return_norms:

has_aux

return_norms

clipped_fn returns

False

False

value

True

False

value, aux

False

True

value, norms

True

True

value, (aux, norms)