Skip to content

Move the pre-2026.09.30 kernels to csrc_legacy/ - #18

Merged
DarkSharpness merged 3 commits into
sglfrom
legacy-split
Oct 20, 2026
Merged

DarkSharpness merged 3 commits into
sglfrom
legacy-split

Conversation

@DarkSharpness

@DarkSharpness DarkSharpness commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

(auto generated by Claude)

First step of splitting this fork so it can ship as a standalone wheel. This commit is the move alone -- the build is wired up in later commits, which land on this same branch, so the branch does not build at this commit.

Why

Upstream's 2026.09.30 release (deepseek-ai#229) rewrote the DeepSeek-V4.1 kernels and deleted everything else: NVIDIA Hopper, the V3.2 / V3.2-no-RoPE / V4 KV cache formats, and dense MLA decoding. SGLang still serves all of them.

Keeping both sets in one tree means merging upstream by hand forever. Moving the deleted ones to csrc_legacy/ and freezing them leaves csrc/ free to track upstream verbatim, so a sync becomes a directory replacement rather than a merge. Carrying this through 2e5429f -> 0f11e178 (4 commits, 605 lines) was zero conflicts.

What is in here

csrc_legacy/ is self-contained: its own params.h, kv_cache_format.h, utils.h and defines.h, its own API layer, its own device_stream.h. It shares no source with csrc/, so neither side constrains the other. Two systematic edits are applied on the way across:

  • A legacy:: prefix on the sm90 / sm100 / smxx namespaces. Upstream reused those names, so without it the two extensions could not be loaded into one process; it also makes a stack frame say which tree it came from.
  • The conversion to PyTorch's stable C++ ABI (torch::stable::Tensor, STD_TORCH_CHECK, STABLE_TORCH_LIBRARY). A wheel's compatibility is the minimum over its extensions, so one abi3 extension beside a CPython-locked one would still lock the wheel.

Nothing is carved out: the V4.1 instantiations and the pre-release block-split V4.1 layout come across too, so the snapshot stays complete and self-consistent. Removing kernels that csrc/ appears to supersede once left the API layer dispatching to instantiations that were no longer there, which links cleanly and fails only at dlopen.

The pybind module entries are the only thing left behind. Registration is STABLE_TORCH_LIBRARY now, and the SGLang compatibility shim's historical signatures, int64 conversions and choice of base-2 statistics are wrapper work that belongs in Python.

How to review

Both edits above are mechanical, so git reads 134 of the 150 files as renames and the per-file diff is the namespace prefix and the ABI conversion, nothing else. What is worth real attention is the handful of files that are not a rename:

  • csrc_legacy/ops.cpp -- the op schemas for this side's namespace, flash_mla_legacy.
  • csrc_legacy/sched_meta.cpp -- the scheduler entry point SGLang hoists out of the decode loop, ported from the fork's csrc/python_api.cpp.
  • csrc_legacy/api/interfaces_legacy.h -- the entry-point declarations.
  • csrc_legacy/api/common.h, csrc_legacy/device_stream.h, csrc_legacy/kernels/sm100/prefill/dense/, and allocate_scale_factor -- taken from upstream's Make FlashMLA CPython and LibTorch ABI stable deepseek-ai/FlashMLA#230, which had already converted the same code, rather than converted again here.

csrc_legacy/README.md records the provenance in full: the upstream commit the snapshot starts from, every fork commit on top of it, and each deliberate deviation.

What follows on this branch

Replacing csrc/ with upstream 0f11e178 verbatim (Ascend kernels dropped, their macros kept), csrc_ops/ for the ops upstream does not register, the two-extension build, and the Python layer that dispatches on use_legacy.

Already verified on that full tree: both extensions compile and load, and sparse decoding over V4.1 (current) and V4 / V3.2-no-RoPE / V3.2 (legacy) plus sparse prefill on both sides match a plain-torch reference. An earlier merged form of the same legacy tree was bitwise equal to the pre-rebase build on 24/24 B200 and 17/17 H200 cases.

@DarkSharpness
DarkSharpness force-pushed the legacy-split branch 3 times, most recently from 0447d2a to 0789cf0 Compare October 20, 2026 09:30
DarkSharpness and others added 2 commits October 20, 2026 05:34
Upstream's 2026.09.30 release (deepseek-ai#229) rewrote the
DeepSeek-V4.1 kernels and deleted everything else: NVIDIA Hopper, the V3.2 /
V3.2-no-RoPE / V4 KV cache formats, and dense MLA decoding. SGLang still serves
all of them.

Keeping both sets in one tree would mean merging upstream by hand forever.
Moving the deleted ones to csrc_legacy/ and freezing them leaves csrc/ free to
track upstream verbatim, so a sync becomes a directory replacement rather than
a merge. The next commit brings in upstream's csrc/ and the build that uses
both; this one is the move alone, so the branch does not build here.

csrc_legacy/ is self-contained: its own params.h, kv_cache_format.h, utils.h
and defines.h, its own API layer, its own device_stream.h. It shares no source
with csrc/, so neither side constrains the other. Two systematic edits are
applied on the way across:

- A `legacy::` prefix on the sm90 / sm100 / smxx namespaces. Upstream reused
  those names, so without it the two extensions could not be loaded into one
  process; it also makes a stack frame say which tree it came from.
- The conversion to PyTorch's stable C++ ABI (torch::stable::Tensor,
  STD_TORCH_CHECK, STABLE_TORCH_LIBRARY). A wheel's compatibility is the
  minimum over its extensions, so one CPython-locked extension here would undo
  the abi3 build of the current kernels.

The conversion needs care in one place that is invisible in a diff: TORCH_BOX
reinterprets a boxed argument slot according to the C++ parameter type instead
of converting it. Every entry point here therefore takes int64_t for a schema
`int` and double for a schema `float`, narrowing explicitly in the body where a
kernel's parameter struct keeps a smaller field. Getting this wrong is silent --
`const float softmax_scale` against a schema `float` reads the low 32 bits of a
double, so 1/sqrt(576) arrives as 1.466e13 and every output is NaN.

The tests for these kernels move to tests/legacy/, next to the kernels they
cover. test_sparse_prefill_stats_units.py is renamed test_sparse_prefill_stats.py.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Brings csrc/ up to upstream 0f11e17 ("Make FlashMLA CPython and LibTorch ABI
stable", deepseek-ai#230) and wires up the build and the Python API
that reach both it and the frozen tree from the previous commit.

csrc/ is byte-identical to upstream apart from the Ascend NPU target, which
this fork does not carry: csrc/ascend_kernels/, csrc/api/ascend_stream.{cpp,h},
kerutils' device/ascend/ and kerutils_for_ascend_npu.h, and the Ascend docs are
removed, and nothing else differs. The inert KERUTILS_IS_BUILD_ON_ASCEND guards
stay, to keep the diff against upstream minimal. The vendored dependencies move
with upstream: csrc/cutlass -> csrc/3rdparty/cutlass (at upstream's pin
4faf1a1) and csrc/kerutils -> csrc/3rdparty/kerutils.

Two extensions, two op namespaces, so that csrc/ can stay a verbatim copy:

  flash_mla.cuda        csrc/,        sm_100a + sm_103a
  flash_mla._C_legacy   csrc_legacy/, sm_90a + sm_100a + sm_103a

Both build against PyTorch's stable C++ ABI (TORCH_TARGET_VERSION 2.10) and
CPython's Limited API, so one abi3 wheel serves every PyTorch 2.10+ / CPython
3.10+ pair. FLASH_MLA_BUILD_LEGACY=0 ships only the current kernels.

csrc_ops/sched_meta.cpp registers get_mla_decoding_metadata, an op upstream
does not have, which SGLang uses to hoist the decode scheduler out of its loop.
It lives outside csrc/ so that directory can still be replaced wholesale.

The CUTLASS dense FMHA prefill is left out of the default build behind
FLASH_MLA_BUILD_DENSE_PREFILL=1 -- half the current extension's object code for
an op SGLang never calls. Its schemas still come from upstream's api.cpp, so
the ops exist and raise when called rather than failing the load. The legacy
copy of the same kernels is byte-identical and never built.

sm100::decode::sparse::head64::flash_fwd_splitkv_mla_fp8_sparse_kernel is
exempted from the post-build spill check: its two split-KV instantiations
really do spill (ptxas reports 20 bytes spill stores / 28 bytes spill loads in
a 16-byte stack frame) on CUDA 13.2, 13.3 and 13.4 alike. It is upstream's
code and upstream's check, upstream's own build fails it, and both kernels were
verified bit-correct on sm_103a hardware. Exempted by name rather than by
raising the baseline, so a real spill elsewhere still fails the build.

On the Python side every entry point takes use_legacy=False; passing True
routes the same call to the frozen kernels, so there is one surface instead of
the compat shim the fork used to compile in beside it. _loader.py loads both
extensions with torch.ops.load_library -- neither defines PyInit_*, since both
register through STABLE_TORCH_LIBRARY during static initialisation, which is
what makes the abi3 wheel possible -- and exposes get_ops(use_legacy), which
takes no default so every call site says which kernels it means.

What the flag reaches beyond the namespace: dense MLA decoding and the fork's
SM90 dense fp8 kernel (via indices=None); kv_format, which disambiguates the
V3.2 / V3.2-no-RoPE / V4 caches and is rejected on the current side where there
is nothing to disambiguate; enable_batch_invariant, which is current-only; and
return_log2_lse on sparse prefill, where None gives natural-log units, False
returns None for both statistics, and True gives base 2 and is legacy-only.

is_fp8_kvcache becomes Optional[bool], inferred from indices. Upstream defaults
it to True, which is right for a build that has only sparse fp8 kernels; this
fork has dense decoding back, and that is the one caller wanting False.

flash_attn_varlen_qkvpacked_func and flash_attn_varlen_kvpacked_func are
dropped -- nothing in SGLang calls either, and the CUTLASS kernel behind them
is not in the default build.

tests/ takes upstream's layout and scripts. tests/kernelkit/platform.py keeps
one deviation: a missing `lspci` no longer raises, since setup.py's spill check
imports it and manylinux images have no pciutils.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The wheel goes from 21 MB to 7.9 MB; the two .so from 101 MB to 18 MB.

-lineinfo and --source-in-ptx are upstream's, and they carry line tables and
PTX source into the fatbin for profiler source correlation. We ship a binary
wheel, so that is weight every user downloads for a tool most of them will not
run. --compress-mode=size (nvcc 13.0+, and the build already requires 13.1)
compresses what is left.

Neither affects the kernels. cuobjdump -res-usage over the rebuilt extension is
identical field for field to the previous build -- the four split-KV h64 decode
instantiations still report REG:168 STACK:16 SHARED:1024 LOCAL:0 and their
no-split siblings STACK:0 -- so register allocation, and therefore codegen, is
unchanged. The spill check still finds and exempts the same four.

A build that wants profiler source correlation can put the two flags back; they
are a packaging choice, not a correctness one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant