← Back to Pull requests

Add vocabulary-parallel fused linear cross entropy #8792

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

Follow-up to #8173.

Motivation

After #8173, a vocabulary-parallel LM head still materializes [tokens, vocab / tp] logits on every rank. Its CE backward also keeps FP32 softmax buffers of the same size. For long sequences and large vocabularies, this is the largest activation in the step. For example, at 256K vocab, TP2, and 16K tokens, the loss path alone peaks at about 28 GiB per rank.

Existing fused linear CE kernels do not cover this case. Liger's FLCE is not TP-aware (linkedin/Liger-Kernel#826, linkedin/Liger-Kernel#868). PyTorch's F.linear_cross_entropy chunks only over the batch dimension and takes no process group.

What

deepspeed.sequence.linear_cross_entropy.vocab_parallel_linear_cross_entropy(hidden, weight, target, bias=None, tp_group=None, sp_group=None, vocab_start_index=None, vocab_end_index=None, ignore_index=-100, reduction="mean", chunk_size=None) (reduction is "none", "sum" or "mean")

  • It replaces head(hidden) plus the vocab-parallel CE, taking weight/bias from VocabParallelLinear. Uneven shards are supported.

  • Forward walks over token chunks. Each chunk computes h @ W_localᵀ straight into FP32 and runs one MAX all-reduce plus one SUM all-reduce (sum-exp and target logit packed together). Only the per-token max and sum-exp are kept.

  • Backward walks over local vocabulary blocks and recomputes each block's logits, with the max subtracted in the GEMM epilogue. Each dW block then comes from one GEMM over all tokens. dh is accumulated in FP32 and all-reduced over TP, matching the column-parallel head's backward. Backward needs no other collectives.

  • No FP32 copy of dW is held, and backward is an ordinary autograd backward. The cost is one extra logits GEMM.

  • By default, each FP32 logits block is about the size of hidden. chunk_size trades memory for speed.
  • Validation

    tests/unit/v1/sequence_parallelism/test_vocab_parallel_linear_cross_entropy.py has 24 tests. Values and gradients are checked against F.linear + F.cross_entropy on the full vocabulary. Coverage:

  • none/sum/mean reduction with a non-uniform upstream gradient, with and without bias, and forced chunking;

  • all tokens ignored, a frozen weight, and input validation;

  • an FP16 product that overflows before a cancelling bias, as F.linear handles it;

  • FP16 with a 65536 loss scale, and BF16 activations with FP32 parameters under autocast;

  • BF16 gradients summed over many chunks and vocabulary blocks;

  • TP=2 with uneven vocab shards, in FP32 and BF16, with default and forced chunking.
  • Ran on Intel Arc Pro B70 (XPU, XCCL): 30 passed. test_vocab_parallel_cross_entropy.py still passes (18 passed).

    Memory and time

    Measured on 1× Intel Arc Pro B70, single-rank shard, H=4096, BF16. Peak is fwd+bwd and excludes inputs, but includes dW/dh. The baseline is head.weight logits followed by vocab_parallel_cross_entropy.

    | Vocab / TP | Tokens | Baseline | This PR | Time (baseline → this PR) | torch linear_cross_entropy (no TP, reference) |
    |---|---|---|---|---|---|
    | 128K / TP8 | 8K | 1757 MiB | 569 MiB | 38.9 → 44.0 ms | 269 MiB, 30.3 ms |
    | 128K / TP8 | 16K | 3508 MiB | 1008 MiB | 78.1 → 84.1 ms | 412 MiB, 59.7 ms |
    | 128K / TP2 | 8K | 7014 MiB | 945 MiB | 158.8 → 173.5 ms | 698 MiB, 124.1 ms |
    | 128K / TP2 | 16K | 14028 MiB | 1385 MiB | 316.8 → 331.4 ms | 892 MiB, 229.2 ms |
    | 256K / TP2 | 8K | 14336 MiB | 1472 MiB | 314.2 → 370.3 ms | 1222 MiB, 372.2 ms |
    | 256K / TP2 | 16K | 28672 MiB | 1921 MiB | 633.6 → 678.9 ms | 1416 MiB, 503.7 ms |

    Peak memory drops by 3–15×, and time is 4–18% higher because backward recomputes the logits. The first design computed gradients during forward and accumulated dW in FP32 over token chunks. That needed an FP32 dW buffer plus a cast copy, which came to about 3× the size of dW: 3.6 GiB peak at 256K / TP2.

    Follow-ups

  • Integration path. HF computes logits before loss_function, so the existing loss hook cannot intercept them. The options are to patch the CausalLM forward behind vocab_parallel_ce_backend: "fused_linear", or to document the functional API only.

  • End-to-end TP training test with VocabParallelLinear and SimpleModel, plus multi-rank e2e memory numbers.

  • Interaction with ZeRO-2/3 gradient hooks on the LM-head weight.

  • Close the remaining speed gap to torch.linear_cross_entropy, e.g. by fusing the softmax-gradient elementwise ops.
  • Limitations

  • The fused FP32-output GEMMs (out_dtype=torch.float32) are used where the backend supports them, such as XPU. Other backends, such as CPU, fall back to unfused ops.

  • Under torch.autocast, the GEMMs run in the autocast dtype, as F.linear would.
  • cc @delock @PKUWZP

    Diff: 591 lines (+443 / -148) across 7 files Reviewer: jinyouzhi Review duration: 9773m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 591 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%
    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.382
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #8792"
    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: 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: 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.7% · Safe to merge
    19.7%