← Back to Pull requests

Add vmap reset() regression test for nnx.metrics (#5483) #5491

Closed
unknown closed this pull request without merging on Jul 21, 2026 22:23 UTC
View on GitHub
unknown unknown commented on Jun 11, 2026 00:26 UTC
Author

What does this PR do?

This adds a regression test for the nnx.metrics behavior reported in #5483.

nnx.metrics.Average.reset() (and Welford.reset()) re-zero their state by assigning a scalar (jnp.array(0, ...)). When a metric is built under nnx.vmap, its state carries a leading batch axis of shape (N,). On the reporter's versions (flax 0.12.0 / jax 0.7.2) the scalar assignment replaced the whole array, collapsing (N,) to (), so a later vmapped update failed with:

ValueError: vmap was requested to map its argument along axis 0, which implies that its rank should be at least 1, but is only 0 (its shape is ())

While investigating I found the crash no longer reproduces on main: the Variable state layer has since changed so a full-index assignment broadcasts a scalar into the existing array in place instead of replacing it, so reset() preserves the batch axis today. I confirmed it still reproduces on flax 0.12.0 / jax 0.7.2 and is gone on main independent of any single commit.

Since there is no longer a live crash to fix on main, this PR is scoped as a regression test only, to lock in the current vmap reset() behavior so it can't silently break again.

What's in this PR

  • tests/nnx/metrics_test.py — test_vmap_reset_preserves_shape: construct a MultiMetric(loss=Average('loss')) under nnx.vmap (state shape (N,)), call reset(), assert the state stays shape (N,), then run a vmapped update and assert compute()['loss'] returns the expected per-batch values.
  • 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/5483

  • The documentation and docstrings adhere to the documentation guidelines.

  • This change includes necessary high-coverage tests. (No quality testing = no merge!)
  • Diff: 268 lines (+201 / -67) across 3 files Reviewer: samanklesaria Review duration: 58917m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.39) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.39 · 268 lines diff across 3 files
    0.39
    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%
    56%
    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.335
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #5491"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% 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: 39.4% (MEDIUM) · Higher = larger blast radius
    39.4%
    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: 56.8% · Lower = rushed or shallow review
    56.8%
    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: 17% · Safe to merge
    17%
    Repository