Let tail chunks co-batch with cache-hit requests (SGLANG_CP_PREFILL_MIX_CHUNKED)
专题 S1 (design: docs_internal/perf/prefill-compute-intensity-plan.md S1.0-S1.11). A batch containing a chunked prefill request has been forced to bs=1 by the CP gate, so every chunk of a long prompt monopolizes a forward while short-extend cache-hit continuations queue — the direct cause of the replay TTFT tail (p90 19.3s / p99 49.2s at 91.8% cache hit). Yet mixed chunk batches already occur today (a freshly-chunked request keeps earlier-admitted small requests), proving the CP forward path is mixed-chunk-safe; only admission was asymmetric. Three changes, the first flag-independent: - add_chunked_req now seeds the budget with the chunk's TRUE prefix (was 0), so the CP cached tally and the buffer estimator's mqa_logits k_rows see the chunk's footprint before any later request is admitted (landmine D1). - New SGLANG_CP_PREFILL_MIX_CHUNKED (default OFF): with it on, the gate admits requests after a chunked one and lets the existing CP caps (extend / cached / buffer, now correctly seeded) bound the batch — a FULL chunk still ends the scan by consuming the chunk-clamped extend cap; only a tail chunk leaves headroom. A chunked prefix that is not page-aligned (rare sub-page final-chunk tail) keeps its batch solo (the CP page-aligned split would fail-fast otherwise). - The symm staging capacity identity (admission extend cap + request slack == staging pages) is asserted when the flag is on, locking the coupling the design relies on (plan doc S1.4 I2). Tests: 4 new adder units (budget seeding; tail chunk admits followers; full chunk solo by budget; non-aligned prefix solo); the 8-rank byte-exactness scenario gains a chunk-shaped request (2048-token page-aligned carried prefix + 512 extend) — all four phases (legacy/v2/symm/prefetch) byte-identical on g0033. Known pre-existing cross-file pollution noted in problems.md P16. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -42,11 +42,17 @@ class _CpGroupShim:
|
||||
|
||||
|
||||
def _build_scenario(rank: int, cp_size: int, device: torch.device):
|
||||
"""bs=4 with mixed prefix/extend lengths, extends crossing page bounds."""
|
||||
"""bs=5 with mixed prefix/extend lengths, extends crossing page bounds.
|
||||
|
||||
The last request is CHUNK-SHAPED (large page-aligned carried prefix +
|
||||
a tail-chunk-sized extend): with mix-chunked co-batching (plan doc S1)
|
||||
a continued chunk is exactly such a (prefix, extend) pair to the CP
|
||||
compose machinery, so this scenario byte-validates the mixed batch.
|
||||
"""
|
||||
page_size = 64
|
||||
batch_size = 4
|
||||
prefix_lens = [640, 1280, 320, 640]
|
||||
extend_lens = [95, 130, 64, 200]
|
||||
prefix_lens = [640, 1280, 320, 640, 2048]
|
||||
extend_lens = [95, 130, 64, 200, 512]
|
||||
batch_size = len(prefix_lens)
|
||||
kv_dim = 656 # fp8 layout bytes/token
|
||||
|
||||
from sglang.srt.mem_cache.cp_shared_kv_compute_owner import (
|
||||
|
||||
@@ -6,6 +6,8 @@ from unittest.mock import MagicMock
|
||||
|
||||
import torch
|
||||
|
||||
from sglang.srt.environ import envs
|
||||
|
||||
if "sgl_kernel" not in sys.modules:
|
||||
sys.modules["sgl_kernel"] = types.ModuleType("sgl_kernel")
|
||||
sys.modules["sgl_kernel"].__file__ = "sgl_kernel_stub.py"
|
||||
@@ -845,6 +847,107 @@ class TestPrefillAdder(CustomTestCase):
|
||||
)
|
||||
self.assertEqual([req.rid for req in adder.can_run_list], ["chunked"])
|
||||
|
||||
def test_add_chunked_req_seeds_true_prefix_into_cp_budget(self):
|
||||
# C1 (plan doc S1.1-1a): the chunk's carried prefix must count toward
|
||||
# the CP cached tally so later admission gates see its footprint.
|
||||
set_global_server_args_for_scheduler(
|
||||
ServerArgs(
|
||||
model_path="dummy",
|
||||
enable_nsa_prefill_context_parallel=True,
|
||||
nsa_prefill_cp_mode="in-seq-split",
|
||||
)
|
||||
)
|
||||
self.mock_token_allocator.available_size.return_value = 10000
|
||||
adder = self.create_adder(
|
||||
self.create_running_batch(),
|
||||
page_size=64,
|
||||
rem_input_tokens=4096,
|
||||
rem_chunk_tokens=4096,
|
||||
enable_cp_shared_kv_prefill_bs_gt1=True,
|
||||
cp_shared_kv_prefill_max_batch_requests=8,
|
||||
cp_shared_kv_prefill_max_total_extend_tokens=4096,
|
||||
)
|
||||
chunked = self.create_prefill_req("chunked", extend_input_len=128)
|
||||
chunked.prefix_indices = torch.zeros((256,), dtype=torch.int64)
|
||||
adder.add_chunked_req(chunked)
|
||||
self.assertEqual(adder.cp_shared_kv_prefill_total_cached_tokens, 256)
|
||||
self.assertEqual(adder.cp_shared_kv_prefill_total_extend_tokens, 128)
|
||||
|
||||
def _mix_chunked_adder(self, *, rem_chunk_tokens, extend_cap):
|
||||
set_global_server_args_for_scheduler(
|
||||
ServerArgs(
|
||||
model_path="dummy",
|
||||
enable_nsa_prefill_context_parallel=True,
|
||||
nsa_prefill_cp_mode="in-seq-split",
|
||||
)
|
||||
)
|
||||
self.mock_token_allocator.available_size.return_value = 100000
|
||||
return self.create_adder(
|
||||
self.create_running_batch(),
|
||||
page_size=64,
|
||||
rem_input_tokens=8192,
|
||||
rem_chunk_tokens=rem_chunk_tokens,
|
||||
enable_cp_shared_kv_prefill_bs_gt1=True,
|
||||
cp_shared_kv_prefill_max_batch_requests=8,
|
||||
cp_shared_kv_prefill_max_total_extend_tokens=extend_cap,
|
||||
)
|
||||
|
||||
def test_cp_prefill_mix_chunked_tail_chunk_admits_following_requests(self):
|
||||
# Plan doc S1: with the flag on, a TAIL chunk (extend below the chunk
|
||||
# budget, page-aligned carried prefix) leaves extend-cap headroom and
|
||||
# the following short-extend request co-batches with it.
|
||||
with envs.SGLANG_CP_PREFILL_MIX_CHUNKED.override(True):
|
||||
adder = self._mix_chunked_adder(rem_chunk_tokens=4096, extend_cap=4096)
|
||||
chunked = self.create_prefill_req("chunked", extend_input_len=1280)
|
||||
chunked.prefix_indices = torch.zeros((4096,), dtype=torch.int64)
|
||||
self.assertIsNone(adder.add_chunked_req(chunked)) # tail chunk
|
||||
normal = self.create_prefill_req("normal", extend_input_len=128)
|
||||
self.assertEqual(
|
||||
adder.add_one_req(
|
||||
normal, has_chunked_req=True, truncation_align_size=None
|
||||
),
|
||||
AddReqResult.CONTINUE,
|
||||
)
|
||||
self.assertEqual(
|
||||
[req.rid for req in adder.can_run_list], ["chunked", "normal"]
|
||||
)
|
||||
|
||||
def test_cp_prefill_mix_chunked_full_chunk_stays_solo_by_budget(self):
|
||||
# A FULL chunk consumes the whole (chunk-clamped) extend cap, so the
|
||||
# first following request is rejected by the extend gate — no special
|
||||
# code, the budget arithmetic ends the scan.
|
||||
with envs.SGLANG_CP_PREFILL_MIX_CHUNKED.override(True):
|
||||
adder = self._mix_chunked_adder(rem_chunk_tokens=256, extend_cap=4096)
|
||||
chunked = self.create_prefill_req("chunked", extend_input_len=512)
|
||||
chunked.prefix_indices = torch.zeros((4096,), dtype=torch.int64)
|
||||
self.assertIs(adder.add_chunked_req(chunked), chunked) # truncated
|
||||
self.assertEqual(chunked.extend_input_len, 256)
|
||||
normal = self.create_prefill_req("normal", extend_input_len=128)
|
||||
self.assertEqual(
|
||||
adder.add_one_req(
|
||||
normal, has_chunked_req=True, truncation_align_size=None
|
||||
),
|
||||
AddReqResult.OTHER,
|
||||
)
|
||||
self.assertEqual([req.rid for req in adder.can_run_list], ["chunked"])
|
||||
|
||||
def test_cp_prefill_mix_chunked_non_aligned_prefix_stays_solo(self):
|
||||
# I1 guard: a chunked prefix that is not a page multiple would break
|
||||
# the CP page-aligned split in a multi-request batch — keep it solo.
|
||||
with envs.SGLANG_CP_PREFILL_MIX_CHUNKED.override(True):
|
||||
adder = self._mix_chunked_adder(rem_chunk_tokens=4096, extend_cap=4096)
|
||||
chunked = self.create_prefill_req("chunked", extend_input_len=1280)
|
||||
chunked.prefix_indices = torch.zeros((100,), dtype=torch.int64)
|
||||
self.assertIsNone(adder.add_chunked_req(chunked))
|
||||
normal = self.create_prefill_req("normal", extend_input_len=128)
|
||||
self.assertEqual(
|
||||
adder.add_one_req(
|
||||
normal, has_chunked_req=True, truncation_align_size=None
|
||||
),
|
||||
AddReqResult.OTHER,
|
||||
)
|
||||
self.assertEqual([req.rid for req in adder.can_run_list], ["chunked"])
|
||||
|
||||
def test_cp_prefill_total_cached_limit_stops_second_cached_request(self):
|
||||
set_global_server_args_for_scheduler(
|
||||
ServerArgs(
|
||||
|
||||
Reference in New Issue
Block a user