Skip to content

refactor linear memory pool#13004

Merged
ispobock merged 5 commits intomainfrom
refactor_linear_model
Nov 17, 2025
Merged

refactor linear memory pool#13004
ispobock merged 5 commits intomainfrom
refactor_linear_model

Conversation

@yizhang2077
Copy link
Collaborator

Motivation

unify kda/gdn/mamba2 logic

python3 -m sglang.launch_server --model moonshotai/Kimi-Linear-48B-A3B-Instruct  --attention-backend triton --port 40000 --tp 2 --trust-remote-code

python3 benchmark/gsm8k/bench_sglang.py --num-questions 1000 --port 40000
Accuracy: 0.891
Invalid: 0.000
Latency: 88.140 s

python3 -m sglang.launch_server --model Qwen/Qwen3-Next-80B-A3B-Instruct  --attention-backend triton --port 40000 --tp 2  --disable-radix-cache --speculative-num-steps 3 --speculative-eagle-topk 1 --speculative-num-draft-tokens 4 --speculative-algorithm NEXTN

python3 benchmark/gsm8k/bench_sglang.py --num-questions 1000 --port 40000
Accuracy: 0.948
Invalid: 0.000
Latency: 134.702 s
Output throughput: 1230.222 token/s

python3 -m sglang.launch_server --model Qwen/Qwen3-Next-80B-A3B-Instruct  --attention-backend triton --port 40000 --tp 2

python3 benchmark/gsm8k/bench_sglang.py --num-questions 1000 --port 40000
Accuracy: 0.944
Invalid: 0.000
Latency: 141.572 s
Output throughput: 1178.861 token/s

Modifications

Accuracy Tests

Benchmarking and Profiling

Checklist

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @yizhang2077, 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 undertakes a significant refactoring of the linear memory pool management within the system. The primary goal is to unify the logic for various linear attention mechanisms (like KDA, GDN, and Mamba2) by introducing a common abstract base class for state parameters and standardizing the representation of convolutional states as lists of tensors. This change streamlines the codebase, reduces redundancy, and improves overall maintainability by removing type-specific conditional handling, making the system more robust and easier to extend.

Highlights

  • Unified Linear State Parameters: Introduced a new abstract base class, BaseLinearStateParams, to consolidate common properties and methods (like dtype, layers, and mamba_cache_per_req) shared across different linear attention mechanisms such as Mamba2 and KimiLinear. This promotes code reuse and consistency.
  • Standardized Convolutional State Representation: The conv state within Mamba2StateShape and MambaPool.State is now consistently represented as a list of tensors. This change eliminates previous conditional logic that handled conv as either a single tensor or a list, simplifying state management.
  • Simplified Cache Management Logic: Removed the is_kda_cache flag and associated conditional branches throughout the MambaPool class. Methods like __init__, free, copy_from, and get_contiguous_buf_infos now operate uniformly on list-based convolutional states, leading to cleaner and more maintainable code.
  • Updated State Access Patterns: Adjusted how conv and intermediate_conv_window states are accessed in hybrid_linear_attn_backend.py and mamba.py to reflect their new list-based structure, typically using [0] indexing to retrieve the primary convolutional state.
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 refactors the memory pool logic for linear attention models by introducing a BaseLinearStateParams class. This is a great improvement as it unifies the logic for KDA, GDN, and Mamba2, reducing code duplication and improving maintainability. The changes across the files are consistent with this refactoring. I've found one issue related to incorrect type hints in the SpeculativeState dataclass that should be addressed.

@yizhang2077 yizhang2077 force-pushed the refactor_linear_model branch from fca34b2 to d893486 Compare November 10, 2025 15:06
yizhang2077 and others added 3 commits November 11, 2025 01:01
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@ispobock ispobock merged commit a3e4fe4 into main Nov 17, 2025
155 of 164 checks passed
@ispobock ispobock deleted the refactor_linear_model branch November 17, 2025 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants