Skip to content

fix(hicache): fix prefetch under heavy workload#9643

Closed
xiaguan wants to merge 2 commits intosgl-project:mainfrom
xiaguan:fix_prefetch_can_alloc
Closed

fix(hicache): fix prefetch under heavy workload#9643
xiaguan wants to merge 2 commits intosgl-project:mainfrom
xiaguan:fix_prefetch_can_alloc

Conversation

@xiaguan
Copy link
Copy Markdown
Contributor

@xiaguan xiaguan commented Aug 26, 2025

Motivation

When the system encounters high hit ratio scenarios (such as dozens of long requests with nearly 100% hit ratios during my stress testing), it causes two critical issues:

  1. Memory allocation failures

My current fix involves disabling prefetch operations.

  1. Rate limiting causes undefined operation states, leading to deadlocks

The solution is to implement a brief requeue mechanism.

Here are some potential solutions worth discussing:

  1. Defer memory allocation until just before batch_get operations to prevent excessive memory pinning in short timeframes, which would simultaneously address both issues mentioned above.

  2. Implement backpressure using queue size limitations.

Modifications

Accuracy Tests

Benchmarking and Profiling

Checklist

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @xiaguan, 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 addresses critical performance and stability issues in the hicache prefetch mechanism, particularly under heavy workloads. The changes aim to mitigate memory allocation failures and deadlocks caused by rate limiting in high hit ratio scenarios. It introduces a more robust approach to managing prefetch operations by implementing a re-queueing mechanism for rate-limited operations and conditionally disabling prefetch when memory allocation fails due to system pressure.

Highlights

  • Improved Prefetch Handling: Implements a re-queueing mechanism for prefetch operations that are rate-limited, preventing them from getting stuck and ensuring eventual processing.
  • Memory Pressure Management: Adds a safeguard to skip prefetch operations when the system is under heavy memory load, preventing potential memory allocation failures.
  • Optimized Prefetch Queries: Caches storage hit query results for prefetch operations to avoid redundant computations and improve efficiency.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

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 important fixes for prefetching under heavy workloads, addressing potential memory allocation failures and deadlocks due to rate limiting. The changes are logical and effectively solve the described problems. The addition of caching for storage hit query results is a good optimization. I have a couple of suggestions to improve code clarity and maintainability.

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 hardcoded delay 0.1 is a magic number. It's better to define it as a constant at the class or module level (e.g., _PREFETCH_REQUEUE_DELAY_S = 0.1) to improve readability and make it easier to change in the future.

Comment thread python/sglang/srt/mem_cache/hiradix_cache.py Outdated
@xiaguan
Copy link
Copy Markdown
Contributor Author

xiaguan commented Aug 26, 2025

I think the prefetch limit is unnecessary. I think the root cause is that prefetch operations in the queue shouldn't consume memory - allocation should only occur when actually needed.

@xiezhq-hermann xiezhq-hermann self-assigned this Aug 27, 2025
@xiezhq-hermann
Copy link
Copy Markdown
Collaborator

I think the prefetch limit is unnecessary. I think the root cause is that prefetch operations in the queue shouldn't consume memory - allocation should only occur when actually needed.

Thank you for the comment and I do think it is a very valid one. The reason to alloc memory beforehand is to ensure the consistency across TP workers. In the current design, any change made to the radix tree has to happen in the main controller thread to avoid potential state divergence.

@xiaguan xiaguan force-pushed the fix_prefetch_can_alloc branch from 8c47935 to 695ebca Compare August 28, 2025 08:50
@xiaguan
Copy link
Copy Markdown
Contributor Author

xiaguan commented Aug 28, 2025

I think the prefetch limit is unnecessary. I think the root cause is that prefetch operations in the queue shouldn't consume memory - allocation should only occur when actually needed.

Thank you for the comment and I do think it is a very valid one. The reason to alloc memory beforehand is to ensure the consistency across TP workers. In the current design, any change made to the radix tree has to happen in the main controller thread to avoid potential state divergence.

Thank you for the explanation. In that case, I'll refrain from modifying the allocation logic in this PR and simply ensure the current code functions properly under extreme scenarios.

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.

2 participants