Skip to content

[Perf] logits: Cast bf16 logits into the fp32 buffer with a Triton kernel - #46167

Merged
hnyls2002 merged 1 commit into
mainfrom
lsyin/dspark-verify-logits-copy
Oct 23, 2026
Merged

hnyls2002 merged 1 commit into
mainfrom
lsyin/dspark-verify-logits-copy

Conversation

@hnyls2002

@hnyls2002 hnyls2002 commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

When a model's lm_head returns bf16 logits and the forward has a shared fp32 logits buffer (CUDA graph decode, speculative verify), _copy_logits_to_buffer fills the buffer with logits_buffer.copy_(logits). On Blackwell this dtype-casting copy runs well below memory bandwidth, and at speculative-verify batch sizes (many rows x a full vocabulary) it shows up as one of the larger single kernels of a decode step.

Modifications

  • softcap.py: softcap_inplace_logits_kernel gains an APPLY_SOFTCAP constexpr; the new cast_to_float32_logits(logits, output) uses the same launcher without the softcap.
  • activation/__init__.py: register activation.cast_to_float32_logits next to softcap_to_float32_logits.
  • logits_processor.py: in the buffered branch of _copy_logits_to_buffer, CUDA fp16/bf16 logits are written by the new kernel; fp32 logits and the unbuffered path are unchanged.

No numerical change: the fp16/bf16 to fp32 conversion is exact, and the buffer contents are byte-identical to the copy_ result.

Verification

  • Byte-identical fp32 buffer against copy_ for bf16 and fp16 inputs, contiguous and vocab-padded (sliced) layouts, at several row counts, with inf, NaN, -0.0 and tied maxima in the inputs; argmax of the buffer unchanged.
  • test/registered/rl/test_fp32_lm_head.py passes on GB300 (covers the buffered path, vocab slicing, softcap and CUDA graph replay).
  • Micro benchmark of _copy_logits_to_buffer at speculative-verify shapes on GB300 under CUDA graphs: faster at every size tested.

CI States

Latest PR Test (Base): 🚫 Run #40030308997
Latest PR Test (Extra): ❌ Run #40030309098
Latest PR Test (AMD ROCm 10): 🚫 Run #40030309145

@hnyls2002

Copy link
Copy Markdown
Collaborator Author

/rerun-test registered/rl/test_fp32_lm_head.py registered/unit/kernels/test_kernels_namespace.py

@hnyls2002

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

🚀 1-gpu-5090 (1 test): ✅ View workflow run

cd test/ && python3 registered/rl/test_fp32_lm_head.py

🚀 ubuntu-latest (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/kernels/test_kernels_namespace.py

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Oct 23, 2026
@hnyls2002
hnyls2002 merged commit f05ad5e into main Oct 23, 2026
181 of 240 checks passed
@hnyls2002
hnyls2002 deleted the lsyin/dspark-verify-logits-copy branch October 23, 2026 22:06
detain pushed a commit to detain/sglang that referenced this pull request Oct 29, 2026
…fer (sgl-project#45631)

Port of merged upstream f1e26dc.

Deviation: the muse_spark.py hunk was dropped (modify/delete conflict).
python/sglang/srt/models/muse_spark.py entered upstream main via
sgl-project#44666, which our stack never merged; the file is absent here and
nothing in-tree subclasses _compute_lm_head, so the drop is
trivially disjoint. If the muse-spark runtime is ever ported, its
_compute_lm_head override must gain the out= kwarg from the PR head.

logits_processor.py merged clean against our drifted copy: the
missing upstream gathered-softcap/defer_verify_gather region sits
between the hunks and the local pcie_ipc=True kwarg (entry 162)
is elsewhere in the file; zero conflict markers.

Part 1 of 3 of the logits trilogy (do not take without sgl-project#45855 and
sgl-project#46167: this commit alone can round FP32 output differently than
the unbuffered path, fixed by sgl-project#45855, and the plain copy is slow,
fixed by sgl-project#46167).

(cherry picked from commit f1e26dc)
(cherry picked from commit d60560f25ae068d209f885f6639fdba2f6c9cda5)
detain pushed a commit to detain/sglang that referenced this pull request Oct 29, 2026
…rnel (sgl-project#46167)

Port of merged upstream f05ad5e.
Closes the logits trilogy with sgl-project#45631 + sgl-project#45855 (take all three).

Union resolutions / deviations:
1. logits_processor.py import block: took cast_to_float32_logits as a
   standalone import (our per-name import idiom) and DROPPED the
   gathered_softcap_logits import line from the conflict's incoming
   side -- that symbol belongs to the upstream gather-fused/deferred-
   verify region our stack never merged (same as at sgl-project#45631's base
   drift; not in our kernels copy).
2. kernels/ops/activation/softcap.py: conflict carried the PR's new
   cast_to_float32_logits wrapper welded to the upstream-only
   _gathered_softcap_logits_kernel/gathered_softcap_logits block;
   kept only the wrapper, dropped the gather block (same reason as 1).
   The APPLY_SOFTCAP constexpr hunks applied clean around our local
   softcap_out autotune surface (untouched).
3. kernels/ops/activation/__init__.py: SILENT auto-merge danger --
   git applied the registration verbatim with capabilities=CUDA_ONLY,
   but our refactor removed that import (file uses local
   _CUDA = frozenset({CapabilityRequirement.CUDA})). Changed to
   capabilities=_CUDA; value-identical to upstream CUDA_ONLY
   (python/sglang/kernels/spec.py:181), verified by import +
   test_kernels_namespace 76P.

(cherry picked from commit f05ad5e)
(cherry picked from commit 6cec0a80a49d18822eeea8bcca9ae9004aea37ed)
detain added a commit to detain/sglang that referenced this pull request Oct 29, 2026
…5631/sgl-project#45855/sgl-project#46167 (entries 271-274)

ONE docs commit: the logits trilogy is a CONTRACT UNIT -- recorded as
'never take 45631/45855 without 46167' per entry 274. Also on record:
sgl-project#44124's STRING_PARAM_TYPES union + entry-159/160 retention + the
streaming-fast-path call-site adaptation (141F before amend); sgl-project#45631's
muse_spark hunk DROP with the sgl-project#44666-then re-check note; sgl-project#46167's
wrapper-only softcap keep and the SILENT-AUTO-MERGE hazard fix
(CUDA_ONLY -> local _CUDA, value-identical, kernels_namespace 76P proves).

Battery EXACT: function_call 652P/1964sub on top of batch-1+2, numeric
4P/156sub, kernels_namespace 76P, spec dir 335P/11S/180sub (batch-1 pin
survives all 14 picks). FLAGGED: sampling 223P/2S/43sub measured vs 263P
predicted -- predicted number irreproducible on the worker's OWN tip with
diff-0 across waves (prediction-vs-method gap, not landed code); logits
56P/7S unverifiable (pre-existing module-level get_device() CPU collect
failure + all 4 GPUs busy with the live prod server) -- folded into the
trilogy's GPU-debt block.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jit-kernel run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant