In this article
vmap maps declared axes; a shared parameter argument must remain unmapped when every example should use the same model.
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 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 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 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 and tensor memory descriptors 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.
Earn rental income from GPUs
Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.