Skip to content

[HiCache] Support host memory size smaller than device memory size - #16909

Closed
stmatengss wants to merge 7 commits into
sgl-project:mainfrom
openanolis:mateng/make_l2_can_less_than_l1
Closed

stmatengss wants to merge 7 commits into
sgl-project:mainfrom
openanolis:mateng/make_l2_can_less_than_l1

Conversation

@stmatengss

Copy link
Copy Markdown
Collaborator

Motivation

To support host memory size can be smaller than device memory size:

  1. In the evict method, adding a check for the return value of write_backup. If write_backup fails (e.g., due to host OOM), the code now gracefully falls back to _evict_regular, which evicts the node from the device without attempting to back it up.
  2. Modifying evict_host to allow evicting host memory even for nodes that are still resident on the device (x.evicted is False). This is crucial when the host cache is smaller than the device cache, as we need to reclaim host memory occupied by redundant copies to make space for new backups.

Modifications

Accuracy Tests

Benchmarking and Profiling

Checklist

Review Process

  1. Ping Merge Oncalls to start the PR flow. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • /tag-run-ci-label, /rerun-failed-ci, /tag-and-rerun-ci
  4. After green CI and required approvals, ask Merge Oncalls to merge.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @stmatengss, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the HiCache system's flexibility by allowing the host memory size to be smaller than the device memory size. This is achieved through refined eviction policies that handle host memory limitations more robustly, including graceful fallback for backup failures and enabling host memory reclamation for device-resident nodes. Additionally, the prefetch capacity limit is now dynamically calculated to adapt to different host and device memory configurations, ensuring efficient memory utilization across varied hardware setups.

Highlights

  • Relaxed Host Memory Size Constraint: The previous assertion requiring host memory to be strictly larger than device memory has been commented out, enabling configurations where host memory can be smaller than device memory.
  • Improved Eviction Resilience: The evict method now gracefully handles failures during write_backup operations (e.g., due to host OOM) by falling back to a regular eviction strategy, preventing data loss or system instability.
  • Enhanced Host Memory Eviction Logic: The evict_host method has been updated to allow eviction of host memory for nodes that are still resident on the device, which is crucial for reclaiming host memory when the host cache is smaller than the device cache.
  • Dynamic Prefetch Capacity Limit: The calculation for prefetch_capacity_limit now dynamically adjusts based on whether host memory is larger or smaller than device memory, optimizing prefetching behavior for various memory configurations.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@stmatengss

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces support for host memory being smaller than device memory in HiCache. The changes are logical and well-implemented. In cache_controller.py, the logic for calculating prefetch_capacity_limit is correctly adapted for this new scenario. In hiradix_cache.py, the evict method is improved for robustness, and the evict_host method is updated to allow evicting host memory for nodes still on the device, which is a key part of this feature. Finally, a now-invalid assertion in memory_pool_host.py is addressed. I have a couple of minor suggestions to improve code quality.

Comment on lines +305 to +310
if self.mem_pool_host.size > self.mem_pool_device.size:
self.prefetch_capacity_limit = int(
0.8 * (self.mem_pool_host.size - self.mem_pool_device.size)
)
else:
self.prefetch_capacity_limit = int(0.5 * self.mem_pool_host.size)

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.

medium

The magic numbers 0.8 and 0.5 for calculating prefetch_capacity_limit should be defined as named constants at the module level to improve readability and maintainability. For example, PREFETCH_CAPACITY_FACTOR_LARGE_HOST = 0.8 and PREFETCH_CAPACITY_FACTOR_SMALL_HOST = 0.5.

Comment on lines +161 to +163
# assert (
# self.size > device_pool.size
# ), "The host memory should be larger than the device memory with the current protocol"

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.

medium

Instead of commenting out the assertion, it's better to remove it completely to keep the code clean, since this assertion is no longer valid with the new changes.

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.

let's only skip this check when write back is selected, or when user specified smaller CPU memory size, fall back to write back policy

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.

Sure, we need a check here

Comment thread python/sglang/srt/mem_cache/hiradix_cache.py Outdated
@stmatengss

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@xiezhq-hermann

Copy link
Copy Markdown
Collaborator

please take a look at this PR as well:
#14718

stmatengss and others added 2 commits February 10, 2026 01:28
Resolved conflicts in:
- python/sglang/srt/managers/cache_controller.py: Applied L2 < L1 fix to the new attach_storage_backend method
- python/sglang/srt/mem_cache/hiradix_cache.py: Merged evictable_host_leaves tracking with conditional eviction logic

Changes preserved from this branch:
1. Support for host memory (L2) smaller than device memory (L1)
2. Conditional prefetch_capacity_limit calculation based on memory sizes
3. Fallback to regular eviction when write_backup fails (returns 0)
4. Conditional tree cleanup only when node is evicted from device
5. Commented out assertion requiring host_size > device_size

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

@stmatengss stmatengss left a comment

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.

Review: [HiCache] Support host memory size smaller than device memory size

Key Issue: evict_host new logic for non-evicted nodes is unreachable

evict_host builds its heap from self.evictable_host_leaves (line 844). But _update_host_leaf_status (line 759-772) only adds nodes where node.evicted is True — non-evicted nodes are excluded by the early return at line 760:

def _update_host_leaf_status(self, node: TreeNode):
    if not node.evicted or node.lock_ref > 0:
        if node in self.evictable_host_leaves:
            self.evictable_host_leaves.remove(node)
        return

This means backed-up nodes still on device will never enter the eviction heap. The new branches in evict_host that handle x.evicted is False (keeping the node in the tree, only clearing host_value) are dead code.

For the PR's goal to work — freeing redundant host copies of on-device nodes when host < device — _update_host_leaf_status needs to be updated to also consider non-evicted nodes that have host_value is not None.

Minor items

  1. Commented-out assertion in memory_pool_host.py: If the constraint is intentionally lifted, delete the assertion entirely rather than commenting it out.

  2. Magic number 0.5 in prefetch_capacity_limit fallback (cache_controller.py:456): Worth a brief comment explaining the rationale for reserving half of host memory for prefetch when host <= device.

  3. Tree invariant is subtle but correct: After evict_host, a node reachable in the tree with evicted=True will always have host_value != None, because evict_host removes evicted nodes from the tree when clearing their host_value. This invariant protects match_prefix_return_kv (line 1156-1157) from hitting len(None). A comment documenting this invariant would help future readers.

@github-actions github-actions Bot added the hicache Hierarchical Caching for SGLang label Feb 25, 2026
@stmatengss

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

1 similar comment
@stmatengss

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

XucSh added 2 commits March 3, 2026 11:40
Signed-off-by: Xuchun Shang <xuchun.shang@linux.alibaba.com>
@XucSh

XucSh commented Mar 4, 2026

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

2 similar comments
@XucSh

XucSh commented Mar 4, 2026

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@XucSh

XucSh commented Mar 5, 2026

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

num_evicted += self.cache_controller.evict_host(x.host_value)
x.host_value = None

if x.evicted:

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.

does this mean we can't evict the host if the data is present on GPU?

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.

Data in L2 should be consistent with the GPU.

@stmatengss

Copy link
Copy Markdown
Collaborator Author

#20535 should be implemented more clearly. This PR might not cover complex cases. @xiezhq-hermann

@stmatengss

Copy link
Copy Markdown
Collaborator Author

After discussing with @vladnosiv, I totally agree with a new --hicache-host-memory-mode buffer_only parameter. When L2 is less than L3, it could improve performance by reducing copying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hicache Hierarchical Caching for SGLang high priority run-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants