← Back to Pull requests

Add vocabulary-parallel logprobs, entropy and KL ops #8791

Closed
unknown closed this pull request without merging on Oct 16, 2026 07:11 UTC
View on GitHub
unknown unknown commented on Oct 09, 2026 16:45 UTC
Author

Implements PR-1 of #8790.

What

This PR adds deepspeed.sequence.vocab_parallel_ops. The ops compute per-token quantities on the local logits shard of a vocabulary-parallel LM head (#8173), without gathering the full vocabulary:

  • vocab_parallel_logprobs(logits, index, ...): log-probs of one id per token, or of top-k ids (index[..., k]). Positions equal to ignore_index return 0.

  • vocab_parallel_entropy(logits, ...)

  • vocab_parallel_kl_div(student, teacher, reverse=False, ...): KL(teacher‖student), or KL(student‖teacher) with reverse=True. Gradients flow to both inputs.
  • # Policy-gradient OPD / GRPO
    logp = vocab_parallel_logprobs(student_logits, sampled_ids, **ids)
    # Teacher top-k forward KL (verl forward_kl_topk)
    s_topk = vocab_parallel_logprobs(student_logits, teacher_topk_ids, **ids)
    kl = (teacher_topk_logp.exp() * (teacher_topk_logp - s_topk)).sum(-1)
    # Dense reverse KL (DeepSpeedExamples OPSD default)
    kl = vocab_parallel_kl_div(student_logits, teacher_logits.detach(), reverse=True, tp_group=g)
    

    How

  • Each op is a custom autograd Function. The forward does one MAX all-reduce and two SUM all-reduces of [N]-sized per-token statistics: the max, the sum-exp, and then the selected logits, entropy, or KL. The KL op packs the student and teacher statistics into the same collectives. vocab_parallel_logprobs also validates the shard layout with a small metadata all-gather per call, plus a MIN/MAX pair when the bounds are not passed explicitly. A caller that already validated the layout can pass global_vocab_size with explicit bounds to skip that collective, as with vocab_parallel_cross_entropy.

  • The global max and the log-sum-exp are kept separate, so a large common logit offset does not lose precision in fp32. Tokens whose log-probability is -inf contribute nothing to entropy, KL, or their gradients. A fully -inf row (e.g. padding) has no distribution: it returns NaN but gets zero gradient, so masking its output is safe. Entropy and KL need no shard bounds and also work when a rank's local shard is empty.

  • The backward is closed-form and local, with no communication. This follows the Megatron convention that every TP rank backprops the same loss.

  • Forward and backward process rows in chunks of at most 16M elements. Only the inputs and [N] statistics are saved.

  • Outputs are fp32 and replicated across the TP group.
  • Tests

    tests/unit/v1/sequence_parallelism/test_vocab_parallel_ops.py has 33 tests. Values and gradients are checked against a full-vocab PyTorch oracle in fp32. bf16 inputs are checked against the fp32 oracle with a tolerance.

  • logprobs (single and top-k), entropy, forward/reverse KL, and teacher top-k forward KL;

  • forced chunking, empty input, in-place masking of outputs, detached teacher, bf16;

  • a large common logit offset (oracle: the unshifted logits), partially -inf-masked vocabularies (oracle: the reduced vocabulary), and infinite KL when the target has mass on a masked token, including when that mass underflows to 0 in fp32, and zero (not NaN) gradients for ignored or masked fully -inf rows in all four ops;

  • argument validation, the trusted global_vocab_size path, and double backward raising an error;

  • TP=2 with uneven vocab shards.
  • Ran on 8× Intel Arc Pro B70 (XPU, XCCL): this file plus test_vocab_parallel_cross_entropy.py, 51 passed. TP=2 tests cover uneven shards and an empty shard on one rank.

    Memory

    1× B70, T=8192, V_local=16032 (128K / TP8), bf16. fwd+bwd peak, excluding inputs:

    | Op | Naive PyTorch | This PR | Time (naive → this PR) |
    |---|---|---|---|
    | logprobs | 1506 MiB | 443 MiB | 7.5 → 21.4 ms |
    | entropy | 3012 MiB | 586 MiB | 30.7 → 41.5 ms |
    | reverse KL | 3012 MiB | 698 MiB | 37.2 → 70.2 ms |
    | forward KL | 2510 MiB | 634 MiB | 24.8 → 64.3 ms |

    Limitations

  • Speed: up to ~2.9× slower than naive eager in these measurements, because softmax is recomputed in backward, work is chunked, and zero-probability tokens are masked explicitly. This PR trades time for memory and numerical robustness.

  • The local logits shard is still materialized. A fused-linear variant is future work.

  • Only first-order gradients are supported. Double backward raises an error.

  • Student and teacher must share the same TP group and vocab sharding.

  • No built-in reduction, mask, or temperature. These are applied by the caller.
  • Diff: 550 lines (+412 / -138) across 6 files Reviewer: jinyouzhi Review duration: 9505m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 550 lines diff across 6 files
    0.48
    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.378
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #8791"
    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: 48.1% (MEDIUM) · Higher = larger blast radius
    48.1%
    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: 19.0% · Safe to merge
    19.0%