adv_jax_math.batch_map
- adv_jax_math.batch_map(fun, fun_input, /, batch_size=None, *, reduction=None, chunk_reduction=<function identity>, strip_dim0=False, shard=False, mesh=None, **kwargs)[source]
Compute
chunk_reduction(fun(fun_input))in batches.Notes
This method does not automatically wrap
funwithvmap. Unlessfunis already wrapped withvmap, the leading dimension offun_inputwill not be stripped before it is passed intofun. This can be inconvenient for nesting calls tobatch_map, since only batching along the first axis is supported. However, thestrip_dim0flag should cover the most common case of nesting calls wherebatch_sizeis one on the outermost call.For a
funthat accepts scalar and batched inputs elementwise,batch_map(fun,inputs)is expected to matchbatch_vmap(fun)(inputs). Functions whose results depend on other examples in a chunk or on the chunk length do not satisfy this elementwise contract.If
funis natively vectorized, this can be preferable tobatch_vmapto reduce compilation time, avoid issues such as executing all branches of code conditioned on dynamic values, or avoid messing up the behavior ofjvp’s andvjp’s under vmap, e.g. https://docs.jax.dev/en/latest/jep/2026-custom-derivatives.html #main-problem-descriptions.Only
out_axes=0is supported.See also
batch_vmapIf the function does not support native vectorization.
- Parameters:
fun (callable) – Vectorized function.
fun_input (pytree) – Data to split into batches to feed to
fun.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, and must be associative because partial chunk results are combined in an implementation-dependent order.chunk_reduction (callable) – Chunk-wise reduction operation. Should summarize a chunk compatibly with
reductionalong the mapped axis, e.g.jnp.add.reduce.strip_dim0 (bool) – Whether to strip the leading dim of
fun_inputbefore passing it tofun; see notes. This flag only works ifbatch_sizeis one. It should be set toFalseiffunis wrapped invmap. Default isFalse.shard (bool) – Whether to shard
fun_inputacross 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. If a non-reduced output has a global remainder, the final concatenated output is replicated across the mesh.mesh (jax.sharding.Mesh or None) – Optional one-dimensional mesh with an
AxisType.Autoaxis. Supplying a mesh selects the devices and topology used byshard=Trueand lets already-sharded inputs retain a compatible layout. Default isNone, which creates a mesh over all available devices.
- Returns:
fun_output – Returns
chunk_reduction(fun(fun_input)).