adv_jax_math.sparse_pullback
- adv_jax_math.sparse_pullback(fn, y, /, batch_size=None, *, reduction=None, chunk_reduction=<function identity>, strip_dim0=False, shard=False, mesh=None, higher_order=False, **kwargs)[source]
Compute
chunk_reduction(fn(fun_input))in batches with sparse pullbacks.Wraps the given map with logic to ensure cotangents flow through the diagonal of its pullback. The derivatives will be exact for maps whose Jacobians are block diagonal.
Notes
This method does not automatically wrap
fnwithvmap. Unlessfnis already wrapped withvmap, the leading dimension ofywill not be stripped before it is passed intofn. This can be inconvenient for nesting calls tosparse_pullback, since only batching along the first axis is currently supported. However, thestrip_dim0flag should cover the most common case of nesting calls wherebatch_sizeis one on the outermost call.See also
sparse_pullback_mapFunctional version.
- Parameters:
fn (callable) – Vectorized map.
y (pytree) – Data to split into batches to feed to
fn.batch_size (int or None) – Size of batches. If no batching should be done or the batch size is the full input then supply
None.chunk_sizeis accepted as an alias whenbatch_sizeisNone.reduction (callable or None) – Binary reduction operation. Should take two arguments and return one output, e.g.
jnp.add.chunk_reduction (callable) – Chunk-wise reduction operation. Should typically apply
reductionalong the mapped axis, e.g.jnp.add.reduce.strip_dim0 (bool) – Whether to strip the leading dim of
ybefore passing it tofn; see notes. This flag only works ifbatch_sizeis one. It should be set toFalseiffnis wrapped invmap. Default isFalse.shard (bool) – Whether to shard
yacross devices before applying chunked batching. The divisible prefix is split across devices; when supplied,batch_sizebounds the batches processed on each device. A local remainder is evaluated once per device, and a final global remainder is evaluated once overall. The input length need not be divisible by either the device count orbatch_size. Default isFalse.mesh (jax.sharding.Mesh or None) – Optional one-dimensional mesh with an
AxisType.Autoaxis to use whenshard=True. Default isNone, which uses all available devices.higher_order (bool) – Whether to support higher-order differentiation with the HiJAX backend at the expense of evaluating the primal
fntwice. The custom-VJP backend supports higher-order differentiation regardless of this flag. Default isFalse.
- Returns:
out (pytree) – Returns
chunk_reduction(fn(y)).
Examples
>>> out = sparse_pullback(fn, y)