-
Notifications
You must be signed in to change notification settings - Fork 8.9k
[HiCache] remove large host mem constraint #28614
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
94f810c
remove large host mem constraint
xiezhq-hermann 8d6599b
minor style
xiezhq-hermann ea595dd
Merge branch 'main' into hicache_smaller_host
xiezhq-hermann f7b4297
Merge branch 'main' of github.com:sgl-project/sglang into hicache_sma…
xiezhq-hermann efa0056
Merge branch 'main' into hicache_smaller_host
xiezhq-hermann 949b469
style fix and cleaning
xiezhq-hermann 031a9cb
Merge branch 'main' into hicache_smaller_host
xiezhq-hermann 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 |
|---|---|---|
|
|
@@ -1035,50 +1035,64 @@ def _update_host_leaf_status(self, node: TreeNode): | |
| def evict(self, params: EvictParams) -> EvictResult: | ||
| start_time = time.perf_counter() | ||
| num_tokens = params.num_tokens | ||
| leaves = list(self.evictable_leaves) | ||
| eviction_heap = [ | ||
| (self.eviction_strategy.get_priority(node), node) for node in leaves | ||
| ] | ||
| heapq.heapify(eviction_heap) | ||
| if self.cache_controller.write_policy == "write_back": | ||
| num_evicted = self._evict_write_back(num_tokens) | ||
| else: | ||
| num_evicted = self._evict_write_through(num_tokens) | ||
| self.update_eviction_metrics(num_evicted, start_time) | ||
| return EvictResult(num_tokens_evicted=num_evicted) | ||
|
|
||
| def _make_eviction_heap(self): | ||
| heap = [ | ||
| (self.eviction_strategy.get_priority(node), node) | ||
| for node in self.evictable_leaves | ||
| ] | ||
| heapq.heapify(heap) | ||
| return heap | ||
|
|
||
| def _promote_parent(self, node: TreeNode, heap) -> None: | ||
| # Once all of a node's children are evicted, it becomes a device leaf. | ||
| p = node.parent | ||
| if p is not self.root_node and all(c.evicted for c in p.children.values()): | ||
| heapq.heappush(heap, (self.eviction_strategy.get_priority(p), p)) | ||
|
|
||
| def _evict_write_through(self, num_tokens: int) -> int: | ||
| """write_through / write_through_selective: drop non-backuped leaves, | ||
| demote already-backuped ones. Nothing is staged to host during eviction, | ||
| so this is a plain on-the-fly pass. | ||
| """ | ||
| heap = self._make_eviction_heap() | ||
| num_evicted = 0 | ||
| write_back_nodes = [] | ||
| while num_evicted < num_tokens and len(eviction_heap): | ||
| _priority, x = heapq.heappop(eviction_heap) | ||
|
|
||
| while num_evicted < num_tokens and heap: | ||
| _priority, x = heapq.heappop(heap) | ||
| if x.lock_ref > 0: | ||
| continue | ||
|
|
||
| if not x.backuped: | ||
| if self.cache_controller.write_policy == "write_back": | ||
| # write to host if the node is not backuped | ||
| written = self.write_backup(x, write_back=True) | ||
| num_evicted += written | ||
| if written > 0: | ||
| write_back_nodes.append(x) | ||
| else: | ||
| num_evicted += self._evict_regular(x) | ||
| else: | ||
| if x.backuped: | ||
| num_evicted += self._evict_backuped(x) | ||
|
|
||
| for child in x.parent.children.values(): | ||
| if child in write_back_nodes: | ||
| continue | ||
| if not child.evicted: | ||
| break | ||
| else: | ||
| # all children are evicted or no children | ||
| new_priority = self.eviction_strategy.get_priority(x.parent) | ||
| heapq.heappush(eviction_heap, (new_priority, x.parent)) | ||
|
|
||
| if self.cache_controller.write_policy == "write_back": | ||
| self.writing_check(write_back=True) | ||
| for node in write_back_nodes: | ||
| assert node.backuped | ||
| self._evict_backuped(node) | ||
| num_evicted += self._evict_regular(x) | ||
| self._promote_parent(x, heap) | ||
| return num_evicted | ||
|
|
||
| self.update_eviction_metrics(num_evicted, start_time) | ||
| return EvictResult(num_tokens_evicted=num_evicted) | ||
| def _evict_write_back(self, num_tokens: int) -> int: | ||
| """eviction for write_back mode: demote already-backuped leaves, stage non-backuped ones to host if possible, otherwise drop them. | ||
| note this path will be deprecated in the future. | ||
| """ | ||
| heap = self._make_eviction_heap() | ||
| num_evicted = 0 | ||
| while num_evicted < num_tokens and heap: | ||
| _priority, x = heapq.heappop(heap) | ||
| if x.lock_ref > 0: | ||
| continue | ||
| if x.backuped: | ||
| num_evicted += self._evict_backuped(x) | ||
| elif self.write_backup(x, write_back=True) > 0: | ||
| self.writing_check(write_back=True) | ||
| num_evicted += self._evict_backuped(x) | ||
| else: | ||
| num_evicted += self._drop_subtree_no_host(x) | ||
| self._promote_parent(x, heap) | ||
| return num_evicted | ||
|
|
||
| def _evict_backuped(self, node: TreeNode): | ||
| # GPU -> CPU demotion: block moves from device to host. | ||
|
|
@@ -1105,6 +1119,45 @@ def _evict_regular(self, node: TreeNode): | |
| self._delete_leaf(node) | ||
| return num_evicted | ||
|
|
||
| def _drop_subtree_no_host(self, root: TreeNode) -> int: | ||
| nodes = [] | ||
| stack = [root] | ||
| while stack: | ||
| n = stack.pop() | ||
| nodes.append(n) | ||
| stack.extend(n.children.values()) | ||
|
|
||
| if any(n.host_ref_counter > 0 for n in nodes): | ||
| return 0 | ||
|
|
||
| logger.warning( | ||
| "write_back: KV cache on device are dropped without backup due to host memory pressure, subtree root %d, num_nodes %d", | ||
| root.id, | ||
| len(nodes), | ||
| ) | ||
|
|
||
| freed_device = 0 | ||
| for n in nodes: | ||
| if n.host_value is not None: | ||
| self._record_remove_event(n, medium=StorageMedium.CPU) | ||
| self.cache_controller.evict_host(n.host_value) | ||
| n.host_value = None | ||
| if n.value is not None: | ||
| self._record_remove_event(n, medium=StorageMedium.GPU) | ||
| self.cache_controller.mem_pool_device_allocator.free(n.value) | ||
| freed_device += len(n.value) | ||
| self.evictable_size_ -= len(n.value) | ||
| n.value = None | ||
| self.ongoing_write_through.pop(n.id, None) | ||
| self.evictable_leaves.discard(n) | ||
| self.evictable_host_leaves.discard(n) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should remove n on 'self.evictable_leaves' here? |
||
|
|
||
| key = root.key.child_key(self.page_size) | ||
| root.parent.children.pop(key, None) | ||
| self._update_leaf_status(root.parent) | ||
| self._update_host_leaf_status(root.parent) | ||
| return freed_device | ||
|
|
||
| def evict_host(self, num_tokens: int): | ||
| leaves = list(self.evictable_host_leaves) | ||
| eviction_heap = [ | ||
|
|
@@ -1169,6 +1222,10 @@ def load_back( | |
| self.dec_lock_ref(ancester_node) | ||
| return None | ||
|
|
||
| # Protect the nodes being loaded from host eviction. | ||
|
xiezhq-hermann marked this conversation as resolved.
|
||
| for n in nodes_to_load: | ||
|
xiezhq-hermann marked this conversation as resolved.
|
||
| n.protect_host() | ||
|
|
||
| device_indices = self.cache_controller.load( | ||
| host_indices=host_indices, | ||
| node_id=last_hit_node.id, | ||
|
|
@@ -1184,6 +1241,8 @@ def load_back( | |
| self.dec_lock_ref(ancester_node) | ||
| if device_indices is None: | ||
| # no sufficient GPU memory to load back KV caches | ||
| for n in nodes_to_load: | ||
| n.release_host() | ||
| logger.warning( | ||
| "load_back: FAILED to load %d tokens for node %d " | ||
| "even after eviction (evictable_size=%d)", | ||
|
|
@@ -1193,6 +1252,8 @@ def load_back( | |
| ) | ||
| return None | ||
|
|
||
| for n in nodes_to_load: | ||
| n.release_host() | ||
| self.ongoing_load_back[last_hit_node.id] = last_hit_node | ||
| offset = 0 | ||
| for node in nodes_to_load: | ||
|
|
||
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
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
Oops, something went wrong.
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.