Skip to content

[1/3] kv: page ownership for prefix caching - #279

Merged
merceod merged 6 commits into
mainfrom
prefix-cache
Sep 23, 2026
Merged

merceod merged 6 commits into
mainfrom
prefix-cache

Conversation

@Gaurav-Shah05

@Gaurav-Shah05 Gaurav-Shah05 commented Sep 21, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Part of #210. First of three PRs for cross-request prefix KV reuse. This is groundwork only, w/o any observable changes.

Owner counts

PageArena keeps one int per page: acquire sets it to one, retain increments, release decrements and frees the page at zero, so a page goes back when its last owner frees it rather than at the allocating request's teardown. Nothing calls retain yet, so every page has one owner and the free rule is main's. PageAllocator and all call sites are untouched.

Sealed pages

seal marks a page as never written again, the precondition for a second owner to read it; freeing clears it. A stream that rewinds across a sealed page drops its pages. The only rewind today is reset_request(free=False) from DummyRowPool.reset, and nothing seals yet. The rule is stated for any rewind so the next rewind entry point inherits it.

The invariant

KVManager.assert_pages_conserved: every page free or owned, never both; an owner count equals the streams holding the page plus one for the sink; only sealed pages are shared; the page a stream is writing has one owner. Runs after admit, commit, reset_request and remove_request under MSTAR_KV_DEBUG_ASSERTS=1 (documented in docs/environment_variables.rst).

Host pages are untouched; CPUPagePool keeps no counts.

How was it tested?

12 tests in test/modular/test_kv_page_ownership.py. pytest test/modular -q: 937 passed, 20 skipped, 1 failed, the failure pre-existing on main. Bagel bit-identical to main at 424a112c on two chats and an image, compared in the same allocation.

Checklist

  • ruff check . passes
  • Added or updated tests / docs where relevant

@merceod merceod left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@garv901 garv901 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

- ``0``
- ``N > 0``: every N iterations log per-phase p50/p95/mean of the
worker main loop (speculate, await_gpu, submit_spec, ...).
* - ``MSTAR_KV_DEBUG_ASSERTS``

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: not a fan of this flag existing but it doesn't hurt ability, although does disperse testing across a wider than already-is range

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests don't need it beacuse they call assert_pages_conserved directly. I kept it for checking a live server. I'm currently stress testing #280 with shared prefixes, cancellations and eviction, and with the flag on, a page that leaks or gets freed while still in use raises at the op that caused it.

@stephen-dwq stephen-dwq left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@NSagan271 NSagan271 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also LGTM!

@merceod
merceod merged commit 3c2f797 into main Sep 23, 2026
3 checks passed
@merceod
merceod deleted the prefix-cache branch September 23, 2026 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants