Skip to content

Fix mamba radix cache eviction logic in alloc_req_slots#11616

Merged
merrymercy merged 7 commits intosgl-project:mainfrom
rogeryoungh:patch-alloc-req-slots
Oct 23, 2025
Merged

Fix mamba radix cache eviction logic in alloc_req_slots#11616
merrymercy merged 7 commits intosgl-project:mainfrom
rogeryoungh:patch-alloc-req-slots

Conversation

@rogeryoungh
Copy link
Contributor

Motivation

This pull request addresses an issue with the cache eviction logic introduced in #11214. The eviction step is implemented in schedule_batch.py​​, but due to the recent refactoring in #11313 (which moved alloc_req_slots and its associated logic to common.py), the eviction logic wasn't being called. This issue was also reported in #11214 (comment) .

This patch ensures that evictions are correctly called from the refactored alloc_req_slots function.

Modifications

Moving eviction logic to common.py.

Accuracy Tests

Benchmarking and Profiling

Checklist

Signed-off-by: rogeryoungh <rogeryoungh@foxmail.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @rogeryoungh, 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 a critical regression in the Mamba radix cache eviction mechanism. A previous refactoring had inadvertently disconnected the eviction logic from its invocation point, leading to potential cache overflow issues. This change rectifies the problem by relocating the Mamba radix cache eviction logic to the common.py module, integrating it directly into the alloc_for_extend function. This ensures that the cache properly manages its memory by evicting older entries when new allocations are needed, thereby maintaining system stability and performance for Mamba models.

Highlights

  • Cache Eviction Logic Fix: This pull request resolves a bug where the Mamba radix cache eviction logic was not being called due to a previous refactoring that moved the alloc_req_slots function.
  • Relocation of Eviction Logic: The Mamba radix cache eviction logic has been moved from schedule_batch.py to common.py to ensure it is correctly invoked within the alloc_for_extend function.
  • Ensuring Proper Cache Management: The change ensures that the Mamba cache properly evicts old entries when new requests require more slots than currently available, preventing potential memory issues.
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.

Copy link
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 correctly addresses a bug where the Mamba radix cache eviction logic was not being called after a recent refactoring. The fix involves moving the eviction logic from the now-deleted alloc_req_slots method into its caller, alloc_for_extend, ensuring it executes at the correct time. The change is logical and well-contained. I have one minor suggestion to improve code conciseness by simplifying a conditional check.

Comment on lines +336 to +341
if mamba_available_size < bs:
if batch.tree_cache is not None and isinstance(
batch.tree_cache, MambaRadixCache
):
mamba_num = max(0, bs - mamba_available_size)
batch.tree_cache.evict_mamba(mamba_num)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The check batch.tree_cache is not None is redundant because isinstance(None, MambaRadixCache) evaluates to False. You can combine the nested if statements for conciseness and better readability.

        if mamba_available_size < bs and isinstance(batch.tree_cache, MambaRadixCache):
            mamba_num = max(0, bs - mamba_available_size)
            batch.tree_cache.evict_mamba(mamba_num)

Copy link
Collaborator

@yizhang2077 yizhang2077 left a comment

Choose a reason for hiding this comment

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

LGTM cc @hnyls2002

Signed-off-by: rogeryoungh <rogeryoungh@foxmail.com>
Signed-off-by: rogeryoungh <rogeryoungh@foxmail.com>
@merrymercy merrymercy merged commit dbd9435 into sgl-project:main Oct 23, 2025
64 of 106 checks passed
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.

5 participants