Skip to content

Refactor mem_pool_host deallocation to main thread#9302

Closed
pansicheng wants to merge 10 commits intosgl-project:mainfrom
pansicheng:converge-free
Closed

Refactor mem_pool_host deallocation to main thread#9302
pansicheng wants to merge 10 commits intosgl-project:mainfrom
pansicheng:converge-free

Conversation

@pansicheng
Copy link
Copy Markdown
Collaborator

Motivation

Currently, host indices pre-allocated for prefetch storage are freed by multiple threads, causing inconsistencies in mem_pool_host across different ranks. This requires synchronization operations between tp groups.

This PR proposes to centralize the freeing of host indices in the main thread, minimizing the need for cross-tp synchronization.

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 @pansicheng, 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 refactors the memory deallocation mechanism for mem_pool_host indices, specifically those pre-allocated for prefetch storage. The primary goal is to centralize the freeing of these host indices to the main thread, thereby resolving inconsistencies that arose from multi-threaded deallocation and minimizing the need for cross-tensor parallel (TP) group synchronization. The changes involve introducing a new queue for deallocation requests, enhancing prefetch operation tracking, and implementing dedicated logic on the main thread to manage the memory freeing process.

Highlights

  • Centralized Memory Deallocation: The core change centralizes the deallocation of mem_pool_host indices, moving this responsibility from multiple threads to the main thread. This aims to resolve inconsistencies and reduce synchronization overhead across different tensor parallel (TP) groups.
  • New Deallocation Queue: A new prefetch_free_queue has been introduced within the CacheController to queue PrefetchOperation objects for deferred memory deallocation. This decouples the freeing process from the prefetch and I/O auxiliary threads.
  • Enhanced Prefetch Operation Tracking: The PrefetchOperation class now includes storage_hit_count, min_completed_tokens, and matched_length attributes. These are used to precisely track and manage the specific segments of pre-allocated host memory that need to be freed or retained.
  • Dedicated Deallocation Logic: New methods, free_operation and check_free_prefetch, have been added to HiRadixCache. These methods are responsible for processing the prefetch_free_queue, performing the actual memory deallocation, and ensuring proper synchronization across TP workers before freeing memory.
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.

@xiezhq-hermann xiezhq-hermann self-assigned this Aug 18, 2025
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 refactors the deallocation of host memory for prefetched data to the main thread, which is a good approach to centralize resource management and avoid synchronization issues. The implementation uses a queue to pass deallocation tasks, which is sound. My review includes two main suggestions for improvement: one to address code duplication for better maintainability, and another to optimize the memory reclamation logic for better efficiency.

Comment thread python/sglang/srt/managers/cache_controller.py Outdated
Comment thread python/sglang/srt/mem_cache/hiradix_cache.py
Comment thread python/sglang/srt/mem_cache/hiradix_cache.py Outdated
Comment thread python/sglang/srt/mem_cache/hiradix_cache.py Outdated
pansicheng and others added 2 commits August 21, 2025 20:37
Co-authored-by: JinYan Su <jinyansu792@gmail.com>
Co-authored-by: JinYan Su <jinyansu792@gmail.com>
if len(written_indices):
self.cache_controller.mem_pool_host.update_prefetch(written_indices)

self.cache_controller.mem_pool_host.free(host_indices[:matched_length])
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.

I think we can still release this here, since matched_length should be synchronized across workers.

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.

I’ve made the changes, please take a look!

@pansicheng
Copy link
Copy Markdown
Collaborator Author

to replace this with #9797

@pansicheng pansicheng closed this Aug 31, 2025
@pansicheng pansicheng deleted the converge-free branch November 12, 2025 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants