[Core] Add xxHash as a high-performance hash option for accelerating prefix caching (#29163)
Signed-off-by: LuminolT <lumischen01@gmail.com> Signed-off-by: Lumis Chen <lumischen01@gmail.com> Co-authored-by: Russell Bryant <rbryant@redhat.com>
This commit is contained in:
co-authored by
Russell Bryant
parent
5aa9b09040
commit
9bcf92295a
+15
-3
@@ -30,7 +30,7 @@ CacheDType = Literal[
|
||||
"fp8_ds_mla",
|
||||
]
|
||||
MambaDType = Literal["auto", "float32"]
|
||||
PrefixCachingHashAlgo = Literal["sha256", "sha256_cbor"]
|
||||
PrefixCachingHashAlgo = Literal["sha256", "sha256_cbor", "xxhash", "xxhash_cbor"]
|
||||
KVOffloadingBackend = Literal["native", "lmcache"]
|
||||
|
||||
|
||||
@@ -77,9 +77,21 @@ class CacheConfig:
|
||||
"""Whether to enable prefix caching."""
|
||||
prefix_caching_hash_algo: PrefixCachingHashAlgo = "sha256"
|
||||
"""Set the hash algorithm for prefix caching:\n
|
||||
- "sha256" uses Pickle for object serialization before hashing.\n
|
||||
- "sha256" uses Pickle for object serialization before hashing. This is the
|
||||
current default, as SHA256 is the most secure choice to avoid potential
|
||||
hash collisions.\n
|
||||
- "sha256_cbor" provides a reproducible, cross-language compatible hash. It
|
||||
serializes objects using canonical CBOR and hashes them with SHA-256."""
|
||||
serializes objects using canonical CBOR and hashes them with SHA-256.\n
|
||||
- "xxhash" uses Pickle serialization with xxHash (128-bit) for faster,
|
||||
non-cryptographic hashing. Requires the optional ``xxhash`` package.
|
||||
IMPORTANT: Use of a hashing algorithm that is not considered
|
||||
cryptographically secure theoretically increases the risk of hash collisions,
|
||||
which can cause undefined behavior or even leak private information in
|
||||
multi-tenant environments. Even if collisions are still very unlikely, it is
|
||||
important to consider your security risk tolerance against the performance
|
||||
benefits before turning this on.\n
|
||||
- "xxhash_cbor" combines canonical CBOR serialization with xxHash for
|
||||
reproducible hashing. Requires the optional ``xxhash`` package."""
|
||||
cpu_offload_gb: float = Field(default=0, ge=0)
|
||||
"""The space in GiB to offload to CPU, per GPU. Default is 0, which means
|
||||
no offloading. Intuitively, this argument can be seen as a virtual way to
|
||||
|
||||
@@ -11,6 +11,17 @@ from typing import Any
|
||||
|
||||
import cbor2
|
||||
|
||||
try:
|
||||
# It is important that this remains an optional dependency.
|
||||
# It would not be allowed in environments with strict security controls,
|
||||
# so it's best not to have it installed when not in use.
|
||||
import xxhash as _xxhash
|
||||
|
||||
if not hasattr(_xxhash, "xxh3_128_digest"):
|
||||
_xxhash = None
|
||||
except ImportError: # pragma: no cover
|
||||
_xxhash = None
|
||||
|
||||
|
||||
def sha256(input: Any) -> bytes:
|
||||
"""Hash any picklable Python object using SHA-256.
|
||||
@@ -47,6 +58,27 @@ def sha256_cbor(input: Any) -> bytes:
|
||||
return hashlib.sha256(input_bytes).digest()
|
||||
|
||||
|
||||
def _xxhash_digest(input_bytes: bytes) -> bytes:
|
||||
if _xxhash is None:
|
||||
raise ModuleNotFoundError(
|
||||
"xxhash is required for the 'xxhash' prefix caching hash algorithms. "
|
||||
"Install it via `pip install xxhash`."
|
||||
)
|
||||
return _xxhash.xxh3_128_digest(input_bytes)
|
||||
|
||||
|
||||
def xxhash(input: Any) -> bytes:
|
||||
"""Hash picklable objects using xxHash."""
|
||||
input_bytes = pickle.dumps(input, protocol=pickle.HIGHEST_PROTOCOL)
|
||||
return _xxhash_digest(input_bytes)
|
||||
|
||||
|
||||
def xxhash_cbor(input: Any) -> bytes:
|
||||
"""Hash objects serialized with CBOR using xxHash."""
|
||||
input_bytes = cbor2.dumps(input, canonical=True)
|
||||
return _xxhash_digest(input_bytes)
|
||||
|
||||
|
||||
def get_hash_fn_by_name(hash_fn_name: str) -> Callable[[Any], bytes]:
|
||||
"""Get a hash function by name, or raise an error if the function is not found.
|
||||
|
||||
@@ -60,6 +92,10 @@ def get_hash_fn_by_name(hash_fn_name: str) -> Callable[[Any], bytes]:
|
||||
return sha256
|
||||
if hash_fn_name == "sha256_cbor":
|
||||
return sha256_cbor
|
||||
if hash_fn_name == "xxhash":
|
||||
return xxhash
|
||||
if hash_fn_name == "xxhash_cbor":
|
||||
return xxhash_cbor
|
||||
|
||||
raise ValueError(f"Unsupported hash function: {hash_fn_name}")
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ from typing import Any, NewType, TypeAlias, overload
|
||||
from vllm import envs
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.logger import init_logger
|
||||
from vllm.utils.hashing import sha256_cbor
|
||||
from vllm.utils.hashing import sha256_cbor, xxhash_cbor
|
||||
from vllm.utils.math_utils import cdiv
|
||||
from vllm.utils.mem_constants import GiB_bytes
|
||||
from vllm.v1.kv_cache_interface import (
|
||||
@@ -83,18 +83,19 @@ logger = init_logger(__name__)
|
||||
#
|
||||
# The function `init_none_hash` initializes this variable globally.
|
||||
NONE_HASH: BlockHash
|
||||
_CBOR_HASH_FUNCTIONS = frozenset({sha256_cbor, xxhash_cbor})
|
||||
|
||||
|
||||
def init_none_hash(hash_fn: Callable[[Any], bytes]):
|
||||
global NONE_HASH
|
||||
|
||||
hash_seed = os.getenv("PYTHONHASHSEED")
|
||||
if hash_seed is None and hash_fn is sha256_cbor:
|
||||
if hash_seed is None and hash_fn in _CBOR_HASH_FUNCTIONS:
|
||||
logger.warning(
|
||||
"PYTHONHASHSEED is not set. This will lead to non-reproducible "
|
||||
"block-hashes when using sha256_cbor as the hash function."
|
||||
"Consider setting PYTHONHASHSEED to a fixed value for "
|
||||
"reproducibility."
|
||||
"block-hashes when using CBOR-based hash functions such as "
|
||||
"sha256_cbor or xxhash_cbor. Consider setting PYTHONHASHSEED to a "
|
||||
"fixed value for reproducibility."
|
||||
)
|
||||
|
||||
if hash_seed is None:
|
||||
|
||||
Reference in New Issue
Block a user