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_boundattribute 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) – IfTrue,funis expected to return a tuple(value, aux). Only the value will be clipped + aggregated,auxwill 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) offuncontain the batch dimension. All arguments specified here must have the same size along the 0th axis.keep_batch_dim (
bool) – IfTrue, batch inputs will be passed tofunwith a leading batch axis of size 1. IfFalse, this size 1 axis will be dropped (reducing the rank of the batch args by 1 before passing tofun).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) – IfTrue, the output PyTree’s norm is rescaled by1.0 / clip_normafter potential clipping. IfFalse, the output PyTree has norm at mostclip_norm.normalize_by (
float) – Divide the clipped output by this value before returning.return_norms (
bool) – IfTrue, 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 usingjax.vmap(). This can be used to reduce peak memory usage at the cost of increased sequential computation.nan_safe (
bool) – IfTrue, 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. IfNone, 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 offunis a PRNG key. The PRNG will be split to have a batch dimension and vmapped over.spmd_axis_name (
str|None) – Seejax.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 bygrid_scale / l2_clip_norm, rounded to the nearest integer, and cast tojnp.int64. The clipping norm is tightened automatically so that the integer L2 norm of each rounded output is at mostgrid_scale. This option is designed for use with the discrete Gaussian mechanism. Incompatible withrescale_to_unit_norm=Trueandnormalize_by != 1.0. When set,dtypeis ignored (output is alwaysjnp.int64).slack (
int|None) – Optional number of SlaClip slack coordinates. Must be a positive Python integer. When set, the returned value is aClippedGradOutputwhose fields must be privatized together. Slack coordinates have float32 dtype. Only scalar global clipping is currently supported.
- Return type:
- Returns:
A
BoundedSensitivityCallablewrapping a new functionclip_fnthat clips the output offunand sums across the batch.clip_fntakes the same positional arguments asfun, and additionally accepts an optional keyword argumentis_padding_example(see__call__()for details). The exact output signature depends onhas_auxandreturn_norms:has_auxreturn_normsclipped_fnreturnsFalseFalsevalueTrueFalsevalue, auxFalseTruevalue, normsTrueTruevalue, (aux, norms)