← Back to Pull requests

Fix nnx.cond tracing cache misses by caching SimpleCondFn wrappers #5518

Closed
unknown closed this pull request without merging on Sep 25, 2026 22:31 UTC
View on GitHub
unknown unknown commented on Jun 27, 2026 16:46 UTC
Author

What does this PR do?

nnx.cond (and nnx.switch) wrap each branch function in a transient
SimpleCondFn adapter before handing it to jax.lax.cond. A new wrapper
object is created on every call, and SimpleCondFn is declared with
@dataclasses.dataclass(eq=False), so it falls back to identity-based
hashing/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 fresh
wrapper 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:

TRACING CACHE MISS at .../flax/nnx/transforms/transforms.py:623 (cond): never seen function
... being re-defined repeatedly, preventing caching

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 SimpleCondFn wrappers so that the same underlying branch
function reuses the same wrapper instance, giving JAX a stable object identity
and restoring trace/compilation cache hits. The cache uses a
weakref.WeakKeyDictionary keyed on the original function so wrappers are
collected 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.cond is stabilized.

Fixes #5512

Notes / scope

  • The wrapper cache is keyed on (f, graph), so it helps the common case where

  • branches 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.
  • An alternative approach would be to make SimpleCondFn hash-by-value

  • (__eq__/__hash__ over (f, graph)). The WeakKeyDictionary approach was
    chosen because it guarantees true object reuse and does not depend on how JAX
    combines hashing with is checks.

    Checklist


  • This PR fixes a minor issue (e.g.: typo or small bug) or improves the docs (you can dismiss the other checks if that's the case).

  • This change is discussed in a Github issue/discussion (please add a link). — https://github.com/google/flax/issues/5512

  • The documentation and docstrings adhere to the documentation guidelines.

  • This change includes necessary high-coverage tests. (No quality testing = no merge!)
  • Diff: 353 lines (+264 / -89) across 4 files Reviewer: cgarciae Review duration: 129945m
    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
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.5% (MEDIUM) · Higher = larger blast radius
    42.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.8% · Safe to merge
    16.8%
    Repository