quantax.utils.chunk_map#
- quantax.utils.chunk_map(f: Callable, in_axes: tuple | int | None = 0, out_axes: tuple | int | None = 0, chunk_size: int | None = None, use_scan: bool = False, shard_batch: bool = False, atleast_1chunk: bool = False, mesh: Mesh | None = None) Callable#
Convert a vmapped function to a function with chunked batches and parallel computation on all available machines. The arguments will be unchanged if the batch size on each machine is smaller than the chunk size, but it will be padded with 0 if the batch size is larger than the chunk size and not a multiple of chunk size.
- Parameters:
f β The function to be converted. The arguments of f are assumed to be sharded.
in_axes β The vmapped axes of f which are to be chunked.
out_axes β The vmapped axes of outputs.
chunk_size β The chunk size on each machine.
use_scan β Whether to use
jax.lax.scanin chunked function apply. The compilation will be accelerated ifscanis used, but the function must be jittable.shard_batch β Whether to run
finsidejax.shard_mapso each device chunks and processes only its own batch shard (a device-locallax.scaninside oneshard_map), instead of GSPMD partitioning a global batch. Needed only for per-sample reverse-mode ops where GSPMD inserts cross-device all-gathers β the symmetry-vmapped Jacobianβs conv weight-gradient β so it defaults to False. Must stay False whenfcalls anotherchunk_map-wrapped function, becauseshard_mapcannot be nested over the same mesh axes.atleast_1chunk β Whether to pad the batch with 0 to the chunk size when the batch size on each machine is smaller than the chunk size, so that
fis always called with batch sizechunk_sizeon each machine. The padded outputs are truncated. Default to False, in which casefis called with the original batch.mesh β The device mesh that determines the device count for chunking and the
shard_batchsharding, default tomake_meshspanning all devices of the current run (resolved at trace time). Pass a compile-only mesh frommake_precompile_meshto stage the computation for a different topology without running on it.