mirror of
https://github.com/lightseekorg/tokenspeed.git
synced 2026-12-04 10:26:50 +08:00
## Summary Adapt DeepSeek V4.1 DSpark target verification using draft confidence and startup-profiled execution costs. Allocate verification rows across requests and execute packed target queries with matching CUDA graphs. Draft generation remains full width, and token acceptance checks are unchanged. - Profile eligible graph capacities at startup and reuse compatible cached costs; no calibration dataset or external cost table is required. - Coordinate a common verification capacity across attention DP ranks while allocating rows locally using the latest available draft confidence. - Support uneven, padded and idle attention DP ranks with a shared physical graph capacity and per-rank allocation over real requests. - Build packed verification layouts with Triton and integrate them with attention, KV writeback and greedy sampling; retain a PyTorch reference only in tests. - Retain asynchronous confidence feedback and conservative full-verification fallback; remove serving-time cost collectors, timing histories and their synchronization. ## Usage ~~~bash --enable-dsv41-adaptive-dspark \ --dsv41-dspark-min-batch-size 4 ~~~ The threshold uses the **largest actual local batch among attention ranks**, not client concurrency. Balanced DP8 reaches local BS4 at C32. Reaching it permits adaptation; the cost model can still select full verification. ## Performance PD deployment: one 8-GPU H20 prefill worker (QCP8/EP8) and one 8-GPU H20 decode worker (DP8/EP8), host Engram, with DP-balanced SMG routing. Both arms use **10,240 input / 4,096 output tokens**, greedy sampling, real DSpark acceptance and minimum local BS4. The baseline disables adaptive verification while retaining native DSpark. Inputs are derived from 32 LongBench templates. Each request has a distinct leading ID; these are not 256 independent documents. Paired arms use identical payloads. Each concurrency runs one warmup and three formal rounds; tables average **formal rounds 2 and 3**, excluding warmup and formal round 1 consistently. - **No prefix cache:** P and D disable prefix caching. Request IDs change across rounds and concurrency levels. Every request reports zero cached tokens. - **Cached prefixes:** P enables prefix caching; D still disables it. Stable session IDs allow reuse across rounds. Every formal request hits 9,984 of 10,240 input tokens. ### Without prefix cache | Concurrency | Native TPOT (ms) | Adaptive TPOT (ms) | TPOT change | Native output (tokens/s) | Adaptive output (tokens/s) | Throughput change | | ---: | ---: | ---: | ---: | ---: | ---: | ---: | | 32 | 8.998 | 8.915 | **-0.92%** | 2,484.5 | 2,493.1 | **+0.34%** | | 64 | 12.365 | 10.892 | **-11.92%** | 3,330.9 | 3,658.9 | **+9.85%** | | 128 | 18.659 | 14.789 | **-20.74%** | 4,064.0 | 4,836.8 | **+19.02%** | | 256 | 30.165 | 21.847 | **-27.58%** | 4,886.1 | 6,076.3 | **+24.36%** | ### With cached prefixes | Concurrency | Native TPOT (ms) | Adaptive TPOT (ms) | TPOT change | Native output (tokens/s) | Adaptive output (tokens/s) | Throughput change | | ---: | ---: | ---: | ---: | ---: | ---: | ---: | | 32 | 9.171 | 9.270 | **+1.08%** | 2,809.3 | 2,709.1 | **-3.57%** | | 64 | 12.859 | 11.425 | **-11.16%** | 3,983.6 | 4,224.8 | **+6.06%** | | 128 | 19.539 | 15.277 | **-21.82%** | 5,058.8 | 5,659.1 | **+11.87%** | | 256 | 32.951 | 25.053 | **-23.97%** | 6,027.8 | 7,663.6 | **+27.14%** | ## Validation - Full repository pre-commit passed for the tested code commit. - Budget selection, confidence feedback, startup profiling and packed layout tests cover native fallback and adaptive execution. - Distributed runtime coverage includes uneven/padded/idle attention ranks and common-capacity agreement. - PD C32/C64/C128/C256 paired serving tests completed with and without prefix cache; all request-level payload, length and cache checks passed. --------- Signed-off-by: wangchangran <shinewang96@gmail.com> Signed-off-by: jumper <shinewang96@gmail.com> Signed-off-by: wangbo981016 <wangbo981016@gmail.com> Co-authored-by: longcat-ia-team <longcat_ia_team@meituan.com> Co-authored-by: WangBo <wangbo981016@gmail.com>
218 lines
8.3 KiB
Python
218 lines
8.3 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.
|
|
|
|
"""CPU tests for TOKENSPEED_SPEC_SIMULATED_ACCEPT_LEN: the simulated widths,
|
|
the tokens that match them, and where ModelExecutor applies both after
|
|
verify."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
import os
|
|
import sys
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
# CPU-only tests scheduled in runtime-1gpu because they import the full runtime.
|
|
sys.path.insert(
|
|
0, os.path.dirname(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=5, suite="runtime-1gpu")
|
|
|
|
from tokenspeed.runtime.execution.accept_simulation import ( # noqa: E402
|
|
parse_simulated_accept_length,
|
|
simulated_accept_lengths,
|
|
simulated_output_tokens,
|
|
)
|
|
from tokenspeed.runtime.execution.model_executor import ModelExecutor # noqa: E402
|
|
from tokenspeed.runtime.execution.output_layout import ( # noqa: E402
|
|
ForwardOutputLayout,
|
|
)
|
|
from tokenspeed.runtime.layers.logits_processor import ( # noqa: E402
|
|
LogitsProcessorOutput,
|
|
)
|
|
from tokenspeed.runtime.sampling.sampling_batch_info import ( # noqa: E402
|
|
SamplingBatchInfo,
|
|
)
|
|
|
|
VERIFY_WIDTH = 4
|
|
|
|
|
|
def _scaled(length: str) -> int:
|
|
return parse_simulated_accept_length(
|
|
length, spec_algorithm="MTP", verify_width=VERIFY_WIDTH, draft_tree=False
|
|
)
|
|
|
|
|
|
def test_draft_trees_are_refused():
|
|
with pytest.raises(ValueError, match="chain drafts only"):
|
|
parse_simulated_accept_length(
|
|
"2", spec_algorithm="EAGLE3", verify_width=VERIFY_WIDTH, draft_tree=True
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("length", ["1", "2", "2.7", "3.25", "4"])
|
|
def test_widths_alternate_around_the_average(length):
|
|
average = float(length)
|
|
cache = torch.tensor([0, 17, 1027, 262143], dtype=torch.int32)
|
|
steps = []
|
|
for _ in range(401):
|
|
widths = simulated_accept_lengths(cache, _scaled(length))
|
|
steps.append(widths)
|
|
cache += widths.to(torch.int32)
|
|
widths = torch.stack(steps, dim=1)
|
|
|
|
assert widths[:, 0].min() >= 1
|
|
assert widths[:, 0].max() <= math.ceil(average)
|
|
steady = widths[:, 1:]
|
|
assert set(steady.unique().tolist()) <= {math.floor(average), math.ceil(average)}
|
|
for total in steady.sum(dim=1).tolist():
|
|
assert abs(total - average * steady.shape[1]) <= 1
|
|
|
|
|
|
def test_simulated_tokens_are_the_drafts_each_width_accepts():
|
|
candidates = torch.tensor([[10, 11, 12, 13]] * 4, dtype=torch.int32)
|
|
# Verify accepted draft 11 and wrote bonus 50; 99 is a stale entry.
|
|
tokens = torch.tensor([[11, 50, 99, 99]] * 4, dtype=torch.int32)
|
|
kept = torch.tensor([1, 2, 3, 4])
|
|
|
|
out = simulated_output_tokens(tokens, candidates, torch.full((4,), 2), kept)
|
|
emitted = [out[row, :width].tolist() for row, width in enumerate(kept.tolist())]
|
|
assert emitted == [[11], [11, 50], [11, 12, 13], [11, 12, 13, 13]]
|
|
|
|
|
|
class _NoAcceptSampler:
|
|
"""Verify accepts no draft: width 1 and each row's bonus token go into
|
|
shared buffers, whose entries past the bonus stay stale."""
|
|
|
|
BONUS = 7
|
|
STALE = 99
|
|
|
|
def __init__(self):
|
|
self.tokens = torch.full((8 * VERIFY_WIDTH,), self.STALE, dtype=torch.int32)
|
|
self.lengths = torch.zeros(8, dtype=torch.int32)
|
|
|
|
def sample(self, logits_output, sampling_info):
|
|
rows = logits_output.next_token_logits.shape[0]
|
|
return torch.zeros(rows, dtype=torch.int32), torch.ones(rows, dtype=torch.int32)
|
|
|
|
def verify(self, logits_output, sampling_info, candidates, *, tree):
|
|
rows = candidates.shape[0]
|
|
tokens = self.tokens[: candidates.numel()]
|
|
tokens.view(rows, -1)[:, 0] = self.BONUS
|
|
self.lengths[:rows].fill_(1)
|
|
return tokens, self.lengths[:rows]
|
|
|
|
|
|
def _candidates(rows: int) -> torch.Tensor:
|
|
return torch.arange(10, 10 + rows * VERIFY_WIDTH, dtype=torch.int32).view(rows, -1)
|
|
|
|
|
|
def _emitted(tokens: torch.Tensor, widths: list[int]) -> list[list[int]]:
|
|
rows = tokens.view(len(widths), -1)
|
|
return [rows[row, :width].tolist() for row, width in enumerate(widths)]
|
|
|
|
|
|
def _accepted_drafts(candidates: torch.Tensor, widths: list[int]) -> list[list[int]]:
|
|
"""What rows whose verify accepted nothing emit when they keep ``widths``."""
|
|
return [
|
|
(
|
|
[_NoAcceptSampler.BONUS]
|
|
if width == 1
|
|
else candidates[row, 1 : width + 1].tolist()
|
|
)
|
|
for row, width in enumerate(widths)
|
|
]
|
|
|
|
|
|
def _executor(pool_indices: list[int], cache_lengths: torch.Tensor) -> ModelExecutor:
|
|
executor = ModelExecutor.__new__(ModelExecutor)
|
|
executor.sampling_backend = _NoAcceptSampler()
|
|
executor.tree_spec = None
|
|
executor._simulated_accept_length = _scaled("2.7")
|
|
req_pool_indices = torch.zeros(8, dtype=torch.int64)
|
|
req_pool_indices[: len(pool_indices)] = torch.tensor(pool_indices)
|
|
executor.input_buffers = SimpleNamespace(
|
|
req_pool_indices_buf=req_pool_indices,
|
|
input_lengths_buf=torch.ones(8, dtype=torch.int32),
|
|
force_single_token_verify_buf=torch.zeros(8, dtype=torch.bool),
|
|
)
|
|
executor.runtime_states = SimpleNamespace(valid_cache_lengths=cache_lengths)
|
|
return executor
|
|
|
|
|
|
def test_decode_verify_keeps_simulated_widths_and_drafts_in_the_sampler_buffers():
|
|
cache_lengths = torch.arange(100, 108, dtype=torch.int32)
|
|
pool = [5, 2, 7]
|
|
executor = _executor(pool, cache_lengths)
|
|
sampler = executor.sampling_backend
|
|
ctx = SimpleNamespace(
|
|
bs=3,
|
|
num_extends=0,
|
|
decode_input_ids=None,
|
|
decode_query_indptr=None,
|
|
output_layout=ForwardOutputLayout(0, 0, 3, VERIFY_WIDTH),
|
|
sampled_top_k=0,
|
|
)
|
|
candidates = _candidates(3)
|
|
|
|
tokens, lengths = executor._sample_and_verify(object(), object(), ctx, candidates)
|
|
expected = simulated_accept_lengths(cache_lengths[pool], _scaled("2.7")).tolist()
|
|
assert expected == [3, 3, 1]
|
|
assert lengths.tolist() == expected
|
|
assert _emitted(tokens, expected) == _accepted_drafts(candidates, expected)
|
|
assert lengths.data_ptr() == sampler.lengths.data_ptr()
|
|
assert tokens.data_ptr() == sampler.tokens.data_ptr()
|
|
|
|
# Rows the scheduler forces to one token keep verify's token.
|
|
executor.input_buffers.force_single_token_verify_buf[1] = True
|
|
ctx.decode_input_ids = [-1, 9, -1]
|
|
tokens, lengths = executor._sample_and_verify(object(), object(), ctx, candidates)
|
|
forced = [expected[0], 1, expected[2]]
|
|
assert lengths.tolist() == forced
|
|
assert _emitted(tokens, forced) == _accepted_drafts(candidates, forced)
|
|
|
|
|
|
def test_mixed_round_simulates_only_its_decode_rows():
|
|
cache_lengths = torch.arange(200, 208, dtype=torch.int32)
|
|
pool = [3, 4, 2, 0]
|
|
executor = _executor(pool, cache_lengths)
|
|
ctx = SimpleNamespace(
|
|
bs=4,
|
|
num_extends=2,
|
|
decode_input_ids=None,
|
|
decode_query_indptr=None,
|
|
output_layout=ForwardOutputLayout(2, 2, 2, VERIFY_WIDTH),
|
|
sampled_top_k=0,
|
|
)
|
|
logits = LogitsProcessorOutput(next_token_logits=torch.zeros(2 + 2 * 4, 16))
|
|
info = SamplingBatchInfo(req_pool_indices=torch.tensor(pool), device="cpu")
|
|
candidates = _candidates(2)
|
|
|
|
tokens, lengths = executor._sample_and_verify(logits, info, ctx, candidates)
|
|
decode = simulated_accept_lengths(cache_lengths[pool[2:]], _scaled("2.7")).tolist()
|
|
assert decode == [3, 2]
|
|
assert lengths.tolist() == [1, 1, *decode]
|
|
assert _emitted(tokens[2:], decode) == _accepted_drafts(candidates, decode)
|