-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Move host memory pools into a separate file #7200
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
Conversation
There was a problem hiding this 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 @merrymercy, 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 is a refactoring aimed at improving code organization within the memory cache module. It separates the logic specifically related to host memory pools into its own file, making the original memory_pool.py
file solely responsible for device memory pools and general memory management concepts.
Highlights
- Code Organization: Extracted the host memory pool related classes and functions into a dedicated file.
- New File: Introduced
python/sglang/srt/mem_cache/memory_pool_host.py
to house the host memory pool logic. - Code Movement: Moved the
MemoryStateInt
enum,synchronized
decorator,HostKVCache
base class, and the concreteMHATokenToKVPoolHost
andMLATokenToKVPoolHost
implementations frommemory_pool.py
to the newmemory_pool_host.py
file.
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 is currently in preview and 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 to provide feedback.
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
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configureGemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this 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 successfully moves the host memory pool related classes (HostKVCache
, MHATokenToKVPoolHost
, MLATokenToKVPoolHost
) and associated utilities (MemoryStateInt
, synchronized
decorator) from python/sglang/srt/mem_cache/memory_pool.py
to a new file python/sglang/srt/mem_cache/memory_pool_host.py
.
Key areas for attention:
- Bug in
synchronized
Decorator: Thesynchronized
decorator in the newmemory_pool_host.py
file (lines 24-37) contains a significant logical flaw that was present in the original code. This flaw can lead to functions running without the intended lock or not running at all. A detailed comment and suggestion for a fix have been provided. - Import Path Correction: The import statement for
GB
,KVCache
, etc., inmemory_pool_host.py
(line 10) needs to be updated to correctly reference thememory_pool.py
module. - Impact on Other Files: Files that previously imported
MHATokenToKVPoolHost
orMLATokenToKVPoolHost
fromsglang.srt.mem_cache.memory_pool
(e.g.,python/sglang/srt/mem_cache/hiradix_cache.py
) will need their import paths updated tosglang.srt.mem_cache.memory_pool_host
. These changes are outside the current diff but are necessary for the codebase to remain functional after this move.
The primary change is a refactoring. Addressing the identified issues, especially the synchronized
decorator, is crucial for maintaining correctness.
if (not debug_only) or self.debug: | ||
return func(self, *args, **kwargs) | ||
with self.lock: | ||
return func(self, *args, **kwargs) | ||
else: | ||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of the synchronized
decorator has a logical flaw that could lead to incorrect behavior:
-
In the
if (not debug_only) or self.debug:
branch (line 28),func
is called and its result is returned (line 29) before thewith self.lock:
block (line 30) is reached. This means the lock is never acquired for these cases, and thewith self.lock:
block is effectively dead code. Methods intended to be fully synchronized (e.g.,alloc
,free
) will run without thread safety. -
In the
else:
branch (line 32), which executes whendebug_only
is true andself.debug
is false, the functionfunc
is not called at all. Instead,True
is returned (line 33). This means that methods decorated with@synchronized(debug_only=True)
(e.g.,get_state
,is_reserved
,update_backup
) will be skipped and will not perform their intended operations whenself.debug
is false; they will simply returnTrue
, which is likely not the desired outcome.
This can lead to race conditions for non-debug_only
methods and incorrect behavior (skipped operations) for debug_only
methods. A revised implementation is needed to correctly apply the lock based on debug_only
and self.debug
and to ensure the decorated function is always executed as intended.
if (not debug_only) or self.debug: | |
return func(self, *args, **kwargs) | |
with self.lock: | |
return func(self, *args, **kwargs) | |
else: | |
return True | |
# Determine if the lock should be used | |
# Lock if: | |
# - Not a debug_only operation (debug_only is False) | |
# - OR (it is a debug_only operation AND self.debug is True) | |
should_lock = not debug_only or (debug_only and self.debug) | |
if should_lock: | |
with self.lock: | |
return func(self, *args, **kwargs) | |
else: | |
# Execute without lock if it's a debug_only operation and not in debug mode | |
return func(self, *args, **kwargs) |
import psutil | ||
import torch | ||
|
||
from sglang.srt.mem_cache import GB, KVCache, MHATokenToKVPool, MLATokenToKVPool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import from sglang.srt.mem_cache import GB, KVCache, ...
attempts to import these names directly from the sglang.srt.mem_cache
package. However, GB
, KVCache
, MHATokenToKVPool
, and MLATokenToKVPool
are defined in the sibling module memory_pool.py
(within the same mem_cache
directory).
To ensure correct and robust importing, this should be changed to import directly from the memory_pool
module. This typically involves specifying the full path to the module or using a relative import if appropriate for the project structure.
from sglang.srt.mem_cache import GB, KVCache, MHATokenToKVPool, MLATokenToKVPool | |
from sglang.srt.mem_cache.memory_pool import GB, KVCache, MHATokenToKVPool, MLATokenToKVPool |
No description provided.