Skip to content

Commit 8a29711

Browse files
authored
[Chore] Separate out hashing utilities from vllm.utils (#27151)
Signed-off-by: dongbo910220 <1275604947@qq.com>
1 parent 191eed0 commit 8a29711

File tree

11 files changed

+74
-62
lines changed

11 files changed

+74
-62
lines changed

tests/utils_/test_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,16 @@
2929
join_host_port,
3030
make_zmq_path,
3131
make_zmq_socket,
32-
sha256,
3332
split_host_port,
3433
split_zmq_path,
3534
unique_filepath,
3635
)
36+
from vllm.utils.hashing import sha256
3737
from vllm.utils.torch_utils import (
3838
common_broadcastable_dtype,
3939
current_stream,
4040
is_lossless_cast,
4141
)
42-
4342
from vllm.utils.mem_utils import MemorySnapshot, memory_profiling
4443
from ..utils import create_new_process_for_each_test, flat_product
4544

tests/v1/core/test_kv_cache_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
PlaceholderRange,
1515
)
1616
from vllm.sampling_params import SamplingParams
17-
from vllm.utils import sha256, sha256_cbor
17+
from vllm.utils.hashing import sha256, sha256_cbor
1818
from vllm.utils.mem_constants import GiB_bytes
1919
from vllm.v1.core.kv_cache_manager import KVCacheManager
2020
from vllm.v1.core.kv_cache_utils import (

tests/v1/core/test_prefix_caching.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
PlaceholderRange,
1717
)
1818
from vllm.sampling_params import SamplingParams
19-
from vllm.utils import sha256, sha256_cbor
19+
from vllm.utils.hashing import sha256, sha256_cbor
2020
from vllm.v1.core.block_pool import BlockHashToBlockMap, BlockPool
2121
from vllm.v1.core.kv_cache_manager import KVCacheManager, Request
2222
from vllm.v1.core.kv_cache_utils import (

tests/v1/core/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
PlaceholderRange,
1818
)
1919
from vllm.sampling_params import SamplingParams
20-
from vllm.utils import sha256
20+
from vllm.utils.hashing import sha256
2121
from vllm.v1.core.kv_cache_utils import get_request_block_hasher, init_none_hash
2222
from vllm.v1.core.sched.async_scheduler import AsyncScheduler
2323
from vllm.v1.core.sched.scheduler import Scheduler

tests/v1/kv_connector/unit/test_offloading_connector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
OffloadingConnectorMetadata,
1919
)
2020
from vllm.forward_context import ForwardContext
21-
from vllm.utils import sha256
21+
from vllm.utils.hashing import sha256
2222
from vllm.v1.core.kv_cache_utils import (
2323
BlockHash,
2424
get_request_block_hasher,

tests/v1/kv_connector/unit/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from vllm.distributed.kv_transfer.kv_connector.v1.shared_storage_connector import ( # noqa
2222
SharedStorageConnector,
2323
)
24-
from vllm.utils import sha256
24+
from vllm.utils.hashing import sha256
2525
from vllm.v1.core.kv_cache_manager import KVCacheBlocks
2626
from vllm.v1.core.kv_cache_utils import get_request_block_hasher, init_none_hash
2727
from vllm.v1.core.sched.scheduler import Scheduler

tools/pre_commit/check_pickle_imports.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"tests/utils.py",
4444
# pickle and cloudpickle
4545
"vllm/utils/__init__.py",
46+
"vllm/utils/hashing.py",
4647
}
4748

4849
PICKLE_RE = re.compile(

vllm/utils/__init__.py

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
import datetime
66
import enum
77
import getpass
8-
import hashlib
98
import importlib
109
import inspect
1110
import ipaddress
1211
import json
1312
import multiprocessing
1413
import os
15-
import pickle
1614
import signal
1715
import socket
1816
import subprocess
@@ -45,7 +43,6 @@
4543
from urllib.parse import urlparse
4644
from uuid import uuid4
4745

48-
import cbor2
4946
import cloudpickle
5047
import psutil
5148
import regex as re
@@ -1463,56 +1460,7 @@ def check_use_alibi(model_config: ModelConfig) -> bool:
14631460
)
14641461

14651462

1466-
def sha256(input: Any) -> bytes:
1467-
"""Hash any picklable Python object using SHA-256.
1468-
1469-
The input is serialized using pickle before hashing, which allows
1470-
arbitrary Python objects to be used. Note that this function does
1471-
not use a hash seed—if you need one, prepend it explicitly to the input.
1472-
1473-
Args:
1474-
input: Any picklable Python object.
1475-
1476-
Returns:
1477-
Bytes representing the SHA-256 hash of the serialized input.
1478-
"""
1479-
input_bytes = pickle.dumps(input, protocol=pickle.HIGHEST_PROTOCOL)
1480-
return hashlib.sha256(input_bytes).digest()
1481-
1482-
1483-
def sha256_cbor(input: Any) -> bytes:
1484-
"""
1485-
Hash objects using CBOR serialization and SHA-256.
1486-
1487-
This option is useful for non-Python-dependent serialization and hashing.
1488-
1489-
Args:
1490-
input: Object to be serialized and hashed. Supported types include
1491-
basic Python types and complex structures like lists, tuples, and
1492-
dictionaries.
1493-
Custom classes must implement CBOR serialization methods.
1494-
1495-
Returns:
1496-
Bytes representing the SHA-256 hash of the CBOR serialized input.
1497-
"""
1498-
input_bytes = cbor2.dumps(input, canonical=True)
1499-
return hashlib.sha256(input_bytes).digest()
1500-
1501-
1502-
def get_hash_fn_by_name(hash_fn_name: str) -> Callable[[Any], bytes]:
1503-
"""Get a hash function by name, or raise an error if
1504-
the function is not found.
1505-
Args:
1506-
hash_fn_name: Name of the hash function.
1507-
Returns:
1508-
A hash function.
1509-
"""
1510-
if hash_fn_name == "sha256":
1511-
return sha256
1512-
if hash_fn_name == "sha256_cbor":
1513-
return sha256_cbor
1514-
1515-
raise ValueError(f"Unsupported hash function: {hash_fn_name}")
1463+
## moved to vllm.utils.hashing
15161464

15171465

15181466
@cache

vllm/utils/hashing.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3+
4+
from __future__ import annotations
5+
6+
import hashlib
7+
import pickle
8+
from collections.abc import Callable
9+
from typing import Any
10+
11+
import cbor2
12+
13+
14+
def sha256(input: Any) -> bytes:
15+
"""Hash any picklable Python object using SHA-256.
16+
17+
The input is serialized using pickle before hashing, which allows
18+
arbitrary Python objects to be used. Note that this function does
19+
not use a hash seed—if you need one, prepend it explicitly to the input.
20+
21+
Args:
22+
input: Any picklable Python object.
23+
24+
Returns:
25+
Bytes representing the SHA-256 hash of the serialized input.
26+
"""
27+
input_bytes = pickle.dumps(input, protocol=pickle.HIGHEST_PROTOCOL)
28+
return hashlib.sha256(input_bytes).digest()
29+
30+
31+
def sha256_cbor(input: Any) -> bytes:
32+
"""Hash objects using CBOR serialization and SHA-256.
33+
34+
This option is useful for non-Python-dependent serialization and hashing.
35+
36+
Args:
37+
input: Object to be serialized and hashed. Supported types include
38+
basic Python types and complex structures like lists, tuples, and
39+
dictionaries.
40+
Custom classes must implement CBOR serialization methods.
41+
42+
Returns:
43+
Bytes representing the SHA-256 hash of the CBOR serialized input.
44+
"""
45+
input_bytes = cbor2.dumps(input, canonical=True)
46+
return hashlib.sha256(input_bytes).digest()
47+
48+
49+
def get_hash_fn_by_name(hash_fn_name: str) -> Callable[[Any], bytes]:
50+
"""Get a hash function by name, or raise an error if the function is not found.
51+
52+
Args:
53+
hash_fn_name: Name of the hash function.
54+
55+
Returns:
56+
A hash function.
57+
"""
58+
if hash_fn_name == "sha256":
59+
return sha256
60+
if hash_fn_name == "sha256_cbor":
61+
return sha256_cbor
62+
63+
raise ValueError(f"Unsupported hash function: {hash_fn_name}")

vllm/v1/core/kv_cache_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from vllm import envs
1313
from vllm.config import VllmConfig
1414
from vllm.logger import init_logger
15-
from vllm.utils import cdiv, sha256_cbor
15+
from vllm.utils import cdiv
16+
from vllm.utils.hashing import sha256_cbor
1617
from vllm.utils.mem_constants import GiB_bytes
1718
from vllm.v1.kv_cache_interface import (
1819
ChunkedLocalAttentionSpec,

0 commit comments

Comments
 (0)