Skip to content

[Fix]: add missing device attribute to ChunkCache#11493

Merged
zhyncs merged 3 commits intosgl-project:mainfrom
leavelet:main
Oct 13, 2025
Merged

[Fix]: add missing device attribute to ChunkCache#11493
zhyncs merged 3 commits intosgl-project:mainfrom
leavelet:main

Conversation

@leavelet
Copy link
Copy Markdown
Contributor

@leavelet leavelet commented Oct 12, 2025

Motivation

Fix #11492 by adding device attribute to 'ChunkCache'.

Modifications

'ChunkCache' will inherit device from its token_to_kv_pool_allocator.

Accuracy Tests

Benchmarking and Profiling

Checklist

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @leavelet, 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 resolves a critical issue by adding a missing device attribute to the ChunkCache class. This ensures that the cache correctly identifies and operates on the appropriate hardware device, either by inheriting it from its associated allocator or defaulting to the CPU. This change is essential for maintaining proper device management and preventing potential errors related to device-specific memory operations within the system.

Highlights

  • Device Attribute Addition: The ChunkCache class now includes a device attribute to explicitly track its operating device.
  • Device Inheritance: The device attribute is initialized by inheriting the device from its token_to_kv_pool_allocator.
  • CPU Fallback: If the token_to_kv_pool_allocator is not available, the device attribute defaults to torch.device("cpu").
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.

@leavelet
Copy link
Copy Markdown
Contributor Author

@cctry please review

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 adds a missing device attribute to the ChunkCache class. The implementation is sound, but I've identified a potential issue regarding the nullability of token_to_kv_pool_allocator. The current code defensively checks if this allocator exists, but other parts of the class do not, which could lead to runtime errors. My review comment suggests simplifying the code to align with the existing type hints, which would also resolve this inconsistency.

Comment on lines +30 to +33
if self.token_to_kv_pool_allocator:
self.device = self.token_to_kv_pool_allocator.device
else:
self.device = torch.device("cpu")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

This logic implies that token_to_kv_pool_allocator can be None. However, the type hint in the __init__ signature (BaseTokenToKVPoolAllocator) suggests it's non-nullable. This is inconsistent.

If token_to_kv_pool_allocator can be None, then cache_finished_req will raise an AttributeError at line 59, as it accesses self.token_to_kv_pool_allocator.free(...) without a check. Please ensure all usages of token_to_kv_pool_allocator are safe if it can be None, and consider updating the type hint to Optional[BaseTokenToKVPoolAllocator].

If token_to_kv_pool_allocator is never None (as the type hint suggests), this conditional logic is unnecessary and can be simplified.

        self.device = self.token_to_kv_pool_allocator.device

@cctry
Copy link
Copy Markdown
Collaborator

cctry commented Oct 12, 2025

Sorry to bring in this bug. I am wondering when token_to_kv_pool_allocator will be None

@leavelet
Copy link
Copy Markdown
Contributor Author

Sorry to bring in this bug. I am wondering when token_to_kv_pool_allocator will be None

Should we just change it to self.device = self.token_to_kv_pool_allocator.device? It should never be None anyway. Sorry about the mistake

@hzh0425
Copy link
Copy Markdown
Collaborator

hzh0425 commented Oct 13, 2025

hi @leavelet , Could you help add a CI to cover this issue? It needs --disable-radix-cache and page_size > 1.

@zhyncs zhyncs merged commit 19ba16a into sgl-project:main Oct 13, 2025
9 of 41 checks passed
lpc0220 pushed a commit to lpc0220/sglang that referenced this pull request Oct 29, 2025
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.

[Bug] 'ChunkCache' object has no attribute 'device'

7 participants