Fix nnx.cond tracing cache misses by caching SimpleCondFn wrappers #5518
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 353 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.351
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #5518"
general
weight: 0.35
cgarciae
What does this PR do?
nnx.cond(andnnx.switch) wrap each branch function in a transientSimpleCondFnadapter before handing it tojax.lax.cond. A new wrapperobject is created on every call, and
SimpleCondFnis declared with@dataclasses.dataclass(eq=False), so it falls back to identity-basedhashing/equality.
JAX keys its trace cache (and, by extension, the persistent compilation cache)
on the identity/hash of the callable passed to
lax.cond. Because a freshwrapper is produced each call — even when the underlying branch function is the
exact same object — JAX treats every invocation as a never-before-seen function.
This produces repeated tracing cache misses and prevents the persistent
compilation cache from ever being reused:
For large modules with long compile times, this means recompilation on every
run instead of a fast warm start from the on-disk cache.
This PR memoizes the
SimpleCondFnwrappers so that the same underlying branchfunction reuses the same wrapper instance, giving JAX a stable object identity
and restoring trace/compilation cache hits. The cache uses a
weakref.WeakKeyDictionarykeyed on the original function so wrappers arecollected automatically when the branch function is garbage-collected (no leak).
This is a performance-only change: the computation and results are unchanged —
only the identity of the wrapper handed to
jax.lax.condis stabilized.Fixes #5512
Notes / scope
(f, graph), so it helps the common case wherebranches are stable function references (e.g. bound methods / module-level
functions). Freshly-created lambdas or closures defined inside the call site
are genuinely new objects each call and will not (and should not) be cached.
SimpleCondFnhash-by-value(
__eq__/__hash__over(f, graph)). TheWeakKeyDictionaryapproach waschosen because it guarantees true object reuse and does not depend on how JAX
combines hashing with
ischecks.Checklist