Skip to content

Conversation

hanming-lu
Copy link
Collaborator

@hanming-lu hanming-lu commented Jun 19, 2025

Motivation

  • Support hybrid LLM architecture prefix cache - interleaved full attention and sliding window attention layers

Modifications

  • Depends on SWA mem pool: Hybrid kv cache for LLaMA4 #6563
  • python/sglang/srt/mem_cache/swa_radix_cache.py:
    • swa prefix cache logic and swa_memory_pool api usage
    • use a LRU linked list to manage eviction order
  • swa_radix_cache usage integration

Testing

Loogle:
python python/sglang/eval/loogle_eval.py --api-url http://127.0.0.1:30002/v1

llama4 fa3 disable swa radix cache: average BERTScore (F1): 84.50%:
python3 -m sglang.launch_server --model-path meta-llama/Llama-4-Scout-17B-16E-Instruct --port 30002 --tp 8 --mem-fraction-static 0.8 --context-length 100000 --attention-backend fa3 --disable-radix-cache --hybrid-kvcache-ratio 0.95

llama4 fa3 enable swa radix cache: average BERTScore (F1): 84.32%
python3 -m sglang.launch_server --model-path meta-llama/Llama-4-Scout-17B-16E-Instruct --port 30002 --tp 8 --mem-fraction-static 0.8 --context-length 100000 --attention-backend fa3 --hybrid-kvcache-ratio 0.95

Checklist

@hanming-lu hanming-lu marked this pull request as draft June 19, 2025 22:53
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.

Summary of Changes

Hello @hanming-lu, 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 integrates a new, specialized radix tree data structure for managing the KV cache within the SGLang framework. The SWARadixCache is tailored to optimize memory efficiency, particularly for models employing Sliding Window Attention, by enabling intelligent prefix sharing and eviction strategies.

Highlights

  • New Component Introduction: This PR introduces a new module, swa_radix_cache.py, which defines the SWARadixCache class and its supporting structures. This class is a specialized radix tree implementation.
  • KV Cache Management: The primary purpose of the SWARadixCache is to efficiently manage the Key-Value (KV) cache for large language models. It stores and retrieves token prefixes to optimize memory usage.
  • Sliding Window Attention (SWA) Integration: The cache is specifically designed to support models utilizing Sliding Window Attention (SWA). It incorporates SWA-aware logic for prefix matching, eviction, and memory allocation, including concepts like 'swa_tombstone' nodes.
  • Memory Optimization and Eviction: It provides mechanisms to track and manage both 'full' and 'SWA' evictable token counts, along with methods to increment/decrement lock references on cache nodes, ensuring that in-use KV cache entries are protected from eviction.
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

  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.

@hanming-lu hanming-lu changed the title [SWA] copy private swa_radix_cache.py [WIP] SWA Prefix Cache Jun 19, 2025
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 introduces SWARadixCache, a new radix tree implementation for managing KV cache, tailored for Sliding Window Attention (SWA). Key suggestions include using time.monotonic() for more reliable LRU timestamps and aligning the match_prefix return type with the BasePrefixCache interface.

@RunkaiTao RunkaiTao mentioned this pull request Jun 27, 2025
6 tasks
@Ying1123 Ying1123 force-pushed the hanming/swa-radix-cache branch 4 times, most recently from 0eddd7d to a8a6de6 Compare July 7, 2025 20:57
@Ying1123 Ying1123 marked this pull request as ready for review July 10, 2025 00:34
@Ying1123 Ying1123 force-pushed the hanming/swa-radix-cache branch 4 times, most recently from acc52a2 to 7823c12 Compare July 13, 2025 07:37
@Ying1123 Ying1123 force-pushed the hanming/swa-radix-cache branch from 7823c12 to a21d35d Compare July 13, 2025 08:06
@Ying1123 Ying1123 force-pushed the hanming/swa-radix-cache branch from b40f8a2 to c2682b2 Compare July 13, 2025 10:58
@Ying1123 Ying1123 merged commit 9379da7 into sgl-project:main Jul 13, 2025
56 of 76 checks passed
ZhengWG pushed a commit to ZhengWG/sglang that referenced this pull request Jul 16, 2025
Co-authored-by: Ying Sheng <sqy1415@gmail.com>
chenxijun1029 pushed a commit to chenxijun1029/sglang that referenced this pull request Jul 17, 2025
Co-authored-by: Ying Sheng <sqy1415@gmail.com>
DiweiSun pushed a commit to DiweiSun/sglang that referenced this pull request Jul 18, 2025
Co-authored-by: Ying Sheng <sqy1415@gmail.com>
@merrymercy merrymercy changed the title [WIP] SWA Prefix Cache SWA Prefix Cache Jul 19, 2025
shuaills pushed a commit to shuaills/sglang that referenced this pull request Jul 21, 2025
Co-authored-by: Ying Sheng <sqy1415@gmail.com>
@JustinTong0323 JustinTong0323 mentioned this pull request Jul 21, 2025
6 tasks
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