﻿# JAX captured globals: a Python update can leave the compiled computation unchanged

Trace JAX configuration and model state through explicit function inputs instead of relying on globals, counters or Python side effects inside compiled code.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-jax-global-state-tracing-boundary/
Language: en

## Notice the stale value before changing the GPU

A scoring function reads a global calibration factor. A notebook changes that factor, but repeated compiled calls keep producing the earlier result. The [JAX sharp-bits guide](https://docs.jax.dev/en/latest/notebooks/Common_Gotchas_in_JAX.html) demonstrates globals captured during tracing and later calls reusing the captured value. This is a program-state problem, not evidence that a GPU ignored new data that was explicitly passed to it.

## Make evolving state an explicit argument

The [stateful-computation guide](https://docs.jax.dev/en/latest/stateful-computations.html) recommends expressing evolving state through function inputs and returned state. In a calibration pipeline, pass the factor into the numerical function rather than updating a global between requests. If a step changes a running statistic, return the new statistic and route it into the next step. This makes the dependency visible to both the transformation and the reader reviewing the inference path.

## Distinguish a trace from an execution log

The [JIT guide](https://docs.jax.dev/en/latest/jit-compilation.html) explains that ordinary Python side effects are not recorded as numerical operations in the traced program. Appending a value to a global list or incrementing a Python counter inside the function therefore does not reliably count runtime invocations. A changed input type or shape can trigger another trace, making the apparent behavior inconsistent. Avoid using such counters to infer how many batches the accelerator actually processed.

## Reproduce the dependency in a small example

Construct a pilot with an explicit calibration input and the same input held in a global. Change the value while preserving ordinary array shapes, then compare the observed outputs. Keep the pilot separate from production state. Its purpose is to identify a missing dependency in the function contract. Clearing compilation state may temporarily conceal the symptom, but it does not repair the implicit connection between external Python state and the desired calculation.

## Keep model state distinct from service bookkeeping

Runtime request counts, audit records and model statistics have different purposes. Store service bookkeeping in the service layer and make numerical state part of the intended function contract. The guides on [autograd computation graphs](/journal/autograd-chain-rule-neural-graphs/) and [request correlation identifiers](/journal/guide-triton-request-id-scope/) provide related distinctions. A dependable system can explain both what arithmetic was requested and which request invoked it without pretending that an unrecorded Python side effect is part of the compiled computation.
