Repository navigation
[Spec] Add LFM2 and LFM2-MoE DSpark speculative decoding support - #31041
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
f31994a to
83e1670
Compare
83e1670 to
0cd3331
Compare
|
The -from typing import Callable, Optional
+from typing import Optional( |
|
@vincentzed thanks for the pointer - fixed in ffc6308, the unused Could you add the |
|
/tag-and-rerun-ci |
|
Lint is green now — thanks. Tagged
Cause: this PR adds # auto + no extra-buffer support (Lfm2) -> no_buffer + overlap disable
self.assertEqual(
_mamba_radix_cache_resolution(_view("Lfm2ForCausalLM")),
{"uses_mamba_radix_cache": True,
"mamba_radix_cache_strategy": "no_buffer",
"disable_overlap_schedule": True},
)Suggested fix — keep the negative branch covered by swapping in an arch that's in # auto + no extra-buffer support -> no_buffer + overlap disable
self.assertEqual(
_mamba_radix_cache_resolution(_view("ZayaForCausalLM")),
{"uses_mamba_radix_cache": True,
"mamba_radix_cache_strategy": "no_buffer",
"disable_overlap_schedule": True},
)
# Lfm2 now supports the extra buffer (DSpark TARGET_VERIFY requires it)
self.assertEqual(
_mamba_radix_cache_resolution(_view("Lfm2ForCausalLM")),
{"uses_mamba_radix_cache": True,
"mamba_radix_cache_strategy": "extra_buffer"},
)One other thing: this branch predates #30780 (merged earlier today, |
|
@tugot17 can you rebase with main? I think some changes in this PR is already on main |
52da142 to
83dd270
Compare
|
Rebased onto main, two commits now. You were right that some of it had landed. #32828 added the same post-verify hybrid state commit generically, and their version does a bit more than mine, so I dropped my I also fixed The old head also predated Re-ran the dense numbers after the rebase to be safe: 1.2B + 5L draft, bs=1, math500, accept 4.98, 2.20x. Same as before. |
There was a problem hiding this comment.
Could we skip this wiring and just teach the existing DSpark kernel interleaved RoPE?
https://github.com/sgl-project/sglang/blob/1df78c2cf12caab93813cdb6f779b436043554a5/python/sglang/kernels/ops/speculative/dspark/fused_kv_write.py
| "Lfm2ForCausalLM", | ||
| "Lfm2MoeForCausalLM", |
There was a problem hiding this comment.
I don't think ShortConvAttn (e.g see Inkling) support extra buffer.
There was a problem hiding this comment.
You're right, both lines dropped and the test file reverted; forcing extra_buffer on does corrupt (2 of 3 prompts change text at temp 0 on a prefix-cache hit).
| @@ -84,6 +183,24 @@ def inject_target_hidden( | |||
| return | |||
|
|
|||
| with torch.inference_mode(): | |||
| if self._fused_kv_helper is not None: | |||
There was a problem hiding this comment.
This will introduce a regression for Qwen3 Dspark drafters. Previous it takes _build_fused_kv_write_bundle → one F.linear + one fused_kv_norm_rope_write that stores directly into the KV pool. Now it replaces that with torch.mm into a workspace, a separate norm/rope kernel, then a per-layer set_kv_buffer pass that reads the workspace back — an extra full write+read of K/V plus N launches per verify step.
There was a problem hiding this comment.
Reverted, so Qwen3 keeps the bundle path; taught the write kernel the interleaved pairing instead, and IS_NEOX=True is bit-identical to the current kernel.
_build_fused_kv_write_bundle bailed on interleaved-rotary drafts, so they fell back to the per-layer eager KV path. Teach the write kernel both rotation pairings instead: neox pairs dims (i, i + D/2), interleaved pairs (2i, 2i + 1), and the cos/sin cache layout is the same for both. Since the cache is identical either way, the existing cross-layer cache check cannot catch a style mismatch, so check the style per layer too. The neox path is unchanged: IS_NEOX=True is bit-identical to the previous kernel on random inputs.
Adds the TARGET_VERIFY short-conv path (per-step conv windows recorded into the speculative tape so the state can roll back to the accept boundary), the DFlash aux-hidden capture hooks, and a dedicated Lfm2DSparkDraftModel arch so the LFM2 draft can later diverge from other DSpark drafts without touching shared classes. Current LFM2 draft checkpoints are attention-only Qwen3-style GQA with interleaved RoPE, so the draft model itself is a thin subclass.
83dd270 to
c26ae04
Compare
|
Rebased on main, down to 5 files and +221/-16; the write kernel handles both pairings now, and dspark.py checks the style per layer since the cos/sin cache is the same either way. |
|
/rerun-test test_dspark_stacked_ctx_kv_parity.py test_dspark_kernel_parity.py test_basic_sanity_dspark.py |
|
Results for 🚀 🚀 |
|
@kpham-sgl Looks unrelated. The failing file is
|
|
/rerun-failed-ci one |
…-project#31041) Co-authored-by: Khoa Pham <[email protected]>
… token, the same tokens LiquidAI/LFM2.5-2.6B-DSpark (@458cedab, 327.7M bf16, downloaded by the phone, read in place) drafts 7 tokens a step from the target's residual streams after layers 2, 9, 17, 21, 27; the target checks them in one 8-row pass and keeps the longest matching prefix plus its own next token, rewinding the conv states to the last accepted position. On the Galaxy Z Fold, 64 tokens after the 415-token prompt: 19.92 / 19.90 / 19.91 ms/token against plain greedy decode's 27.52 / 27.77 / 27.53 (interleaved, idle phone), 4.5 tokens a target pass, the same 64 tokens in every run. - lfm2/dspark_ref.py: the drafter in PyTorch from SGLang's implementation (sgl-project/sglang#31041); --simulate gives the acceptance along a recorded run (the phone's accepts match it step for step), --export its manifest. - lfm2/dspark.mojo, lfm2/kernels/dsk.mojo: the drafter on the GPU, the target's verify, the few-row products: 8 weight rows a workgroup, 4 groups of 16 lanes splitting x's rows, FMA chains (0.69 ms for gate/up at 8 rows, against 2.35 on cooperative matrices and 0.38 for decode's 1 row; lfm2/bench_rows.mojo); an int8 copy of the Q6_K head for them. - lfm2/model.mojo: verify(), commit(), taps; lfm2/lfm2.mojo: LFM2_DSPARK (speculate()); lk.short_conv keeps a verify's B x rows; lfm2/stage_phone.sh re-stages a wiped lab phone; android/build.py lfm2bench. Co-Authored-By: Claude Opus 5.5 <[email protected]>
What
Adds DSpark speculative-decoding support for the LFM2 hybrid (ShortConv + attention) family: both the dense LFM2 and MoE LFM2-MoE targets. Follow-up to #30261 (DSpark), now that it has landed on
main.Two commits:
IS_NEOXconstexpr selecting the pairing (neox:(i, i+rotary/2); interleaved/GPT-J:(2i, 2i+1)); no change for existing neox drafts. LFM2 draft exports rotate interleaved and need this.Lfm2DSparkDraftModelarch (a thinDSparkDraftModelsubclass, mirroringQwen3DSparkModel) so the LFM2 draft can gain LFM2-specific layers - e.g. ShortConv/conv1d - later without touching the shared classes. Interleaved RoPE and the confidence head come from the checkpoint config; the kernel change handles the rotation. Only the target side needs code beyond that: aux-hidden capture + ShortConv block verify/rollback inlfm2.py/lfm2_moe.py, hybrid-state commit after verify in the DSpark worker (mirrors the DFlash worker so decoding stays lossless), routing the draft KV injector through the fused-KV path, and marking the LFM2 archs eligible for theextra_buffermamba radix strategy that spec-v2 rollback needs.Net: 7 files, +422/-23, no changes to existing DSpark behavior.
Base LFM2-MoE serving parity (the attention-backend / radix-cache override tables for
Lfm2MoeForCausalLM) is independent of DSpark and is in its own PR: #30780. Pair with it for the 8B target's serving defaults.Speedup
1xH100 80GB, bs=1, temperature 0, block size 7, accept length / speedup vs the same server minus spec flags:
Floor is short chat on the small dense target (~1.05x); ceiling is long reasoning on the MoE (2.3-2.4x, accept near the block-7 limit). Accuracy at parity with baseline on every scoreable bench.
Note: interleaved RoPE (commit 1) is not optional for these drafts. Serving them neox collapses accept length to ~1.9 vs ~5.1 interleaved.
Public draft checkpoints
LiquidAI/LFM2.5-1.2B-Instruct)LiquidAI/LFM2.5-8B-A1B)Testing
Validated in serving on 1xH100 across the benches above (dense + MoE, draft depths 2L/3L/5L). The MoE ShortConv decode uses the same CUDA
causal_conv1d_updatekernel as dense (an earlier triton swap was removed after confirming identical accept, 5.936 both ways). Happy to add a gated CI test undertest/registered/spec/dspark/against the public checkpoints.Supersedes #30776, which was opened against the DSpark PR branch before it merged; rebased onto
main.CI States
Latest PR Test (Base): ❌ Run #33310994737
Latest PR Test (Extra): ❌ Run #33310994555
Latest PR Test (AMD ROCm 7.2): ❌ Run #33310994595