← Back to Pull requests

[trainer, perf] fix: count old_logprobs mismatches with a masked sum instead of boolean indexing #8663

Closed
unknown closed this pull request without merging on Nov 18, 2026 05:10 UTC
View on GitHub
unknown unknown commented on Nov 12, 2026 00:07 UTC
Author

What does this PR do?

actor/old_logprobs_mismatch_count, added in #8026, is computed in ppo_loss with boolean-mask indexing:

old_logprobs_mismatch = (log_prob.float() != old_log_prob.float())[response_mask].sum()

The indexed tensor has one element per valid response token of the micro-batch, so its shape changes with every micro-batch. On TPU (torch_tpu compiles nonzero for every new input length and every new number of nonzeros, google-pytorch/torch_tpu#5085), each micro-batch then compiles new programs for the nonzero / index and for the multiply / sum that follows, before the metric's .item() returns. This PR computes the same count with a masked sum, whose shape does not depend on the mask contents:

old_logprobs_mismatch = ((log_prob.float() != old_log_prob.float()) & response_mask).sum()

response_mask is boolean here, so the count is unchanged; the FP32 comparison, the dp_size scaling and AggregationType.SUM stay as they are. The loss and the gradients do not use this value.

Checklist Before Starting

  • Search for similar PRs. Paste at least one query link here: https://github.com/verl-project/verl/pulls?q=old_logprobs_mismatch , https://github.com/verl-project/verl/issues?q=old_logprobs_mismatch (no results)

  • Format the PR title as [{modules}] {type}: {description} (This will be checked by the CI)
  • Test

  • pytest -q tests/workers/test_old_logprobs_mismatch_on_cpu.py: 5 passed (CPU). The added test_masked_out_tokens_are_not_counted passes before and after this change: it checks that the counting semantics are kept with a partially valid mask (the existing tests all use fully valid masks); it does not check the shape behaviour.

  • TPU v7x, Qwen3.5-35B-A3B GRPO on 16 trainer + 16 rollout chips (TorchTitan TPU engine from verl-hardware-plugin, FSDP 32, 16,384-token micro-batches, 2,048 responses per step). Single runs:

  • - Profile of one actor update before this change (41.0 s, 2 micro-batches): 4 compiles inside the update, 29.1 s in total (nonzero / index and mul, 6–9 s each, two per micro-batch). The TensorCores were idle for 80% of the update, and 71% of the actor thread's Python stack samples were on the two lines of this metric. After this change the profiled update has a TensorCore duty cycle of 87.8% (19.5% before).
    - Same batch and same initial weights (step 1 replayed from a saved batch), before vs. after: rank-0 train_batch 54.5 s → 21.0 s (both include step-1 one-time costs). Exactly equal: the per-token log-probs on ranks 0 and 31, the 37 actor metrics on each of those ranks (including pg_loss and grad_norm), the per-rank update statistics of all 853 parameters on all 32 ranks, and the full updates of 39 rank-0 tensors (52.5 M elements; full updates of other ranks were not saved).
    - Steps with the same number of micro-batches, rank-0 forward/backward time: 40.3–52.0 s → 8.4–8.5 s with 2 micro-batches; 65.5 s → 12.1–19.3 s with 3. The two 8-step runs differ in their micro-batch counts per step, so their mean step times (110.46 s → 69.07 s) are not a paired comparison.

    API and Usage Example

    No API or config change.

    Design & Code Changes

  • verl/workers/utils/losses.py: masked sum instead of boolean indexing, with a two-line comment on why (+3 / −1).

  • tests/workers/test_old_logprobs_mismatch_on_cpu.py: _metrics() takes an optional response_mask; new test_masked_out_tokens_are_not_counted (+13 / −2).
  • Checklist Before Submitting

  • Read the Contribute Guide.

  • Apply pre-commit checks: pre-commit run --files verl/workers/utils/losses.py tests/workers/test_old_logprobs_mismatch_on_cpu.py: all hooks passed (ruff, ruff format, mypy, config generation, license and the other repo checks).

  • Add / Update the documentation. Not needed: no user-facing change.

  • Add unit or end-to-end test(s) to the CI workflow to cover all the code: tests/workers/test_old_logprobs_mismatch_on_cpu.py (CPU).

  • Once your PR is ready for CI, send a message in the ci-request channel in the verl Slack workspace.

  • If your PR is related to the recipe submodule, please also update the reference to the submodule commit via git submodule update --remote or cd recipe && git pull origin main. Not related.
  • Diff: 592 lines (+444 / -148) across 7 files Reviewer: jialei777 Review duration: 8942m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.50) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 592 lines diff across 7 files
    0.50
    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.382
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #8663"
    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: 50.0% (MEDIUM) · Higher = larger blast radius
    50.0%
    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: 21.6% · Safe to merge
    21.6%