[Refactor] Remove utils dead code (#53941)

Signed-off-by: yewentao256 <zhyanwentao@126.com>

Wentao Ye committed Sep 8, 2026 at 11:22 UTC 6b15bea080ac64c9e123982b48cbbffe34957ec0
15 files changed +4 -278
tests/models/multimodal/generation/test_memory_leak.py
+2 -1
@@ -15,7 +15,8 @@ from vllm import LLM, SamplingParams
15 from vllm.distributed import cleanup_dist_env_and_memory
16 from vllm.entrypoints.chat_utils import ChatCompletionMessageParam
17 from vllm.platforms import current_platform
18 -from vllm.utils.mem_utils import KiB_bytes, MiB_bytes, format_mib
18 +from vllm.utils.mem_constants import KiB_bytes, MiB_bytes
19 +from vllm.utils.mem_utils import format_mib
20
21 MODEL_NAME = "Qwen/Qwen3-VL-4B-Instruct"
22 RANDOM_PREFIX_LEN = 100
vllm/compilation/passes/fx_utils.py
-10
@@ -1,7 +1,6 @@
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 -import operator
4 from collections.abc import Iterable, Iterator
5
6 from torch import fx
@@ -29,15 +28,6 @@ def find_auto_fn(nodes: Iterable[fx.Node], op: OpOverload) -> fx.Node:
28 return node
29
30
32 -# Returns the getitem node that extracts the idx-th element from node
33 -# (if it exists)
34 -def find_getitem_maybe(node: fx.Node, idx: int) -> fx.Node | None:
35 - for user in node.users:
36 - if is_func(user, operator.getitem) and user.args[1] == idx:
37 - return user
38 - return None
39 -
40 -
31 # An auto-functionalization-aware utility for finding nodes with a specific op
32 # Also handles op overload packets and finds all overloads
33 def find_op_nodes(
vllm/distributed/kv_transfer/kv_connector/v1/lmcache_integration/utils.py
-74
@@ -23,11 +23,6 @@ _config_instance: V1Config | None = None
23 _config_lock = threading.Lock()
24
25
26 -def is_false(value: str) -> bool:
27 - """Check if the given string value is equivalent to 'false'."""
28 - return value.lower() in ("false", "0", "no", "n", "off")
29 -
30 -
26 def lmcache_get_or_create_config() -> V1Config:
27 """Get the LMCache configuration from the environment variable
28 `LMCACHE_CONFIG_FILE`. If the environment variable is not set, this
@@ -97,75 +92,6 @@ def mla_enabled(model_config: "ModelConfig") -> bool:
92 )
93
94
100 -def create_lmcache_metadata(
101 - vllm_config=None, model_config=None, parallel_config=None, cache_config=None
102 -):
103 - """
104 - Create LMCacheEngineMetadata from vLLM configuration.
105 -
106 - This function extracts common metadata creation logic that was duplicated
107 - across multiple files.
108 -
109 - Args:
110 - vllm_config (VllmConfig): vLLM configuration object containing model,
111 - parallel, and cache configs (alternative to
112 - individual config parameters)
113 - model_config (ModelConfig): Model configuration (alternative to
114 - vllm_config)
115 - parallel_config (ParallelConfig): Parallel configuration (alternative
116 - to vllm_config)
117 - cache_config (CacheConfig): Cache configuration (alternative to
118 - vllm_config)
119 - """
120 - # Third Party
121 - # First Party
122 - from lmcache.config import LMCacheEngineMetadata
123 -
124 - from vllm.utils.torch_utils import get_kv_cache_torch_dtype
125 -
126 - config = lmcache_get_or_create_config()
127 - # Support both vllm_config object and individual config parameters
128 - if vllm_config is not None:
129 - model_cfg = vllm_config.model_config
130 - parallel_cfg = vllm_config.parallel_config
131 - cache_cfg = vllm_config.cache_config
132 - else:
133 - if model_config is None or parallel_config is None or cache_config is None:
134 - raise ValueError(
135 - "Either vllm_config must be provided, or all of "
136 - "model_config, parallel_config, and cache_config must be provided."
137 - )
138 - model_cfg = model_config
139 - parallel_cfg = parallel_config
140 - cache_cfg = cache_config
141 -
142 - # Get KV cache dtype
143 - kv_dtype = get_kv_cache_torch_dtype(cache_cfg.cache_dtype, model_cfg.dtype)
144 -
145 - # Check if MLA is enabled
146 - use_mla = mla_enabled(model_cfg)
147 -
148 - # Construct KV shape (for memory pool)
149 - num_layer = model_cfg.get_num_layers(parallel_cfg)
150 - chunk_size = config.chunk_size
151 - num_kv_head = model_cfg.get_num_kv_heads(parallel_cfg)
152 - head_size = model_cfg.get_head_size()
153 - kv_shape = (num_layer, 1 if use_mla else 2, chunk_size, num_kv_head, head_size)
154 -
155 - # Create metadata
156 - metadata = LMCacheEngineMetadata(
157 - model_cfg.model,
158 - parallel_cfg.world_size,
159 - parallel_cfg.rank,
160 - "vllm",
161 - kv_dtype,
162 - kv_shape,
163 - use_mla,
164 - )
165 -
166 - return metadata, config
167 -
168 -
95 def extract_mm_features(
96 request: Union["Request", "NewRequestData"], modify: bool = False
97 ) -> tuple[list[str], list["PlaceholderRange"]]:
vllm/model_executor/layers/utils.py
-8
@@ -22,14 +22,6 @@ from vllm.utils.torch_utils import direct_register_custom_op
22
23 logger = init_logger(__name__)
24
25 -MOE_LAYER_ROUTER_GATE_SUFFIXES = {
26 - "gate",
27 - "router",
28 - "router_gate",
29 - "shared_expert_gate",
30 - "expert_gate",
31 -}
32 -
25
26 def get_token_bin_counts_and_mask(
27 tokens: torch.Tensor,
vllm/model_executor/model_loader/weight_utils.py
-15
@@ -1241,21 +1241,6 @@ def multi_thread_pt_weights_iterator(
1241 del state
1242
1243
1244 -def convert_pyslice_to_tensor(x: Any) -> torch.Tensor:
1245 - """convert PySafeSlice object from safetensors to torch.Tensor
1246 -
1247 - PySafeSlice object supports indexing, which is done before loading the
1248 - actual tensor and can reduce the amount of memory being read into the
1249 - memory. However, it does not support more advanced functionalities
1250 - like `.view()` or `.t()`. Therefore, if we need to modify the loaded
1251 - tensor with these more complicated operators, we need to convert to
1252 - tensor first.
1253 - """
1254 - if not isinstance(x, torch.Tensor):
1255 - x = x[:]
1256 - return x
1257 -
1258 -
1244 def default_weight_loader(param: torch.Tensor, loaded_weight: torch.Tensor) -> None:
1245 """Default weight loader."""
1246 try:
vllm/utils/flashinfer.py
-38
@@ -995,44 +995,6 @@ if has_flashinfer():
995 return torch.empty(A.shape[0], B.shape[1], dtype=out_dtype, device=A.device)
996
997
998 -def flashinfer_mm_mxfp8(
999 - a: torch.Tensor,
1000 - b: torch.Tensor,
1001 - block_scale_a: torch.Tensor,
1002 - block_scale_b: torch.Tensor,
1003 - out_dtype: torch.dtype,
1004 - backend: str = "cutlass",
1005 -) -> torch.Tensor:
1006 - """MXFP8 MM helper - mirrors flashinfer_scaled_fp4_mm API.
1007 -
1008 - Takes non-transposed weights and handles transpose internally.
1009 -
1010 - CRITICAL: mm_mxfp8 CUTLASS kernel requires SWIZZLED 1D scales for optimal
1011 - performance and accuracy. Both input and weight scales should be in
1012 - swizzled format from FlashInfer's mxfp8_quantize(is_sf_swizzled_layout=True).
1013 - """
1014 - # a shape [M, K]
1015 - # b shape [K, N]
1016 - assert a.ndim == 2 and b.ndim == 2
1017 - assert a.shape[1] == b.shape[1] # K dimension must match
1018 -
1019 - if block_scale_b.ndim != 1:
1020 - raise ValueError(
1021 - "mm_mxfp8 expects 1D swizzled weight scales for CUTLASS; "
1022 - f"got shape={tuple(block_scale_b.shape)}"
1023 - )
1024 -
1025 - # Output tensor [M, N]
1026 - return mm_mxfp8(
1027 - a,
1028 - b.t(), # Transpose weight: [N, K] -> [K, N]
1029 - block_scale_a,
1030 - block_scale_b,
1031 - out_dtype,
1032 - backend=backend,
1033 - )
1034 -
1035 -
998 def flashinfer_scaled_fp4_mm(
999 a: torch.Tensor,
1000 b: torch.Tensor,
vllm/utils/hpc.py
-41
@@ -76,27 +76,6 @@ def fuse_moe_impl(
76 )
77
78
79 -# @torch.library.register_fake(
80 -# "vllm::fuse_moe_impl",
81 -# )
82 -def fuse_moe_impl_fake(
83 - x: torch.Tensor,
84 - gate_up_weight: torch.Tensor,
85 - down_weight: torch.Tensor,
86 - gate_up_scale: torch.Tensor,
87 - down_scale: torch.Tensor,
88 - act_and_mul_scale: torch.Tensor,
89 - topk_ids: torch.Tensor,
90 - topk_scale: torch.Tensor,
91 - rank_ep: int,
92 - num_expert_total: int,
93 - use_bf16_mul: bool = True,
94 - shared_output: torch.Tensor = None,
95 - output: torch.Tensor = None,
96 -) -> torch.Tensor:
97 - return torch.empty_like(x)
98 -
99 -
79 def hpc_fuse_moe(
80 x: torch.Tensor,
81 gate_up_weight: torch.Tensor,
@@ -166,26 +145,6 @@ def fuse_moe_blockwise_impl(
145 )
146
147
169 -# @torch.library.register_fake(
170 -# "vllm::fuse_moe_blockwise_impl",
171 -# )
172 -def fuse_moe_blockwise_impl_fake(
173 - x: torch.Tensor,
174 - x_scale: torch.Tensor,
175 - gate_up_weight: torch.Tensor,
176 - gate_up_weight_scale: torch.Tensor,
177 - down_weight: torch.Tensor,
178 - down_weight_scale: torch.Tensor,
179 - topk_ids: torch.Tensor,
180 - topk_scale: torch.Tensor,
181 - rank_ep: int,
182 - num_expert_total: int,
183 - shared_output: torch.Tensor = None,
184 - output: torch.Tensor = None,
185 -) -> torch.Tensor:
186 - return torch.empty_like(x)
187 -
188 -
148 def hpc_fuse_moe_blockwise(
149 x: torch.Tensor,
150 x_scale: torch.Tensor,
vllm/utils/mem_utils.py
-5
@@ -43,11 +43,6 @@ def get_max_shared_memory_bytes(gpu: int = 0) -> int:
43 return int(max_shared_mem)
44
45
46 -def get_cpu_memory() -> int:
47 - """Returns the total CPU memory of the node in bytes."""
48 - return psutil.virtual_memory().total
49 -
50 -
46 _UMA_PRESSURE_THRESHOLD = 0.8
47 _UMA_MIN_RELEASE_BYTES = 512 * MiB_bytes
48
vllm/utils/torch_utils.py
-9
@@ -497,15 +497,6 @@ def get_kv_cache_quant_algo_string(quant_cfg: dict[str, Any]) -> str | None:
497 return None
498
499
500 -def get_kv_cache_quant_algo_dtype(quant_cfg: dict[str, Any]) -> torch.dtype | None:
501 - """Get the KV cache quantization algorithm dtype from the quantization config."""
502 - kv_algo_str = get_kv_cache_quant_algo_string(quant_cfg)
503 - if kv_algo_str is not None and kv_algo_str != "auto":
504 - # Only convert if we have a valid dtype string (not "auto" fallback)
505 - return STR_DTYPE_TO_TORCH_DTYPE[kv_algo_str]
506 - return None
507 -
508 -
500 def resolve_kv_cache_dtype_string(
501 kv_cache_dtype: str, model_config: ModelConfig
502 ) -> str:
vllm/v1/attention/backends/utils.py
+1 -14
@@ -3,7 +3,7 @@
3 import math
4 from collections import defaultdict
5 from collections.abc import Callable, Iterable, Sequence
6 -from dataclasses import dataclass, field, fields, make_dataclass
6 +from dataclasses import dataclass, field, fields
7 from typing import (
8 TYPE_CHECKING,
9 Any,
@@ -975,19 +975,6 @@ def reshape_attn_output_for_spec_decode(attn_output: torch.Tensor) -> torch.Tens
975 return attn_output.view(total_tokens, attn_output.shape[2], attn_output.shape[3])
976
977
978 -def subclass_attention_metadata(
979 - name_prefix: str,
980 - metadata_cls: Any,
981 - fields: list[tuple[str, Any, Any]],
982 -) -> Any:
983 - """
984 - Return a new subclass of `metadata_cls` with additional fields
985 - """
986 - name: str = name_prefix + metadata_cls.__name__ # type: ignore
987 - Wrapped = make_dataclass(name, fields, bases=(metadata_cls,))
988 - return Wrapped
989 -
990 -
978 @runtime_checkable
979 class KVSharingFastPrefillMetadata(Protocol):
980 logits_indices_padded: torch.Tensor | None = None
vllm/v1/core/kv_cache_coordinator.py
-2
@@ -86,8 +86,6 @@ class KVCacheCoordinator(ABC):
86 num_prefill_lookahead: int = 0,
87 ):
88 self.kv_cache_config = kv_cache_config
89 - self.max_model_len = max_model_len
90 - self.enable_caching = enable_caching
89 # The scheduling granularity (LCM of all group block sizes), must be a multiple
90 # of the hash_block_size and the block size of each group.
91 assert scheduler_block_size % hash_block_size == 0 and all(
vllm/v1/core/single_type_kv_cache_manager.py
-2
@@ -2148,8 +2148,6 @@ class SinkFullAttentionManager(FullAttentionManager):
2148 )
2149 sink_len = kv_cache_spec.sink_len
2150 assert sink_len is not None and sink_len > 0 and sink_len % self.block_size == 0
2151 - num_sink_block = sink_len // self.block_size
2152 - self.sink_blocks = self.block_pool.free_block_queue.popleft_n(num_sink_block)
2151
2152
2153 def get_manager_for_kv_cache_spec(
vllm/v1/engine/core.py
+1 -20
@@ -85,7 +85,7 @@ from vllm.v1.fault_tolerance.engine_core_sentinel import (
85 EngineCoreSentinel,
86 fault_tolerant_wrapper,
87 )
88 -from vllm.v1.kv_cache_interface import KVCacheConfig, get_kv_cache_spec_kind
88 +from vllm.v1.kv_cache_interface import KVCacheConfig
89 from vllm.v1.metrics.stats import SchedulerIterationDetails, SchedulerStats
90 from vllm.v1.outputs import ModelRunnerOutput
91 from vllm.v1.request import Request, RequestStatus
@@ -441,25 +441,6 @@ class EngineCore:
441 supported_pooling_tasks,
442 )
443
444 - def get_kv_cache_group_metadata(self) -> list[dict[str, int | str | None]]:
445 - """Return msgspec-serializable metadata for scheduler KV cache groups."""
446 - kv_cache_config = getattr(self.scheduler, "kv_cache_config", None)
447 - if kv_cache_config is None:
448 - return []
449 -
450 - metadata: list[dict[str, int | str | None]] = []
451 - for group_idx, group in enumerate(kv_cache_config.kv_cache_groups):
452 - spec = group.kv_cache_spec
453 - metadata.append(
454 - {
455 - "group_idx": group_idx,
456 - "kind": get_kv_cache_spec_kind(spec).value,
457 - "block_size": spec.block_size,
458 - "sliding_window": getattr(spec, "sliding_window", None),
459 - }
460 - )
461 - return metadata
462 -
444 def add_request(self, request: Request, request_wave: int = 0):
445 """Add request to the scheduler.
446
vllm/v1/executor/ray_utils.py
-29
@@ -176,9 +176,6 @@ try:
176 output = output.get_output()
177 return output
178
179 - def override_env_vars(self, vars: dict[str, str]):
180 - os.environ.update(vars)
181 -
179 def _is_intermediate_tensors(self, output) -> bool:
180 return isinstance(output, IntermediateTensors)
181
@@ -663,29 +660,3 @@ def initialize_ray_cluster(
660 )
661 # Set the placement group in the parallel config
662 parallel_config.placement_group = current_placement_group
666 -
667 -
668 -def get_num_tpu_nodes() -> int:
669 - from ray._private.accelerators import TPUAcceleratorManager
670 -
671 - cluster_resources = ray.cluster_resources()
672 - total_tpus = int(cluster_resources["TPU"])
673 - tpus_per_node = TPUAcceleratorManager.get_current_node_num_accelerators()
674 - assert total_tpus % tpus_per_node == 0
675 - return total_tpus // tpus_per_node
676 -
677 -
678 -def get_num_nodes_in_placement_group() -> int:
679 - pg_table = ray.util.placement_group_table()
680 - current_pg = ray.util.get_current_placement_group()
681 - num_nodes = 0
682 -
683 - if current_pg:
684 - nodes_in_pg = set()
685 - for pg_key, pg in pg_table.items():
686 - if pg_key == current_pg.id.hex():
687 - for _, node in pg["bundles_to_node_id"].items():
688 - nodes_in_pg.add(node)
689 - num_nodes = len(nodes_in_pg)
690 -
691 - return num_nodes
vllm/v1/worker/cp_utils.py
-10
@@ -6,7 +6,6 @@ from typing import TYPE_CHECKING, Any, cast
6 import torch
7
8 from vllm.config import VllmConfig, get_layers_from_vllm_config
9 -from vllm.distributed import get_dcp_group
9 from vllm.logger import init_logger
10 from vllm.v1.attention.backend import CommonAttentionMetadata
11 from vllm.v1.attention.backends.utils import split_decodes_prefills_and_extends
@@ -52,15 +51,6 @@ def check_attention_cp_compatibility(vllm_config: VllmConfig) -> None:
51 )
52
53
55 -def get_kv_cache_shard_count() -> int:
56 - try:
57 - dcp_world_size = get_dcp_group().world_size
58 - except AssertionError:
59 - # DCP might not be initialized in testing
60 - dcp_world_size = 1
61 - return dcp_world_size
62 -
63 -
54 def get_dcp_dummy_context_len(
55 dcp_world_size: int,
56 cp_kv_cache_interleave_size: int,