brainstate.transform.pmap2_new_states#
- brainstate.transform.pmap2_new_states(module, init_kwargs, state_tag=None, axis_size=None, state_out_axes=None, axis_name=None)#
Initialize and parallelize newly created states across devices.
Wraps
module.init_all_states(**init_kwargs)in apmap2()-style transform, executes it acrossaxis_sizedevices, and restores the device-distributed values back onto the freshly created state objects. Random states are split per device.Note
init_all_statesruns twice (a probe pass plus the mapped pass) and must be idempotent; see_map_new_states()for details.- Parameters:
module (Module) – Module whose
init_all_statescreates the states to parallelize.init_kwargs (Dict) – Keyword arguments forwarded to
module.init_all_states.state_tag (str) – Tag applied to the newly created states.
axis_size (int) – Number of devices to map over. Defaults to
jax.local_device_count().state_out_axes (Dict[int, Filter]) – Output-axis selectors.
None(default) shards every state on axis0exceptNonBatchState, which is replicated.axis_name (AxisName | None) – Mapped-axis name for collective primitives.
- Returns:
Mapping of axis identifier to the lists of parallelized states.
- Return type:
Dict
See also
brainstate.transform.pmap2Parallel mapping across devices.
brainstate.transform.vmap2_new_statesSingle-device variant.
jax.pmapUnderlying JAX parallel mapping primitive.
Examples
>>> import brainstate >>> import jax >>> import jax.numpy as jnp >>> >>> class ParallelCounter(brainstate.nn.Module): ... def init_state(self): ... self.count = brainstate.ShortTermState(jnp.zeros(())) >>> >>> module = ParallelCounter() >>> _ = brainstate.transform.pmap2_new_states( ... module, init_kwargs={}, axis_size=jax.local_device_count() ... ) >>> module.count.value.shape (jax.local_device_count(),)