Skip to content

fix(sglang): derive free page indices on CPU#384

Open
shipiyouniao wants to merge 1 commit into
ovg-project:mainfrom
shipiyouniao:fix/sglang-free-page-indices-cpu
Open

fix(sglang): derive free page indices on CPU#384
shipiyouniao wants to merge 1 commit into
ovg-project:mainfrom
shipiyouniao:fix/sglang-free-page-indices-cpu

Conversation

@shipiyouniao

Copy link
Copy Markdown

Summary

Fixes #382.

Move ElasticPagedTokenToKVPoolAllocator.free() page-id derivation to CPU before converting free indices into Python page ids for kvcached_allocator.free().

This keeps allocator bookkeeping out of the CUDA execution path and avoids relying on GPU tensor conversion behavior in the SGLang free path.

Validation

  • python -m pytest tests/test_sglang_allocator_free_cpu.py -q
  • python -m compileall -q kvcached/integration/sglang tests/test_sglang_allocator_free_cpu.py
  • git diff --check

Copilot AI review requested due to automatic review settings July 5, 2026 11:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses issue #382 by ensuring the SGLang elastic paged allocator computes unique page IDs on the CPU before converting them to Python integers for kvcached_allocator.free(), reducing allocator bookkeeping coupling to CUDA execution behavior.

Changes:

  • Move free_index to CPU before deriving page_ids via torch.unique in ElasticPagedTokenToKVPoolAllocator.free().
  • Adjust the CPU-side conversion of page_ids into Python indices passed to kvcached_allocator.free().
  • Add a regression test to assert the CPU move happens before torch.unique and that the allocator receives expected page IDs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/test_sglang_allocator_free_cpu.py Adds a unit test verifying free_index is moved to CPU before unique page-id derivation and that the freed page IDs are correct.
kvcached/integration/sglang/patches.py Updates the SGLang allocator free path to derive unique page IDs on CPU prior to Python conversion and freeing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread kvcached/integration/sglang/patches.py Outdated
Comment on lines 289 to 293
page_ids = torch.unique(free_index_cpu // self.page_size)
try:
indices: list[int] = page_ids.cpu().numpy().tolist()
indices: list[int] = page_ids.numpy().tolist()
except Exception:
indices = list(page_ids)
@shipiyouniao shipiyouniao force-pushed the fix/sglang-free-page-indices-cpu branch 2 times, most recently from 6259195 to 26c7de3 Compare July 6, 2026 16:31
@shipiyouniao shipiyouniao force-pushed the fix/sglang-free-page-indices-cpu branch from 26c7de3 to 3494825 Compare July 6, 2026 17:32
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.

SGLang paged allocator free path should move indices to CPU before torch.unique

2 participants