Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
47570d7
first shot
Cyrilvallez Nov 25, 2025
69e5c9e
default to reversing
Cyrilvallez Nov 25, 2025
8fdc41f
oupso
Cyrilvallez Nov 25, 2025
9388354
oupsi 2
Cyrilvallez Nov 25, 2025
a9eb211
oupsi 3
Cyrilvallez Nov 25, 2025
ab05578
fix renamed kwargs
Cyrilvallez Nov 25, 2025
96880e0
fix timm_wrapper
Cyrilvallez Nov 25, 2025
a73101b
remove fix_state_dict methods
Cyrilvallez Nov 25, 2025
eaf9ef3
can do it all the time, with __init__ as well
Cyrilvallez Nov 25, 2025
aa71dc4
doc
Cyrilvallez Nov 25, 2025
04eeab9
oupsi
Cyrilvallez Nov 25, 2025
67d9288
fix
Cyrilvallez Nov 25, 2025
ac25074
create helper
Cyrilvallez Nov 25, 2025
9df2627
fix annotation annoying isue
Cyrilvallez Nov 26, 2025
2530a52
small fix
Cyrilvallez Nov 26, 2025
d996f7c
small fixes
Cyrilvallez Nov 26, 2025
9a80e7b
alright commit all that already
Cyrilvallez Nov 26, 2025
6b161e0
oupsi
Cyrilvallez Nov 26, 2025
d5cecba
the fix
Cyrilvallez Nov 26, 2025
17a8628
update quantizers
Cyrilvallez Nov 26, 2025
5b4e695
this works
Cyrilvallez Nov 26, 2025
a4f2cb5
the hardcoded regex got me hard....
Cyrilvallez Nov 26, 2025
faa2179
style
Cyrilvallez Nov 26, 2025
5c64084
the final one
Cyrilvallez Nov 26, 2025
30bae5f
cleanup a bit
Cyrilvallez Nov 26, 2025
58cc5b4
better
Cyrilvallez Nov 26, 2025
2ef7399
style
Cyrilvallez Nov 26, 2025
20e6927
oupsi readded it
Cyrilvallez Nov 26, 2025
b1a6621
do it inside the ops instead - no need for full names anymore
Cyrilvallez Nov 27, 2025
20bcbdd
reverse quantizers and simplify signatures
Cyrilvallez Nov 27, 2025
9e1bb41
small thingy
Cyrilvallez Nov 27, 2025
1a240e6
add no_grad decorator
Cyrilvallez Nov 27, 2025
8cda0fd
utils to rename keys
Cyrilvallez Nov 27, 2025
ca6445b
oupssii again
Cyrilvallez Nov 27, 2025
6362901
add test
Cyrilvallez Nov 27, 2025
59a97ad
simplify nicely
Cyrilvallez Nov 27, 2025
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
127 changes: 105 additions & 22 deletions src/transformers/conversion_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

from copy import deepcopy
from typing import TYPE_CHECKING

from .core_model_loading import Concatenate, MergeModulelist, WeightConverter, WeightRenaming
from .utils import is_torch_available
Expand All @@ -23,16 +26,21 @@
import torch


if TYPE_CHECKING:
from .modeling_utils import PreTrainedModel
from .quantizers import HfQuantizer


def _build_checkpoint_conversion_mapping():
mapping = {
"mixtral": [
WeightRenaming(".block_sparse_moe.gate", ".mlp.gate"),
WeightConverter(
source_keys=[
source_patterns=[
"block_sparse_moe.experts.*.w1.weight",
"block_sparse_moe.experts.*.w3.weight",
], # you give me a list of 2 keys, I collect a list of a list of tensors
target_keys="mlp.experts.gate_up_proj", # target key gets the list of two tensors
target_patterns="mlp.experts.gate_up_proj", # target key gets the list of two tensors
operations=[
MergeModulelist(
dim=0
Expand All @@ -41,10 +49,10 @@ def _build_checkpoint_conversion_mapping():
], # we want the loading to add this shard operation here. Though we can't shard after concats and merge, needs to be first
),
WeightConverter(
source_keys=[
source_patterns=[
"block_sparse_moe.experts.*.w2.weight",
],
target_keys="mlp.experts.down_proj", # target key gets the list of two tensors
target_patterns="mlp.experts.down_proj", # target key gets the list of two tensors
operations=[
MergeModulelist(
dim=0
Expand All @@ -54,50 +62,58 @@ def _build_checkpoint_conversion_mapping():
],
"qwen2_moe": [
WeightConverter(
source_keys=[
source_patterns=[
"mlp.experts.*.gate_proj.weight",
"mlp.experts.*.up_proj.weight",
],
target_keys="mlp.experts.gate_up_proj",
target_patterns="mlp.experts.gate_up_proj",
operations=[MergeModulelist(dim=0), Concatenate(dim=1)],
),
WeightConverter(
source_keys=["mlp.experts.*.down_proj.weight"],
target_keys="mlp.experts.down_proj",
source_patterns=["mlp.experts.*.down_proj.weight"],
target_patterns="mlp.experts.down_proj",
operations=[MergeModulelist(dim=0)],
),
],
"timm_wrapper": [
# Simply add the prefix `timm_model`
# TODO: Would be probably much cleaner with a `add_prefix` argument in WeightRenaming
WeightRenaming(
source_patterns=r"(.+)",
target_patterns=r"timm_model.\1",
)
],
"legacy": [
WeightRenaming(
source_keys="LayerNorm.gamma",
target_keys="LayerNorm.weight",
source_patterns="LayerNorm.gamma",
target_patterns="LayerNorm.weight",
),
WeightRenaming(
source_keys="LayerNorm.beta",
target_keys="LayerNorm.bias",
source_patterns="LayerNorm.beta",
target_patterns="LayerNorm.bias",
),
],
}
if hasattr(torch.nn.utils.parametrizations, "weight_norm"):
mapping["legacy"] += [
WeightRenaming(
source_keys="weight_g",
target_keys="parametrizations.weight.original0",
source_patterns="weight_g",
target_patterns="parametrizations.weight.original0",
),
WeightRenaming(
source_keys="weight_v",
target_keys="parametrizations.weight.original1",
source_patterns="weight_v",
target_patterns="parametrizations.weight.original1",
),
]
else:
mapping["legacy"] += [
WeightRenaming(
source_keys="parametrizations.weight.original0",
target_keys="weight_g",
source_patterns="parametrizations.weight.original0",
target_patterns="weight_g",
),
WeightRenaming(
source_keys="parametrizations.weight.original1",
target_keys="weight_v",
source_patterns="parametrizations.weight.original1",
target_patterns="weight_v",
),
]

Expand Down Expand Up @@ -127,5 +143,72 @@ def _build_checkpoint_conversion_mapping():
def get_checkpoint_conversion_mapping(model_type):
global _checkpoint_conversion_mapping_cache
_checkpoint_conversion_mapping_cache = _build_checkpoint_conversion_mapping()
globals()["_checkpoint_conversion_mapping"] = _checkpoint_conversion_mapping_cache
return deepcopy(_checkpoint_conversion_mapping_cache.get(model_type, None))
return deepcopy(_checkpoint_conversion_mapping_cache.get(model_type))


# DO NOT MODIFY, KEPT FOR BC ONLY
VLMS = [
"aria",
"ayavision",
"colpali",
"emu3",
"fuyu",
"gotocr2",
"gemma3",
"internvl",
"llava", # all llava prefixed models fall under this check
"mistral3",
"mllama",
"paligemma",
"shieldgemma2",
"qwen2vl",
"qwen2_5_vl",
"videollava",
"vipllava",
"sam3_video",
"sam3",
"sam3_tracker",
"sam3_tracker_video",
]


def get_model_conversion_mapping(
model: PreTrainedModel,
key_mapping: dict[str, str] | None = None,
hf_quantizer: HfQuantizer | None = None,
add_legacy: bool = True,
) -> list[WeightConverter | WeightRenaming]:
"""
For a given `model`, obtain the weight conversion mapping if any are registered either as a simple renaming
`_checkpoint_conversion_mapping` class argument, or in the general WeightConverter mapping.
"""
weight_conversions = []

# Load models with key mapping
if key_mapping is not None:
weight_conversions = [WeightRenaming(source_patterns=k, target_patterns=v) for k, v in key_mapping.items()]
elif any(
allowed_name in class_name.__name__.lower()
for class_name in model.__class__.__mro__[:-1]
for allowed_name in VLMS
):
weight_conversions = [
WeightRenaming(source_patterns=k, target_patterns=v)
for k, v in model._checkpoint_conversion_mapping.items()
]

# TODO: should be checked recursively on submodels!!
model_type = getattr(model.config, "model_type", None)
Comment thread
ArthurZucker marked this conversation as resolved.
if model_type is not None:
model_specific_conversions = get_checkpoint_conversion_mapping(model_type)
if model_specific_conversions is not None:
weight_conversions.extend(model_specific_conversions)

if add_legacy:
weight_conversions.extend(get_checkpoint_conversion_mapping("legacy"))

# Add the ones from the quantizer as well if provided
if hf_quantizer is not None:
weight_conversions.extend(hf_quantizer.get_weight_conversions())

return weight_conversions
Loading