Skip to content

[Refactor] mem_cache: Give unified request rows one lifecycle state and one reset - #48010

Merged
ch-wan merged 1 commit into
mainfrom
cheng/refactor/unified-kv-planner-reclaim
Nov 9, 2026
Merged

ch-wan merged 1 commit into
mainfrom
cheng/refactor/unified-kv-planner-reclaim

Conversation

@ch-wan

@ch-wan ch-wan commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

This PR is part of a stack (oldest at bottom):

Motivation

Under --enable-unified-memory, what holds a request row was spread over several structures: the request table's free list, the PD transfers and receive holds the storage keeps per request, and the scheduler's session slots. Nothing tied them to one row state, so each reader derived "who holds rows" on its own:

  • A reset of the request table (flush_cache, the PD role switch, the one-batch benchmark) freed every row but left the per-request PD transfer and receive-hold records behind.
  • Releasing a streaming session's slot went through the request path, which reads a request id that a session slot does not have.
  • Unlimited-OCR document reuse freed a retained window's row through the table directly, past the storage.

This PR gives each unified request row one state, owned by the request table, and one reset.

Modifications

  • RowState in mem_cache/kv/requests.py: FREE, ACTIVE (a request's, running or waiting; a PD decode node's preallocated receive row too), PARKED (a streaming session's between turns) and HELD (beam members, a retained document window). The table's methods are the only transitions (alloc_rows(state=), free_rows, park, unpark, hold, clear), it counts rows by state (count, state_of), and a release of a row that is already free fails loudly instead of handing it out twice.
  • RequestTable.clear() resets every row state and then runs its clear hooks. KVStorage registers one that ends every recorded network transfer and receive hold, so nothing keyed by a row or a request outlives a reset. The scheduler already clears the table on every reset; no scheduler change.
  • KVStorage (mem_cache/kv/storage.py):
    • release_row ends network transfers only for an ACTIVE row: a parked or held row's request ended them when the row passed on, and a session slot or a retained window has no request id.
    • park_row, unpark_row and hold_row move a row through the table; take_rows hands beam members HELD rows.
  • Streaming sessions (session/streaming_session.py): a finished turn parks its slot's row; the next turn unparks it when its row is allocated (take), not when it matches, since a rejected turn runs nothing on the row.
  • Unlimited-OCR document reuse moves a retained window's row to HELD and releases it through the storage.
  • The idle invariant check asserts the state counts for a unified table: no ACTIVE row, and PARKED plus HELD rows equal what sessions and retained windows own. A static request pool keeps its count check.
  • StaticKVStorage answers park_row, unpark_row and hold_row as no-ops.
  • Tests: new test/registered/unit/mem_cache/kv/test_request_rows.py (state counts, a flush with parked sessions, a role-switch reset ending transfers and receive holds, closing a parked session, the idle check, a double release failing); test_uocr_doc_reuse_unit.py routes its fakes through a storage and checks the hold.

Behaviour changes

  • With --enable-unified-memory: closing a streaming session releases its row by state; a reset ends every recorded PD transfer and receive hold; a double release of a request row raises.
  • The admission rule is unchanged in this PR: nothing reads the row states for admission yet (the next PRs do).
  • No change without --enable-unified-memory: the static storage's new calls are no-ops, and a static request pool keeps its idle check.

Accuracy Tests

This stack is validated at its top, on main a4b8801197: the affected unit tests at the default and strict invariant levels, the CPU unit suite, the page-safety stress run and sweep, and serving accuracy and speed against main, all reported in #48014.

Checklist

  • Format with pre-commit on the changed files.
  • Unit tests added and updated.

CI States

Latest PR Test (Base): ❌ Run #42608653550
Latest PR Test (Extra): 🚫 Run #42608653432
Latest PR Test (AMD ROCm 10): ❌ Run #42608653389

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

@github-actions github-actions Bot added hicache Hierarchical Caching for SGLang unified-radix-cache memory-pool labels Nov 3, 2026
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-page-ledger branch from 5134760 to 7b372ac Compare November 4, 2026 06:18
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-planner-reclaim branch from a11a036 to c148fe9 Compare November 4, 2026 06:18
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-page-ledger branch from 7b372ac to 64a142d Compare November 4, 2026 07:42
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-planner-reclaim branch 2 times, most recently from 8c38f94 to 98ca05e Compare November 4, 2026 08:02
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-page-ledger branch 2 times, most recently from 8650182 to b620c98 Compare November 4, 2026 09:14
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-planner-reclaim branch from 98ca05e to 293a82f Compare November 4, 2026 09:14
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-page-ledger branch from b620c98 to 93db0fd Compare November 4, 2026 09:31
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-planner-reclaim branch from 293a82f to 2891d73 Compare November 4, 2026 09:31
This was referenced Nov 4, 2026
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-planner-reclaim branch from 0a7d0ca to 6454eae Compare November 5, 2026 11:24
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-page-ledger branch from ea0a16c to 35d4c63 Compare November 5, 2026 22:15
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-planner-reclaim branch from 6454eae to dfe1b18 Compare November 5, 2026 22:15
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-page-ledger branch from 35d4c63 to 0185108 Compare November 6, 2026 00:00
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-planner-reclaim branch from dfe1b18 to 4ce32c4 Compare November 6, 2026 00:00
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-page-ledger branch from 0185108 to ed6c74d Compare November 6, 2026 01:16
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-planner-reclaim branch from 4ce32c4 to 2482b08 Compare November 6, 2026 01:16
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-page-ledger branch from ed6c74d to 965972b Compare November 6, 2026 03:37
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-planner-reclaim branch from 2482b08 to 3aba5c2 Compare November 6, 2026 03:37
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-page-ledger branch from 965972b to cc62158 Compare November 6, 2026 06:34
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-planner-reclaim branch from 3aba5c2 to 87cba28 Compare November 6, 2026 06:34
@github-actions

This comment has been minimized.

@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-page-ledger branch from cc62158 to 2c9b8ec Compare November 6, 2026 10:43
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-planner-reclaim branch from 87cba28 to 6655400 Compare November 6, 2026 10:43
@github-actions

This comment has been minimized.

@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-page-ledger branch from 2c9b8ec to 66fc9a0 Compare November 6, 2026 12:31
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-planner-reclaim branch from 6655400 to d36d4fa Compare November 6, 2026 12:31
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-page-ledger branch from 66fc9a0 to 5f68220 Compare November 6, 2026 13:40
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-planner-reclaim branch from d36d4fa to 9076fef Compare November 6, 2026 13:40
@ch-wan
ch-wan force-pushed the cheng/refactor/unified-kv-page-ledger branch from 5f68220 to 9d0d417 Compare November 6, 2026 21:21
@github-actions

This comment has been minimized.

…nd one reset

The unified request table now keeps one state per row: free, active (a
request's, running or waiting, PD decode preallocated rows included), parked
(a streaming session between turns) or held (beam members, a retained
Unlimited-OCR document window). Its methods are the only transitions, and a
release of a row that is already free fails loudly.

- A streaming session parks its row when a turn finishes and unparks it when
  the next turn is allocated; a match alone does not unpark it, since a
  rejected turn runs nothing on the row. Beam members take held rows.
- `RequestTable.clear()` resets every row state and runs the storage's hook,
  which ends every recorded network transfer and receive hold. `flush_cache`,
  the PD role switch and the one-batch benchmark already clear the table, so
  nothing keyed by a row or a request outlives the reset.
- `release_row` ends network transfers only for an active row. A session
  slot or a retained window is no request and has no request id; a parked
  or held row's request ended its transfers when the row passed on.
- Unlimited-OCR document reuse moves a retained window's row to held and
  releases it through the storage instead of the table directly.
- The idle invariant check asserts the state counts for a unified table:
  no active row, and parked plus held rows equal what sessions and retained
  windows own. A static request pool keeps its count check.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hicache Hierarchical Caching for SGLang memory-pool unified-radix-cache

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant