mirror of
https://github.com/lightseekorg/tokenspeed.git
synced 2026-11-24 15:58:35 +08:00
## Summary
Each of the four paged MLA leaves carried its own copy of
`init_forward_metadata` and `override_num_extends`:
- `MLAAttnBackend` (`paged/mla.py`)
- `FlashMLABackend` (`paged/flashmla.py`)
- `CuteDSLMLABackend` (`paged/tokenspeed_mla.py`)
- `TRTLLMMLABackend` (`paged/trtllm_mla.py`)
Every entry took the same four steps:
1. reject a query shard;
2. refuse a decode forward;
3. build prefill metadata for the leading extend requests;
4. refresh decode metadata for a target's mixed or idle batch.
The four overrides were near-identical. Both methods now live in a new
`PagedMLABackend` in `paged/base.py`, and each leaf keeps only its
`_init_prefill_metadata`. `Dots3NoteSWABackend`, `AscendMLABackend` and
the DSA wrapper reach the shared entry through the leaves they extend or
wrap.
Where the copies differed:
- **FlashMLA** is the only leaf that reads `extend_with_prefix`, which
picks ragged or paged prefill. It records the flag in a small
`init_forward_metadata` wrapper before calling the shared entry. This
keeps the hook's arguments the same for every leaf, including the
subclasses and the tests that call it directly.
- **TRT-LLM MLA**'s hook now receives `page_table_cpu` and discards it,
as its entry did before.
- **Error messages** now name the leaf class:
- the decode-forward error reads `TRTLLMMLABackend decode metadata goes
through ...`; it used to say `trtllm_mla`;
- the query-shard error from `Dots3NoteSWABackend` and
`AscendMLABackend` names the subclass instead of `MLAAttnBackend`.
No kernel code changes.
## Test Plan
On GB200, main (5b26453b0) against this branch, with the engine and
scheduler built at that commit.
- [x] **Same metadata.** Each leaf, as target and as draft, ran the same
set of calls:
- extend without a prefix, with a prefix, and with a prefix long enough
for several chunk loops;
- mixed batches with one and with two decode requests;
- idle;
- the decode-mode and query-shard errors;
- `override_num_extends`, including an exception raised inside it.
After each call, every tensor in the prefill, chunked-prefill and decode
metadata was hashed, along with every tensor attribute of the leaf. That
gave 1238 hashes over the four leaves and the dots3 window leaf, and all
match main. The only differences are the two query-shard messages of the
dots3 window leaf, described above. Two runs of main give identical
hashes.
- [x] **Same host time.** Main's entry was rebuilt from its source as a
subclass of each leaf. Both versions ran in one process and alternated
call by call, 1000 calls each, on an extend with prefixes and on a mixed
batch. Every median is within 0.4% of main (0.998x to 1.004x).
- [x] **Same test results.** 35 runtime test files cover these leaves:
router and cache setup, MLA, FlashMLA, CuTe DSL and TRT-LLM MLA
metadata, block decode, tree verify, DSA selection, dots3, Kimi K3,
DFlash and the drafters. Both sides give 983 passed and 18 skipped, with
no failures.
- [x] **Every module imports.** All modules under
`tokenspeed.runtime.layers` and `tokenspeed.runtime.models` import on
both sides: 149 of 149.
- [x] **New test.** `test/runtime/test_mla_forward_metadata_entry.py`
builds real MLA, FlashMLA and TRT-LLM MLA leaves (12 cases) and checks
that:
- a target's mixed batch refreshes its decode requests;
- a draft's mixed batch leaves decode metadata to its wrapper;
- `override_num_extends` restores the count after an exception;
- a decode forward is refused.
It passes on main too.
- [x] pre-commit on the changed files.
Signed-off-by: Nicolas <nperrin.ai@gmail.com>