brainstate.transform.vmap_new_states#
- brainstate.transform.vmap_new_states(fun=<brainstate.typing.Missing object>, *, in_axes=0, out_axes=0, axis_name=None, axis_size=None, spmd_axis_name=None, state_tag=None, state_to_exclude=None, in_states=None, out_states=None)[source]#
Vectorize a function over the new states it creates.
Unlike
vmap(), which maps over states that already exist, this transform maps over states created during the call tofun(for example, parameters allocated inside a module’s initializer). Each mapped lane creates its own copy of every new state, and random states are split per lane so randomly initialized values differ across the mapped axis. It is implemented as a single mapping pass over the shared engine helpers.Note
funis executed twice – an eager probe (to discover the random states it touches) plus the mapped pass – so it must be idempotent and free of un-rolled-back side effects.NonBatchState(andINIT_NO_BATCHING-tagged) states created insidefunare replicated rather than batched.- Parameters:
fun (
Callable) – Function to vectorize. If omitted, this acts as a decorator.in_axes (
int|None|Sequence[Any]) – Mapped-axis alignment per positional argument, followingjax.vmap()semantics.Nonemarks an argument as broadcast.out_axes (
Any) – Placement of the mapped axis in the return value.axis_name (
Hashable|None) – Name for the mapped axis so collective primitives can target it.axis_size (
int|None) – Explicit mapped-axis size. Inferred from the inputs when omitted.spmd_axis_name (
Hashable|tuple[Hashable,...] |None) – Axis labels for nested SPMD transforms.state_tag (
str|None) – Tag applied to the newly created states so they can be retrieved later.state_to_exclude (
type|str|Callable[[Tuple[Key,...],Any],bool] |bool|EllipsisType|None|Tuple[Filter,...] |List[Filter]) – Selector for new states that should be left untouched (not vectorized).in_states (
Dict[int,Dict] |Any|None) – Not supported by this transform.vmap_new_statesmaps over states created insidefun, so there are no pre-existing states to declare. Passing either raisesValueError; usevmap()to vectorize over pre-existing states.out_states (
Dict[int,Dict] |Any|None) – Not supported by this transform.vmap_new_statesmaps over states created insidefun, so there are no pre-existing states to declare. Passing either raisesValueError; usevmap()to vectorize over pre-existing states.
- Returns:
A vectorized version of
funthat handles new-state creation, or a decorator iffunis omitted.- Return type:
- Raises:
ValueError – If
axis_sizeis provided but not a positive integer, or ifin_states/out_statesis provided (usevmap()for pre-existing states).NotImplementedError – If keyword arguments are passed to the vectorized function.
See also
brainstate.transform.vmapVectorize over pre-existing states.
brainstate.transform.vmap2_new_statesModule-oriented new-state mapping.
Examples
>>> import brainstate >>> import jax.numpy as jnp >>> >>> @brainstate.transform.vmap_new_states(in_axes=0, axis_size=4) ... def build(x): ... scratch = brainstate.ShortTermState(jnp.zeros(())) ... scratch.value = scratch.value + x ... return scratch.value >>> >>> build(jnp.arange(4.)) Array([0., 1., 2., 3.], dtype=float32)