TECHNOLOGY

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

In this article
THE QUICK TAKE

Runtime branch selection, tracing validity and batched execution are distinct requirements of a conditional calculation.

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 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 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 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 and tensor memory contracts 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.

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.

Explore GPU hosting
← All articles