mirror of
https://github.com/lightseekorg/tokenspeed.git
synced 2026-12-04 10:26:50 +08:00
## Summary Under `--enable-speculative-sampling` a DFLASH draft's tokens enter the served trajectory, so the draft's rows must not depend on the batch they run in. Two of its operators do. The new flag `--enable-draft-forward-batch-invariant` replaces them. It is off by default, also under `--numerics rl-bitwise`, and the default path is unchanged. **Decode attention.** The adaptive Triton MHA decode splits a request's keys by batch size and SM count, so the order in which key blocks fold into a row's sum follows the co-batch. For a GQA block draft (32/8 heads, head dim 128, window 1024 + 16 sink, width 8) on H200 the split count goes 5, 3, 2, 1 from 1 to 8 requests, and a request's rows differed in 12837 of 32768 elements between batch 1 and 8. - New leaf `triton_invariant_mha_decode_with_kvcache` declares `batch_invariant`. Each program folds one row block's keys in index order, with no split dimension. - `mha_decode_with_kvcache` takes a required `batch_invariant` argument. The draft's `MHAAttnBackend` sets it from the flag; the target's backend does not. - Selection fails rather than falling back, and no split schedule is passed. The leaf takes no logit cap and no q/k/v scales. **Context K|V write.** The write multiplies the round's context rows, a count the accepted lengths and the co-batch decide, by `qkv_proj`. cuBLAS moves a row between row counts for K = 4096 to 8192 projections. - `DFlashAttention` plans the per-layer slice with `plan_dense_gemm(batch_invariant=True)` at construction, so an unmet flag stops the launch. - Every round writes through the per-layer path; the stacked fused write is off under the flag. A round takes the fused write only when no prefill shares it, and that kernel rounds K once where the per-layer write rounds it after the norm and again after the rotation. Left on, a request's cached K rows would follow whether another request prefills in the same round. ## Requirements - A bitwise numerics envelope (`--numerics rl-bitwise`). The flag is refused under `auto`: it replaces two operators only, and the draft's other GEMMs (`fc`, the query side of `qkv_proj`, `o_proj`, the MLP, the head projection) plan batch-invariantly under a bitwise envelope alone. - A batch-invariant `gemm.mm` leaf for BF16. This tree registers one only for gfx950, with FP32 output. Elsewhere a plugin provides it; without one the launch stops when the draft model is built, as for any other missing provider. - Supported: a DFLASH GQA draft whose layers all use a sliding window, unquantized. Any other draft fails when the draft model is built: another speculative algorithm, a draft built from another decoder layer class (DFlash2), an MLA draft, a draft with a full-attention layer, a quantized draft. ## Cost The attention leaf, on H200 under CUDA graph replay, as time against the adaptive leaf: a windowed block draft takes 1.75x at batch 1 and 0.90-0.95x from batch 8; with no window 5-12x at batch 1 over 4K-128K keys and parity from batch 64. The per-layer context write replaces one fused launch with a GEMM, a norm, a rotation and a scatter per layer. ## Test Plan - Kernel: the leaf against `torch_dense_attention` (block draft, decode, verify width, window or not, sinks, lse); row bits alone against batches of 2 to 16; no recompile across batch and length; K page offsets past 2**31. - Draft: the planned GEMMs; the real attention backend with the flag on and off, for target and draft; `_sample_block_from_q` over 1 to 64 requests with a reference GEMM provider; the structures the flag refuses and the same drafts building without it. - Context write: the same context row leaves the same K and V in a decode-only round and in a mixed round (1 to 512 rows) and beside other rows; the fused write is off under the flag and on without it. - Arguments: the flag is refused under `auto` and for a speculative algorithm other than DFLASH, accepted under `rl-bitwise`, and carried into `global_server_args_dict`. - End to end on H200, one prefill and one decode instance (4 GPUs each, DP4/EP4), a DFLASH GQA sliding-window draft, `--enable-speculative-sampling` on decode, the flag on both roles, `--numerics rl-bitwise`: 620 prompts generated together and again each alone, 620/620 identical tokens and scores; five fixed prefix-hit lengths with 32 requests each, 160/160 identical to the cold run; and every one of the 620 requests at its own hit length with the hit requests running together, 620/620 identical. --------- Signed-off-by: linguoyuan <linguoyuan@meituan.com> Co-authored-by: WangBo <wangbo981016@gmail.com>
814 lines
29 KiB
Python
814 lines
29 KiB
Python
# Copyright (c) 2026 LightSeek Foundation
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
# in the Software without restriction, including without limitation the rights
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
# furnished to do so, subject to the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be included in
|
|
# all copies or substantial portions of the Software.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
# SOFTWARE.
|
|
|
|
"""A DFLASH draft under ``--enable-draft-forward-batch-invariant``.
|
|
|
|
Checks the two parts of the draft whose result depends on how many rows ride
|
|
together: the context K|V write must run the batch-invariant GEMM leaf its plan
|
|
selects (per layer and stacked), and the block proposal's tokens and recorded
|
|
``q`` must depend only on the request's seed, frontier and draft step. A
|
|
reference GEMM provider that sums K in index order stands in for the platform's
|
|
leaf. The attention leaf is tested in ``tokenspeed-kernel``.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
from tokenspeed_kernel.registry import KernelRegistry, KernelSpec, Priority
|
|
from tokenspeed_kernel.selection import NoKernelFoundError
|
|
from tokenspeed_kernel.signature import format_signatures
|
|
from transformers import PretrainedConfig
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
from ci_system.ci_register import register_cuda_ci # noqa: E402
|
|
|
|
register_cuda_ci(est_time=60, suite="runtime-1gpu")
|
|
|
|
from tokenspeed.runtime.distributed.mapping import Mapping # noqa: E402
|
|
from tokenspeed.runtime.execution.drafter.dflash import DFlash # noqa: E402
|
|
from tokenspeed.runtime.execution.drafter.speculative_sampling import ( # noqa: E402
|
|
DraftProposalSampler,
|
|
)
|
|
from tokenspeed.runtime.execution.input_buffer import InputBuffers # noqa: E402
|
|
from tokenspeed.runtime.execution.runtime_states import RuntimeStates # noqa: E402
|
|
from tokenspeed.runtime.layers.attention.backends.paged.mha import ( # noqa: E402
|
|
MHAAttnBackend,
|
|
)
|
|
from tokenspeed.runtime.layers.attention.configs.base import AttnConfig # noqa: E402
|
|
from tokenspeed.runtime.layers.attention.configs.mha import MHAConfig # noqa: E402
|
|
from tokenspeed.runtime.layers.logits_processor import LogitsProcessor # noqa: E402
|
|
from tokenspeed.runtime.layers.paged_attention import PagedAttention # noqa: E402
|
|
from tokenspeed.runtime.layers.vocab_parallel_embedding import ( # noqa: E402
|
|
ParallelLMHead,
|
|
)
|
|
from tokenspeed.runtime.model_loader.loader import ( # noqa: E402
|
|
post_load_transformed_modules,
|
|
set_default_torch_dtype,
|
|
)
|
|
from tokenspeed.runtime.models import dflash as model_module # noqa: E402
|
|
from tokenspeed.runtime.models.dflash import ( # noqa: E402
|
|
DFlashAttention,
|
|
DFlashDecoderLayer,
|
|
DFlashDraftModel,
|
|
)
|
|
from tokenspeed.runtime.sampling.backends.base import ( # noqa: E402
|
|
SpeculativeSamplingPools,
|
|
)
|
|
from tokenspeed.runtime.utils.env import global_server_args_dict # noqa: E402
|
|
|
|
pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="requires a GPU")
|
|
|
|
PROVIDER = "test_row_ordered_mm"
|
|
HEADS = 32
|
|
KV_HEADS = 8
|
|
HEAD_DIM = 128
|
|
|
|
|
|
def row_ordered_mm(a, b, a_scales, b_scales, out_dtype, *, alpha, block_size, out=None):
|
|
"""``a @ b.T`` with the sum over K taken in index order in fp32, which makes
|
|
a row's bits independent of the other rows."""
|
|
total = torch.zeros(a.shape[0], b.shape[0], dtype=torch.float32, device=a.device)
|
|
for k in range(a.shape[1]):
|
|
total.add_(a[:, k, None].float() * b[None, :, k].float())
|
|
result = total.to(out_dtype)
|
|
if out is not None:
|
|
out.copy_(result)
|
|
return out
|
|
return result
|
|
|
|
|
|
@pytest.fixture
|
|
def flag(monkeypatch):
|
|
"""Run under the bitwise envelope with ``--enable-draft-forward-batch-invariant`` on;
|
|
``flag(False)`` is the same launch without it. The reference GEMM provider
|
|
is registered at the lowest priority, so only a plan that requires
|
|
``batch_invariant`` reaches it."""
|
|
registry = KernelRegistry.get()
|
|
snapshot = registry.snapshot()
|
|
registry.register(
|
|
KernelSpec(
|
|
name=PROVIDER,
|
|
family="gemm",
|
|
mode="mm",
|
|
solution="test_reference",
|
|
format_signatures=format_signatures(("a", "b"), "dense", {torch.bfloat16}),
|
|
features=frozenset({"batch_invariant"}),
|
|
priority=int(Priority.REFERENCE),
|
|
),
|
|
row_ordered_mm,
|
|
)
|
|
monkeypatch.setitem(global_server_args_dict, "numerics", "rl-bitwise")
|
|
|
|
def select(enabled: bool) -> None:
|
|
monkeypatch.setitem(
|
|
global_server_args_dict, "enable_draft_forward_batch_invariant", enabled
|
|
)
|
|
|
|
select(True)
|
|
yield select
|
|
registry.restore(snapshot)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Context K|V write
|
|
# ---------------------------------------------------------------------------
|
|
|
|
HIDDEN = 2048
|
|
ROWS = (1, 2, 8, 16, 64, 128, 256, 512)
|
|
|
|
|
|
def _config() -> PretrainedConfig:
|
|
return PretrainedConfig(
|
|
hidden_size=HIDDEN,
|
|
num_hidden_layers=2,
|
|
num_attention_heads=HEADS,
|
|
num_key_value_heads=KV_HEADS,
|
|
head_dim=HEAD_DIM,
|
|
intermediate_size=5120,
|
|
hidden_act="silu",
|
|
rms_norm_eps=1e-5,
|
|
rope_theta=10000.0,
|
|
max_position_embeddings=4096,
|
|
layer_types=["sliding_attention"] * 2,
|
|
sliding_window=1024,
|
|
sliding_window_sink=16,
|
|
dflash_config={"target_layer_ids": [1, 2, 3, 4], "block_size": 8},
|
|
)
|
|
|
|
|
|
def _build(config=None, **kwargs) -> DFlashDraftModel:
|
|
"""Built as the model loader builds it: in the checkpoint's dtype, on the
|
|
device, so the plans made at construction name the dtype the weights hold."""
|
|
with set_default_torch_dtype(torch.bfloat16), torch.device("cuda"):
|
|
return DFlashDraftModel(
|
|
_config() if config is None else config,
|
|
Mapping(rank=0, world_size=1),
|
|
**kwargs,
|
|
)
|
|
|
|
|
|
def _model() -> DFlashDraftModel:
|
|
torch.manual_seed(0)
|
|
model = _build()
|
|
# Constructed weights are uninitialised and may be zero.
|
|
for layer in model.layers:
|
|
layer.self_attn.qkv_proj.weight.data.normal_(0, 0.02)
|
|
return model
|
|
|
|
|
|
def _attention() -> DFlashAttention:
|
|
return _model().layers[0].self_attn
|
|
|
|
|
|
def _drafter() -> DFlash:
|
|
"""A DFlash holding a real two-layer draft and a pool of the right shape."""
|
|
model = _model()
|
|
|
|
class Pool:
|
|
def get_key_buffer(self, layer_id):
|
|
return torch.zeros(
|
|
4, 128, KV_HEADS, HEAD_DIM, device="cuda", dtype=torch.bfloat16
|
|
)
|
|
|
|
get_value_buffer = get_key_buffer
|
|
|
|
drafter = DFlash.__new__(DFlash)
|
|
drafter.draft_model_runner = SimpleNamespace(
|
|
model=model, mapping=SimpleNamespace(attn=SimpleNamespace(kvp_size=1))
|
|
)
|
|
drafter.token_to_kv_pool = Pool()
|
|
drafter.device = torch.device("cuda")
|
|
drafter.spec_num_tokens = 8
|
|
drafter.input_buffers = InputBuffers(
|
|
max_bs=max(ROWS) // drafter.spec_num_tokens,
|
|
max_num_tokens=max(ROWS),
|
|
state_write_padding_pool_index=0,
|
|
device="cuda",
|
|
)
|
|
return drafter
|
|
|
|
|
|
def _rows() -> torch.Tensor:
|
|
generator = torch.Generator(device="cuda").manual_seed(1)
|
|
return torch.randn(
|
|
max(ROWS), HIDDEN, generator=generator, device="cuda", dtype=torch.bfloat16
|
|
)
|
|
|
|
|
|
def test_the_flag_plans_the_kv_gemm_and_off_does_not(flag) -> None:
|
|
assert _attention().kv_proj_plan.kernel.name == PROVIDER
|
|
flag(False)
|
|
assert _attention().kv_proj_plan is None
|
|
|
|
|
|
@pytest.mark.parametrize("count", ROWS)
|
|
def test_the_per_layer_write_projects_with_the_planned_leaf(flag, count) -> None:
|
|
layer = _attention()
|
|
rows = _rows()[:count]
|
|
k, v = layer.kv_proj_only(rows)
|
|
weight = layer.qkv_proj.weight[layer.q_size : layer.q_size + 2 * layer.kv_size]
|
|
expected = row_ordered_mm(
|
|
rows, weight, None, None, torch.bfloat16, alpha=None, block_size=None
|
|
)
|
|
assert torch.equal(torch.cat([k, v], dim=-1), expected)
|
|
|
|
|
|
class _WritePool:
|
|
"""Per-layer ``[pages, page, heads, dim]`` K and V planes that the stacked
|
|
scatter and the per-layer ``set_kv_buffer`` both write, as the paged pool does."""
|
|
|
|
PAGES = 4
|
|
|
|
def __init__(self, layers: int):
|
|
shape = (self.PAGES, 128, KV_HEADS, HEAD_DIM)
|
|
self.k = [
|
|
torch.zeros(shape, device="cuda", dtype=torch.bfloat16)
|
|
for _ in range(layers)
|
|
]
|
|
self.v = [torch.zeros_like(plane) for plane in self.k]
|
|
|
|
def get_key_buffer(self, layer_id):
|
|
return self.k[layer_id]
|
|
|
|
def get_value_buffer(self, layer_id):
|
|
return self.v[layer_id]
|
|
|
|
def set_kv_buffer(self, layer, loc, cache_k, cache_v):
|
|
from tokenspeed_kernel.ops.kvcache.triton import store_kv_cache
|
|
|
|
store_kv_cache(
|
|
cache_k, cache_v, self.k[layer.layer_id], self.v[layer.layer_id], loc
|
|
)
|
|
|
|
|
|
def _written_rows(drafter: DFlash, count: int, *, decode_only: bool):
|
|
"""The K and V rows ``_write_native_cache`` leaves for ``count`` context rows
|
|
when the round is ``decode_only`` (no prefill shares it) or not."""
|
|
layers = drafter.draft_model_runner.model.layers
|
|
pool = _WritePool(len(layers))
|
|
drafter.token_to_kv_pool = pool
|
|
# The GQA per-layer write takes no attention backend; it is only passed along.
|
|
drafter.attn_backend = None
|
|
drafter._init_fused_kv_helper()
|
|
rows = _rows()[:count]
|
|
taps = torch.zeros(
|
|
count,
|
|
drafter.draft_model_runner.model.context_in_features,
|
|
device="cuda",
|
|
dtype=torch.bfloat16,
|
|
)
|
|
# Positions inside one page of the pool and distinct, so every row is its own slot.
|
|
positions = torch.arange(count, device="cuda", dtype=torch.int64) + 5
|
|
slots = torch.arange(count, device="cuda", dtype=torch.int64)
|
|
model = drafter.draft_model_runner.model
|
|
# Hand both writes the same projected context rows: only the K|V write differs.
|
|
model.project_target_hidden = lambda _taps: rows
|
|
drafter._write_native_cache(taps, positions, slots, decode_only=decode_only)
|
|
# [layers, pages * page rows, heads, dim]: every row written, not only page 0.
|
|
flat = (
|
|
lambda planes: torch.stack(planes).flatten(1, 2)[:, :count].clone()
|
|
) # noqa: E731
|
|
return flat(pool.k), flat(pool.v)
|
|
|
|
|
|
def test_the_flag_writes_every_round_through_the_per_layer_path(flag) -> None:
|
|
"""The stacked write rounds K once and the per-layer write rounds it after the
|
|
norm and again after the rotation, and a round takes the stacked one only when no
|
|
prefill shares it. With the flag on the fused write stays off."""
|
|
drafter = _drafter()
|
|
drafter._init_fused_kv_helper()
|
|
assert not drafter._fused_kv_enabled
|
|
|
|
|
|
def test_the_fused_write_stays_on_without_the_flag(flag) -> None:
|
|
flag(False)
|
|
drafter = _drafter()
|
|
drafter._init_fused_kv_helper()
|
|
assert drafter._fused_kv_enabled
|
|
|
|
|
|
@pytest.mark.parametrize("count", (1, 3, 8, 64, 512))
|
|
def test_a_context_row_is_written_the_same_in_a_decode_round_and_a_mixed_round(
|
|
flag, count
|
|
) -> None:
|
|
"""``decode_only`` is ``num_extends == 0``: whether a prefill shares the round
|
|
must not change the bits a decode request's context rows leave in the cache."""
|
|
drafter = _drafter()
|
|
decode_k, decode_v = _written_rows(drafter, count, decode_only=True)
|
|
mixed_k, mixed_v = _written_rows(drafter, count, decode_only=False)
|
|
assert torch.equal(decode_v, mixed_v)
|
|
assert torch.equal(decode_k, mixed_k)
|
|
|
|
|
|
def test_a_context_row_is_written_the_same_whatever_rows_ride_with_it(flag) -> None:
|
|
drafter = _drafter()
|
|
alone_k, alone_v = _written_rows(drafter, 1, decode_only=True)
|
|
for count in (2, 8, 64, 512):
|
|
k, v = _written_rows(drafter, count, decode_only=True)
|
|
assert torch.equal(k[:, :1], alone_k), count
|
|
assert torch.equal(v[:, :1], alone_v), count
|
|
|
|
|
|
def test_no_batch_invariant_leaf_stops_the_launch_instead_of_slowing_it(
|
|
flag, monkeypatch
|
|
) -> None:
|
|
"""A flag whose GEMM has no batch-invariant leaf must stop the launch when the
|
|
draft is built, not serve on with a library GEMM."""
|
|
|
|
def no_leaf(**kwargs):
|
|
raise NoKernelFoundError("no batch-invariant leaf")
|
|
|
|
monkeypatch.setattr(model_module, "plan_dense_gemm", no_leaf)
|
|
with pytest.raises(NoKernelFoundError):
|
|
_model()
|
|
|
|
|
|
def test_the_draft_stays_reloadable_in_place(flag) -> None:
|
|
"""Planning at construction leaves no post-load hook, so the draft is not
|
|
counted among the modules a checkpoint reload cannot rewrite."""
|
|
assert post_load_transformed_modules(_model()) == []
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Block attention
|
|
# ---------------------------------------------------------------------------
|
|
|
|
CONTEXT = 2048
|
|
PAGE = 128
|
|
ATTN_BATCHES = (1, 2, 8, 16)
|
|
|
|
|
|
class _Cache:
|
|
"""KV storage for one layer; the backend and the kernels stay real."""
|
|
|
|
def __init__(self, pages: int):
|
|
generator = torch.Generator(device="cuda").manual_seed(3)
|
|
self.k, self.v = (
|
|
torch.randn(
|
|
pages,
|
|
PAGE,
|
|
KV_HEADS,
|
|
HEAD_DIM,
|
|
generator=generator,
|
|
device="cuda",
|
|
dtype=torch.bfloat16,
|
|
)
|
|
for _ in range(2)
|
|
)
|
|
|
|
def get_key_buffer(self, layer_id):
|
|
return self.k
|
|
|
|
def get_value_buffer(self, layer_id):
|
|
return self.v
|
|
|
|
|
|
def _backend(*, is_draft: bool, backend_name: str = "triton") -> MHAAttnBackend:
|
|
spec = MHAConfig(
|
|
backend_name=backend_name,
|
|
num_attention_heads=HEADS,
|
|
num_kv_heads=KV_HEADS,
|
|
head_dim=HEAD_DIM,
|
|
attn_tp_size=1,
|
|
)
|
|
config = AttnConfig(
|
|
device="cuda",
|
|
dtype=torch.bfloat16,
|
|
kv_cache_dtype=torch.bfloat16,
|
|
kv_cache_quant_method="none",
|
|
context_len=CONTEXT,
|
|
max_bs=max(ATTN_BATCHES),
|
|
max_resident_requests=max(ATTN_BATCHES),
|
|
speculative_num_draft_tokens=8,
|
|
is_draft=is_draft,
|
|
draft_block_decode=is_draft,
|
|
components=(spec,),
|
|
)
|
|
return MHAAttnBackend(config, spec, kernel_page_size=PAGE)
|
|
|
|
|
|
def _block_decode(bs: int, backend_name: str = "triton") -> torch.Tensor:
|
|
"""The block rows of the first ``bs`` requests, decoded through the real
|
|
attention backend of a block draft with a sliding window and a sink."""
|
|
spec = MHAConfig(
|
|
backend_name=backend_name,
|
|
num_attention_heads=HEADS,
|
|
num_kv_heads=KV_HEADS,
|
|
head_dim=HEAD_DIM,
|
|
attn_tp_size=1,
|
|
)
|
|
config = AttnConfig(
|
|
device="cuda",
|
|
dtype=torch.bfloat16,
|
|
kv_cache_dtype=torch.bfloat16,
|
|
kv_cache_quant_method="none",
|
|
context_len=CONTEXT,
|
|
max_bs=max(ATTN_BATCHES),
|
|
max_resident_requests=max(ATTN_BATCHES),
|
|
speculative_num_draft_tokens=8,
|
|
is_draft=True,
|
|
draft_block_decode=True,
|
|
components=(spec,),
|
|
)
|
|
backend = MHAAttnBackend(config, spec, kernel_page_size=PAGE)
|
|
backend.init_cuda_graph_state(max(ATTN_BATCHES))
|
|
layer = PagedAttention(
|
|
HEADS,
|
|
HEAD_DIM,
|
|
HEAD_DIM**-0.5,
|
|
KV_HEADS,
|
|
0,
|
|
sliding_window_size=1024,
|
|
sliding_window_sink=16,
|
|
rotary_emb=None,
|
|
qk_norm=None,
|
|
)
|
|
pages = CONTEXT // PAGE
|
|
cache = _Cache(max(ATTN_BATCHES) * pages + 1)
|
|
table = torch.arange(
|
|
1, max(ATTN_BATCHES) * pages + 1, device="cuda", dtype=torch.int32
|
|
).view(max(ATTN_BATCHES), pages)
|
|
generator = torch.Generator(device="cuda").manual_seed(4)
|
|
q = torch.randn(
|
|
max(ATTN_BATCHES) * 8,
|
|
HEADS,
|
|
HEAD_DIM,
|
|
generator=generator,
|
|
device="cuda",
|
|
dtype=torch.bfloat16,
|
|
)
|
|
lengths = torch.full((bs,), CONTEXT, dtype=torch.int32, device="cuda")
|
|
backend.refresh_decode_metadata(bs, bs, lengths, table[:bs])
|
|
return backend.forward_decode(q[: bs * 8], None, None, layer, None, cache, bs)
|
|
|
|
|
|
def test_the_backend_decodes_a_draft_block_the_same_alone_and_batched(flag) -> None:
|
|
"""The draft's attention backend selects the batch-invariant leaf under the
|
|
flag, so request 0's block rows are the same whatever rides with them. A
|
|
backend that left the flag out of its selection would take the adaptive
|
|
leaf, whose key split follows the batch."""
|
|
alone = _block_decode(1)
|
|
for bs in ATTN_BATCHES[1:]:
|
|
assert torch.equal(_block_decode(bs)[: alone.shape[0]], alone), bs
|
|
|
|
|
|
def test_without_the_flag_the_draft_keeps_the_adaptive_leaf(flag) -> None:
|
|
flag(False)
|
|
assert not _backend(is_draft=True).batch_invariant
|
|
|
|
|
|
def test_the_flag_reaches_the_draft_backend_only(flag) -> None:
|
|
"""The target's backend keeps the adaptive leaf: the flag is the draft's."""
|
|
assert _backend(is_draft=True).batch_invariant
|
|
assert not _backend(is_draft=False).batch_invariant
|
|
|
|
|
|
def _drafter_with(backend, *, name: str) -> DFlash:
|
|
"""A drafter stub holding ``backend``, as the launch check sees it."""
|
|
drafter = DFlash.__new__(DFlash)
|
|
drafter.attn_backend = backend
|
|
drafter.draft_model_runner = SimpleNamespace(
|
|
server_args=SimpleNamespace(drafter_attention_backend=name)
|
|
)
|
|
return drafter
|
|
|
|
|
|
def test_the_mha_backend_declares_it_reads_the_flag(flag) -> None:
|
|
"""The drafter check trusts this declaration, so the backend that sets
|
|
``batch_invariant`` from the flag is the one that makes it."""
|
|
assert MHAAttnBackend.supports_draft_forward_batch_invariance
|
|
assert _backend(is_draft=True).batch_invariant
|
|
|
|
|
|
def test_a_backend_that_never_reads_the_flag_stops_the_launch(flag) -> None:
|
|
"""``trtllm`` keeps its batch-dependent decode under the flag, with a K|V
|
|
plan and a write that already follow it, so the launch must not look right."""
|
|
drafter = _drafter_with(
|
|
SimpleNamespace(supports_draft_forward_batch_invariance=False), name="trtllm"
|
|
)
|
|
|
|
with pytest.raises(ValueError, match="'trtllm'.*does not read the flag"):
|
|
drafter._validate_draft_forward_batch_invariance()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"module,name",
|
|
[
|
|
("paged.base", "PagedAttentionBackend"),
|
|
("paged.trtllm", "TRTLLMMHAAttnBackend"),
|
|
("paged.qsa", "QSAAttnBackend"),
|
|
],
|
|
)
|
|
def test_only_the_leaf_that_reads_the_flag_declares_it(module, name) -> None:
|
|
"""The drafter check trusts the declaration, so a leaf that does not read the
|
|
flag must say so: ``trtllm``, and QSA whose sparse decode skips the call sites."""
|
|
import importlib
|
|
|
|
cls = getattr(
|
|
importlib.import_module(
|
|
f"tokenspeed.runtime.layers.attention.backends.{module}"
|
|
),
|
|
name,
|
|
)
|
|
|
|
assert not cls.supports_draft_forward_batch_invariance
|
|
|
|
|
|
@pytest.mark.parametrize("backend_name", ["mha", "triton", "fa3", "fa4", "flashinfer"])
|
|
def test_a_pinned_backend_is_accepted_exactly_when_its_decode_selects_a_kernel(
|
|
flag, backend_name
|
|
) -> None:
|
|
"""The one decode kernel declaring ``batch_invariant`` is Triton's, so a
|
|
backend pinned to another solution fails kernel selection at its first draft
|
|
decode. Its launch check must refuse it up front, not accept and then fail."""
|
|
try:
|
|
_block_decode(1, backend_name)
|
|
decodes = True
|
|
except NoKernelFoundError:
|
|
decodes = False
|
|
drafter = _drafter_with(
|
|
_backend(is_draft=True, backend_name=backend_name), name=backend_name
|
|
)
|
|
|
|
if decodes:
|
|
drafter._validate_draft_forward_batch_invariance()
|
|
else:
|
|
with pytest.raises(ValueError, match=f"{backend_name!r}.*cannot"):
|
|
drafter._validate_draft_forward_batch_invariance()
|
|
assert decodes == (backend_name in ("mha", "triton"))
|
|
|
|
|
|
def test_a_backend_that_reads_the_flag_is_accepted(flag) -> None:
|
|
drafter = _drafter_with(_backend(is_draft=True), name="mha")
|
|
|
|
drafter._validate_draft_forward_batch_invariance()
|
|
|
|
|
|
def test_without_the_flag_any_drafter_backend_is_accepted(flag) -> None:
|
|
flag(False)
|
|
drafter = _drafter_with(
|
|
SimpleNamespace(supports_draft_forward_batch_invariance=False), name="trtllm"
|
|
)
|
|
|
|
drafter._validate_draft_forward_batch_invariance()
|
|
|
|
|
|
def test_a_batch_invariant_draft_builds(flag) -> None:
|
|
"""The structure the flag supports: a GQA draft whose layers all slide."""
|
|
assert _model().layers
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"change,reason",
|
|
[
|
|
(dict(layer_types=["full_attention", "sliding_attention"]), "layer_types"),
|
|
(dict(layer_types=None), "layer_types"),
|
|
(dict(attention_method="mla"), "mla=True"),
|
|
],
|
|
ids=["a full-attention layer", "no layer types", "mla"],
|
|
)
|
|
def test_another_draft_structure_stops_the_launch(flag, change, reason) -> None:
|
|
config = _config()
|
|
for name, value in change.items():
|
|
setattr(config, name, value)
|
|
with pytest.raises(ValueError, match=reason):
|
|
_build(config)
|
|
# Without the flag the same draft builds: the limit belongs to the flag.
|
|
flag(False)
|
|
_build(config)
|
|
|
|
|
|
def test_a_quantized_draft_stops_the_launch(flag) -> None:
|
|
with pytest.raises(ValueError, match="quantized=True"):
|
|
_build(quant_config=object())
|
|
|
|
|
|
def test_a_subclass_keeping_the_layer_class_builds(flag) -> None:
|
|
"""A deployment's own subclass of the draft model, which builds the same
|
|
GQA layers, is covered: the limit is on the layers, not on the class."""
|
|
|
|
class Subclass(DFlashDraftModel):
|
|
pass
|
|
|
|
with set_default_torch_dtype(torch.bfloat16), torch.device("cuda"):
|
|
assert Subclass(_config(), Mapping(rank=0, world_size=1)).layers
|
|
|
|
|
|
def test_another_layer_class_stops_the_launch(flag) -> None:
|
|
"""DFlash2 builds its own layer class, which the flag does not cover."""
|
|
|
|
class OtherLayer(DFlashDecoderLayer):
|
|
pass
|
|
|
|
class Other(DFlashDraftModel):
|
|
decoder_layer_cls = OtherLayer
|
|
|
|
with pytest.raises(ValueError, match="OtherLayer"):
|
|
with set_default_torch_dtype(torch.bfloat16), torch.device("cuda"):
|
|
Other(_config(), Mapping(rank=0, world_size=1))
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Block proposal
|
|
# ---------------------------------------------------------------------------
|
|
|
|
SAMPLER_HIDDEN = 512
|
|
VOCAB = 32768
|
|
N = 8 # the draft's verify width: an anchor plus seven sampled rows
|
|
STEPS = N - 1
|
|
POOL = 72
|
|
MAX_BS = 64
|
|
# Requests sharing one round. Row 0 is the request under test.
|
|
BATCHES = (1, 2, 4, 8, 16, 32, 64)
|
|
SEED = 1234
|
|
|
|
|
|
def _sampling_drafter():
|
|
"""A DFlash holding the state its block proposal runs on."""
|
|
torch.manual_seed(0)
|
|
states = RuntimeStates(
|
|
req_pool_size=POOL,
|
|
input_vocab_size=VOCAB,
|
|
vocab_size=VOCAB,
|
|
output_length=N,
|
|
device="cuda",
|
|
)
|
|
states.init_draft_probs(spec_num_tokens=N, reject_threshold=1.0)
|
|
buffers = InputBuffers(
|
|
max_bs=MAX_BS,
|
|
max_num_tokens=MAX_BS * N,
|
|
state_write_padding_pool_index=0,
|
|
device="cuda",
|
|
)
|
|
pools = SpeculativeSamplingPools(
|
|
# Temperature 1 with top_k disabled: every row samples, so the
|
|
# proposal is the stochastic path rather than a hidden argmax.
|
|
temperature=torch.ones(POOL + 1, dtype=torch.float32, device="cuda"),
|
|
top_k=torch.zeros(POOL + 1, dtype=torch.int32, device="cuda"),
|
|
top_p=torch.ones(POOL + 1, dtype=torch.float32, device="cuda"),
|
|
# A distinct seed per slot, so a row that reads the wrong slot draws
|
|
# visibly different tokens.
|
|
seed=(
|
|
SEED + torch.arange(POOL + 1, dtype=torch.int64, device="cuda")
|
|
).contiguous(),
|
|
)
|
|
sampler = DraftProposalSampler(
|
|
pools=pools,
|
|
runtime_states=states,
|
|
input_buffers=buffers,
|
|
spec_num_tokens=N,
|
|
sampling_stream="per-request",
|
|
vocab_map=None,
|
|
device="cuda",
|
|
)
|
|
with set_default_torch_dtype(torch.bfloat16), torch.device("cuda"):
|
|
head = ParallelLMHead(VOCAB, SAMPLER_HIDDEN, bias=False)
|
|
head.weight.data.normal_(0, 0.02)
|
|
head.quant_method.process_weights_after_loading(head)
|
|
|
|
drafter = DFlash.__new__(DFlash)
|
|
drafter.hidden_size = SAMPLER_HIDDEN
|
|
drafter.spec_num_tokens = N
|
|
drafter.lm_head = head
|
|
drafter.logits_processor = LogitsProcessor(
|
|
PretrainedConfig(vocab_size=VOCAB),
|
|
dp_lm_head_tp=False,
|
|
attn_tp_size=1,
|
|
)
|
|
drafter.draft_sampler = sampler
|
|
return drafter, states, sampler, buffers
|
|
|
|
|
|
def _slots(bs: int) -> torch.Tensor:
|
|
"""Pool slots of a round: row 0 keeps slot 1 whatever rides with it."""
|
|
return torch.arange(1, bs + 1, dtype=torch.int64, device="cuda")
|
|
|
|
|
|
def _propose(drafter, states, sampler, buffers, hidden, bs, *, frontier=64):
|
|
"""One round's block proposal for ``bs`` requests.
|
|
|
|
Returns row 0's drafted tokens and the ``q`` rows recorded for its slot.
|
|
"""
|
|
slots = _slots(bs)
|
|
buffers.req_pool_indices_buf[:bs].copy_(slots)
|
|
buffers.state_write_req_pool_indices_buf[:bs].copy_(slots)
|
|
# Every request sits at the same frontier, so a row that read a peer's
|
|
# offset would still be caught by its seed.
|
|
buffers.seq_lens_buf[:bs].fill_(frontier)
|
|
states.valid_cache_lengths[slots] = frontier
|
|
sampler.prepare_round(
|
|
torch.zeros(bs, dtype=torch.int32, device="cuda"), num_extends=0
|
|
)
|
|
tokens = torch.zeros(bs, N, dtype=torch.int32, device="cuda")
|
|
drafter._sample_block_from_q(hidden[:bs], sampler, tokens)
|
|
return tokens[0].clone(), states.draft_probs[int(slots[0])].clone()
|
|
|
|
|
|
def _hidden(rows: int) -> torch.Tensor:
|
|
"""Generated once at the largest batch and sliced: CUDA ``randn`` values
|
|
depend on the tensor's element count, so generating per batch size would
|
|
change row 0's input along with its co-batch."""
|
|
generator = torch.Generator(device="cuda").manual_seed(7)
|
|
return torch.randn(
|
|
rows,
|
|
N,
|
|
SAMPLER_HIDDEN,
|
|
generator=generator,
|
|
device="cuda",
|
|
dtype=torch.bfloat16,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def rounds(flag):
|
|
"""Row 0's proposal at each batch size, from one set of hidden states."""
|
|
drafter, states, sampler, buffers = _sampling_drafter()
|
|
hidden = _hidden(max(BATCHES))
|
|
return {
|
|
bs: _propose(drafter, states, sampler, buffers, hidden, bs) for bs in BATCHES
|
|
}
|
|
|
|
|
|
def test_the_drafted_tokens_do_not_move_with_the_batch(rounds) -> None:
|
|
"""The tokens a request drafts are the same alone and in a batch of 64."""
|
|
expected, _ = rounds[1]
|
|
for bs in BATCHES[1:]:
|
|
tokens, _ = rounds[bs]
|
|
assert torch.equal(tokens, expected), bs
|
|
|
|
|
|
def test_the_recorded_q_does_not_move_with_the_batch(rounds) -> None:
|
|
"""The verifier reads ``q`` bit for bit, so it must not move either."""
|
|
_, expected = rounds[1]
|
|
for bs in BATCHES[1:]:
|
|
_, recorded = rounds[bs]
|
|
assert torch.equal(recorded, expected), bs
|
|
|
|
|
|
def test_every_step_drafted_a_token_from_its_own_distribution(rounds) -> None:
|
|
"""The comparisons above are not passing on empty rows: each of the seven
|
|
sampled columns holds a token whose recorded ``q`` is a distribution, and
|
|
the tokens are not all the same column. A step that proposes under another
|
|
step's index draws from the wrong one."""
|
|
tokens, recorded = rounds[1]
|
|
drafted = tokens[1:]
|
|
assert drafted.min() >= 0 and drafted.max() < VOCAB
|
|
assert len(set(drafted.tolist())) > 1
|
|
rows = recorded[:STEPS]
|
|
assert torch.allclose(rows.sum(dim=-1), torch.ones(STEPS, device="cuda"), atol=1e-3)
|
|
# Sampled rows, not a one-hot greedy record.
|
|
assert rows.max() < 1.0
|
|
|
|
|
|
def test_a_different_seed_drafts_different_tokens(flag) -> None:
|
|
"""What the invariance above is worth: the seed is what fixes the draw, so
|
|
a request with another seed draws another trajectory. Without this, rows
|
|
could match for want of any randomness at all."""
|
|
drafter, states, sampler, buffers = _sampling_drafter()
|
|
hidden = _hidden(1)
|
|
first, _ = _propose(drafter, states, sampler, buffers, hidden, 1)
|
|
sampler._pools.seed.fill_(SEED + 9999)
|
|
second, _ = _propose(drafter, states, sampler, buffers, hidden, 1)
|
|
assert not torch.equal(first, second)
|
|
|
|
|
|
def test_the_same_round_drafts_the_same_tokens_twice(flag) -> None:
|
|
"""Run invariance: the same request at the same frontier redraws its own
|
|
trajectory, which is what lets a replay compare against a recorded run."""
|
|
drafter, states, sampler, buffers = _sampling_drafter()
|
|
hidden = _hidden(4)
|
|
first, first_q = _propose(drafter, states, sampler, buffers, hidden, 4)
|
|
second, second_q = _propose(drafter, states, sampler, buffers, hidden, 4)
|
|
assert torch.equal(first, second)
|
|
assert torch.equal(first_q, second_q)
|
|
|
|
|
|
def test_a_later_frontier_drafts_a_different_round(flag) -> None:
|
|
"""The draw is keyed by the committed frontier, so the next round of the
|
|
same request is a fresh draw rather than a repeat of this one."""
|
|
drafter, states, sampler, buffers = _sampling_drafter()
|
|
hidden = _hidden(1)
|
|
here, _ = _propose(drafter, states, sampler, buffers, hidden, 1, frontier=64)
|
|
later, _ = _propose(drafter, states, sampler, buffers, hidden, 1, frontier=65)
|
|
assert not torch.equal(here, later)
|