|
36 | 36 |
|
37 | 37 |
|
38 | 38 | _MODEL_TO_CONVERSION_PATTERN = { |
| 39 | + # ViT-style vision models (old HuggingFace checkpoint format → new modular format) |
| 40 | + "audio-spectrogram-transformer": "vit", |
| 41 | + "deit": "vit", |
| 42 | + "ijepa": "vit", |
| 43 | + "vit_mae": "vit", |
| 44 | + "vit_msn": "vit", |
| 45 | + "vivit": "vit", |
39 | 46 | # Mixtral-style MoE |
40 | 47 | "minimax": "mixtral", |
41 | 48 | "minimax_m2": "mixtral", |
|
95 | 102 |
|
96 | 103 | def _build_checkpoint_conversion_mapping(): |
97 | 104 | mapping = { |
| 105 | + "vit": [ |
| 106 | + WeightRenaming(r"encoder\.layer\.", "layers."), |
| 107 | + WeightRenaming("attention.query", "q_proj"), |
| 108 | + WeightRenaming("attention.key", "k_proj"), |
| 109 | + WeightRenaming("attention.value", "v_proj"), |
| 110 | + WeightRenaming("attention.output.dense", "attention.o_proj"), |
| 111 | + WeightRenaming("intermediate.dense", "mlp.fc1"), |
| 112 | + WeightRenaming("output.dense", "mlp.fc2"), |
| 113 | + ], |
| 114 | + "lw_detr": [ |
| 115 | + WeightRenaming("attention.attention.query", "attention.q_proj"), |
| 116 | + WeightRenaming("attention.attention.key", "attention.k_proj"), |
| 117 | + WeightRenaming("attention.attention.value", "attention.v_proj"), |
| 118 | + WeightRenaming("attention.output", "attention.o_proj"), |
| 119 | + ], |
| 120 | + "segformer": [ |
| 121 | + # Structural: legacy `encoder.*` lists → SegformerModel.stages (no `encoder` submodule on SegformerModel) |
| 122 | + WeightRenaming(r"encoder.patch_embeddings.(\d+).", r"stages.\1.patch_embeddings."), |
| 123 | + WeightRenaming(r"encoder.block.(\d+).", r"stages.\1.blocks."), |
| 124 | + WeightRenaming(r"encoder.layer_norm.(\d+)", r"stages.\1.layer_norm"), |
| 125 | + # Attention projection renames |
| 126 | + WeightRenaming("attention.self.query", "attention.q_proj"), |
| 127 | + WeightRenaming("attention.self.key", "attention.k_proj"), |
| 128 | + WeightRenaming("attention.self.value", "attention.v_proj"), |
| 129 | + WeightRenaming("attention.self.sr", "attention.sequence_reduction.sequence_reduction"), |
| 130 | + WeightRenaming("attention.self.layer_norm", "attention.sequence_reduction.layer_norm"), |
| 131 | + WeightRenaming("attention.output.dense", "attention.o_proj"), |
| 132 | + # MLP renames |
| 133 | + WeightRenaming("mlp.dense1", "mlp.fc1"), |
| 134 | + WeightRenaming("mlp.dense2", "mlp.fc2"), |
| 135 | + # LayerNorm renames |
| 136 | + WeightRenaming("layer_norm_1", "layernorm_before"), |
| 137 | + WeightRenaming("layer_norm_2", "layernorm_after"), |
| 138 | + # Decode head: legacy checkpoints name the MLP list `linear_c` |
| 139 | + WeightRenaming("decode_head.linear_c", "decode_head.linear_projections"), |
| 140 | + ], |
| 141 | + "swin": [ |
| 142 | + # Attention projection renames (SwinSelfAttention.{query,key,value} → SwinAttention.{q,k,v}_proj) |
| 143 | + WeightRenaming("attention.self.query", "attention.q_proj"), |
| 144 | + WeightRenaming("attention.self.key", "attention.k_proj"), |
| 145 | + WeightRenaming("attention.self.value", "attention.v_proj"), |
| 146 | + # Relative position bias: SwinSelfAttention → dedicated SwinRelativePositionBias submodule |
| 147 | + WeightRenaming( |
| 148 | + "attention.self.relative_position_bias_table", |
| 149 | + "attention.relative_position_bias.relative_position_bias_table", |
| 150 | + ), |
| 151 | + # Output projection rename (SwinSelfOutput.dense → SwinAttention.o_proj) |
| 152 | + WeightRenaming("attention.output.dense", "attention.o_proj"), |
| 153 | + # MLP renames (SwinIntermediate.dense → SwinMLP.fc1, SwinOutput.dense → SwinMLP.fc2) |
| 154 | + WeightRenaming("intermediate.dense", "mlp.fc1"), |
| 155 | + WeightRenaming("output.dense", "mlp.fc2"), |
| 156 | + ], |
98 | 157 | "altclip": [ |
99 | 158 | WeightRenaming(source_patterns=r"layer\.", target_patterns="layers."), |
100 | 159 | ], |
@@ -344,6 +403,17 @@ def _build_checkpoint_conversion_mapping(): |
344 | 403 | operations=[ErnieFuseAndSplitTextVisionExperts(stack_dim=0, concat_dim=1)], |
345 | 404 | ), |
346 | 405 | ], |
| 406 | + # MaskFormer embeds both a Swin backbone (model_type="swin") and a DETR decoder |
| 407 | + # (model_type="detr") as submodules. Both get their mappings collected automatically, but |
| 408 | + # the Swin reverse mapping "mlp.fc1 → intermediate.dense" would corrupt DETR decoder keys |
| 409 | + # if it runs before the DETR reverse "layers.N.mlp.fc1 → layers.N.fc1". |
| 410 | + # By adding a "maskformer"-level mapping with the DETR fc1 rename, it is collected first |
| 411 | + # (model-level before submodule-level), so its reverse runs first and removes "mlp.fc1" |
| 412 | + # from DETR decoder paths before the Swin reverse can match them. |
| 413 | + "maskformer": [ |
| 414 | + WeightRenaming(r"layers.(\d+).fc1", r"layers.\1.mlp.fc1"), |
| 415 | + WeightRenaming(r"layers.(\d+).fc2", r"layers.\1.mlp.fc2"), |
| 416 | + ], |
347 | 417 | "detr": [ |
348 | 418 | WeightRenaming("backbone.conv_encoder", "backbone"), |
349 | 419 | WeightRenaming("out_proj", "o_proj"), |
@@ -585,6 +655,40 @@ def _build_checkpoint_conversion_mapping(): |
585 | 655 | mapping["ernie4_5_moe"] += [ |
586 | 656 | WeightRenaming("mlp.moe_statics.e_score_correction_bias", "mlp.gate.moe_statics.e_score_correction_bias") |
587 | 657 | ] |
| 658 | + mapping["beit"] = [ |
| 659 | + WeightRenaming("attention.query", "q_proj"), |
| 660 | + WeightRenaming("attention.key", "k_proj"), |
| 661 | + WeightRenaming("attention.value", "v_proj"), |
| 662 | + WeightRenaming("attention.output.dense", "attention.o_proj"), |
| 663 | + WeightRenaming("intermediate.dense", "mlp.fc1"), |
| 664 | + WeightRenaming("output.dense", "mlp.fc2"), |
| 665 | + WeightRenaming("encoder.relative_position_bias", "shared_position_bias"), |
| 666 | + WeightRenaming(r"attention.attention.relative_position_bias\.", r"relative_position_bias."), |
| 667 | + WeightRenaming("decode_head.bottleneck.", "decode_head.psp_bottleneck."), |
| 668 | + WeightRenaming(r"(?<!psp_modules\.[0-9]\.1\.)bn\.", "normalization."), |
| 669 | + WeightRenaming(r"(?<!psp_modules\.[0-9]\.1\.)conv\.weight", "convolution.weight"), |
| 670 | + WeightRenaming( |
| 671 | + r"decode_head\.psp_modules\.(\d+)\.1\.conv\.weight", |
| 672 | + r"decode_head.psp_modules.blocks.\1.conv.convolution.weight", |
| 673 | + ), |
| 674 | + WeightRenaming( |
| 675 | + r"decode_head\.psp_modules\.(\d+)\.1\.bn\.", |
| 676 | + r"decode_head.psp_modules.blocks.\1.conv.normalization.", |
| 677 | + ), |
| 678 | + WeightRenaming(r"^fpn1\.0\.", "fpn.fpn1.conv_transpose1."), |
| 679 | + WeightRenaming(r"^fpn1\.1\.", "fpn.fpn1.normalization."), |
| 680 | + WeightRenaming(r"^fpn1\.3\.", "fpn.fpn1.conv_transpose2."), |
| 681 | + WeightRenaming(r"^fpn2\.0\.", "fpn.fpn2."), |
| 682 | + *mapping["vit"].copy(), |
| 683 | + WeightRenaming(r"^encoder\.", "beit."), |
| 684 | + ] |
| 685 | + |
| 686 | + mapping["pixio"] = mapping["vit"].copy() |
| 687 | + mapping["pixio"] += [ |
| 688 | + WeightRenaming("norm1", "layernorm_before"), |
| 689 | + WeightRenaming("norm2", "layernorm_after"), |
| 690 | + WeightRenaming(r"^encoder\.", "pixio."), |
| 691 | + ] |
588 | 692 |
|
589 | 693 | mapping["minimax_m2"] = mapping["mixtral"].copy() |
590 | 694 | mapping["minimax_m2"] += [ |
|
0 commit comments