[Core] Make prefix-cache NONE_HASH deterministic by default (#51875)

Signed-off-by: Russell Bryant <rbryant@redhat.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Russell Bryant
2026-08-18 23:14:39 +00:00
committed by GitHub
co-authored by Claude Opus 4.8
parent 8f4a7f45c5
commit ef47a897e2
10 changed files with 204 additions and 77 deletions
+8 -7
View File
@@ -95,13 +95,14 @@ class FileSystemTierManager(SecondaryTierManager):
get_finished_jobs() polls job completion and returns completed JobResults.
Cross-process sharing:
In order to enable KV cache sharing between multiple vLLM instances
using the same ``root_dir`` (e.g., via a shared PVC) the environment
variable ``PYTHONHASHSEED`` must be set to the same fixed value
(e.g., "0") on all instances. Without this, each process initializes
``NONE_HASH`` (the chain-hash seed for block content hashes) with
random bytes, producing different block filenames for identical token
content.
KV cache sharing between multiple vLLM instances using the same
``root_dir`` (e.g., via a shared PVC) works by default: ``NONE_HASH``
(the chain-hash seed for block content hashes) is derived from a fixed
default seed, so identical token content produces identical block
filenames across instances. Setting the ``PYTHONHASHSEED`` environment
variable to the same value on all instances overrides the default seed,
and is required to share a cache when using a non-cryptographic
prefix-caching hash algorithm, which seeds ``NONE_HASH`` randomly.
"""
medium: ClassVar[Medium] = Medium.STORAGE
+18 -17
View File
@@ -8,7 +8,6 @@ Owns transports and a single bidirectional P2PSession per remote peer.
from __future__ import annotations
import os
import time
import uuid
from collections.abc import Iterable, Sequence
@@ -19,6 +18,7 @@ from typing_extensions import override
import vllm.envs as envs
from vllm.logger import init_logger
from vllm.v1.core.kv_cache_utils import get_none_hash_seed
from vllm.v1.kv_offload.base import (
LookupResult,
OffloadKey,
@@ -247,20 +247,15 @@ class P2PSecondaryTierManager(SecondaryTierManager):
**kwargs: Reserved for future tier-specific options.
"""
super().__init__(offloading_spec, primary_kv_view, tier_type)
# Block hashes chain from NONE_HASH, seeded from PYTHONHASHSEED
# (see init_none_hash in v1/core/kv_cache_utils.py). Peers with
# different seeds compute different hashes for identical content, so
# lookups silently miss and no KV crosses the wire. Require it here so
# a misconfigured P2P instance fails at startup rather than degrading
# silently; the value is also verified against each peer on handshake.
hash_seed = os.getenv("PYTHONHASHSEED")
if hash_seed is None:
raise ValueError(
"PYTHONHASHSEED must be set for P2P KV offload so that block "
"hashes match across instances. Set it to a fixed value (e.g. "
"PYTHONHASHSEED=0) on every P2P peer."
)
self._hash_seed = hash_seed
# Block hashes chain from NONE_HASH (see v1/core/kv_cache_utils.py).
# Peers whose seeds differ compute different hashes for identical
# content, so lookups silently miss and no KV crosses the wire. The
# seed is advertised and verified against each peer during the
# handshake, so a mismatch is rejected loudly instead of degrading
# silently. Resolved lazily in _get_hash_seed: this tier is built
# before init_none_hash runs, and a non-cryptographic hash algorithm
# seeds NONE_HASH randomly, so the value is only known afterwards.
self._hash_seed: str | None = None
if host is None:
host = envs.VLLM_P2P_SIDE_CHANNEL_HOST
if port is None:
@@ -602,6 +597,12 @@ class P2PSecondaryTierManager(SecondaryTierManager):
# Internal
# ------------------------------------------------------------------
def _get_hash_seed(self) -> str:
"""The seed NONE_HASH was derived from, resolved on first session."""
if self._hash_seed is None:
self._hash_seed = get_none_hash_seed()
return self._hash_seed
def _get_or_create_session(self, peer_id: str) -> P2PSession:
"""Return the existing session for peer_id, or open one outbound.
@@ -621,7 +622,7 @@ class P2PSecondaryTierManager(SecondaryTierManager):
local_id=self._local_id,
transport=self._data,
local_block_len=self._data.block_len,
local_hash_seed=self._hash_seed,
local_hash_seed=self._get_hash_seed(),
conn=conn,
)
self._sessions[peer_id] = session
@@ -643,7 +644,7 @@ class P2PSecondaryTierManager(SecondaryTierManager):
local_id=self._local_id,
transport=self._data,
local_block_len=self._data.block_len,
local_hash_seed=self._hash_seed,
local_hash_seed=self._get_hash_seed(),
conn=conn,
)
logger.info(
@@ -312,8 +312,9 @@ class ClientRole:
if st is not None and load is not None:
logger.warning(
"P2PSession %s: load request %s (job_id=%d) timed out; "
"load job completed with failure. If this recurs, ensure "
"PYTHONHASHSEED is set to the same value on all nodes.",
"load job completed with failure. If this recurs, ensure all "
"nodes use the same prefix-cache hash seed (matching "
"PYTHONHASHSEED, if set) and hash algorithm.",
self._peer_id,
kv_request_id,
load.job_id,
@@ -125,9 +125,10 @@ class ConnectMsg:
BLOCK_LEN: Size in bytes of each block (must match between peers).
CONFIG_FINGERPRINT: SHA-256 prefix of the model configuration.
Peers with different fingerprints are incompatible.
HASH_SEED: The peer's PYTHONHASHSEED. Block hashes chain from a seed
derived from it, so peers with different values compute different
hashes for identical content and must not exchange blocks.
HASH_SEED: The peer's effective prefix-cache hash seed (PYTHONHASHSEED
if set, otherwise the built-in default). Block hashes chain from a
seed derived from it, so peers with different values compute
different hashes for identical content and must not exchange blocks.
"""
TYPE = "connect"
@@ -458,9 +458,10 @@ class P2PSession:
)
if msg[ConnectMsg.HASH_SEED] != self._local_hash_seed:
raise ValueError(
f"PYTHONHASHSEED mismatch from {self.peer_id}: "
f"hash seed mismatch from {self.peer_id}: "
f"remote={msg[ConnectMsg.HASH_SEED]!r}, "
f"local={self._local_hash_seed!r}"
f"local={self._local_hash_seed!r}. Ensure PYTHONHASHSEED "
"(if set) matches on all P2P peers."
)
self._transport.add_remote_peer(
self.peer_id,