﻿# JAX cond: a skipped runtime branch still has to trace, and batching changes the contract

Understand why JAX cond traces both branches and why vmap over predicates can evaluate both paths when reviewing conditional model computation.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-jax-cond-tracing-batched-predicate/
Language: en

## Start with the branch that appears unused

A preprocessing function applies a costly correction only when a quality flag is set. Its developer places an unsupported Python conversion in the other branch, expecting that branch to be harmless for the current input. The [cond reference](https://docs.jax.dev/en/latest/_autosummary/jax.lax.cond.html) states that both branches are traced. A branch that is unselected at runtime must still be valid for the transformation. The predicate does not exempt its source code from staging requirements.

## Separate tracing from executing numerical work

The [tracing guide](https://docs.jax.dev/en/latest/tracing.html) explains how transformations work with traced values and abstract information. A trace-time Python action is different from a device calculation performed for each call. Do not infer runtime branch cost from a print that appeared while constructing the program. Conversely, do not use Python side effects inside either branch as evidence that the device processed a particular example. Keep external actions outside the numerical conditional.

## Check the branch interface before the predicate

Give both branches a consistent result structure and compatible numerical outputs for the calculation. A correction path that returns a vector cannot be paired casually with a fallback returning a metadata dictionary. Define the fallback value explicitly, including missing-data semantics. The conditional should choose between valid implementations of the same interface rather than hide an unimplemented path behind a predicate that happens to be false in an initial notebook experiment.

## Review batching as a new execution arrangement

The cond documentation explains that vmap over a batch of predicates converts the conditional to select. The [select reference](https://docs.jax.dev/en/latest/_autosummary/jax.lax.select.html) describes general evaluation of both branches, subject to compiler elimination. Therefore a per-example conditional is not automatically a guarantee that expensive work occurs only for selected examples after batching. Inspect the transformed workload and make both candidate calculations numerically safe for the inputs they can receive.

## Validate behavior at each intended transformation

Exercise each predicate outcome, then the actual compiled and batched wrapper. Compare valid outputs and record where conditional savings are expected. The guides on [broadcasting mask axes](/journal/broadcasting-attention-mask-axes/) and [tensor memory contracts](/journal/learn-tensor-memory-descriptor/) provide useful related context. A sound conditional design establishes traceability and numerical correctness before using the predicate to explain resource consumption. Batching can preserve selected results while changing how their candidate values are produced.
