﻿# JAX vmap axes: keep shared parameters separate from the example dimension

Design JAX vmap in_axes and out_axes for batched examples, shared model parameters and nested inputs without accidentally pairing weights with examples.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-jax-vmap-shared-parameter-axes/
Language: en

## Describe the unbatched function first

A prediction function accepts model parameters and one embedding. A batch wrapper should apply the same model to every example, not take a different slice of weights for each row. The [vmap API](https://docs.jax.dev/en/latest/_autosummary/jax.vmap.html) assigns mapping behavior through `in_axes`, using None for an argument without a mapped axis. Write down what each argument means before assuming that every leading dimension is an example dimension.

## Separate sample batching from model ensembles

Shared parameters and a batch of independent parameter sets describe different computations. For an ensemble, mapping parameters may be deliberate; for ordinary batched inference, it may silently change the task. The [automatic-vectorization tutorial](https://docs.jax.dev/en/latest/automatic-vectorization.html) demonstrates transformation of a per-example calculation. Use a small reference calculation to establish which examples and parameter sets should be paired, rather than deciding solely from arrays having compatible dimension lengths.

## Specify axes inside nested arguments

Real inputs can contain a feature array, a validity mask and shared calibration data. The [pytree guide](https://docs.jax.dev/en/latest/pytrees.html) explains how transformation parameters can mirror or prefix nested argument structures. Map leaves by their semantic role. A single setting applied to an entire container may unintentionally batch shared metadata or leave a sample-dependent field unmapped. Keep the axis specification alongside the input schema when the container changes.

## Check keyword arguments and output placement

The vmap reference documents a separate convention for keyword arguments: they are mapped over their leading axis. A refactor from positional to keyword invocation can therefore require renewed review of the batching contract. Also inspect `out_axes`, which determines where mapped output axes appear. A numerically correct tensor can still be misread downstream if the consumer assumes examples occupy a different dimension from the one returned by the transformed function.

## Compare identities rather than only shapes

Use distinguishable examples and parameter values in a pilot so unintended pairings are visible. Check ordering as well as numerical agreement with the intended unbatched calculation. The guides on [broadcasting attention masks](/journal/broadcasting-attention-mask-axes/) and [tensor memory descriptors](/journal/learn-tensor-memory-descriptor/) explain related axis contracts. Shape compatibility is necessary, but the decisive question is whether each output still belongs to the intended example and model state.
