fix(pd): size default retraction snapshots for resident capacity

Signed-off-by: sunshengqi <69040952+simple-sun@users.noreply.github.com>
This commit is contained in:
sunshengqi
2026-11-16 19:01:29 +08:00
parent f2457e444d
commit 84644abef3
7 changed files with 364 additions and 43 deletions
+23 -14
View File
@@ -532,7 +532,7 @@ Trainer-side contract:
| `--prefill-graph-capture-batch-sizes` | Request capacities for inline recurrent-layer (KDA, Mamba2, GDN) prefill capture. Replay selects the smallest compatible capacity that fits the batch, padding the missing requests. Unset: `4` (capped by `--max-num-seqs` per data-parallel rank), so a prefill round of up to 4 requests runs its recurrent layers inside the graph; a round of more requests, or one too full for the padding, runs them at eager breaks. Each added count is one more graph per token bucket, with its capture time and memory. |
| `--retraction-snapshot-host-gb` | Explicit size, in gigabytes, of the per-rank pinned Host pool that holds a retraction image's tail (a retracted request's blocks of groups Host L2 never holds, a state group's unpublished input block, and its slot state; the published blocks, the partial last page and the live state block included, go to Host L2 and stay pinned there until the request is restored; with `--disable-kvstore` the pool holds whole images). Wins over `--retraction-snapshot-ratio` when set; `0` (the default) means not set, like `--kvstore-size` against `--kvstore-ratio`. A request a capacity retraction suspends is imaged here and resumes exactly where it stopped once the image is copied back; nothing is recomputed. When no resident's image fits -- the pool or its rows are exhausted -- the scheduler aborts the newest resident instead (finish `err_type` 524, see "Retraction snapshot pool" below), so the pool bounds how much can be suspended at once, never how long an admission waits. Fused and decode roles only; the prefill and encode roles never retract and ignore it with a log. Allowed under `--kv-parallel-size > 1`. |
| `--retraction-snapshot-ratio` | Size of the retraction snapshot pool as a multiple of this rank's Device KV capacity (the base `--kvstore-ratio` scales too), used when `--retraction-snapshot-host-gb` is not set. Unset (the default) derives the pool at device build: 0.1 of the Device KV without the Host KVStore (whole images, kept cheap to pin; `1` uses the Device KV pool's usable LCM-block count, with request rows limited separately), else the image tails of `--retraction-snapshot-max-requests` requests (table below). An explicit `0` is the one way to run **without a pool**: nothing can be imaged, so every capacity block aborts its victim (the newest resident). |
| `--retraction-snapshot-max-requests` | Slot-state image rows of the retraction snapshot pool, that is, the most requests suspended at once, and the request count the derived pool size is multiplied by. `0` (the default) derives to `--max-num-seqs / attention-DP size`, the rank-local execution limit B; extra resident slots do not increase this default. Refused with `--retraction-snapshot-ratio 0`, which has no rows to size. When no row is free the scheduler aborts a resident rather than waiting. |
| `--retraction-snapshot-max-requests` | Slot-state image rows of the retraction snapshot pool, that is, the most requests suspended at once, and the request count the derived pool size is multiplied by. `0` (the default) derives to the rank-local resident capacity C: B+E on decode and B on fused, where B is max-num-seqs divided by attention-DP size. An explicit positive cap is preserved. Refused with `--retraction-snapshot-ratio 0`, which has no rows to size. When no row is free the scheduler aborts a resident rather than waiting. |
| `--debug-force-retraction-interval` | **Test only.** Every `N` scheduler plans retract the oldest quiescent decoding request (`N > 0`), or with `-N` the one prefilling request between its chunks, without capacity pressure, so a test run exercises the suspend/restore path on every request (the bitwise continuation oracle of [Numerics](../design/numerics.md)). Refused with `--retraction-snapshot-ratio 0` (no pool to image into). `0` (the default) is off; never set it in serving. |
For pure prefill, token capacities count newly computed tokens, not cached
@@ -1055,9 +1055,10 @@ Other cache layouts have different costs.
Use an explicit extra count when measuring the feature. Set E=0 at startup
to retain C=B; shrinking a live request pool is not supported. A request
keeps its original slot when reception completes and waits for an execution
permit before its first decode. Extra slots do not raise the default
snapshot request limit; see [Retraction snapshot pool](#retraction-snapshot-pool)
for the independent request-count and Host-memory limits.
permit before its first decode. The default snapshot request limit also
grows to C; an explicit positive limit is preserved. See
[Retraction snapshot pool](#retraction-snapshot-pool) for its Host-memory
cost and independent image-storage limits.
This change requires matching TokenSpeed runtime and native components, even
when E=0. Do not mix runtime or scheduler builds from before this API change
@@ -1454,19 +1455,27 @@ The first rule that applies wins:
| Arguments | Pool size | Request cap (`--retraction-snapshot-max-requests` when `0`) |
|---|---|---|
| `--retraction-snapshot-host-gb G` (G > 0) | `G` GB, whole LCM blocks | `--max-num-seqs / attention-DP size` |
| `--retraction-snapshot-host-gb G` (G > 0) | `G` GB, whole LCM blocks | Rank-local resident capacity C (B+E on decode; B on fused) |
| `--retraction-snapshot-ratio R` (R > 0) | `R` x this rank's Device LCM blocks | same |
| `--retraction-snapshot-ratio 0` | **no pool**: a capacity block aborts its victim | `0` (an explicit cap is refused) |
| neither, `--disable-kvstore` | 0.1 x this rank's Device LCM blocks (whole images) | same as above |
| neither, Host KVStore on | request cap x one image tail | same as above |
| neither, `--disable-kvstore` | 0.1 x this rank's Device LCM blocks (whole images) | C |
| neither, Host KVStore on | request cap x one image tail | C |
The default snapshot request cap is B, the rank-local execution limit,
even when a decode rank has C=B+E resident rows. Increasing E does not
increase this cap. It limits the number of slot-state images independently
of the KV bytes in the snapshot pool and Host L2. Set
--retraction-snapshot-max-requests explicitly to allow more simultaneous
images, and size the Host pools for those images; raising either limit
alone does not guarantee that every resident can be suspended.
The default snapshot request cap is the rank-local resident capacity C:
B+E on decode and B on fused. An explicit positive
--retraction-snapshot-max-requests overrides this default. The cap budgets
simultaneous images independently of Host KV bytes. A retraction frees a
Device request row for another admission, so continued admissions can create
demand for more than C simultaneous images; C is a default budget, not a
guarantee that all such requests can be suspended. Raise the explicit cap
and provide enough Host space when the workload needs more.
A larger cap allocates more slot-state rows. With Host KVStore enabled and
neither snapshot size nor ratio specified, it also increases the derived
tail pool. Without Host KVStore, the default snapshot KV pool remains 0.1
of the Device KV block count; raising the row cap does not increase that
ratio. Explicit snapshot sizes and ratios remain unchanged. In every case,
both image rows and Host storage must fit.
One image tail, per cache group, is one block for a group that publishes
to Host L2 (a state group's unpublished input block; a history group's
+4 -4
View File
@@ -728,10 +728,10 @@ slot; `scheduler.md` §2 and §4 describe the FSM side):
(`--disable-kvstore`) the whole image goes there, and the derived default
is a tenth of the Device KV -- an on-by-default pool stays cheap to pin;
retraction-snapshot-ratio=1 uses Device KV's usable LCM-block count for
snapshot KV. Image rows are limited separately and default to the
rank-local execution limit, excluding extra resident slots; enough Host
space alone does not make
every resident simultaneously suspendable.
snapshot KV. Image rows are limited separately and default to rank-local
resident capacity, including extra decode slots; an explicit positive cap
takes precedence. Both rows and Host space must fit, so the default does
not guarantee that every resident can be suspended.
The runtime side is **one** `HostCacheExecutor` (`cache/l2/executor.py`)
owning two `HostCacheStorage` buffers over the same field geometry, each with
+7 -5
View File
@@ -1940,11 +1940,13 @@ with no request suspended both must read empty and zero.
the newest resident instead of imaging one; §2) and `max_retracted_requests`
(`0` with no pool, `> 0` with one; the runtime's slot-state arena has that
many rows plus the null row) are validated together. The P role refuses a
snapshot pool. The runtime defaults the snapshot request cap to the
rank-local execution limit B, independently of C=B+E resident rows. Extra
residency does not increase the snapshot row count or guarantee enough Host
space to image every resident. Rows and image storage must both fit; when
no eligible image fits, capacity recovery uses the abort policy in §2.
snapshot pool. The runtime defaults the snapshot request cap to rank-local
resident capacity C (B+E on D, B on fused); an explicit positive cap takes
precedence. This is a budget for suspended images: retraction frees Device
rows for new admissions, which can create further demand for images. The
row budget does not guarantee enough Host space. Rows and image storage
must both fit; when no eligible image fits, capacity recovery uses the abort
policy in §2.
L3 addresses page-cyclic sharded groups through replica-stable
object keys ([cache concepts](cache-concepts.md)).
`debug_force_retraction_interval` (§2) is the only
+5 -4
View File
@@ -51,8 +51,9 @@ from dataclasses import dataclass
#: KV: whole images at a price an on-by-default pool can pin. A ratio of 1
#: gives snapshot KV the same usable LCM-block count as Device KV; enabling
#: the KVStore moves published pages to Host L2. Neither raises the image-row
#: cap, which defaults to execution capacity rather than resident capacity.
#: Both Host space and rows must fit.
#: cap, which defaults to rank-local resident capacity, including extra
#: decode slots. Explicit positive caps are preserved; Host space and rows
#: must both fit.
DEFAULT_DEVICE_RATIO = 0.1
@@ -68,8 +69,8 @@ class RetractionPoolRequest:
Attributes:
host_gb: ``--retraction-snapshot-host-gb``; 0 is not set.
ratio: ``--retraction-snapshot-ratio``; None is not set, 0 is no pool.
max_retracted_requests: Slot-state rows (explicit, or the rank-local
execution limit, excluding extra resident slots); 0 only with no pool.
max_retracted_requests: Slot-state rows (explicit, or rank-local resident
capacity, including extra decode slots); 0 only with no pool.
tail_lcm_blocks_per_request: LCM blocks one image's tail needs beside
Host L2 (``tail_lcm_blocks_per_request``), the derived size's
unit when an L2 tier exists.
+25 -15
View File
@@ -626,8 +626,8 @@ class ServerArgs:
# explicit 0 disables the pool: every capacity block then aborts its
# victim), else derived at device build -- 0.1 of the Device KV without
# L2, the tails of ``retraction_snapshot_max_requests`` images with L2
# (cache/l2/sizing.py). The request cap derives from --max-num-seqs per
# attention-DP rank when 0 (resolve_retraction_snapshot_options).
# (cache/l2/sizing.py). When 0, the request cap derives from rank-local
# resident capacity, including decode extra slots (resolve_retraction_snapshot_options).
retraction_snapshot_host_gb: float = 0.0
retraction_snapshot_ratio: float | None = None
retraction_snapshot_max_requests: int = 0
@@ -1655,9 +1655,9 @@ class ServerArgs:
a fraction of this rank's Device KV capacity, an explicit 0 disabling
it; neither given means the device builder derives the size
(``cache/l2/sizing.py``). ``--retraction-snapshot-max-requests`` 0
derives to the rank-local execution limit B (max-num-seqs over the
attention-DP ranks). Extra resident slots do not increase this cap;
snapshot request rows and Host memory are separate limits.
derives to rank-local resident capacity C: max-num-seqs over the
attention-DP ranks plus decode extra slots. Explicit positive caps
are preserved; snapshot request rows and Host memory are separate limits.
The prefill and encode roles never retract: their knobs are resolved
to no pool here with a log, as the KVStore's are off on the encode
role.
@@ -1717,22 +1717,30 @@ class ServerArgs:
)
return
if max_requests == 0:
# The cap is max_num_seqs per attention-DP rank: __post_init__
# resolves both before the cache.
if self.mapping is None or self.max_num_seqs is None:
# Extra slots are already rank-local; only the execution limit
# is divided by attention DP. __post_init__ resolves both first.
if (
self.mapping is None
or self.max_num_seqs is None
or self.disaggregation_decode_extra_slots is None
):
raise ValueError(
"resolve_parallelism() and resolve_memory_and_scheduling() must "
"run before resolve_cache(): the derived "
"--retraction-snapshot-max-requests is max_num_seqs per "
"attention-DP rank"
"--retraction-snapshot-max-requests uses rank-local "
"execution capacity plus resolved decode extra slots"
)
dp_size = max(self.mapping.attn.dp_size, 1)
self.retraction_snapshot_max_requests = self.max_num_seqs // dp_size
self.retraction_snapshot_max_requests = (
self.max_num_seqs // dp_size + self.disaggregation_decode_extra_slots
)
if self.retraction_snapshot_max_requests == 0:
raise ValueError(
"the retraction snapshot pool derives its request cap from "
f"--max-num-seqs ({self.max_num_seqs}) per attention-DP rank "
f"({dp_size}), which is 0 here; raise --max-num-seqs to at "
f"({dp_size}) plus --disaggregation-decode-extra-slots "
f"({self.disaggregation_decode_extra_slots}), which is 0 here; "
"raise --max-num-seqs to at "
"least the attention-DP size or pass "
"--retraction-snapshot-max-requests"
)
@@ -3218,7 +3226,8 @@ class ServerArgs:
"on decode, and zero on other roles. Set to zero to disable the "
"extra capacity. Persistent request state grows with resident "
"capacity and can reduce the KV budget; all requests share one KV "
"pool. The snapshot request limit is configured separately.",
"pool. The snapshot request limit defaults to resident capacity "
"unless explicitly configured.",
)
parser.add_argument(
"--max-total-tokens",
@@ -3461,8 +3470,9 @@ class ServerArgs:
default=ServerArgs.retraction_snapshot_max_requests,
help="Slot-state image rows of the retraction snapshot pool, i.e. the "
"most requests suspended at once. 0 (the default) derives to this "
"rank's execution limit B, --max-num-seqs / attention-DP size. "
"Extra resident slots do not increase this default. Image rows and "
"rank's resident capacity C: --max-num-seqs / attention-DP size "
"plus --disaggregation-decode-extra-slots. An explicit positive "
"limit is preserved. Image rows and "
"Host KV space are separate limits; when no eligible image fits, "
"capacity recovery aborts a resident rather than imaging it. "
"Refused with --retraction-snapshot-ratio 0.",
+74 -1
View File
@@ -88,6 +88,48 @@ def test_default_is_a_derived_pool_with_the_request_cap_from_max_num_seqs():
ServerArgs(model="x", world_size=2, data_parallel_size=2, max_num_seqs=1)
@pytest.mark.parametrize(
"dp,extra,cap,want",
[
(1, None, 0, 32),
(2, None, 0, 16),
(1, 0, 0, 16),
(2, 0, 0, 8),
(1, 5, 0, 21),
(2, 5, 0, 13),
(2, None, 3, 3),
(2, 5, 20, 20),
],
)
def test_decode_snapshot_rows_follow_resident_capacity_unless_overridden(
dp, extra, cap, want
):
args = ServerArgs(
model="x",
world_size=dp,
data_parallel_size=dp,
max_num_seqs=16,
disaggregation_mode="decode",
disaggregation_decode_extra_slots=extra,
retraction_snapshot_max_requests=cap,
)
assert args.retraction_snapshot_max_requests == want
assert args.max_num_seqs == 16
@pytest.mark.parametrize("host_gb,want", [(0.0, 0), (1.0, 32)])
def test_decode_snapshot_disable_and_size_override_with_extra_slots(host_gb, want):
args = ServerArgs(
model="x",
max_num_seqs=16,
disaggregation_mode="decode",
retraction_snapshot_ratio=0.0,
retraction_snapshot_host_gb=host_gb,
)
assert args.retraction_snapshot_max_requests == want
assert args.retraction_snapshot_pool_disabled == (want == 0)
def test_explicit_size_and_ratio_pass_through_and_each_is_enough_alone():
args = ServerArgs(model="x", retraction_snapshot_host_gb=4.0)
assert (args.retraction_snapshot_host_gb, args.retraction_snapshot_ratio) == (
@@ -253,10 +295,41 @@ def test_resolution_order_size_then_ratio_then_derived(l2_tier):
assert sizing.lcm_blocks == (8 * 3 if l2_tier else int(DEVICE * 0.1))
assert sizing.source.startswith("derived")
if not l2_tier:
# Explicit ratio 1 is how a deployment makes every resident suspendable.
# Ratio 1 matches Device blocks; request rows remain a separate limit.
assert _resolve(_request(ratio=1.0), l2_tier=False).lcm_blocks == DEVICE
@pytest.mark.parametrize(
"l2_tier,host_gb,ratio,want_blocks",
[
(True, 0.0, None, 48),
(False, 0.0, None, 4),
(True, 0.0105, None, 10),
(True, 0.0, 0.25, 10),
],
)
def test_extra_residency_grows_only_automatically_sized_image_tails(
l2_tier, host_gb, ratio, want_blocks
):
args = ServerArgs(
model="x",
max_num_seqs=8,
disaggregation_mode="decode",
disable_kvstore=not l2_tier,
retraction_snapshot_host_gb=host_gb,
retraction_snapshot_ratio=ratio,
)
request = RetractionPoolRequest(
host_gb=args.retraction_snapshot_host_gb,
ratio=args.retraction_snapshot_ratio,
max_retracted_requests=args.retraction_snapshot_max_requests,
tail_lcm_blocks_per_request=3,
)
sizing = _resolve(request, l2_tier=args.enable_kvstore)
assert sizing.max_retracted_requests == 16
assert sizing.lcm_blocks == want_blocks
def test_ratio_zero_resolves_to_no_pool_and_too_small_sizes_are_refused():
assert _resolve(_request(ratio=0.0, rows=0), l2_tier=True) is NO_POOL
assert NO_POOL == RetractionPoolSizing(0, 0, "--retraction-snapshot-ratio 0")
@@ -435,3 +435,229 @@ def test_extra_request_slots_do_not_bypass_kv_admission():
assert not plan.aborts
assert scheduler.remote_prefilling_size() == 1
assert scheduler.retracted_size() == 0
def _decode_growth_blocked_by_ready_requests(snapshot_rows, snapshot_pages):
cfg = _config(1, 3)
cfg.num_device_pages = 17
cfg.num_snapshot_pages = snapshot_pages
cfg.max_retracted_requests = snapshot_rows
groups = cfg.cache_groups
groups[0].total_pages = 17
cfg.cache_groups = groups
scheduler = ts.Scheduler(cfg)
scheduler.submit_requests(
[
_spec("running", [1, 2]),
_spec("waiting-a", list(range(101, 109))),
_spec("waiting-b", list(range(201, 209))),
]
)
for rid in ("running", "waiting-a", "waiting-b"):
_send(scheduler, ts.PD.BootstrappedEvent(rid))
_admit_all(scheduler, ("running", "waiting-a", "waiting-b"))
_ready(scheduler, "running")
assert _forward_ids(scheduler.next_execution_plan()) == ["running"]
_advance(scheduler, "running", [43])
_ready(scheduler, "waiting-a", "waiting-b")
assert scheduler.ready_waiting_size() == 2
assert scheduler.available_lcm_blocks() == 4
# The running request grows from 2 to 15 blocks. Both 5-block waiters
# must give way: the 4 free blocks plus either one's release cannot fit it.
_send(
scheduler,
_event(
"UpdateReserveNumTokens",
"running",
reserve_num_tokens_in_next_schedule_event=26,
),
)
return scheduler, scheduler.next_execution_plan()
def test_ready_waiting_requests_retract_beyond_batch_size_and_resume():
scheduler, pressure = _decode_growth_blocked_by_ready_requests(3, 257)
[snapshot] = [op for op in pressure.cache if isinstance(op, ts.Cache.SnapshotOp)]
assert list(snapshot.request_ids) == ["waiting-a", "waiting-b"]
assert _forward_ids(pressure) == ["running"]
assert not pressure.aborts
assert scheduler.retracted_size() == 2 # More suspended requests than B=1.
assert scheduler.allocated_request_slots() == 1
assert scheduler.active_decode_slots() == 1
assert scheduler.ready_waiting_size() == 0
assert scheduler.snapshot_pool_free_blocks() == 248
_ack(scheduler, snapshot, ts.Cache.SnapshotDoneEvent)
_advance(scheduler, "running", [44])
_send(
scheduler,
_event(
"UpdateReserveNumTokens",
"running",
reserve_num_tokens_in_next_schedule_event=1,
),
)
busy = scheduler.next_execution_plan()
assert _forward_ids(busy) == ["running"]
assert not any(isinstance(op, ts.Cache.RestoreOp) for op in busy.cache)
assert not busy.aborts
_advance(scheduler, "running", [45])
_finish(scheduler, "running")
restore_a = scheduler.next_execution_plan()
[restore] = [op for op in restore_a.cache if isinstance(op, ts.Cache.RestoreOp)]
assert list(restore.request_ids) == ["waiting-a"]
assert _forward_ids(restore_a) == []
assert scheduler.allocated_request_slots() == 1
assert scheduler.active_decode_slots() == 0
_ack(scheduler, restore, ts.Cache.RestoreDoneEvent)
assert scheduler.ready_waiting_size() == 1
assert scheduler.request_token_size("waiting-a") == 9
restore_b = scheduler.next_execution_plan()
[restore] = [op for op in restore_b.cache if isinstance(op, ts.Cache.RestoreOp)]
assert list(restore.request_ids) == ["waiting-b"]
assert _forward_ids(restore_b) == ["waiting-a"]
assert list(_find_forward_op(restore_b).decode_input_ids) == [42]
assert scheduler.active_decode_slots() == 1
assert scheduler.allocated_request_slots() == 2
_ack(scheduler, restore, ts.Cache.RestoreDoneEvent)
assert scheduler.ready_waiting_size() == 1
assert scheduler.retracted_size() == 0
assert scheduler.snapshot_pool_free_blocks() == 256
_advance(scheduler, "waiting-a", [46])
assert scheduler.request_token_size("waiting-a") == 10
_finish(scheduler, "waiting-a")
resumed = scheduler.next_execution_plan()
assert _forward_ids(resumed) == ["waiting-b"]
assert list(_find_forward_op(resumed).decode_input_ids) == [42]
assert scheduler.request_token_size("waiting-b") == 9
assert resumed.remote_prefill is None
_advance(scheduler, "waiting-b", [47])
assert scheduler.request_token_size("waiting-b") == 10
_finish(scheduler, "waiting-b")
assert _forward_ids(scheduler.next_execution_plan()) == []
assert scheduler.allocated_request_slots() == 0
assert scheduler.active_decode_slots() == 0
assert scheduler.available_lcm_blocks() == 16
assert scheduler.snapshot_pool_free_blocks() == 256
@pytest.mark.parametrize(
"snapshot_rows,snapshot_pages,suspended,aborted,limiting_arg",
[
(1, 257, ["waiting-a"], ["waiting-b"], "--retraction-snapshot-max-requests"),
(3, 2, [], ["waiting-b", "waiting-a"], "--retraction-snapshot-host-gb"),
],
ids=["snapshot-rows-exhausted", "snapshot-kv-exhausted"],
)
def test_ready_waiting_capacity_abort_preserves_progress(
snapshot_rows, snapshot_pages, suspended, aborted, limiting_arg
):
scheduler, pressure = _decode_growth_blocked_by_ready_requests(
snapshot_rows, snapshot_pages
)
snapshots = [op for op in pressure.cache if isinstance(op, ts.Cache.SnapshotOp)]
assert [rid for op in snapshots for rid in op.request_ids] == suspended
assert [abort.request_id for abort in pressure.aborts] == aborted
for abort in pressure.aborts:
assert abort.reason == ts.AbortReason.ImageDoesNotFit
assert limiting_arg in abort.detail
assert _forward_ids(pressure) == ["running"]
assert scheduler.retracted_size() == len(suspended)
assert scheduler.allocated_request_slots() == 1
assert scheduler.active_decode_slots() == 1
for snapshot in snapshots:
_ack(scheduler, snapshot, ts.Cache.SnapshotDoneEvent)
_advance(scheduler, "running", [44])
assert scheduler.request_token_size("running") == 5
_finish(scheduler, "running")
for rid in suspended:
plan = scheduler.next_execution_plan()
[restore] = [op for op in plan.cache if isinstance(op, ts.Cache.RestoreOp)]
assert list(restore.request_ids) == [rid]
assert not plan.aborts
_ack(scheduler, restore, ts.Cache.RestoreDoneEvent)
resumed = scheduler.next_execution_plan()
assert _forward_ids(resumed) == [rid]
assert list(_find_forward_op(resumed).decode_input_ids) == [42]
_advance(scheduler, rid, [45])
assert scheduler.request_token_size(rid) == 10
_finish(scheduler, rid)
drained = scheduler.next_execution_plan()
assert _forward_ids(drained) == []
assert not drained.aborts
assert scheduler.retracted_size() == 0
assert scheduler.allocated_request_slots() == 0
assert scheduler.active_decode_slots() == 0
assert scheduler.available_lcm_blocks() == 16
assert scheduler.snapshot_pool_free_blocks() == snapshot_pages - 1
def test_long_decode_retracts_a_ready_request_when_reserved_headroom_runs_out():
cfg = _config(1, 2)
cfg.prefix_granularity = 64
cfg.num_device_pages = 193
cfg.num_snapshot_pages = 193
cfg.max_retracted_requests = 2
cfg.cache_groups = [
ts.CacheGroupConfig(
group_id="history",
block_granularity=64,
total_pages=193,
attn_kind=ts.AttnKind.Full,
transfer_policy=ts.CacheTransferPolicy.FullSuffix,
),
]
scheduler = ts.Scheduler(cfg)
requests = [
_spec("running", list(range(1024))),
_spec("waiting", list(range(10000, 12048))),
]
for request in requests:
request.max_new_tokens = 9000
scheduler.submit_requests(requests)
for rid in ("running", "waiting"):
_send(scheduler, ts.PD.BootstrappedEvent(rid))
_admit_all(scheduler, ("running", "waiting"))
_ready(scheduler, "running", "waiting")
# Admission prepays 4096 generation tokens each: 80 + 96 of 192 blocks.
assert scheduler.available_lcm_blocks() == 16
victims = []
max_used_blocks = 0
for token in range(20000, 28192):
plan = scheduler.next_execution_plan()
assert _forward_ids(plan) == ["running"]
assert not plan.aborts
assert not any(isinstance(op, ts.Cache.RestoreOp) for op in plan.cache)
for op in plan.cache:
if isinstance(op, ts.Cache.SnapshotOp):
victims.extend(op.request_ids)
_ack(scheduler, op, ts.Cache.SnapshotDoneEvent)
max_used_blocks = max(max_used_blocks, scheduler.active_lcm_blocks())
_advance(scheduler, "running", [token])
assert victims == ["waiting"]
assert max_used_blocks == 192
assert scheduler.request_token_size("running") == 9217
assert scheduler.retracted_size() == 1
_finish(scheduler, "running")
plan = scheduler.next_execution_plan()
[restore] = [op for op in plan.cache if isinstance(op, ts.Cache.RestoreOp)]
assert list(restore.request_ids) == ["waiting"]
assert scheduler.active_decode_slots() == 0
_ack(scheduler, restore, ts.Cache.RestoreDoneEvent)
resumed = scheduler.next_execution_plan()
assert _forward_ids(resumed) == ["waiting"]
assert list(_find_forward_op(resumed).decode_input_ids) == [42]
assert scheduler.request_token_size("waiting") == 2049
_advance(scheduler, "waiting", [77])
assert scheduler.request_token_size("waiting") == 2050
_finish(scheduler, "waiting")
assert _forward_ids(scheduler.next_execution_plan()) == []
assert scheduler.retracted_size() == 0
assert scheduler.allocated_request_slots() == 0
assert scheduler.active_decode_slots() == 0
assert scheduler.available_lcm_blocks() == 192
assert scheduler.snapshot_pool_free_blocks() == 192