Use Transformers v5 WeightRenaming for Transformers modeling backend (#31545)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
@@ -7,6 +7,7 @@ from contextlib import contextmanager
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Literal, Protocol, overload
|
||||
|
||||
import regex as re
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from torch.nn.modules.module import register_module_module_registration_hook
|
||||
@@ -38,17 +39,17 @@ from vllm.utils.torch_utils import (
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
WeightsMapping = Mapping[str, str | None]
|
||||
"""If a key maps to a value of `None`, the corresponding weight is ignored."""
|
||||
|
||||
|
||||
@dataclass
|
||||
class WeightsMapper:
|
||||
"""Maps the name of each weight if they match the following patterns."""
|
||||
"""Maps the name of each weight if they match the following patterns.
|
||||
|
||||
orig_to_new_substr: WeightsMapping = field(default_factory=dict)
|
||||
orig_to_new_prefix: WeightsMapping = field(default_factory=dict)
|
||||
orig_to_new_suffix: WeightsMapping = field(default_factory=dict)
|
||||
If a key maps to a value of `None`, the corresponding weight is ignored."""
|
||||
|
||||
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)
|
||||
orig_to_new_suffix: Mapping[str, str | None] = field(default_factory=dict)
|
||||
|
||||
def __or__(self, other: "WeightsMapper") -> "WeightsMapper":
|
||||
"""Combine two `WeightsMapper`s by merging their mappings."""
|
||||
@@ -59,6 +60,13 @@ class WeightsMapper:
|
||||
)
|
||||
|
||||
def _map_name(self, key: str) -> str | None:
|
||||
for pattern, new_key in self.orig_to_new_regex.items():
|
||||
if pattern.search(key):
|
||||
if new_key is None:
|
||||
return None
|
||||
|
||||
key = pattern.sub(new_key, key)
|
||||
|
||||
for substr, new_key in self.orig_to_new_substr.items():
|
||||
if substr in key:
|
||||
if new_key is None:
|
||||
|
||||
Reference in New Issue
Block a user