-
Notifications
You must be signed in to change notification settings - Fork 119
fix: defer tp prealloc until first alloc #341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shipiyouniao
wants to merge
2
commits into
ovg-project:main
Choose a base branch
from
shipiyouniao:fix/tp2-kvcached-startup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| # SPDX-FileCopyrightText: Copyright contributors to the kvcached project | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import importlib | ||
| import sys | ||
| import types | ||
| from typing import Any | ||
|
|
||
| kcm: Any = None | ||
|
|
||
|
|
||
| def _load_kv_cache_manager(monkeypatch, *, use_worker_ipc: bool = False): | ||
| monkeypatch.setitem(sys.modules, "torch", types.ModuleType("torch")) | ||
|
|
||
| fake_vmm_ops: Any = types.ModuleType("kvcached.vmm_ops") | ||
| fake_vmm_ops.PageAllocator = object | ||
| fake_vmm_ops.InternalPage = object | ||
| fake_vmm_ops.kv_tensors_created = lambda *args, **kwargs: True | ||
| fake_vmm_ops.map_to_kv_tensors = lambda *args, **kwargs: True | ||
| fake_vmm_ops.unmap_from_kv_tensors = lambda *args, **kwargs: True | ||
| monkeypatch.setitem(sys.modules, "kvcached.vmm_ops", fake_vmm_ops) | ||
|
|
||
| fake_interfaces: Any = types.ModuleType("kvcached.integration.vllm.interfaces") | ||
| fake_interfaces.should_use_worker_ipc = lambda: use_worker_ipc | ||
| monkeypatch.setitem( | ||
| sys.modules, | ||
| "kvcached.integration.vllm.interfaces", | ||
| fake_interfaces, | ||
| ) | ||
|
|
||
| kcm = importlib.import_module("kvcached.kv_cache_manager") | ||
| return importlib.reload(kcm) | ||
|
|
||
|
|
||
| class FakeInternalPage: | ||
| def __init__(self, page_id: int, page_size: int): | ||
| self.page_id = page_id | ||
| self.page_size = page_size | ||
| self._free_blocks: list[int] = [] | ||
|
|
||
| def init(self, block_mem_size: int): | ||
| blocks_per_page = self.page_size // block_mem_size | ||
| self._free_blocks = list(range(self.page_id * blocks_per_page, | ||
| (self.page_id + 1) * blocks_per_page)) | ||
|
|
||
| def num_free_blocks(self) -> int: | ||
| return len(self._free_blocks) | ||
|
|
||
| def alloc(self, num_blocks: int = 1): | ||
| allocated = self._free_blocks[:num_blocks] | ||
| self._free_blocks = self._free_blocks[num_blocks:] | ||
| return allocated | ||
|
|
||
| def full(self) -> bool: | ||
| return not self._free_blocks | ||
|
|
||
| @staticmethod | ||
| def get_num_blocks(page_size: int, block_mem_size: int) -> int: | ||
| return page_size // block_mem_size | ||
|
|
||
|
|
||
| class FakePageAllocator: | ||
| def __init__(self, *args, **kwargs): | ||
| self.start_prealloc_calls = 0 | ||
| self.stop_prealloc_calls = 0 | ||
| self._free_pages = [0, 1, 2] | ||
|
|
||
| def set_should_use_worker_ipc_callback(self, callback): | ||
| self.should_use_worker_ipc_callback = callback | ||
|
|
||
| def set_broadcast_map_callback(self, callback): | ||
| self.broadcast_map_callback = callback | ||
|
|
||
| def set_broadcast_unmap_callback(self, callback): | ||
| self.broadcast_unmap_callback = callback | ||
|
|
||
| def start_prealloc_thread(self): | ||
| self.start_prealloc_calls += 1 | ||
|
|
||
| def _stop_prealloc_thread(self, timeout=None): | ||
| self.stop_prealloc_calls += 1 | ||
|
|
||
| def alloc_page(self): | ||
| return FakeInternalPage(self._free_pages.pop(0), kcm.PAGE_SIZE) | ||
|
|
||
| def free_pages(self, page_ids): | ||
| self._free_pages.extend(sorted(page_ids)) | ||
|
|
||
| def trim(self): | ||
| return None | ||
|
|
||
| def reset_free_page_order(self): | ||
| self._free_pages = sorted(self._free_pages) | ||
|
|
||
| def get_num_free_pages(self): | ||
| return len(self._free_pages) | ||
|
|
||
| def get_avail_physical_pages(self): | ||
| return len(self._free_pages) | ||
|
|
||
| def get_num_reserved_pages(self): | ||
| return 0 | ||
|
|
||
|
|
||
| def _build_manager(monkeypatch, *, world_size: int): | ||
| global kcm | ||
| kcm = _load_kv_cache_manager(monkeypatch) | ||
| monkeypatch.setattr(kcm, "PageAllocator", FakePageAllocator) | ||
| monkeypatch.setattr(kcm, "InternalPage", FakeInternalPage) | ||
| monkeypatch.setattr(kcm, "broadcast_kv_tensors_created", | ||
| lambda *args, **kwargs: True) | ||
|
|
||
| manager = kcm.KVCacheManager( | ||
| num_blocks=1024, | ||
| block_size=16, | ||
| cell_size=1024, | ||
| num_layers=16, | ||
| world_size=world_size, | ||
| async_sched=True, | ||
| ) | ||
| assert manager._post_init_done.wait(timeout=1) | ||
| return manager | ||
|
|
||
|
|
||
| def test_multi_process_prealloc_waits_until_first_alloc(monkeypatch): | ||
| manager = _build_manager(monkeypatch, world_size=2) | ||
|
|
||
| assert manager.page_allocator.start_prealloc_calls == 0 | ||
|
|
||
| block_ids = manager.alloc(1) | ||
|
|
||
| assert block_ids == [0] | ||
| assert manager.page_allocator.start_prealloc_calls == 1 | ||
|
|
||
|
|
||
| def test_single_process_keeps_eager_prealloc(monkeypatch): | ||
| manager = _build_manager(monkeypatch, world_size=1) | ||
|
|
||
| assert manager.page_allocator.start_prealloc_calls == 1 | ||
|
|
||
|
|
||
| def test_clear_restarts_prealloc_thread(monkeypatch): | ||
| manager = _build_manager(monkeypatch, world_size=1) | ||
|
|
||
| assert manager.page_allocator.start_prealloc_calls == 1 | ||
|
|
||
| manager.clear() | ||
|
|
||
| assert manager.page_allocator.stop_prealloc_calls == 1 | ||
| assert manager.page_allocator.start_prealloc_calls == 2 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.