Skip to content

[Core] Add blend_server_v2#2677

Merged
YaoJiayi merged 7 commits intodevfrom
localdev/roll-match
Mar 9, 2026
Merged

[Core] Add blend_server_v2#2677
YaoJiayi merged 7 commits intodevfrom
localdev/roll-match

Conversation

@YaoJiayi
Copy link
Copy Markdown
Collaborator

@YaoJiayi YaoJiayi commented Mar 4, 2026

What this PR does / why we need it:

Using rolling hash to get rid of separators.

Special notes for your reviewers:

If applicable:

  • this PR contains user facing changes - docs added
  • this PR contains unit tests

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 introduces a significant enhancement to the KV cache system by adding a new blend server, blend_server_v2. This server enables more flexible and efficient KV cache reuse by allowing matches of token sub-sequences at any position within a request, moving beyond the limitation of common prefixes. The core of this functionality is powered by new Numba-optimized hashing algorithms and a dedicated matcher, which together provide fast and accurate identification of reusable cache segments. The changes also include updated data structures and communication protocols to support these advanced blending capabilities.

Highlights

  • New Blend Server (V2): Introduced blend_server_v2.py, a new KV cache server designed for reusing KV cache across requests that share token sub-sequences at arbitrary positions, not just common prefixes.
  • Sub-sequence Matching Logic: Implemented BlendTokenRangeMatcher, a fast local matcher using Numba-optimized polynomial rolling/chunk hashes and a direct-address lookup table to efficiently find shared token sub-sequences.
  • Numba-Optimized Hashing Functions: Added several Numba-optimized functions (rolling_hash_windows_numba, chunk_hash_windows_numba, update_table_id_numba, unique_hits_direct_id_numba) to token_hasher.py for high-performance hash computations.
  • Enhanced Match Result Structure: Defined a new CBMatchResult dataclass in custom_types.py to provide richer information about matched sub-sequences, including original and current positions, and the chunk hash, simplifying direct storage key lookups.
  • New Protocol Definitions: Introduced new RequestType enums (CB_LOOKUP_PRE_COMPUTED_V2, CB_RETRIEVE_PRE_COMPUTED_V2) and corresponding protocol definitions in blend_v2.py to support the V2 blend operations.
  • Integration and Testing: Integrated the new blend operations into the run_cache_server function and added a comprehensive test suite in test_blend_server_v2.py to validate the functionality and data correctness of the new server.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • lmcache/v1/distributed/api.py
    • Added a static method HashToKey to ObjectKey for creating an ObjectKey from an integer hash.
  • lmcache/v1/multiprocess/blend_server_v2.py
    • Added a new file implementing BlendEngineV2, a KV cache server for copy-blend operations.
    • Implemented BlendTokenRangeMatcher for fast token-range matching using polynomial rolling/chunk hashes.
    • Defined methods for registering/unregistering KV caches, looking up pre-computed chunks, storing pre-computed chunks, retrieving pre-computed chunks, and storing final chunks.
    • Integrated new blend operation handlers into the run_cache_server function.
  • lmcache/v1/multiprocess/custom_types.py
    • Added the CBMatchResult dataclass to represent the result of a sub-sequence match, including position information and the token hash.
  • lmcache/v1/multiprocess/protocols/init.py
    • Imported the new blend_v2 protocol module.
    • Registered the blend_v2 protocol definitions.
  • lmcache/v1/multiprocess/protocols/base.py
    • Added new RequestType enums: CB_LOOKUP_PRE_COMPUTED_V2 and CB_RETRIEVE_PRE_COMPUTED_V2 for V2 blend operations.
  • lmcache/v1/multiprocess/protocols/blend_v2.py
    • Added a new file defining the V2 blend protocol definitions, specifically for lookup and retrieve operations using CBMatchResult.
  • lmcache/v1/multiprocess/token_hasher.py
    • Imported numba and numpy for optimized numerical operations.
    • Added rolling_hash_windows_numba for computing rolling polynomial hashes over arrays.
    • Added chunk_hash_windows_numba for specialized non-overlapping chunk hashing.
    • Added update_table_id_numba for updating direct-address lookup tables.
    • Added unique_hits_direct_id_numba for performing direct-address lookup with deduplication.
  • tests/v1/multiprocess/test_blend_server_v2.py
    • Added a new test file containing unit tests for BlendTokenRangeMatcher and integration tests for BlendEngineV2.
    • Included tests for server startup, KV cache registration, storing/looking up/retrieving pre-computed chunks, and storing final chunks, with a focus on sub-sequence matching and data correctness.
  • tests/v1/multiprocess/test_token_hasher.py
    • Imported the newly added Numba-optimized hashing functions.
    • Added comprehensive unit tests for rolling_hash_windows_numba, chunk_hash_windows_numba, update_table_id_numba, and unique_hits_direct_id_numba.
Activity
  • The pull request introduces a new blend server version, blend_server_v2, and associated functionalities.
  • New Numba-optimized hashing functions have been added to improve performance of token range matching.
  • A new CBMatchResult dataclass was introduced to better structure the results of sub-sequence matches.
  • New protocol definitions and request types were added to support the V2 blend operations.
  • Comprehensive unit and integration tests were added for the new blend server and hashing utilities.
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
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 introduces blend_server_v2, enhancing KV cache reuse of token sub-sequences using a BlendTokenRangeMatcher and numba-accelerated hashing. A security audit revealed critical vulnerabilities, including insecure deserialization leading to Remote Code Execution (RCE), a high-severity buffer overflow in the Numba-jitted hashing code, and insecure data handling with unvalidated user-supplied offsets for GPU memory access. These issues pose risks of system compromise or crashes. Furthermore, a potential double-unlock bug in cb_retrieve_pre_computed was identified, and there are opportunities to improve performance by avoiding busy-waiting and reducing memory allocations, as well as making zip calls stricter for safety.

Comment thread lmcache/v1/multiprocess/blend_server_v2.py
Comment thread lmcache/v1/multiprocess/token_hasher.py
Comment thread lmcache/v1/multiprocess/blend_server_v2.py
Comment thread lmcache/v1/multiprocess/blend_server_v2.py
Comment thread lmcache/v1/multiprocess/blend_server_v2.py
Comment thread lmcache/v1/multiprocess/blend_server_v2.py
Comment thread lmcache/v1/multiprocess/blend_server_v2.py
Comment thread lmcache/v1/multiprocess/blend_server_v2.py
Comment thread lmcache/v1/multiprocess/blend_server_v2.py
YaoJiayi added 2 commits March 4, 2026 06:46
Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>
Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>
Copy link
Copy Markdown
Contributor

@ApostaC ApostaC left a comment

Choose a reason for hiding this comment

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

Follow-up TODO items after this PR (all related to token-matcher):

  1. Dynamic lookup table handling avoids hash collisions
  2. Have a design for the global token matcher module (need to discuss whether the storage layer needs to give the token matcher module feedback)
  3. Thread safety problem.

Comment thread lmcache/v1/distributed/api.py Outdated
Comment thread lmcache/v1/multiprocess/token_hasher.py
Comment thread lmcache/v1/multiprocess/blend_server_v2.py
Comment thread lmcache/v1/multiprocess/blend_server_v2.py
Comment thread lmcache/v1/multiprocess/blend_server_v2.py Outdated
Comment thread lmcache/v1/multiprocess/blend_server_v2.py
assert k.chunk_hash is not None
token_hashes.append(k.chunk_hash)

self._token_range_matcher.on_new_token_hashes(list(key.token_ids), token_hashes)
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.

[Critical] When we have TP > 1, this function will be called multiple times with the same token_ids. Will it create problems?

Copy link
Copy Markdown
Collaborator Author

@YaoJiayi YaoJiayi Mar 5, 2026

Choose a reason for hiding this comment

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

Changed the code to only update the fingerprint if the ipc_key's rank is 0.

YaoJiayi and others added 2 commits March 5, 2026 00:39
Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>
@YaoJiayi YaoJiayi requested a review from ApostaC March 5, 2026 23:47
Copy link
Copy Markdown
Contributor

@KuntaiDu KuntaiDu left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Copy Markdown
Contributor

@sammshen sammshen left a comment

Choose a reason for hiding this comment

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

LGTM!

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>
@YaoJiayi YaoJiayi enabled auto-merge (squash) March 9, 2026 07:15
@github-actions github-actions Bot added the full Run comprehensive tests on this PR label Mar 9, 2026
@YaoJiayi YaoJiayi merged commit b86d7f9 into dev Mar 9, 2026
29 of 35 checks passed
@YaoJiayi YaoJiayi deleted the localdev/roll-match branch March 10, 2026 17:36
shaoxiawjc pushed a commit to shaoxiawjc/LMCache that referenced this pull request Mar 11, 2026
* add tests and fix bugs

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* update comment

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* add harder tests

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* fix minor comments

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* fix tests

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

---------

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>
Signed-off-by: shaoxiawjc <wjc2800@163.com>
realAaronWu pushed a commit to realAaronWu/LMCache that referenced this pull request Mar 20, 2026
* add tests and fix bugs

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* update comment

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* add harder tests

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* fix minor comments

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* fix tests

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

---------

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>
Signed-off-by: Aaron Wu <aaron.wu@dell.com>
jooho-XCENA pushed a commit to xcena-dev/LMCache that referenced this pull request Apr 2, 2026
* add tests and fix bugs

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* update comment

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* add harder tests

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* fix minor comments

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* fix tests

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

---------

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>
jooho-XCENA pushed a commit to xcena-dev/LMCache that referenced this pull request Apr 2, 2026
* add tests and fix bugs

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* update comment

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* add harder tests

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* fix minor comments

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

* fix tests

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>

---------

Signed-off-by: YaoJiayi <120040070@link.cuhk.edu.cn>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full Run comprehensive tests on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants