[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:
co-authored by
Claude Opus 4.8
parent
8f4a7f45c5
commit
ef47a897e2
@@ -15,7 +15,7 @@ from typing import Any, NamedTuple, NewType, TypeAlias, cast, overload
|
||||
from vllm import envs
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.logger import init_logger
|
||||
from vllm.utils.hashing import sha256_cbor, xxhash_cbor
|
||||
from vllm.utils.hashing import xxhash, xxhash_cbor
|
||||
from vllm.utils.math_utils import cdiv, round_up
|
||||
from vllm.utils.mem_utils import format_gib
|
||||
from vllm.utils.torch_utils import get_dtype_size
|
||||
@@ -87,32 +87,73 @@ logger = init_logger(__name__)
|
||||
|
||||
# The hash seed for the first block of any prefix block sequence.
|
||||
#
|
||||
# We use a random value to avoid hash collisions or PYTHONHASHSEED environment
|
||||
# variable if set such that processes can share the seed if needed. This aligns
|
||||
# with the behavior of Python's hash() function, which also uses a random seed
|
||||
# if PYTHONHASHSEED is not set.
|
||||
# For cryptographic hash algorithms it is derived deterministically from a fixed
|
||||
# default seed, so independent vLLM processes compute identical block hashes for
|
||||
# identical content and can share a prefix cache (e.g. KV cache reuse across
|
||||
# nodes) without extra configuration. This does not weaken collision resistance,
|
||||
# which for SHA-256 does not depend on keeping the seed secret; ``cache_salt``
|
||||
# remains the mechanism for intentional cache isolation.
|
||||
#
|
||||
# Non-cryptographic algorithms keep a per-process random seed, because a
|
||||
# predictable seed would let an attacker precompute colliding blocks offline
|
||||
# (see #12621). Setting PYTHONHASHSEED overrides the seed in both cases.
|
||||
#
|
||||
# The function `init_none_hash` initializes this variable globally.
|
||||
NONE_HASH: BlockHash
|
||||
_CBOR_HASH_FUNCTIONS = frozenset({sha256_cbor, xxhash_cbor})
|
||||
|
||||
# Fixed seed used when the PYTHONHASHSEED environment variable is not set and
|
||||
# the hash algorithm is cryptographic.
|
||||
DEFAULT_NONE_HASH_SEED = "vllm-none-hash"
|
||||
|
||||
# Algorithms that are not collision resistant, so the seed must stay secret.
|
||||
_NON_CRYPTO_HASH_FUNCTIONS = frozenset({xxhash, xxhash_cbor})
|
||||
|
||||
# The seed NONE_HASH was derived from, set by init_none_hash.
|
||||
_NONE_HASH_SEED: str | None = None
|
||||
|
||||
|
||||
def resolve_none_hash_seed(hash_fn: Callable[[Any], bytes]) -> str:
|
||||
"""Resolve the seed to derive NONE_HASH from.
|
||||
|
||||
PYTHONHASHSEED wins if set. Otherwise cryptographic algorithms get the
|
||||
fixed default (shareable across processes) and non-cryptographic ones get
|
||||
fresh random bytes, keeping the seed unpredictable where collision
|
||||
resistance depends on it.
|
||||
"""
|
||||
hash_seed = os.getenv("PYTHONHASHSEED")
|
||||
if hash_seed is not None:
|
||||
return hash_seed
|
||||
if hash_fn in _NON_CRYPTO_HASH_FUNCTIONS:
|
||||
return os.urandom(32).hex()
|
||||
return DEFAULT_NONE_HASH_SEED
|
||||
|
||||
|
||||
def get_none_hash_seed() -> str:
|
||||
"""Return the seed NONE_HASH was derived from.
|
||||
|
||||
Components that must agree on NONE_HASH across processes (the P2P tier
|
||||
advertises this during its connect handshake) read the resolved seed here
|
||||
instead of re-deriving it, so they observe the random seed too. Falls back
|
||||
to the deterministic seed before ``init_none_hash`` has run.
|
||||
"""
|
||||
if _NONE_HASH_SEED is None:
|
||||
return DEFAULT_NONE_HASH_SEED
|
||||
return _NONE_HASH_SEED
|
||||
|
||||
|
||||
def init_none_hash(hash_fn: Callable[[Any], bytes]):
|
||||
global NONE_HASH
|
||||
global NONE_HASH, _NONE_HASH_SEED
|
||||
|
||||
hash_seed = os.getenv("PYTHONHASHSEED")
|
||||
if hash_seed is None and hash_fn in _CBOR_HASH_FUNCTIONS:
|
||||
_NONE_HASH_SEED = resolve_none_hash_seed(hash_fn)
|
||||
if hash_fn in _NON_CRYPTO_HASH_FUNCTIONS and os.getenv("PYTHONHASHSEED") is None:
|
||||
logger.warning(
|
||||
"PYTHONHASHSEED is not set. This will lead to non-reproducible "
|
||||
"block-hashes when using CBOR-based hash functions such as "
|
||||
"sha256_cbor or xxhash_cbor. Consider setting PYTHONHASHSEED to a "
|
||||
"fixed value for reproducibility."
|
||||
"Using a random per-process NONE_HASH seed because %s is not "
|
||||
"collision resistant. Block hashes are therefore not reproducible "
|
||||
"across processes; set PYTHONHASHSEED to a shared value to reuse "
|
||||
"the prefix cache across instances, or use sha256.",
|
||||
hash_fn.__name__,
|
||||
)
|
||||
|
||||
if hash_seed is None:
|
||||
NONE_HASH = BlockHash(os.urandom(32))
|
||||
else:
|
||||
NONE_HASH = BlockHash(hash_fn(hash_seed))
|
||||
NONE_HASH = BlockHash(hash_fn(_NONE_HASH_SEED))
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user