Skip to content

vllm.utils.hashing

Functions:

  • get_hash_fn_by_name

    Get a hash function by name, or raise an error if the function is not found.

  • safe_hash

    Hash for configs, defaulting to md5 but falling back to sha256

  • sha256

    Hash any picklable Python object using SHA-256.

  • sha256_cbor

    Hash objects using CBOR serialization and SHA-256.

  • xxhash

    Hash picklable objects using xxHash.

  • xxhash_cbor

    Hash objects serialized with CBOR using xxHash.

get_hash_fn_by_name(hash_fn_name)

Get a hash function by name, or raise an error if the function is not found.

Parameters:

  • hash_fn_name

    (str) –

    Name of the hash function.

Returns:

Source code in vllm/utils/hashing.py
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.

    Args:
        hash_fn_name: Name of the hash function.

    Returns:
        A hash function.
    """
    if hash_fn_name == "sha256":
        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}")

safe_hash(data, usedforsecurity=True)

Hash for configs, defaulting to md5 but falling back to sha256 in FIPS constrained environments.

Parameters:

  • data

    (bytes) –

    bytes

  • usedforsecurity

    (bool, default: True ) –

    Whether the hash is used for security purposes

Returns:

  • HASH

    Hash object

Source code in vllm/utils/hashing.py
def safe_hash(data: bytes, usedforsecurity: bool = True) -> HASH:
    """Hash for configs, defaulting to md5 but falling back to sha256
    in FIPS constrained environments.

    Args:
        data: bytes
        usedforsecurity: Whether the hash is used for security purposes

    Returns:
        Hash object
    """
    try:
        return hashlib.md5(data, usedforsecurity=usedforsecurity)
    except (UnsupportedDigestmodError, ValueError):
        return hashlib.sha256(data)

sha256(input)

Hash any picklable Python object using SHA-256.

The input is serialized using pickle before hashing, which allows arbitrary Python objects to be used. Note that this function does not use a hash seed—if you need one, prepend it explicitly to the input.

Parameters:

  • input

    (Any) –

    Any picklable Python object.

Returns:

  • bytes

    Bytes representing the SHA-256 hash of the serialized input.

Source code in vllm/utils/hashing.py
def sha256(input: Any) -> bytes:
    """Hash any picklable Python object using SHA-256.

    The input is serialized using pickle before hashing, which allows
    arbitrary Python objects to be used. Note that this function does
    not use a hash seed—if you need one, prepend it explicitly to the input.

    Args:
        input: Any picklable Python object.

    Returns:
        Bytes representing the SHA-256 hash of the serialized input.
    """
    input_bytes = pickle.dumps(input, protocol=pickle.HIGHEST_PROTOCOL)
    return hashlib.sha256(input_bytes).digest()

sha256_cbor(input)

Hash objects using CBOR serialization and SHA-256.

This option is useful for non-Python-dependent serialization and hashing.

Parameters:

  • input

    (Any) –

    Object to be serialized and hashed. Supported types include basic Python types and complex structures like lists, tuples, and dictionaries. Custom classes must implement CBOR serialization methods.

Returns:

  • bytes

    Bytes representing the SHA-256 hash of the CBOR serialized input.

Source code in vllm/utils/hashing.py
def sha256_cbor(input: Any) -> bytes:
    """Hash objects using CBOR serialization and SHA-256.

    This option is useful for non-Python-dependent serialization and hashing.

    Args:
        input: Object to be serialized and hashed. Supported types include
            basic Python types and complex structures like lists, tuples, and
            dictionaries.
            Custom classes must implement CBOR serialization methods.

    Returns:
        Bytes representing the SHA-256 hash of the CBOR serialized input.
    """
    input_bytes = cbor2.dumps(input, canonical=True)
    return hashlib.sha256(input_bytes).digest()

xxhash(input)

Hash picklable objects using xxHash.

Source code in vllm/utils/hashing.py
def xxhash(input: Any) -> bytes:
    """Hash picklable objects using xxHash."""
    input_bytes = pickle.dumps(input, protocol=pickle.HIGHEST_PROTOCOL)
    return _xxhash_digest(input_bytes)

xxhash_cbor(input)

Hash objects serialized with CBOR using xxHash.

Source code in vllm/utils/hashing.py
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)