A symbol scan over every tracked file found 123 top-level definitions in vllm/ with zero references, including references inside strings. All of them came from features this fork deleted earlier. - Delete dead attention helpers: MLADCPManager, split_prefill_chunks, the reshape helpers, compute_varlen_chunk_metadata, the LINEAR backend's unused BailingLinearAttentionBackend, and the sparse flash-attn entry points. - Delete the CPU and AMX op wrappers from _custom_ops.py: cpu_mla_decode, cpu_mla_extend, cpu_mla_get_scheduler_metadata, gather_mla_context_cache_cpu. - Delete dead quantization helpers: awq_pack, gptq_pack, gptq_quantize_weights, get_and_maybe_dequant_weights, marlin_quant_fp8_torch, the rand_marlin_weight_*_like pair, ocp_mx_weight_dtype_and_rows, get_fp8_block_weight_scale, input_to_float8. - Delete dead model helpers: resolve_visual_encoder_outputs, run_dp_sharded_vision_model, fast_topk, get_layer_index, is_pp_missing_parameter, skip_spec_layers, KimiK3MergedQKVGateLinear, selective_scan_fn, causal_conv1d_update_torch. - Delete dead utils: format_kib, format_mib, chunk_list, prev_power_of_2, split_host_port, join_host_port, make_zmq_path, make_async_with_semaphore, is_meta_module, is_non_overlapping_and_dense, supports_xpu_graph, create_kv_caches_with_random*, and the unused has_* capability probes. - Delete the unreachable deep-gemm mega-MoE warmup path, which imported vllm.models.deepseek_v4.nvidia.model and vllm.models.kimi_k3.nvidia.model, neither of which exists anymore. - Drop the imports and the two now-empty guards left behind. Verified statically: every changed file compiles, no unused import remains, and no deleted name is referenced in vllm, tests or csrc. The runtime gates still need the Python environment rebuilt. Co-authored-by: Hinata <hinata@idihore.id> Signed-off-by: MOVZX <movzx@yahoo.com>
152 lines
5.4 KiB
Python
152 lines
5.4 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
|
"""Utils for model executor."""
|
|
|
|
import copy
|
|
from contextlib import contextmanager
|
|
from contextvars import ContextVar
|
|
from typing import Any
|
|
|
|
import torch
|
|
|
|
from vllm.utils.torch_utils import is_torch_equal_or_newer
|
|
|
|
_weights_pre_processed: ContextVar[bool] = ContextVar(
|
|
"weights_pre_processed", default=False
|
|
)
|
|
|
|
|
|
@contextmanager
|
|
def weights_already_processed():
|
|
"""Mark that weights are already in post-processed (runtime) format, so
|
|
``process_weights_after_loading`` skips tensor transforms (used by the
|
|
weight cache IPC loader).
|
|
"""
|
|
token = _weights_pre_processed.set(True)
|
|
try:
|
|
yield
|
|
finally:
|
|
_weights_pre_processed.reset(token)
|
|
|
|
|
|
def is_weights_pre_processed() -> bool:
|
|
return _weights_pre_processed.get()
|
|
|
|
|
|
def set_weight_attrs(
|
|
weight: torch.Tensor,
|
|
weight_attrs: dict[str, Any] | None,
|
|
):
|
|
"""Set attributes on a weight tensor.
|
|
|
|
This method is used to set attributes on a weight tensor. This method
|
|
will not overwrite existing attributes.
|
|
|
|
Args:
|
|
weight: The weight tensor.
|
|
weight_attrs: A dictionary of attributes to set on the weight tensor.
|
|
|
|
"""
|
|
if weight_attrs is None:
|
|
return
|
|
for key, value in weight_attrs.items():
|
|
assert not hasattr(weight, key), f"Overwriting existing tensor attribute: {key}"
|
|
|
|
# NOTE(woosuk): During weight loading, we often do something like:
|
|
# narrowed_tensor = param.data.narrow(0, offset, len)
|
|
# narrowed_tensor.copy_(real_weight)
|
|
# expecting narrowed_tensor and param.data to share the same storage.
|
|
# However, on TPUs, narrowed_tensor will lazily propagate to the base
|
|
# tensor, which is param.data, leading to the redundant memory usage.
|
|
# This sometimes causes OOM errors during model loading. To avoid this,
|
|
# we sync the param tensor after its weight loader is called.
|
|
# TODO(woosuk): Remove this hack once we have a better solution.
|
|
from vllm.platforms import current_platform
|
|
|
|
if current_platform.use_sync_weight_loader() and key == "weight_loader":
|
|
value = current_platform.make_synced_weight_loader(value)
|
|
setattr(weight, key, value)
|
|
|
|
|
|
def replace_parameter(
|
|
layer: torch.nn.Module,
|
|
param_name: str,
|
|
new_tensor: torch.Tensor | None,
|
|
prefer_copy: bool = False,
|
|
):
|
|
"""Replace a parameter of a layer while maintaining the ability to reload the
|
|
weight. Called within implementations of the `process_weights_after_loading` method.
|
|
|
|
Custom attributes set on ``new_tensor`` (e.g. kernel dispatch flags such as
|
|
``is_shuffled``) are carried over to the replacement parameter, except
|
|
``weight_loader``, which is always taken from the existing parameter.
|
|
|
|
Attributes of the existing parameter are otherwise dropped when a new
|
|
parameter is registered, but kept when ``prefer_copy`` reuses it in place.
|
|
|
|
This function should not be called on weights which are tied/shared
|
|
|
|
Args:
|
|
layer: Layer containing parameter to replace
|
|
param_name: Name of parameter to replace
|
|
new_tensor: New data of the new parameter, or None to set the parameter
|
|
to None
|
|
prefer_copy: If True and the existing parameter is compatible with
|
|
``new_tensor`` (same shape, dtype, and device), copy ``new_tensor``
|
|
into the existing parameter in place rather than re-registering
|
|
a new parameter. This preserves the parameter's storage address
|
|
(``data_ptr``), which is required for captured CUDA graphs to
|
|
remain valid across weight updates (e.g. in RL training loops).
|
|
|
|
"""
|
|
# should not be used on a tied/shared param
|
|
|
|
# If new_tensor is None, set the parameter to None
|
|
if new_tensor is None:
|
|
setattr(layer, param_name, None)
|
|
return
|
|
|
|
old_param: torch.nn.Parameter | None = getattr(layer, param_name, None)
|
|
|
|
new_tensor_attrs = dict(new_tensor.__dict__)
|
|
|
|
# `weight_loader` is the only attribute not ported over from new_tensor,
|
|
# old_param.weight_loader only is supported. `_weight_loader` is
|
|
# `BasevLLMParameter`'s backing field for its `weight_loader` property, so
|
|
# a loader riding along under that name has to be dropped as well.
|
|
new_tensor_attrs.pop("weight_loader", None)
|
|
new_tensor_attrs.pop("_weight_loader", None)
|
|
|
|
if isinstance(new_tensor, torch.nn.Parameter):
|
|
new_tensor = new_tensor.data
|
|
|
|
if (
|
|
prefer_copy
|
|
and old_param is not None
|
|
and old_param.shape == new_tensor.shape
|
|
and old_param.dtype == new_tensor.dtype
|
|
and old_param.device == new_tensor.device
|
|
):
|
|
old_param.copy_(new_tensor)
|
|
for attr_name, attr in new_tensor_attrs.items():
|
|
setattr(old_param, attr_name, attr)
|
|
return
|
|
|
|
new_param = torch.nn.Parameter(new_tensor, requires_grad=False)
|
|
|
|
for attr_name, attr in new_tensor_attrs.items():
|
|
setattr(new_param, attr_name, attr)
|
|
|
|
if old_param is not None and hasattr(old_param, "weight_loader"):
|
|
weight_loader = old_param.weight_loader
|
|
set_weight_attrs(new_param, {"weight_loader": weight_loader})
|
|
|
|
setattr(layer, param_name, new_param)
|
|
|
|
|
|
def maybe_disable_graph_partition(current_backend: str) -> dict[str, bool]:
|
|
if current_backend == "inductor" and is_torch_equal_or_newer("2.9.0.dev"):
|
|
return {"graph_partition": False}
|
|
else:
|
|
return {}
|