← Back to Pull requests

Add Triton kernels for vocab-parallel logprobs, entropy and KL #8838

Closed
unknown closed this pull request without merging on Oct 21, 2026 16:05 UTC
View on GitHub
unknown unknown commented on Oct 16, 2026 14:47 UTC
Author

Follow-up to #8791 (RFC #8790). The vocab-parallel logprobs / entropy / KL ops ran each row chunk as a dozen separate eager fp32 kernels. On the same local shard they were about 2–2.4× slower than a dense log_softmax, which offsets most of the memory benefit when they are used for OPSD / RL losses (e.g. deepspeedai/DeepSpeedExamples#1019).

Changes


  • New deepspeed/ops/triton_ops/vocab_parallel_softmax.py: Triton kernels with one program per row, looping over the local vocabulary in blocks. The module has no communication.

  • - Local max and sum-exp are computed in a single read. They are then rescaled to the global max, so TP still needs only one MAX and one SUM all-reduce.
    - Fused forward and backward for entropy and KL, and a fused backward for logprobs (the top-k one-hot terms are added in fp32 before the single cast, as before).
  • deepspeed/sequence/vocab_parallel_ops.py dispatches to these kernels on CUDA/XPU when the inputs are contiguous and deepspeed.HAS_TRITON is true. Otherwise it runs the existing eager path, which is unchanged. Collectives and the autograd structure are the same.

  • Docs: a note in autotp-training.md.
  • Performance


    Intel Arc Pro B70, bf16, 75968-column shard (Qwen vocab / TP2), forward + backward:

    | op | N | master | this PR | extra peak memory |
    |---|---|---|---|---|
    | reverse KL | 8192 | 340.8 ms | 15.5 ms | 510 MiB → ~0 |
    | forward KL | 8192 | 313.0 ms | 15.5 ms | 446 MiB → ~0 |
    | entropy | 8192 | 205.5 ms | 9.1 ms | 335 MiB → ~0 |
    | logprobs (k=1) | 8192 | 99.5 ms | 7.2 ms | 192 MiB → ~0 |
    | reverse KL | 2048 | 85.1 ms | 4.0 ms | |

  • KL is within ~10% of the 608 GB/s memory-bandwidth floor.

  • Logprobs with a large top-k is slower (k=256: 35 ms) but still about 3× faster than master.

  • For reference, torch.compile of the eager chunk functions reached 26.9 ms for reverse KL (N=8192), so I chose hand-written kernels.
  • Tests


  • New test_triton_kernels_match_eager_path (fp32 / bf16 × logprobs / entropy / forward KL / reverse KL). On an accelerator it compares the Triton outputs and gradients with the CPU eager path. Cases: multiple kernel blocks, partially masked vocabulary, all--inf rows, support mismatch with underflowing probability, large offsets, duplicate / ignored top-k ids, and a fully ignored row.

  • - I checked that it fails when the kernel's support-mismatch inf is removed.
  • The TP=2 tests (uneven and empty shards) now run through the Triton path.
  • Executed on 2× Intel Arc Pro B70 (XPU), torch 2.14.0.dev+xpu, Triton 3.8.0:

  • tests/unit/v1/sequence_parallelism/test_vocab_parallel_ops.py: 41 passed.

  • The existing single-process semantic tests, with XPU as the default device (so they run through Triton): 30 passed.
  • Update: single-pass entropy/KL forward (by @0z5a)

    The tables above were measured on the first commit. @0z5a's e8038bed3 reads each logit once in the entropy/KL forward, using online weighted statistics. Both commits were re-measured with the same script on 1× B70: bf16, a 75968-column shard, forward + backward, median of 20 runs.

    | op | N | 99f054192 | head 97d4f66df |
    |---|---|---|---|
    | reverse KL | 8192 | 18.7 ms | 14.6 ms |
    | forward KL | 8192 | 18.7 ms | 14.6 ms |
    | entropy | 8192 | 9.2 ms | 7.1 ms |
    | logprobs (k=1) | 8192 | 7.4 ms | 7.4 ms |
    | reverse KL | 2048 | 4.8 ms | 3.9 ms |

    Extra peak memory, excluding inputs and gradients, is below 1 MiB on both commits.

    Numerics vs the first commit. Compared on 1× and 2× B70 (TP=1, TP=2), fp32 and bf16. Cases: 512×32000, 256×151936, and 64×32000 with a +1000 offset and -inf masks. Each output and gradient was also compared with an fp64 CPU reference.

  • logprobs: outputs and gradients are bitwise identical.

  • entropy / KL: not bitwise identical, because the summation order changed. Forward outputs differ by at most 35 ULP. The forward is closer to fp64 on almost every case: e.g. fp32 entropy relative error drops from 3e-7 to 1.5e-7, and KL from about 6–9e-7 to 3–8e-7. Gradient errors stay at the same level, and bf16 gradient errors are unchanged (about 2e-3).

  • There are no inf/NaN placement differences.
  • Contribution checklist

    Related work. This is a performance follow-up to #8791 (RFC #8790). A search for vocab-parallel Triton kernels and entropy/KL found no overlapping PR.

    Commands and results. Hardware and software: 2× Intel Arc Pro B70 (XPU, XCCL), Level Zero driver 1.15.39122 (compute runtime 26.27.39122.11), torch 2.14.0.dev20260804+xpu, Triton 3.8.0.

    pytest -p no:randomly tests/unit/v1/sequence_parallelism/test_vocab_parallel_ops.py
    

    At head 8f463213f: 49 passed on 2× B70; with DS_ACCELERATOR=cpu, 33 passed and the 16 Triton tests are skipped.

    Compatibility matrix.

    | | Validated | Not validated |
    |---|---|---|
    | Precision | fp32, bf16 | fp16 |
    | Parallelism | TP=1, TP=2 (uneven and empty shards) | TP>2 on device |
    | Layout | Contiguous rows, plus row_layout strides from e8038bed3 | |
    | Accelerator | XPU (Triton); CUDA (Triton, 2× H100, validated by @0z5a); CPU (eager fallback, Triton tests skipped) | |

    ZeRO and offload do not interact with these ops.

    AI assistance. This PR was developed with AI assistance (GitHub Copilot CLI). I reviewed every changed line and ran the tests above.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jinyouzhi Review duration: 7277m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    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.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #8838"
    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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 20.5% · Safe to merge
    20.5%