Skip to content

feat(lora): opt-in PDL + atomic-add expand + adaptive kv_b block sizes - #3

Closed
zcnrex wants to merge 1 commit into
lora-splitk-pr8-basefrom
lora-kernel-pdl-atomic-blocksize
Closed

zcnrex wants to merge 1 commit into
lora-splitk-pr8-basefrom
lora-kernel-pdl-atomic-blocksize

Conversation

@zcnrex

@zcnrex zcnrex commented Jun 2, 2026 •

Copy link
Copy Markdown
Owner

LoRA Triton kernel changes — PDL (opt-in), atomic-add expand, adaptive kv_b block sizes

Scoped to one commit (9c219ae) on top of the lora-splitk-pr8 series. Base is the
pre-session commit (4b6a72a "manual tune") so the diff is only these kernel
changes (+320/−98, 10 files). It is not based on sgl-project/sglang main: this
branch is ~14 commits / 750 files behind/diverged from upstream main, so a real
upstream PR would need a rebase + extraction of just these hunks first.

What's here

1. Opt-in PDL — SGLANG_LORA_ENABLE_PDL (EnvBool, default off)
Programmatic Dependent Launch on the LoRA kernels: dense shrink (sgemm_lora_a,
incl. split-K), expand (sgemm_lora_b / qkv_lora_b / gate_up_lora_b), absorbed
kv_b correction (step_a/b_q/v), MoE virtual-expert shrink/expand, and the
routing-prep kernels. Each loads its static inputs (LoRA weights + routing metadata)
in the prologue, gdc_wait()s before the dynamic activation, and
gdc_launch_dependents()s at the end. AND'd with is_arch_support_pdl() (Hopper+;
no-op on HIP/MUSA). Helpers lora_pdl_enabled() / lora_pdl_launch_kwargs() in
triton_ops/kernel_utils.py.
Measured ~neutral E2E (Kimi-K2.5-NVFP4, tp8, bs64: −0.26%, noise), so it ships
off as a knob for further overlap work (the genuine win needs a producer-only
shrink / consumer expand split, not the blanket gate).

2. LoRA-B expand fused base-add via atomic_add
sgemm_lora_b is a single K-tile (BLOCK_K = next_pow2(rank) ≥ rank), so the K-loop
is replaced by a straight line and the base read+add+store becomes a relaxed
atomic_add. This drops the output-read stall from the critical path (~7–13% faster
expand on B200). qkv_lora_b / gate_up_lora_b adopt the same fused atomic_add.
Correct in the dense path: each (token, n) output tile has exactly one writer
(ordered after the base GEMM on the same stream).

3. Absorbed kv_b adaptive block sizes
step_a/b_{q,v} derive BLOCK_K/BLOCK_N from the real per-step dims via
next_power_of_2 (contraction K and output N), wide contractions capped at 256,
instead of fixed constants; step_b accumulates into base via atomic_add.

Testing

Correctness validated on B200 (PDL on == off, bit-identical, matches fp32 reference)
for the shrink and step_a kernels via a scratch harness (not included). No direct
unit test exists for the absorbed kv_b kernels yet — only indirect E2E coverage via
test/registered/lora/test_lora_kimi_k25_logprob_diff.py. A direct registered kernel
test (à la test_virtual_experts_kernels.py) is a recommended follow-up.

🤖 Generated with Claude Code

Investigated (this session): does a num_adapter==1 cuBLAS fast path beat these Triton expands?

For a single loaded adapter every token routes to slot 0, so the segmented SGMM
collapses to a plain dense GEMM. The pool already zeroes the B [rank:] tail, so a
cuBLAS path contracting over the full padded R is bit-identical to Triton
(max_err = 0.0000 everywhere below). Scaling is folded into x on-device and slice
offsets baked as host ints, so the path is CUDA-graph capturable (indexing the GPU
output_offset forces an illegal capture-time sync). Benched triton-vs-cublas on B200
with do_bench (cuda-graph + L2-cold). speedup = triton_us / cublas_us (>1 ⇒ cuBLAS wins):

sgemm_lora_b — cuBLAS LOSES across decode; keep Triton.

regime speedup winner
bs 1–64, N 896/2304, rank 16–64 0.45–0.87 Triton (1.2–2.4×)
bs=128 + N=2304 + rank=64 1.63 cuBLAS (compute-bound tail)

The expand's contraction K = rank is tiny (16–64), a "fat, tiny-K" GEMM that
under-utilizes cuBLAS and pays its ~3.2 µs launch/heuristic floor — more than Triton's
whole ~1.9–2.6 µs runtime. (Opposite of the LoRA-A shrink, where the large-K
contraction lets F.linear win 1.2–1.7×.) No dispatch added — sgemm_lora_b.py
is unchanged; a weights.shape[0]==1 → addmm branch was prototyped then reverted
because it regresses decode ~1.5–2×.

qkv_lora_b — shape-dependent; cuBLAS wins high-batch + prefill, loses low-bs decode.

shape bs≤32 decode bs 64–128 prefill (seg 512/2048)
TP8 GQA per-GPU, N=768 Triton (0.5–0.8) cuBLAS from bs128 (1.2–1.8×) cuBLAS (1.3–1.8×)
Kimi single-slice, N=1536 ~tie (0.9–1.3) cuBLAS (1.7–3.0×) cuBLAS (1.9–3.2×)
full GQA, N=6144 (no TP) cuBLAS from bs16 cuBLAS up to 9.8× cuBLAS (3–6×)

The fused qkv kernel tiles each length-1 decode segment independently (grid axis2 = bs,
BLOCK_S=16 wastes 15/16 rows, × n_slices × BLOCK_OUT=64 fine N-tiles), so its real
GPU time grows ~linearly with bs (45–81 µs at bs=128, N=6144) while the dense cuBLAS path
stays flat ~7 µs. A bs/seg-gated cuBLAS dispatch for qkv_lora_b is a plausible
follow-up (helps high-concurrency decode + prefill), but it is not a clean low-bs decode
win and is left out of this PR.

Benches added (standalone, not wired into CI): bench_sgemm_lora_b_cublas.py,
bench_qkv_lora_b_cublas.py (--slice-dims).


CI States

Latest PR Test (Base): ❌ Missing run-ci label -- add it to run CI tests.
Latest PR Test (Extra): ❌ Blocked -- run-ci is required first.

Triton LoRA kernel changes (all dense MLA + MoE virtual-expert paths):

- SGLANG_LORA_ENABLE_PDL (EnvBool, default off): Programmatic Dependent Launch
  on the LoRA kernels — dense shrink (sgemm_lora_a, incl. split-K), expand
  (sgemm_lora_b / qkv_lora_b / gate_up_lora_b), absorbed kv_b correction
  (step_a/b_q/v), MoE virtual-expert shrink/expand, and the routing-prep
  kernels. Each issues its static loads (LoRA weights + routing metadata) in
  the prologue, then gdc_wait() before the dynamic activation, and
  gdc_launch_dependents() at the end. AND'd with is_arch_support_pdl() (Hopper+;
  no-op on HIP/MUSA). Helpers lora_pdl_enabled() / lora_pdl_launch_kwargs() in
  triton_ops/kernel_utils.py. Opt-in: measured ~neutral E2E at bs64, so it is
  off by default and exposed as a knob for further overlap work.

- LoRA-B expand fused base-add via atomic_add: sgemm_lora_b is a single K-tile
  (BLOCK_K = next_pow2(rank) >= rank), so the loop is replaced by a straight
  line and the base read+add+store becomes a relaxed atomic_add. This drops the
  output-read stall from the critical path (~7-13% faster expand on B200);
  qkv_lora_b / gate_up_lora_b adopt the same atomic_add fused add. Correct in
  the dense path because each (token, n) output tile has a single writer.

- Absorbed kv_b kernels: block sizes derived from the real per-step dims via
  next_power_of_2 (contraction K and output N) instead of fixed constants, with
  the wide contractions capped at 256; step_b kernels accumulate into base via
  atomic_add.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added the lora label Jun 2, 2026
@zcnrex zcnrex closed this Jun 3, 2026
@zcnrex
zcnrex deleted the lora-kernel-pdl-atomic-blocksize branch June 3, 2026 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant