Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions vllm/model_executor/models/transformers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,13 @@ def _create_hf_to_vllm_mapper(self):
- Any quantization config specific mappings
"""
self.hf_to_vllm_mapper = WeightsMapper()
orig_to_new_renamings = self.hf_to_vllm_mapper.orig_to_new_renamings
orig_to_new_regex = self.hf_to_vllm_mapper.orig_to_new_regex

for mapping in get_model_conversion_mapping(self.model):
# Handle weights which have been renamed in Transformers
if isinstance(mapping, WeightRenaming):
# Recompile using regex (Transformers used re)
compiled_sources = re.compile(
mapping.compiled_sources.pattern, mapping.compiled_sources.flags
)
target_pattern = mapping.target_patterns[0]
orig_to_new_regex[compiled_sources] = target_pattern
orig_to_new_renamings.append(mapping)
# TODO: Handle WeightConverter to enable layer merging

# Handle unexpected weights which should be ignored
Expand Down
8 changes: 8 additions & 0 deletions vllm/model_executor/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class WeightsMapper:

If a key maps to a value of `None`, the corresponding weight is ignored."""

orig_to_new_renamings: list[Any] = field(default_factory=list)
orig_to_new_regex: Mapping[re.Pattern, str | None] = field(default_factory=dict)
orig_to_new_substr: Mapping[str, str | None] = field(default_factory=dict)
orig_to_new_prefix: Mapping[str, str | None] = field(default_factory=dict)
Expand All @@ -52,13 +53,20 @@ class WeightsMapper:
def __or__(self, other: "WeightsMapper") -> "WeightsMapper":
"""Combine two `WeightsMapper`s by merging their mappings."""
return WeightsMapper(
orig_to_new_renamings=[
*self.orig_to_new_renamings,
*other.orig_to_new_renamings,
],
orig_to_new_regex={**self.orig_to_new_regex, **other.orig_to_new_regex},
orig_to_new_substr={**self.orig_to_new_substr, **other.orig_to_new_substr},
orig_to_new_prefix={**self.orig_to_new_prefix, **other.orig_to_new_prefix},
orig_to_new_suffix={**self.orig_to_new_suffix, **other.orig_to_new_suffix},
)

def _map_name(self, key: str) -> str | None:
for renaming in self.orig_to_new_renamings:
key, _ = renaming.rename_source_key(key)

for pattern, new_key in self.orig_to_new_regex.items():
if pattern.search(key):
if new_key is None:
Expand Down
Loading