Coverage for src/bartz/_jaxext/_autobatch.py: 99%
187 statements
« prev ^ index » next coverage.py v7.17.0, created at 2026-11-30 12:50 +0000
« prev ^ index » next coverage.py v7.17.0, created at 2026-11-30 12:50 +0000
1# bartz/src/bartz/_jaxext/_autobatch.py
2#
3# Copyright (c) 2025-2026, The Bartz Contributors
4#
5# This file is part of bartz.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in all
15# copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
25"""Implementation of `autobatch`."""
27import math
28from collections.abc import Callable
29from functools import partial, wraps
30from typing import Any, Protocol, runtime_checkable
31from warnings import warn
33from jax import ShapeDtypeStruct, eval_shape, lax, tree, typeof
34from jax import numpy as jnp
35from jax.typing import ArrayLike, DTypeLike
36from jaxtyping import Array, PyTree, Shaped
37from numpy.lib.array_utils import normalize_axis_index
39from bartz._jaxext._jit import jit
42@runtime_checkable
43class BinaryUfunc(Protocol):
44 """Duck type of binary `jax.numpy.ufunc`s like `jnp.add`.
46 Mirrors the stub-only protocol `jax.numpy.BinaryUfunc`, which does not
47 exist at runtime.
48 """
50 @property
51 def identity(self) -> bool | int | float: ...
53 def __call__(
54 self, x: Shaped[ArrayLike, '...'], y: Shaped[ArrayLike, '...'], /
55 ) -> Shaped[Array, '...']: ...
57 def reduce(
58 self, a: Shaped[ArrayLike, '...'], /, *, axis: int | None = 0
59 ) -> Shaped[Array, '...']: ...
62def expand_axes(
63 axes: PyTree[int | None], tree_arg: PyTree, *, none_is_leaf: bool = True
64) -> PyTree[int | None]:
65 """Expand `axes` such that they match the pytreedef of `tree_arg`."""
67 def expand_axis(axis: int | None, subtree: PyTree) -> PyTree[int | None]:
68 return tree.map(lambda _: axis, subtree)
70 is_leaf = (lambda x: x is None) if none_is_leaf else None
71 return tree.map(expand_axis, axes, tree_arg, is_leaf=is_leaf)
74def normalize_axes(
75 axes: PyTree[int | None, ' T'],
76 tree_arg: PyTree[Array | ShapeDtypeStruct | None, ' T'],
77) -> PyTree[int | None, ' T']:
78 """Normalize axes to be non-negative and valid for the corresponding arrays in the tree_arg."""
80 def normalize_axis(
81 axis: int | None, x: Shaped[Array, '...'] | ShapeDtypeStruct | None
82 ) -> int | None:
83 if axis is None:
84 return None
85 else:
86 assert x is not None
87 return normalize_axis_index(axis, len(x.shape))
89 return tree.map(normalize_axis, axes, tree_arg, is_leaf=lambda x: x is None)
92def remove_axis(
93 x: PyTree[ShapeDtypeStruct, ' T'], axis: PyTree[int, ' T'], ufunc: BinaryUfunc
94) -> PyTree[ShapeDtypeStruct, ' T']:
95 """Remove an axis from dummy arrays and change the type to reduction type."""
97 def remove_axis(x: ShapeDtypeStruct, axis: int) -> ShapeDtypeStruct:
98 new_shape = x.shape[:axis] + x.shape[axis + 1 :]
99 new_dtype = reduction_dtype(ufunc, x.dtype)
100 return ShapeDtypeStruct(new_shape, new_dtype)
102 return tree.map(remove_axis, x, axis)
105def extract_size(axes: PyTree[int | None], tree_arg: PyTree) -> int:
106 """Get the size of each array in tree_arg at the axis in axes, check they are equal and return it."""
108 def get_size(
109 x: Shaped[Array, '...'] | ShapeDtypeStruct, axis: int | None
110 ) -> int | None:
111 if axis is None:
112 return None
113 else:
114 return x.shape[axis]
116 sizes = tree.map(get_size, tree_arg, axes)
117 sizes, _ = tree.flatten(sizes)
118 assert all(s == sizes[0] for s in sizes)
119 return sizes[0]
122def sum_nbytes(tree_arg: PyTree[Array | ShapeDtypeStruct]) -> int:
123 def nbytes(x: Shaped[Array, '...'] | ShapeDtypeStruct) -> int:
124 return math.prod(x.shape) * x.dtype.itemsize
126 return tree.reduce(lambda size, x: size + nbytes(x), tree_arg, 0)
129def next_divisor_small(dividend: int, min_divisor: int) -> int:
130 for divisor in range(min_divisor, int(math.sqrt(dividend)) + 1):
131 if dividend % divisor == 0:
132 return divisor
133 return dividend
136def next_divisor_large(dividend: int, min_divisor: int) -> int:
137 max_inv_divisor = dividend // min_divisor
138 for inv_divisor in range(max_inv_divisor, 0, -1):
139 if dividend % inv_divisor == 0:
140 return dividend // inv_divisor
141 return dividend
144def next_divisor(dividend: int, min_divisor: int) -> int:
145 """Return divisor >= min_divisor such that dividend % divisor == 0."""
146 if dividend == 0:
147 return min_divisor
148 if min_divisor * min_divisor <= dividend:
149 return next_divisor_small(dividend, min_divisor)
150 return next_divisor_large(dividend, min_divisor)
153def pull_nonbatched(
154 axes: PyTree[int | None], tree_arg: PyTree
155) -> tuple[PyTree, PyTree]:
156 def pull_nonbatched(x: object, axis: int | None) -> object:
157 if axis is None:
158 return None
159 else:
160 return x
162 return tree.map(pull_nonbatched, tree_arg, axes), tree_arg
165def push_nonbatched(
166 axes: PyTree[int | None], tree_arg: PyTree, original_tree: PyTree
167) -> PyTree[Any]:
168 def push_nonbatched(original_x: object, x: object, axis: int | None) -> object:
169 if axis is None:
170 return original_x
171 else:
172 return x
174 return tree.map(push_nonbatched, original_tree, tree_arg, axes)
177def move_axes_out(axes: PyTree[int], tree_arg: PyTree[Array]) -> PyTree[Array]:
178 def move_axis_out(x: Shaped[Array, '...'], axis: int) -> Shaped[Array, '...']:
179 return jnp.moveaxis(x, axis, 0)
181 return tree.map(move_axis_out, tree_arg, axes)
184def move_axes_in(axes: PyTree[int], tree_arg: PyTree[Array]) -> PyTree[Array]:
185 def move_axis_in(x: Shaped[Array, '...'], axis: int) -> Shaped[Array, '...']:
186 return jnp.moveaxis(x, 0, axis)
188 return tree.map(move_axis_in, tree_arg, axes)
191def batch(tree_arg: PyTree[Array, ' T'], nbatches: int) -> PyTree[Array, ' T']:
192 """Split the first axis into two axes, the first of size `nbatches`."""
194 def batch(x: Shaped[Array, '...']) -> Shaped[Array, '...']:
195 return x.reshape(nbatches, x.shape[0] // nbatches, *x.shape[1:])
197 return tree.map(batch, tree_arg)
200def unbatch(tree_arg: PyTree[Array, ' T']) -> PyTree[Array, ' T']:
201 """Merge the first two axes into a single axis."""
203 def unbatch(x: Shaped[Array, '...']) -> Shaped[Array, '...']:
204 return x.reshape(x.shape[0] * x.shape[1], *x.shape[2:])
206 return tree.map(unbatch, tree_arg)
209def reduce(
210 ufunc: BinaryUfunc,
211 x: PyTree[Array, ' T'],
212 axes: PyTree[int, ' T'],
213 initial: PyTree[Array, ' T'] | None,
214) -> PyTree[Array, ' T']:
215 """Reduce each array in `x` along the axes in `axes` starting from `initial` using `ufunc.reduce`."""
216 if initial is None: 216 ↛ anywhereline 216 didn't jump anywhere: it always raised an exception.
218 def reduce(x: Shaped[Array, '...'], axis: int) -> Shaped[Array, '...']:
219 return ufunc.reduce(x, axis=axis)
221 return tree.map(reduce, x, axes)
223 else:
225 def reduce(
226 x: Shaped[Array, '...'], initial: Shaped[Array, '...'], axis: int
227 ) -> Shaped[Array, '...']:
228 reduced = ufunc.reduce(x, axis=axis)
229 return ufunc(initial, reduced)
231 return tree.map(reduce, x, initial, axes)
234def identity(
235 ufunc: BinaryUfunc, x: PyTree[ShapeDtypeStruct, ' T']
236) -> PyTree[Array, ' T']:
237 """Get the identity element for `ufunc` and each array in `x`."""
239 def identity(x: ShapeDtypeStruct) -> Shaped[Array, '...']:
240 identity = identity_for(ufunc, x.dtype)
241 return jnp.broadcast_to(identity, x.shape)
243 return tree.map(identity, x)
246def reduction_dtype(ufunc: BinaryUfunc, input_dtype: DTypeLike) -> DTypeLike:
247 """Return the output dtype for a reduction with `ufunc` on inputs of type `dtype`."""
248 return ufunc.reduce(jnp.empty(1, input_dtype)).dtype
251def identity_for(ufunc: BinaryUfunc, input_dtype: DTypeLike) -> Shaped[Array, '']:
252 """Return the identity for ufunc as an array scalar with the right dtype."""
253 # get output type from input type, e.g., int8 is accumulated to int32
254 dtype = reduction_dtype(ufunc, input_dtype)
256 # return as explicitly typed array
257 return jnp.array(ufunc.identity, dtype)
260def vary(x: Shaped[Array, '*shape'], axes: tuple[str, ...]) -> Shaped[Array, '*shape']:
261 """Mark `x` as varying over the manual `shard_map` mesh `axes`."""
262 # WORKAROUND(jax<0.8.2): pcast is absent, use its predecessor pvary; once
263 # unsupported, delete this function and call `lax.pcast(..., to='varying')`
264 if hasattr(lax, 'pcast'):
265 return lax.pcast(x, axes, to='varying')
266 else:
267 return lax.pvary(x, axes)
270def varying_axes(x: Shaped[Array, '*shape'] | ShapeDtypeStruct) -> frozenset[str]:
271 """Return the manual `shard_map` mesh axes over which `x` varies."""
272 aval = typeof(x)
273 # WORKAROUND(jax<0.10.0): the axes are in `vma`; once unsupported, delete
274 # this function and use `typeof(x).manual_axis_type.varying`
275 if hasattr(aval, 'manual_axis_type'):
276 return aval.manual_axis_type.varying
277 else:
278 return aval.vma
281def check_same(tree1: PyTree, tree2: PyTree) -> None:
282 def check_same(
283 x1: Shaped[Array, '*shape'] | ShapeDtypeStruct,
284 x2: Shaped[Array, '*shape'] | ShapeDtypeStruct,
285 ) -> None:
286 assert x1.shape == x2.shape
287 assert x1.dtype == x2.dtype
289 tree.map(check_same, tree1, tree2)
292class NotDefined:
293 pass
296def autobatch(
297 func: Callable,
298 max_io_nbytes: int,
299 in_axes: PyTree[int | None] = 0,
300 out_axes: PyTree[int] = 0,
301 *,
302 return_nbatches: bool = False,
303 reduce_ufunc: BinaryUfunc | None = None,
304 reduce_vary_axes: tuple[str, ...] = (),
305 warn_on_overflow: bool = True,
306 result_shape_dtype: PyTree[ShapeDtypeStruct] | type[NotDefined] = NotDefined,
307) -> Callable:
308 """
309 Batch a function such that each batch is smaller than a threshold.
311 Parameters
312 ----------
313 func
314 A jittable function with positional arguments only, with inputs and
315 outputs pytrees of arrays.
316 max_io_nbytes
317 The maximum number of input + output bytes in each batch (excluding
318 unbatched arguments.)
319 in_axes
320 A tree matching (a prefix of) the structure of the function input,
321 indicating along which axes each array should be batched. A `None` axis
322 indicates to not batch an argument.
323 out_axes
324 The same for outputs (but non-batching is not allowed).
325 return_nbatches
326 If True, the number of batches is returned as a second output.
327 reduce_ufunc
328 Function used to reduce the output along the batched axis (e.g.,
329 `jax.numpy.add`).
330 reduce_vary_axes
331 Manual `jax.shard_map` mesh axes over which the reduction accumulator
332 varies. Under a `shard_map`, the reduction seed is cast to vary over
333 these axes so its type matches the shard-varying loop body, satisfying
334 the VMA checker. Ignored unless `reduce_ufunc` is set.
335 warn_on_overflow
336 If True, a warning is raised if the memory limit could not be
337 respected.
338 result_shape_dtype
339 A pytree of dummy arrays matching the expected output. If not provided,
340 the function is traced an additional time to determine the output
341 structure.
343 Returns
344 -------
345 A function with the same signature as `func`, save for the return value if `return_nbatches`.
347 Notes
348 -----
349 Unless `return_nbatches` or `reduce_ufunc` are set, `autobatch` at given
350 arguments is idempotent. Furthermore, `autobatch` can be applied multiple
351 times over multiple axes with the same `max_io_nbytes` limit to work on
352 multiple axes; in this case it won't unnecessarily loop over additional axes
353 if one or more outer `autobatch` are already sufficient.
355 To handle memory used in intermediate values: assuming all intermediate
356 values have size that scales linearly with the axis batched over, say the
357 batched input/output total size is ``batched_size * core_io_size``, and the
358 intermediate values have size ``batched_size * core_int_size``, then to take
359 them into account divide `max_io_nbytes` by ``(1 + core_int_size /
360 core_io_size)``.
361 """
363 @jit
364 @wraps(func)
365 def autobatch_wrapper(*args: PyTree) -> PyTree:
366 return batched_func(
367 func,
368 args,
369 max_io_nbytes=max_io_nbytes,
370 in_axes=in_axes,
371 out_axes=out_axes,
372 return_nbatches=return_nbatches,
373 reduce_ufunc=reduce_ufunc,
374 reduce_vary_axes=reduce_vary_axes,
375 warn_on_overflow=warn_on_overflow,
376 result_shape_dtype=result_shape_dtype,
377 )
379 return autobatch_wrapper
382def batched_func(
383 func: Callable,
384 args: tuple[PyTree[Array], ...],
385 *,
386 max_io_nbytes: int,
387 in_axes: PyTree[int | None],
388 out_axes: PyTree[int],
389 return_nbatches: bool,
390 reduce_ufunc: BinaryUfunc | None,
391 reduce_vary_axes: tuple[str, ...],
392 warn_on_overflow: bool,
393 result_shape_dtype: PyTree[ShapeDtypeStruct] | type[NotDefined],
394) -> PyTree[Array] | tuple[PyTree[Array], int]:
395 """Implement the wrapper used in `autobatch`."""
396 # determine the output structure of the function
397 if result_shape_dtype is NotDefined:
398 example_result = eval_shape(func, *args)
399 else:
400 example_result = result_shape_dtype
402 # expand the axes pytrees if they are prefixes
403 in_axes = expand_axes(in_axes, args)
404 out_axes = expand_axes(out_axes, example_result, none_is_leaf=False)
406 # check the axes are valid
407 in_axes = normalize_axes(in_axes, args)
408 out_axes = normalize_axes(out_axes, example_result)
410 # get the size of the batched axis
411 size = extract_size((in_axes, out_axes), (args, example_result))
413 # split arguments in batched and not batched
414 original_args = args
415 args, nonbatched_args = pull_nonbatched(in_axes, args)
417 # determine the number of batches to respect the memory limit
418 total_nbytes = sum_nbytes((args, example_result))
419 min_nbatches = total_nbytes // max_io_nbytes + bool(total_nbytes % max_io_nbytes)
420 min_nbatches = max(1, min_nbatches)
421 nbatches = next_divisor(size, min_nbatches)
422 assert 1 <= nbatches <= max(1, size)
423 assert size % nbatches == 0
424 assert total_nbytes % nbatches == 0
426 # warn if the memory limit could not be respected
427 batch_nbytes = total_nbytes // nbatches
428 if batch_nbytes > max_io_nbytes and warn_on_overflow:
429 assert size == nbatches
430 msg = f'batch_nbytes = {batch_nbytes:_} > max_io_nbytes = {max_io_nbytes:_}'
431 warn(msg)
433 # squeeze out the output dims that will be reduced
434 if reduce_ufunc is not None:
435 example_result = remove_axis(example_result, out_axes, reduce_ufunc)
437 if nbatches > 1:
438 # prepare arguments for looping
439 args = move_axes_out(in_axes, args)
440 args = batch(args, nbatches)
442 # prepare carry for reduction
443 if reduce_ufunc is None:
444 initial = None
445 else:
446 initial = identity(reduce_ufunc, example_result)
447 # under a `shard_map`, the loop body output varies over the manual
448 # axes while this seed is replicated; mark it varying so the scan's
449 # carry types match and the VMA checker is satisfied
450 if reduce_vary_axes:
451 initial = tree.map(partial(vary, axes=reduce_vary_axes), initial)
453 # loop and invoke the function in batches
454 loop = partial(
455 batching_loop,
456 func=func,
457 nonbatched_args=nonbatched_args,
458 in_axes=in_axes,
459 out_axes=out_axes,
460 reduce_ufunc=reduce_ufunc,
461 )
462 reduced_result, result = lax.scan(loop, initial, args)
464 # remove auxiliary batching axis and reverse transposition
465 if reduce_ufunc is None:
466 assert reduced_result is None
467 result = unbatch(result)
468 result = move_axes_in(out_axes, result)
469 else:
470 assert result is None
471 result = reduced_result
473 # trivial case: no batching needed
474 else:
475 result = func(*original_args)
476 if reduce_ufunc is not None:
477 result = reduce(reduce_ufunc, result, out_axes, None)
479 check_same(example_result, result)
481 if return_nbatches:
482 return result, nbatches
483 return result
486def batching_loop(
487 initial: PyTree[Array] | None,
488 args: PyTree[Array],
489 *,
490 func: Callable,
491 nonbatched_args: PyTree,
492 in_axes: PyTree[int | None],
493 out_axes: PyTree[int],
494 reduce_ufunc: BinaryUfunc | None,
495) -> tuple[PyTree[Array], None] | tuple[None, PyTree[Array]]:
496 """Implement the batching loop in `autobatch`."""
497 # evaluate the function
498 args = move_axes_in(in_axes, args)
499 args = push_nonbatched(in_axes, args, nonbatched_args)
500 result = func(*args)
502 # unreduced case: transpose for concatenation and return
503 if reduce_ufunc is None:
504 result = move_axes_out(out_axes, result)
505 return None, result
507 # reduced case: reduce starting from initial
508 else:
509 reduced_result = reduce(reduce_ufunc, result, out_axes, initial)
510 return reduced_result, None