A high-throughput and memory-efficient inference and serving engine for LLMs
Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git
Synced Nov 14, 05:30 UTC
[Chore] Separate out hashing utilities from vllm.utils (#27151)
Signed-off-by: dongbo910220 <1275604947@qq.com>
dongbo910220 committed
Oct 19, 2025 at 11:09 UTC
8a297115e2367d463b781adb86b55ac740594cf6
11 files changed
+74
-62
tests/utils_/test_utils.py
+1
-2
@@ -29,17 +29,16 @@ from vllm.utils import (
29
join_host_port,
30
make_zmq_path,
31
make_zmq_socket,
32
- sha256,
32
split_host_port,
33
split_zmq_path,
34
unique_filepath,
35
)
36
+from vllm.utils.hashing import sha256
37
from vllm.utils.torch_utils import (
38
common_broadcastable_dtype,
39
current_stream,
40
is_lossless_cast,
41
)
42
-
42
from vllm.utils.mem_utils import MemorySnapshot, memory_profiling
43
from ..utils import create_new_process_for_each_test, flat_product
44
tests/v1/core/test_kv_cache_utils.py
+1
-1
@@ -14,7 +14,7 @@ from vllm.multimodal.inputs import (
14
PlaceholderRange,
15
)
16
from vllm.sampling_params import SamplingParams
17
-from vllm.utils import sha256, sha256_cbor
17
+from vllm.utils.hashing import sha256, sha256_cbor
18
from vllm.utils.mem_constants import GiB_bytes
19
from vllm.v1.core.kv_cache_manager import KVCacheManager
20
from vllm.v1.core.kv_cache_utils import (
tests/v1/core/test_prefix_caching.py
+1
-1
@@ -16,7 +16,7 @@ from vllm.multimodal.inputs import (
16
PlaceholderRange,
17
)
18
from vllm.sampling_params import SamplingParams
19
-from vllm.utils import sha256, sha256_cbor
19
+from vllm.utils.hashing import sha256, sha256_cbor
20
from vllm.v1.core.block_pool import BlockHashToBlockMap, BlockPool
21
from vllm.v1.core.kv_cache_manager import KVCacheManager, Request
22
from vllm.v1.core.kv_cache_utils import (
tests/v1/core/utils.py
+1
-1
@@ -17,7 +17,7 @@ from vllm.multimodal.inputs import (
17
PlaceholderRange,
18
)
19
from vllm.sampling_params import SamplingParams
20
-from vllm.utils import sha256
20
+from vllm.utils.hashing import sha256
21
from vllm.v1.core.kv_cache_utils import get_request_block_hasher, init_none_hash
22
from vllm.v1.core.sched.async_scheduler import AsyncScheduler
23
from vllm.v1.core.sched.scheduler import Scheduler
tests/v1/kv_connector/unit/test_offloading_connector.py
+1
-1
@@ -18,7 +18,7 @@ from vllm.distributed.kv_transfer.kv_connector.v1.offloading_connector import (
18
OffloadingConnectorMetadata,
19
)
20
from vllm.forward_context import ForwardContext
21
-from vllm.utils import sha256
21
+from vllm.utils.hashing import sha256
22
from vllm.v1.core.kv_cache_utils import (
23
BlockHash,
24
get_request_block_hasher,
tests/v1/kv_connector/unit/utils.py
+1
-1
@@ -21,7 +21,7 @@ from vllm.distributed.kv_transfer.kv_connector.factory import KVConnectorFactory
21
from vllm.distributed.kv_transfer.kv_connector.v1.shared_storage_connector import ( # noqa
22
SharedStorageConnector,
23
)
24
-from vllm.utils import sha256
24
+from vllm.utils.hashing import sha256
25
from vllm.v1.core.kv_cache_manager import KVCacheBlocks
26
from vllm.v1.core.kv_cache_utils import get_request_block_hasher, init_none_hash
27
from vllm.v1.core.sched.scheduler import Scheduler
tools/pre_commit/check_pickle_imports.py
+1
@@ -43,6 +43,7 @@ ALLOWED_FILES = {
43
"tests/utils.py",
44
# pickle and cloudpickle
45
"vllm/utils/__init__.py",
46
+ "vllm/utils/hashing.py",
47
}
48
49
PICKLE_RE = re.compile(
vllm/utils/__init__.py
+1
-53
@@ -5,14 +5,12 @@ import contextlib
5
import datetime
6
import enum
7
import getpass
8
-import hashlib
8
import importlib
9
import inspect
10
import ipaddress
11
import json
12
import multiprocessing
13
import os
15
-import pickle
14
import signal
15
import socket
16
import subprocess
@@ -45,7 +43,6 @@ from typing import TYPE_CHECKING, Any, TextIO, TypeVar
43
from urllib.parse import urlparse
44
from uuid import uuid4
45
48
-import cbor2
46
import cloudpickle
47
import psutil
48
import regex as re
@@ -1463,56 +1460,7 @@ def check_use_alibi(model_config: ModelConfig) -> bool:
1460
)
1461
1462
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
1464
1465
1466
@cache
vllm/utils/hashing.py
new
+63
@@ -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
+2
-1
@@ -12,7 +12,8 @@ from typing import Any, NewType, TypeAlias
12
from vllm import envs
13
from vllm.config import VllmConfig
14
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
17
from vllm.utils.mem_constants import GiB_bytes
18
from vllm.v1.kv_cache_interface import (
19
ChunkedLocalAttentionSpec,
vllm/v1/engine/core.py
+1
-1
@@ -30,11 +30,11 @@ from vllm.tasks import POOLING_TASKS, SupportedTask
30
from vllm.transformers_utils.config import maybe_register_config_serialize_by_value
31
from vllm.utils import (
32
decorate_logs,
33
- get_hash_fn_by_name,
33
make_zmq_socket,
34
set_process_title,
35
)
36
from vllm.utils.gc_utils import maybe_attach_gc_debug_callback
37
+from vllm.utils.hashing import get_hash_fn_by_name
38
from vllm.utils.import_utils import resolve_obj_by_qualname
39
from vllm.v1.core.kv_cache_utils import (
40
BlockHash,