support memory pool page first layout#8061
support memory pool page first layout#8061huangtingwei9988 wants to merge 14 commits intosgl-project:mainfrom
Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @huangtingwei9988, 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 introduces support for a 'page-first' memory layout for KV caches, addressing performance and management challenges encountered when integrating with remote KV cache systems that benefit from contiguous memory blocks. It updates the memory pool management and KV cache transfer mechanisms to be layout-aware, enabling efficient data movement between different memory organizations at the kernel level.
Highlights
- New Memory Layout Support: Introduced a 'page-first' memory layout for KV caches, alongside the existing 'layer-first' layout, to improve integration with remote KV cache pools like Mooncake store.
- Flexible KV Cache Transfer: Enhanced KV cache transfer functions (both per-layer and all-layer, and MLA/non-MLA variants) to handle transfers between different memory layouts (layer-first to layer-first, and page-first to layer-first).
- Layout Dimension Tracking: Added
layout_dimproperties tomemory_poolandmemory_pool_hostclasses to accurately track the dimension of a single layer/page, which is crucial for calculating offsets in the new layout. - Kernel-level Layout Awareness: Modified underlying CUDA kernels to accept parameters indicating source and destination memory layouts, allowing them to dynamically select the correct global offset calculation logic (
get_global_offset_lffor layer-first,get_global_offset_pffor page-first). - Expanded Testing: Added new test cases to verify the correctness of KV cache transfers, specifically including scenarios for converting from a page-first layout to a layer-first layout.
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 configure Gemini 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.
Code Review
The pull request introduces support for a page-first memory layout, which improves integration with remote KV cache pools. The changes include modifications to Python, C++, and CUDA code to handle different memory layouts. The new functionality is well-tested. I have provided some suggestions to improve readability and maintainability.
8f31c9e to
f861882
Compare
|
replaced by #8651 |
Motivation
Currently, sglang's
memory_poolandmemory_pool_hostare both layer_first layouts, which is conducive to layer wise operations, for example, loading KV caches from host memory to device memory layer by layer.However, when integrating with a remote kv cache pool like mooncake store, the layer_first layout will make it impossible to obtain the complete kv cache in the continuous address space. (For example, the layer0 caches of all tokens are together, but the layer0 caches of each token are not together with their layer1 caches.)
Therefore, when zero copying, it can only obtain the kv cache by layer. This will cause the number of keys in mooncake store to become layer_num times, which will reduce performance and make it difficult to manage for mooncake store
Co-author @AniZpZ @zhaoyongke @zhangzuo21
Modifications
support memory pool page first layout
Checklist