mirror of
https://github.com/wassname/vllm.git
synced 2026-08-14 12:50:37 +08:00
[Misc] Merge bitsandbytes_stacked_params_mapping and packed_modules_mapping (#11924)
Signed-off-by: Jee Jee Li <pandaleefree@gmail.com>
This commit is contained in:
@@ -39,7 +39,8 @@ from vllm.model_executor.layers.quantization.base_config import (
|
||||
from vllm.model_executor.model_loader.tensorizer import (
|
||||
TensorizerConfig, is_vllm_tensorized, load_with_tensorizer,
|
||||
serialize_vllm_model, tensorizer_weights_iterator)
|
||||
from vllm.model_executor.model_loader.utils import (get_model_architecture,
|
||||
from vllm.model_executor.model_loader.utils import (ParamMapping,
|
||||
get_model_architecture,
|
||||
set_default_torch_dtype)
|
||||
from vllm.model_executor.model_loader.weight_utils import (
|
||||
download_safetensors_index_file_from_hf, download_weights_from_hf,
|
||||
@@ -983,21 +984,11 @@ class BitsAndBytesModelLoader(BaseModelLoader):
|
||||
|
||||
def _get_bnb_target_modules(self, model: nn.Module) -> None:
|
||||
|
||||
# TODO: Maybe we can replace bitsandbytes_stacked_params_mapping with
|
||||
# packed_modules_mapping.
|
||||
inverse_stacked_mapping: Dict[str, List[str]] = {}
|
||||
for orig, (
|
||||
packed,
|
||||
idx,
|
||||
) in model.bitsandbytes_stacked_params_mapping.items():
|
||||
if packed not in inverse_stacked_mapping:
|
||||
inverse_stacked_mapping[packed] = []
|
||||
inverse_stacked_mapping[packed].insert(idx, orig)
|
||||
|
||||
for name, module in model.named_modules():
|
||||
if isinstance(module, (LinearBase, )):
|
||||
last_name = name.split(".")[-1]
|
||||
if sub_modules := inverse_stacked_mapping.get(last_name, []):
|
||||
if sub_modules := self.modules_mapping.packed_mapping.get(
|
||||
last_name, []):
|
||||
# Map vllm's names to transformers's names.
|
||||
for sub_name in sub_modules:
|
||||
self.target_modules.append(
|
||||
@@ -1018,15 +1009,19 @@ class BitsAndBytesModelLoader(BaseModelLoader):
|
||||
"The required method 'load_weights' is not defined in class"
|
||||
f" {type(model).__name__}.")
|
||||
|
||||
if not hasattr(model, "bitsandbytes_stacked_params_mapping"):
|
||||
if not hasattr(model, "packed_modules_mapping"):
|
||||
raise AttributeError(
|
||||
f"Model {type(model).__name__} does not support BitsAndBytes "
|
||||
"quantization yet.")
|
||||
"quantization yet. No 'packed_modules_mapping' found.")
|
||||
|
||||
self.modules_mapping = ParamMapping(
|
||||
copy.deepcopy(model.packed_modules_mapping))
|
||||
|
||||
# For some models like Molmo, we need to use hf_to_vllm_mapper
|
||||
# to ensure correct loading of weights.
|
||||
if hf_to_vllm_mapper := getattr(model, "hf_to_vllm_mapper", None):
|
||||
self.weight_mapper = lambda name: hf_to_vllm_mapper._map_name(name)
|
||||
|
||||
# Modules whose weights might have fused on disk
|
||||
# we need their output_sizes to make shard in flight correctly with TP
|
||||
self.maybe_fused_weights_modules: Dict[str, List[int]] = {}
|
||||
@@ -1109,7 +1104,7 @@ class BitsAndBytesModelLoader(BaseModelLoader):
|
||||
for shard_name, (
|
||||
weight_name,
|
||||
index,
|
||||
) in model.bitsandbytes_stacked_params_mapping.items():
|
||||
) in self.modules_mapping.inverse_packed_mapping.items():
|
||||
shard_pos = quant_param_name.find(shard_name)
|
||||
# Some models, such as MiniCPM V2.5/2.6, contain both
|
||||
# module names 'kv_proj' and 'qkv_proj'. To prevent 'kv_proj'
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Utilities for selecting and loading models."""
|
||||
import contextlib
|
||||
from typing import Tuple, Type
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, List, Tuple, Type
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
@@ -49,3 +50,26 @@ def get_model_architecture(
|
||||
|
||||
def get_architecture_class_name(model_config: ModelConfig) -> str:
|
||||
return get_model_architecture(model_config)[1]
|
||||
|
||||
|
||||
@dataclass
|
||||
class ParamMapping:
|
||||
"""
|
||||
A class to handle parameter mapping for model weight loading.
|
||||
It creates a bidirectional mapping between packed parameters and their
|
||||
constituent parts.
|
||||
"""
|
||||
packed_mapping: Dict[str, List[str]]
|
||||
inverse_packed_mapping: Dict[str, Tuple[str,
|
||||
int]] = field(default_factory=dict)
|
||||
|
||||
def __post_init__(self):
|
||||
for packed_name, sub_params in self.packed_mapping.items():
|
||||
# Skip self-contained cases (e.g., {"W_pack": ["W_pack"]})
|
||||
if len(sub_params) == 1 and sub_params[0] == packed_name:
|
||||
continue
|
||||
for index, param_name in enumerate(sub_params):
|
||||
self.inverse_packed_mapping[param_name] = (
|
||||
packed_name,
|
||||
index,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user