GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — stable-diffusion.cpp/src/name_conversion.cpp at master · noctrex/stable-diffusion.cpp Expand file tree/ Copy pathname_conversion.cppMore file actions 1907 lines (1698 loc) · 87 KB / Copy pathname_conversion.cppFile metadata and controls1907 lines (1698 loc) · 87 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 #include <map> #include <unordered_map> #include <unordered_set> #include "core/util.h" #include "name_conversion.h" std::string convert_relayvsr_tensor_name(std::string name) { if (!starts_with(name, "wan.")) return name; const std::vector<std::pair<std::string, std::string>> mappings = { {"positive_conditioning", "prompt_embeds"}, {"lq_proj_in.linear.", "lq_proj.out_proj."}, {"lq_proj_in.", "lq_proj."}, {"text_embedding.0.", "condition_embedder.text_embedder.linear_1."}, {"text_embedding.2.", "condition_embedder.text_embedder.linear_2."}, {"time_embedding.0.", "condition_embedder.time_embedder.linear_1."}, {"time_embedding.2.", "condition_embedder.time_embedder.linear_2."}, {"time_projection.1.", "condition_embedder.time_proj."}, {"head.head.", "proj_out."}, {"head.modulation", "scale_shift_table"}, {".norm3.", ".norm2."}, {".modulation", ".scale_shift_table"}, {".ffn.0.", ".ffn.net.0.proj."}, {".ffn.2.", ".ffn.net.2."}, {".self_attn.", ".attn1."}, {".cross_attn.", ".attn2."}, {".q.", ".to_q."}, {".k.", ".to_k."}, {".v.", ".to_v."}, {".o.", ".to_out.0."}, }; for (const auto& mapping : mappings) { auto pos = name.find(mapping.first); if (pos != std::string::npos) name.replace(pos, mapping.first.size(), mapping.second); } return name; } void replace_with_name_map(std::string& name, const std::vector<std::pair<std::string, std::string>>& name_map) { for (auto kv : name_map) { size_t pos = name.find(kv.first); if (pos != std::string::npos) { name.replace(pos, kv.first.size(), kv.second); } } } void replace_with_prefix_map(std::string& name, const std::vector<std::pair<std::string, std::string>>& prefix_map) { for (const auto& [old_prefix, new_prefix] : prefix_map) { if (starts_with(name, old_prefix)) { name = new_prefix + name.substr(old_prefix.size()); break; } } } void replace_with_prefix_map(std::string& name, const std::unordered_map<std::string, std::string>& prefix_map) { for (const auto& [old_prefix, new_prefix] : prefix_map) { if (starts_with(name, old_prefix)) { name = new_prefix + name.substr(old_prefix.size()); break; } } } std::string convert_open_clip_to_hf_clip_name(std::string name) { static std::unordered_map<std::string, std::string> open_clip_to_hf_clip_model = { {"model.ln_final.bias", "transformer.text_model.final_layer_norm.bias"}, {"model.ln_final.weight", "transformer.text_model.final_layer_norm.weight"}, {"model.positional_embedding", "transformer.text_model.embeddings.position_embedding.weight"}, {"model.token_embedding.weight", "transformer.text_model.embeddings.token_embedding.weight"}, {"model.text_projection", "transformer.text_model.text_projection"}, {"model.visual.class_embedding", "transformer.vision_model.embeddings.class_embedding"}, {"model.visual.conv1.weight", "transformer.vision_model.embeddings.patch_embedding.weight"}, {"model.visual.ln_post.bias", "transformer.vision_model.post_layernorm.bias"}, {"model.visual.ln_post.weight", "transformer.vision_model.post_layernorm.weight"}, {"model.visual.ln_pre.bias", "transformer.vision_model.pre_layernorm.bias"}, {"model.visual.ln_pre.weight", "transformer.vision_model.pre_layernorm.weight"}, {"model.visual.positional_embedding", "transformer.vision_model.embeddings.position_embedding.weight"}, {"model.visual.proj", "transformer.visual_projection.weight"}, }; static std::unordered_map<std::string, std::string> open_clip_to_hf_clip_resblock = { {"attn.in_proj_bias", "self_attn.in_proj.bias"}, {"attn.in_proj_weight", "self_attn.in_proj.weight"}, {"attn.out_proj.bias", "self_attn.out_proj.bias"}, {"attn.out_proj.weight", "self_attn.out_proj.weight"}, {"ln_1.bias", "layer_norm1.bias"}, {"ln_1.weight", "layer_norm1.weight"}, {"ln_2.bias", "layer_norm2.bias"}, {"ln_2.weight", "layer_norm2.weight"}, {"mlp.c_fc.bias", "mlp.fc1.bias"}, {"mlp.c_fc.weight", "mlp.fc1.weight"}, {"mlp.c_proj.bias", "mlp.fc2.bias"}, {"mlp.c_proj.weight", "mlp.fc2.weight"}, }; static std::unordered_map<std::string, std::string> cond_model_name_map = { {"transformer.vision_model.pre_layrnorm.weight", "transformer.vision_model.pre_layernorm.weight"}, {"transformer.vision_model.pre_layrnorm.bias", "transformer.vision_model.pre_layernorm.bias"}, }; if (open_clip_to_hf_clip_model.find(name) != open_clip_to_hf_clip_model.end()) { name = open_clip_to_hf_clip_model[name]; } if (cond_model_name_map.find(name) != cond_model_name_map.end()) { name = cond_model_name_map[name]; } std::string open_clip_resblock_prefix = "model.transformer.resblocks."; std::string hf_clip_resblock_prefix = "transformer.text_model.encoder.layers."; auto replace_suffix = [&]() { if (name.find(open_clip_resblock_prefix) == 0) { std::string remain = name.substr(open_clip_resblock_prefix.length()); std::string idx = remain.substr(0, remain.find(".")); std::string suffix = remain.substr(idx.length() + 1); if (open_clip_to_hf_clip_resblock.find(suffix) != open_clip_to_hf_clip_resblock.end()) { std::string new_suffix = open_clip_to_hf_clip_resblock[suffix]; name = hf_clip_resblock_prefix + idx + "." + new_suffix; } } }; replace_suffix(); open_clip_resblock_prefix = "model.visual.transformer.resblocks."; hf_clip_resblock_prefix = "transformer.vision_model.encoder.layers."; replace_suffix(); return name; } std::string convert_llada2_moe_te_name(std::string name); static std::string convert_ming_image_te_name(std::string name) { if (name == "llm.query_tokens") { name = "llm.query_tokens_dict.16x16"; } static const std::vector<std::pair<std::string, std::string>> name_map = { {"thinker.", "backbone."}, {"attention.", "self_attn."}, {"self_attn.dense.", "self_attn.o_proj."}, {"gate.proj.", "gate."}, }; replace_with_name_map(name, name_map); return name; } std::string convert_cond_stage_model_name(std::string name, std::string prefix, SDVersion version) { if (version == VERSION_MING_IMAGE && prefix == "text_encoders." && starts_with(name, "llm.")) { name = convert_ming_image_te_name(name); } static const std::vector<std::pair<std::string, std::string>> clip_name_map{ {"transformer.text_projection.weight", "transformer.text_model.text_projection"}, {"model.text_projection.weight", "transformer.text_model.text_projection"}, {"vision_model.visual_projection.weight", "visual_projection.weight"}, }; // llama.cpp to original static const std::vector<std::pair<std::string, std::string>> t5_name_map{ {"enc.", "encoder."}, {"blk.", "block."}, {"output_norm.", "final_layer_norm."}, {"attn_q.", "layer.0.SelfAttention.q."}, {"attn_k.", "layer.0.SelfAttention.k."}, {"attn_v.", "layer.0.SelfAttention.v."}, {"attn_o.", "layer.0.SelfAttention.o."}, {"attn_norm.", "layer.0.layer_norm."}, {"ffn_norm.", "layer.1.layer_norm."}, {"ffn_up.", "layer.1.DenseReluDense.wi_1."}, {"ffn_down.", "layer.1.DenseReluDense.wo."}, {"ffn_gate.", "layer.1.DenseReluDense.wi_0."}, {"attn_rel_b.", "layer.0.SelfAttention.relative_attention_bias."}, {"token_embd.", "shared."}, }; static const std::vector<std::pair<std::string, std::string>> llm_name_map{ {"attn_sinks.weight", "self_attn.sinks"}, {"token_embd.", "model.embed_tokens."}, {"blk.", "model.layers."}, {"attn_q.", "self_attn.q_proj."}, {"attn_k.", "self_attn.k_proj."}, {"attn_v.", "self_attn.v_proj."}, {"attn_q_norm.", "self_attn.q_norm."}, {"attn_k_norm.", "self_attn.k_norm."}, {"attn_output.", "self_attn.o_proj."}, {"attn_norm.", "input_layernorm."}, {"attn_post_norm.", "post_attention_norm."}, {"ffn_gate_inp.", "mlp.router."}, {"ffn_gate_exps.", "mlp.experts.gate_proj."}, {"ffn_up_exps.", "mlp.experts.up_proj."}, {"ffn_down_exps.", "mlp.experts.down_proj."}, {"ffn_down.", "mlp.down_proj."}, {"ffn_gate.", "mlp.gate_proj."}, {"ffn_up.", "mlp.up_proj."}, {"ffn_post_norm.", "post_ffw_norm."}, {"ffn_norm.", "post_attention_layernorm."}, {"layer_output_scale.weight", "layer_scalar"}, {"output_norm.", "model.norm."}, }; static const std::vector<std::pair<std::string, std::string>> llm_vision_name_map{ {"mm.", "merger.mlp."}, {"v.post_ln.", "merger.ln_q."}, {"v.patch_embd.weight", "patch_embed.proj.0.weight"}, {"patch_embed.proj.0.weight.1", "patch_embed.proj.1.weight"}, {"v.patch_embd.weight.1", "patch_embed.proj.1.weight"}, {"v.blk.", "blocks."}, {"attn_q.", "attn.q_proj."}, {"attn_k.", "attn.k_proj."}, {"attn_v.", "attn.v_proj."}, {"attn_out.", "attn.proj."}, {"ffn_down.", "mlp.down_proj."}, {"ffn_gate.", "mlp.gate_proj."}, {"ffn_up.", "mlp.up_proj."}, {"ln1.", "norm1."}, {"ln2.", "norm2."}, }; if (contains(name, "t5xxl")) { replace_with_name_map(name, t5_name_map); } else if (contains(name, "llm")) { if (contains(name, "llm.visual")) { replace_with_name_map(name, llm_vision_name_map); } else { replace_with_name_map(name, llm_name_map); name = convert_llada2_moe_te_name(name); const std::string language_prefix = "llm.model.language_model."; if (starts_with(name, language_prefix)) { name = "llm.model." + name.substr(language_prefix.size()); } } } else { name = convert_open_clip_to_hf_clip_name(name); replace_with_name_map(name, clip_name_map); } return name; } std::string convert_qwen3_vl_vision_name(std::string name) { static const std::vector<std::pair<std::string, std::string>> qwen3_vl_deepstack_name_map{ {"v.deepstack_merger_list.", "deepstack_merger_list."}, {"v.deepstack.5.", "deepstack_merger_list.0."}, {"v.deepstack.8.", "deepstack_merger_list.0."}, {"v.deepstack.11.", "deepstack_merger_list.1."}, {"v.deepstack.16.", "deepstack_merger_list.1."}, {"v.deepstack.17.", "deepstack_merger_list.2."}, {"v.deepstack.24.", "deepstack_merger_list.2."}, {"fc1.", "linear_fc1."}, {"fc2.", "linear_fc2."}, {"ffn_up.", "linear_fc1."}, {"ffn_down.", "linear_fc2."}, {"ffn_norm.", "norm."}, }; static const std::vector<std::pair<std::string, std::string>> qwen3_vl_vision_name_map{ {"mm.0.", "merger.linear_fc1."}, {"mm.2.", "merger.linear_fc2."}, {"v.post_ln.", "merger.norm."}, {"v.position_embd.weight", "pos_embed.weight"}, {"v.patch_embd.weight.1", "patch_embed.proj.1.weight"}, {"v.patch_embd.weight", "patch_embed.proj.0.weight"}, {"v.patch_embd.bias", "patch_embed.bias"}, {"v.blk.", "blocks."}, {"attn_qkv.", "attn.qkv."}, {"attn_out.", "attn.proj."}, {"ffn_up.", "mlp.linear_fc1."}, {"ffn_down.", "mlp.linear_fc2."}, {"ln1.", "norm1."}, {"ln2.", "norm2."}, }; if (contains(name, "v.deepstack_merger_list.") || contains(name, "v.deepstack.")) { replace_with_name_map(name, qwen3_vl_deepstack_name_map); return name; } replace_with_name_map(name, qwen3_vl_vision_name_map); return name; } // ref: https://github.com/huggingface/diffusers/blob/main/scripts/convert_diffusers_to_original_stable_diffusion.py std::string convert_diffusers_unet_to_original_sd1(std::string name) { // (stable-diffusion, HF Diffusers) static const std::vector<std::pair<std::string, std::string>> unet_conversion_map = { {"time_embed.0.weight", "time_embedding.linear_1.weight"}, {"time_embed.0.bias", "time_embedding.linear_1.bias"}, {"time_embed.2.weight", "time_embedding.linear_2.weight"}, {"time_embed.2.bias", "time_embedding.linear_2.bias"}, {"input_blocks.0.0.weight", "conv_in.weight"}, {"input_blocks.0.0.bias", "conv_in.bias"}, {"out.0.weight", "conv_norm_out.weight"}, {"out.0.bias", "conv_norm_out.bias"}, {"out.2.weight", "conv_out.weight"}, {"out.2.bias", "conv_out.bias"}, }; static const std::vector<std::pair<std::string, std::string>> unet_conversion_map_resnet = { {"in_layers.0", "norm1"}, {"in_layers.2", "conv1"}, {"out_layers.0", "norm2"}, {"out_layers.3", "conv2"}, {"emb_layers.1", "time_emb_proj"}, {"skip_connection", "conv_shortcut"}, }; static std::vector<std::pair<std::string, std::string>> unet_conversion_map_layer; if (unet_conversion_map_layer.empty()) { for (int i = 0; i < 4; ++i) { // down_blocks for (int j = 0; j < 2; ++j) { std::string hf_down_res_prefix = "down_blocks." + std::to_string(i) + ".resnets." + std::to_string(j) + "."; std::string sd_down_res_prefix = "input_blocks." + std::to_string(3 * i + j + 1) + ".0."; unet_conversion_map_layer.emplace_back(sd_down_res_prefix, hf_down_res_prefix); if (i < 3) { std::string hf_down_atn_prefix = "down_blocks." + std::to_string(i) + ".attentions." + std::to_string(j) + "."; std::string sd_down_atn_prefix = "input_blocks." + std::to_string(3 * i + j + 1) + ".1."; unet_conversion_map_layer.emplace_back(sd_down_atn_prefix, hf_down_atn_prefix); } } // up_blocks for (int j = 0; j < 3; ++j) { std::string hf_up_res_prefix = "up_blocks." + std::to_string(i) + ".resnets." + std::to_string(j) + "."; std::string sd_up_res_prefix = "output_blocks." + std::to_string(3 * i + j) + ".0."; unet_conversion_map_layer.emplace_back(sd_up_res_prefix, hf_up_res_prefix); if (/*i > 0*/ true) { // for tiny unet std::string hf_up_atn_prefix = "up_blocks." + std::to_string(i) + ".attentions." + std::to_string(j) + "."; std::string sd_up_atn_prefix = "output_blocks." + std::to_string(3 * i + j) + ".1."; unet_conversion_map_layer.emplace_back(sd_up_atn_prefix, hf_up_atn_prefix); } } if (i < 3) { std::string hf_downsample_prefix = "down_blocks." + std::to_string(i) + ".downsamplers.0.conv."; std::string sd_downsample_prefix = "input_blocks." + std::to_string(3 * (i + 1)) + ".0.op."; unet_conversion_map_layer.emplace_back(sd_downsample_prefix, hf_downsample_prefix); std::string hf_upsample_prefix = "up_blocks." + std::to_string(i) + ".upsamplers.0."; std::string sd_upsample_prefix = "output_blocks." + std::to_string(3 * i + 2) + "." + std::to_string(i == 0 ? 1 : 2) + "."; unet_conversion_map_layer.emplace_back(sd_upsample_prefix, hf_upsample_prefix); } } // mid block unet_conversion_map_layer.emplace_back("middle_block.1.", "mid_block.attentions.0."); for (int j = 0; j < 2; ++j) { std::string hf_mid_res_prefix = "mid_block.resnets." + std::to_string(j) + "."; std::string sd_mid_res_prefix = "middle_block." + std::to_string(2 * j) + "."; unet_conversion_map_layer.emplace_back(sd_mid_res_prefix, hf_mid_res_prefix); } } std::string result = name; for (const auto& p : unet_conversion_map) { if (result == p.second) { result = p.first; return result; } } if (contains(result, "resnets")) { for (const auto& p : unet_conversion_map_resnet) { size_t pos = result.find(p.second); if (pos != std::string::npos) { result.replace(pos, p.second.size(), p.first); } } } for (const auto& p : unet_conversion_map_layer) { size_t pos = result.find(p.second); if (pos != std::string::npos) { result.replace(pos, p.second.size(), p.first); } } static const std::vector<std::pair<std::string, std::string>> name_map{ {"to_out.weight", "to_out.0.weight"}, {"to_out.bias", "to_out.0.bias"}, }; replace_with_name_map(result, name_map); return result; } // ref: https://github.com/huggingface/diffusers/blob/main/scripts/convert_diffusers_to_original_sdxl.py std::string convert_diffusers_unet_to_original_sdxl(std::string name) { // (stable-diffusion, HF Diffusers) static const std::vector<std::pair<std::string, std::string>> unet_conversion_map = { {"time_embed.0.weight", "time_embedding.linear_1.weight"}, {"time_embed.0.bias", "time_embedding.linear_1.bias"}, {"time_embed.2.weight", "time_embedding.linear_2.weight"}, {"time_embed.2.bias", "time_embedding.linear_2.bias"}, {"input_blocks.0.0.weight", "conv_in.weight"}, {"input_blocks.0.0.bias", "conv_in.bias"}, {"out.0.weight", "conv_norm_out.weight"}, {"out.0.bias", "conv_norm_out.bias"}, {"out.2.weight", "conv_out.weight"}, {"out.2.bias", "conv_out.bias"}, // --- SDXL add_embedding mappings --- {"label_emb.0.0.weight", "add_embedding.linear_1.weight"}, {"label_emb.0.0.bias", "add_embedding.linear_1.bias"}, {"label_emb.0.2.weight", "add_embedding.linear_2.weight"}, {"label_emb.0.2.bias", "add_embedding.linear_2.bias"}, }; static const std::vector<std::pair<std::string, std::string>> unet_conversion_map_resnet = { {"in_layers.0", "norm1"}, {"in_layers.2", "conv1"}, {"out_layers.0", "norm2"}, {"out_layers.3", "conv2"}, {"emb_layers.1", "time_emb_proj"}, {"skip_connection", "conv_shortcut"}, }; static std::vector<std::pair<std::string, std::string>> unet_conversion_map_layer; if (unet_conversion_map_layer.empty()) { for (int i = 0; i < 3; ++i) { // --- down_blocks --- for (int j = 0; j < 2; ++j) { std::string hf_down_res_prefix = "down_blocks." + std::to_string(i) + ".resnets." + std::to_string(j) + "."; std::string sd_down_res_prefix = "input_blocks." + std::to_string(3 * i + j + 1) + ".0."; unet_conversion_map_layer.emplace_back(sd_down_res_prefix, hf_down_res_prefix); if (i > 0) { std::string hf_down_atn_prefix = "down_blocks." + std::to_string(i) + ".attentions." + std::to_string(j) + "."; std::string sd_down_atn_prefix = "input_blocks." + std::to_string(3 * i + j + 1) + ".1."; unet_conversion_map_layer.emplace_back(sd_down_atn_prefix, hf_down_atn_prefix); } } // --- up_blocks --- for (int j = 0; j < 4; ++j) { std::string hf_up_res_prefix = "up_blocks." + std::to_string(i) + ".resnets." + std::to_string(j) + "."; std::string sd_up_res_prefix = "output_blocks." + std::to_string(3 * i + j) + ".0."; unet_conversion_map_layer.emplace_back(sd_up_res_prefix, hf_up_res_prefix); if (i < 2) { std::string hf_up_atn_prefix = "up_blocks." + std::to_string(i) + ".attentions." + std::to_string(j) + "."; std::string sd_up_atn_prefix = "output_blocks." + std::to_string(3 * i + j) + ".1."; unet_conversion_map_layer.emplace_back(sd_up_atn_prefix, hf_up_atn_prefix); } } if (i < 3) { std::string hf_downsample_prefix = "down_blocks." + std::to_string(i) + ".downsamplers.0.conv."; std::string sd_downsample_prefix = "input_blocks." + std::to_string(3 * (i + 1)) + ".0.op."; unet_conversion_map_layer.emplace_back(sd_downsample_prefix, hf_downsample_prefix); std::string hf_upsample_prefix = "up_blocks." + std::to_string(i) + ".upsamplers.0."; std::string sd_upsample_prefix = "output_blocks." + std::to_string(3 * i + 2) + "." + std::to_string(i == 0 ? 1 : 2) + "."; unet_conversion_map_layer.emplace_back(sd_upsample_prefix, hf_upsample_prefix); } } unet_conversion_map_layer.emplace_back("output_blocks.2.2.conv.", "output_blocks.2.1.conv."); // mid block unet_conversion_map_layer.emplace_back("middle_block.1.", "mid_block.attentions.0."); for (int j = 0; j < 2; ++j) { std::string hf_mid_res_prefix = "mid_block.resnets." + std::to_string(j) + "."; std::string sd_mid_res_prefix = "middle_block." + std::to_string(2 * j) + "."; unet_conversion_map_layer.emplace_back(sd_mid_res_prefix, hf_mid_res_prefix); } } std::string result = name; for (const auto& p : unet_conversion_map) { if (result == p.second) { result = p.first; return result; } } if (contains(result, "resnets")) { for (const auto& p : unet_conversion_map_resnet) { size_t pos = result.find(p.second); if (pos != std::string::npos) { result.replace(pos, p.second.size(), p.first); } } } for (const auto& p : unet_conversion_map_layer) { size_t pos = result.find(p.second); if (pos != std::string::npos) { result.replace(pos, p.second.size(), p.first); } } static const std::vector<std::pair<std::string, std::string>> name_map{ {"to_out.weight", "to_out.0.weight"}, {"to_out.bias", "to_out.0.bias"}, }; replace_with_name_map(result, name_map); return result; } std::string convert_diffusers_dit_to_original_sd3(std::string name) { int num_layers = 38; static std::unordered_map<std::string, std::string> sd3_name_map; if (sd3_name_map.empty()) { // --- time_text_embed --- sd3_name_map["time_text_embed.timestep_embedder.linear_1.weight"] = "t_embedder.mlp.0.weight"; sd3_name_map["time_text_embed.timestep_embedder.linear_1.bias"] = "t_embedder.mlp.0.bias"; sd3_name_map["time_text_embed.timestep_embedder.linear_2.weight"] = "t_embedder.mlp.2.weight"; sd3_name_map["time_text_embed.timestep_embedder.linear_2.bias"] = "t_embedder.mlp.2.bias"; sd3_name_map["time_text_embed.text_embedder.linear_1.weight"] = "y_embedder.mlp.0.weight"; sd3_name_map["time_text_embed.text_embedder.linear_1.bias"] = "y_embedder.mlp.0.bias"; sd3_name_map["time_text_embed.text_embedder.linear_2.weight"] = "y_embedder.mlp.2.weight"; sd3_name_map["time_text_embed.text_embedder.linear_2.bias"] = "y_embedder.mlp.2.bias"; sd3_name_map["pos_embed.pos_embed"] = "pos_embed"; sd3_name_map["pos_embed.proj.weight"] = "x_embedder.proj.weight"; sd3_name_map["pos_embed.proj.bias"] = "x_embedder.proj.bias"; // --- transformer blocks --- for (int i = 0; i < num_layers; ++i) { std::string block_prefix = "transformer_blocks." + std::to_string(i) + "."; std::string dst_prefix = "joint_blocks." + std::to_string(i) + "."; sd3_name_map[block_prefix + "norm1.linear.weight"] = dst_prefix + "x_block.adaLN_modulation.1.weight"; sd3_name_map[block_prefix + "norm1.linear.bias"] = dst_prefix + "x_block.adaLN_modulation.1.bias"; sd3_name_map[block_prefix + "norm1_context.linear.weight"] = dst_prefix + "context_block.adaLN_modulation.1.weight"; sd3_name_map[block_prefix + "norm1_context.linear.bias"] = dst_prefix + "context_block.adaLN_modulation.1.bias"; // attn sd3_name_map[block_prefix + "attn.to_q.weight"] = dst_prefix + "x_block.attn.qkv.weight"; sd3_name_map[block_prefix + "attn.to_q.bias"] = dst_prefix + "x_block.attn.qkv.bias"; sd3_name_map[block_prefix + "attn.to_k.weight"] = dst_prefix + "x_block.attn.qkv.weight.1"; sd3_name_map[block_prefix + "attn.to_k.bias"] = dst_prefix + "x_block.attn.qkv.bias.1"; sd3_name_map[block_prefix + "attn.to_v.weight"] = dst_prefix + "x_block.attn.qkv.weight.2"; sd3_name_map[block_prefix + "attn.to_v.bias"] = dst_prefix + "x_block.attn.qkv.bias.2"; sd3_name_map[block_prefix + "attn.add_q_proj.weight"] = dst_prefix + "context_block.attn.qkv.weight"; sd3_name_map[block_prefix + "attn.add_q_proj.bias"] = dst_prefix + "context_block.attn.qkv.bias"; sd3_name_map[block_prefix + "attn.add_k_proj.weight"] = dst_prefix + "context_block.attn.qkv.weight.1"; sd3_name_map[block_prefix + "attn.add_k_proj.bias"] = dst_prefix + "context_block.attn.qkv.bias.1"; sd3_name_map[block_prefix + "attn.add_v_proj.weight"] = dst_prefix + "context_block.attn.qkv.weight.2"; sd3_name_map[block_prefix + "attn.add_v_proj.bias"] = dst_prefix + "context_block.attn.qkv.bias.2"; // attn2 sd3_name_map[block_prefix + "attn2.to_q.weight"] = dst_prefix + "x_block.attn2.qkv.weight"; sd3_name_map[block_prefix + "attn2.to_q.bias"] = dst_prefix + "x_block.attn2.qkv.bias"; sd3_name_map[block_prefix + "attn2.to_k.weight"] = dst_prefix + "x_block.attn2.qkv.weight.1"; sd3_name_map[block_prefix + "attn2.to_k.bias"] = dst_prefix + "x_block.attn2.qkv.bias.1"; sd3_name_map[block_prefix + "attn2.to_v.weight"] = dst_prefix + "x_block.attn2.qkv.weight.2"; sd3_name_map[block_prefix + "attn2.to_v.bias"] = dst_prefix + "x_block.attn2.qkv.bias.2"; sd3_name_map[block_prefix + "attn2.add_q_proj.weight"] = dst_prefix + "context_block.attn2.qkv.weight"; sd3_name_map[block_prefix + "attn2.add_q_proj.bias"] = dst_prefix + "context_block.attn2.qkv.bias"; sd3_name_map[block_prefix + "attn2.add_k_proj.weight"] = dst_prefix + "context_block.attn2.qkv.weight.1"; sd3_name_map[block_prefix + "attn2.add_k_proj.bias"] = dst_prefix + "context_block.attn2.qkv.bias.1"; sd3_name_map[block_prefix + "attn2.add_v_proj.weight"] = dst_prefix + "context_block.attn2.qkv.weight.2"; sd3_name_map[block_prefix + "attn2.add_v_proj.bias"] = dst_prefix + "context_block.attn2.qkv.bias.2"; // norm sd3_name_map[block_prefix + "attn.norm_q.weight"] = dst_prefix + "x_block.attn.ln_q.weight"; sd3_name_map[block_prefix + "attn.norm_k.weight"] = dst_prefix + "x_block.attn.ln_k.weight"; sd3_name_map[block_prefix + "attn.norm_added_q.weight"] = dst_prefix + "context_block.attn.ln_q.weight"; sd3_name_map[block_prefix + "attn.norm_added_k.weight"] = dst_prefix + "context_block.attn.ln_k.weight"; // norm2 sd3_name_map[block_prefix + "attn2.norm_q.weight"] = dst_prefix + "x_block.attn2.ln_q.weight"; sd3_name_map[block_prefix + "attn2.norm_k.weight"] = dst_prefix + "x_block.attn2.ln_k.weight"; // ff sd3_name_map[block_prefix + "ff.net.0.proj.weight"] = dst_prefix + "x_block.mlp.fc1.weight"; sd3_name_map[block_prefix + "ff.net.0.proj.bias"] = dst_prefix + "x_block.mlp.fc1.bias"; sd3_name_map[block_prefix + "ff.net.2.weight"] = dst_prefix + "x_block.mlp.fc2.weight"; sd3_name_map[block_prefix + "ff.net.2.bias"] = dst_prefix + "x_block.mlp.fc2.bias"; sd3_name_map[block_prefix + "ff_context.net.0.proj.weight"] = dst_prefix + "context_block.mlp.fc1.weight"; sd3_name_map[block_prefix + "ff_context.net.0.proj.bias"] = dst_prefix + "context_block.mlp.fc1.bias"; sd3_name_map[block_prefix + "ff_context.net.2.weight"] = dst_prefix + "context_block.mlp.fc2.weight"; sd3_name_map[block_prefix + "ff_context.net.2.bias"] = dst_prefix + "context_block.mlp.fc2.bias"; // output projections sd3_name_map[block_prefix + "attn.to_out.0.weight"] = dst_prefix + "x_block.attn.proj.weight"; sd3_name_map[block_prefix + "attn.to_out.0.bias"] = dst_prefix + "x_block.attn.proj.bias"; sd3_name_map[block_prefix + "attn.to_add_out.weight"] = dst_prefix + "context_block.attn.proj.weight"; sd3_name_map[block_prefix + "attn.to_add_out.bias"] = dst_prefix + "context_block.attn.proj.bias"; // output projections 2 sd3_name_map[block_prefix + "attn2.to_out.0.weight"] = dst_prefix + "x_block.attn2.proj.weight"; sd3_name_map[block_prefix + "attn2.to_out.0.bias"] = dst_prefix + "x_block.attn2.proj.bias"; sd3_name_map[block_prefix + "attn2.to_add_out.weight"] = dst_prefix + "context_block.attn2.proj.weight"; sd3_name_map[block_prefix + "attn2.to_add_out.bias"] = dst_prefix + "context_block.attn2.proj.bias"; } // --- final layers --- sd3_name_map["proj_out.weight"] = "final_layer.linear.weight"; sd3_name_map["proj_out.bias"] = "final_layer.linear.bias"; sd3_name_map["norm_out.linear.weight"] = "final_layer.adaLN_modulation.1.weight"; sd3_name_map["norm_out.linear.bias"] = "final_layer.adaLN_modulation.1.bias"; } replace_with_prefix_map(name, sd3_name_map); return name; } std::string convert_diffusers_dit_to_original_flux(std::string name) { int num_layers = 19; int num_single_layers = 38; static std::unordered_map<std::string, std::string> flux_name_map; if (flux_name_map.empty()) { // --- time_embed (longcat) --- flux_name_map["time_embed.timestep_embedder.linear_1.weight"] = "time_in.in_layer.weight"; flux_name_map["time_embed.timestep_embedder.linear_1.bias"] = "time_in.in_layer.bias"; flux_name_map["time_embed.timestep_embedder.linear_2.weight"] = "time_in.out_layer.weight"; flux_name_map["time_embed.timestep_embedder.linear_2.bias"] = "time_in.out_layer.bias"; // --- time_text_embed --- flux_name_map["time_text_embed.timestep_embedder.linear_1.weight"] = "time_in.in_layer.weight"; flux_name_map["time_text_embed.timestep_embedder.linear_1.bias"] = "time_in.in_layer.bias"; flux_name_map["time_text_embed.timestep_embedder.linear_2.weight"] = "time_in.out_layer.weight"; flux_name_map["time_text_embed.timestep_embedder.linear_2.bias"] = "time_in.out_layer.bias"; flux_name_map["time_text_embed.text_embedder.linear_1.weight"] = "vector_in.in_layer.weight"; flux_name_map["time_text_embed.text_embedder.linear_1.bias"] = "vector_in.in_layer.bias"; flux_name_map["time_text_embed.text_embedder.linear_2.weight"] = "vector_in.out_layer.weight"; flux_name_map["time_text_embed.text_embedder.linear_2.bias"] = "vector_in.out_layer.bias"; // guidance flux_name_map["time_text_embed.guidance_embedder.linear_1.weight"] = "guidance_in.in_layer.weight"; flux_name_map["time_text_embed.guidance_embedder.linear_1.bias"] = "guidance_in.in_layer.bias"; flux_name_map["time_text_embed.guidance_embedder.linear_2.weight"] = "guidance_in.out_layer.weight"; flux_name_map["time_text_embed.guidance_embedder.linear_2.bias"] = "guidance_in.out_layer.bias"; // --- context_embedder / x_embedder --- flux_name_map["context_embedder.weight"] = "txt_in.weight"; flux_name_map["context_embedder.bias"] = "txt_in.bias"; flux_name_map["x_embedder.weight"] = "img_in.weight"; flux_name_map["x_embedder.bias"] = "img_in.bias"; // --- double transformer blocks --- for (int i = 0; i < num_layers; ++i) { std::string block_prefix = "transformer_blocks." + std::to_string(i) + "."; std::string dst_prefix = "double_blocks." + std::to_string(i) + "."; flux_name_map[block_prefix + "norm1.linear.weight"] = dst_prefix + "img_mod.lin.weight"; flux_name_map[block_prefix + "norm1.linear.bias"] = dst_prefix + "img_mod.lin.bias"; flux_name_map[block_prefix + "norm1_context.linear.weight"] = dst_prefix + "txt_mod.lin.weight"; flux_name_map[block_prefix + "norm1_context.linear.bias"] = dst_prefix + "txt_mod.lin.bias"; // attn flux_name_map[block_prefix + "attn.to_q.weight"] = dst_prefix + "img_attn.qkv.weight"; flux_name_map[block_prefix + "attn.to_q.bias"] = dst_prefix + "img_attn.qkv.bias"; flux_name_map[block_prefix + "attn.to_k.weight"] = dst_prefix + "img_attn.qkv.weight.1"; flux_name_map[block_prefix + "attn.to_k.bias"] = dst_prefix + "img_attn.qkv.bias.1"; flux_name_map[block_prefix + "attn.to_v.weight"] = dst_prefix + "img_attn.qkv.weight.2"; flux_name_map[block_prefix + "attn.to_v.bias"] = dst_prefix + "img_attn.qkv.bias.2"; flux_name_map[block_prefix + "attn.add_q_proj.weight"] = dst_prefix + "txt_attn.qkv.weight"; flux_name_map[block_prefix + "attn.add_q_proj.bias"] = dst_prefix + "txt_attn.qkv.bias"; flux_name_map[block_prefix + "attn.add_k_proj.weight"] = dst_prefix + "txt_attn.qkv.weight.1"; flux_name_map[block_prefix + "attn.add_k_proj.bias"] = dst_prefix + "txt_attn.qkv.bias.1"; flux_name_map[block_prefix + "attn.add_v_proj.weight"] = dst_prefix + "txt_attn.qkv.weight.2"; flux_name_map[block_prefix + "attn.add_v_proj.bias"] = dst_prefix + "txt_attn.qkv.bias.2"; // norm flux_name_map[block_prefix + "attn.norm_q.weight"] = dst_prefix + "img_attn.norm.query_norm.scale"; flux_name_map[block_prefix + "attn.norm_k.weight"] = dst_prefix + "img_attn.norm.key_norm.scale"; flux_name_map[block_prefix + "attn.norm_added_q.weight"] = dst_prefix + "txt_attn.norm.query_norm.scale"; flux_name_map[block_prefix + "attn.norm_added_k.weight"] = dst_prefix + "txt_attn.norm.key_norm.scale"; // Comfy-Org/LongCat-Image stores already-converted RMSNorm tensors as *.weight. flux_name_map[dst_prefix + "img_attn.norm.query_norm.weight"] = dst_prefix + "img_attn.norm.query_norm.scale"; flux_name_map[dst_prefix + "img_attn.norm.key_norm.weight"] = dst_prefix + "img_attn.norm.key_norm.scale"; flux_name_map[dst_prefix + "txt_attn.norm.query_norm.weight"] = dst_prefix + "txt_attn.norm.query_norm.scale"; flux_name_map[dst_prefix + "txt_attn.norm.key_norm.weight"] = dst_prefix + "txt_attn.norm.key_norm.scale"; // ff flux_name_map[block_prefix + "ff.net.0.proj.weight"] = dst_prefix + "img_mlp.0.weight"; flux_name_map[block_prefix + "ff.net.0.proj.bias"] = dst_prefix + "img_mlp.0.bias"; flux_name_map[block_prefix + "ff.net.2.weight"] = dst_prefix + "img_mlp.2.weight"; flux_name_map[block_prefix + "ff.net.2.bias"] = dst_prefix + "img_mlp.2.bias"; flux_name_map[block_prefix + "ff_context.net.0.proj.weight"] = dst_prefix + "txt_mlp.0.weight"; flux_name_map[block_prefix + "ff_context.net.0.proj.bias"] = dst_prefix + "txt_mlp.0.bias"; flux_name_map[block_prefix + "ff_context.net.2.weight"] = dst_prefix + "txt_mlp.2.weight"; flux_name_map[block_prefix + "ff_context.net.2.bias"] = dst_prefix + "txt_mlp.2.bias"; // output projections flux_name_map[block_prefix + "attn.to_out.0.weight"] = dst_prefix + "img_attn.proj.weight"; flux_name_map[block_prefix + "attn.to_out.0.bias"] = dst_prefix + "img_attn.proj.bias"; flux_name_map[block_prefix + "attn.to_add_out.weight"] = dst_prefix + "txt_attn.proj.weight"; flux_name_map[block_prefix + "attn.to_add_out.bias"] = dst_prefix + "txt_attn.proj.bias"; } // --- single transformer blocks --- for (int i = 0; i < num_single_layers; ++i) { std::string block_prefix = "single_transformer_blocks." + std::to_string(i) + "."; std::string dst_prefix = "single_blocks." + std::to_string(i) + "."; flux_name_map[block_prefix + "norm.linear.weight"] = dst_prefix + "modulation.lin.weight"; flux_name_map[block_prefix + "norm.linear.bias"] = dst_prefix + "modulation.lin.bias"; flux_name_map[block_prefix + "attn.to_q.weight"] = dst_prefix + "linear1.weight"; flux_name_map[block_prefix + "attn.to_q.bias"] = dst_prefix + "linear1.bias"; flux_name_map[block_prefix + "attn.to_k.weight"] = dst_prefix + "linear1.weight.1"; flux_name_map[block_prefix + "attn.to_k.bias"] = dst_prefix + "linear1.bias.1"; flux_name_map[block_prefix + "attn.to_v.weight"] = dst_prefix + "linear1.weight.2"; flux_name_map[block_prefix + "attn.to_v.bias"] = dst_prefix + "linear1.bias.2"; flux_name_map[block_prefix + "proj_mlp.weight"] = dst_prefix + "linear1.weight.3"; flux_name_map[block_prefix + "proj_mlp.bias"] = dst_prefix + "linear1.bias.3"; flux_name_map[block_prefix + "attn.norm_q.weight"] = dst_prefix + "norm.query_norm.scale"; flux_name_map[block_prefix + "attn.norm_k.weight"] = dst_prefix + "norm.key_norm.scale"; // Comfy-Org/LongCat-Image stores already-converted RMSNorm tensors as *.weight. flux_name_map[dst_prefix + "norm.query_norm.weight"] = dst_prefix + "norm.query_norm.scale"; flux_name_map[dst_prefix + "norm.key_norm.weight"] = dst_prefix + "norm.key_norm.scale"; flux_name_map[block_prefix + "proj_out.weight"] = dst_prefix + "linear2.weight"; flux_name_map[block_prefix + "proj_out.bias"] = dst_prefix + "linear2.bias"; } // --- final layers --- flux_name_map["proj_out.weight"] = "final_layer.linear.weight"; flux_name_map["proj_out.bias"] = "final_layer.linear.bias"; flux_name_map["norm_out.linear.weight"] = "final_layer.adaLN_modulation.1.weight"; flux_name_map["norm_out.linear.bias"] = "final_layer.adaLN_modulation.1.bias"; } replace_with_prefix_map(name, flux_name_map); return name; } static std::string convert_diffusers_dit_to_original_flux3(std::string name) { static const std::vector<std::pair<std::string, std::string>> prefix_map = { {"single_transformer_blocks.", "single_blocks."}, {"mode_blocks.", "content_mode_blocks."}, }; static const std::vector<std::pair<std::string, std::string>> name_map = { {".attn.to_q.", ".q_proj."}, {".attn.to_k.", ".k_proj."}, {".attn.to_v.", ".v_proj."}, {".attn.to_out.0.", ".attn_out."}, {".ff.linear_in.", ".mlp_in."}, {".ff.linear_out.", ".mlp_out."}, }; replace_with_prefix_map(name, prefix_map); replace_with_name_map(name, name_map); return name; } std::string convert_hunyuan_video_to_original_flux(std::string name) { int num_layers = 54; int num_single_layers = 0; static std::unordered_map<std::string, std::string> hy_name_map; if (hy_name_map.empty()) { // --- double transformer blocks --- for (int i = 0; i < num_layers; ++i) { std::string block_prefix = "double_blocks." + std::to_string(i) + "."; std::string dst_prefix = "double_blocks." + std::to_string(i) + "."; hy_name_map[block_prefix + "img_mod.linear"] = dst_prefix + "img_mod.lin"; hy_name_map[block_prefix + "txt_mod.linear"] = dst_prefix + "txt_mod.lin"; // attn hy_name_map[block_prefix + "img_attn_qkv"] = dst_prefix + "img_attn.qkv"; hy_name_map[block_prefix + "txt_attn_qkv"] = dst_prefix + "txt_attn.qkv"; // norm hy_name_map[block_prefix + "img_attn_q_norm.weight"] = dst_prefix + "img_attn.norm.query_norm.scale"; hy_name_map[block_prefix + "img_attn_k_norm.weight"] = dst_prefix + "img_attn.norm.key_norm.scale"; hy_name_map[block_prefix + "txt_attn_q_norm.weight"] = dst_prefix + "txt_attn.norm.query_norm.scale"; hy_name_map[block_prefix + "txt_attn_k_norm.weight"] = dst_prefix + "txt_attn.norm.key_norm.scale"; // ff hy_name_map[block_prefix + "img_mlp.fc1"] = dst_prefix + "img_mlp.0"; hy_name_map[block_prefix + "img_mlp.fc2"] = dst_prefix + "img_mlp.2"; hy_name_map[block_prefix + "txt_mlp.fc1"] = dst_prefix + "txt_mlp.0"; hy_name_map[block_prefix + "txt_mlp.fc2"] = dst_prefix + "txt_mlp.2"; // output projections hy_name_map[block_prefix + "img_attn_proj"] = dst_prefix + "img_attn.proj"; hy_name_map[block_prefix + "txt_attn_proj"] = dst_prefix + "txt_attn.proj"; } } hy_name_map["time_in.mlp.0"] = "time_in.in_layer"; hy_name_map["time_in.mlp.2"] = "time_in.out_layer"; hy_name_map["time_r_in.mlp.0"] = "time_r_in.in_layer"; hy_name_map["time_r_in.mlp.2"] = "time_r_in.out_layer"; hy_name_map["vector_in.mlp.0"] = "vector_in.in_layer"; hy_name_map["vector_in.mlp.2"] = "vector_in.out_layer"; hy_name_map["guidance_in.mlp.0"] = "guidance_in.in_layer"; hy_name_map["guidance_in.mlp.2"] = "guidance_in.out_layer"; hy_name_map["txt_in.c_embedder.linear_1"] = "txt_in.c_embedder.in_layer"; hy_name_map["txt_in.c_embedder.linear_2"] = "txt_in.c_embedder.out_layer"; hy_name_map["txt_in.t_embedder.mlp.0"] = "txt_in.t_embedder.in_layer"; hy_name_map["txt_in.t_embedder.mlp.2"] = "txt_in.t_embedder.out_layer"; replace_with_prefix_map(name, hy_name_map); static const std::vector<std::pair<std::string, std::string>> generic_name_map = { {"_attn_qkv.", "_attn.qkv."}, {"_attn_proj.", "_attn.proj."}, {"mlp.fc1.", "mlp.0."}, {"mlp.fc2.", "mlp.2."}, {".modulation.linear.", ".modulation.lin."}, }; replace_with_name_map(name, generic_name_map); return name; } // LLaDA-Image's LLaDA2-MoE text encoder. Both published layouts use these names; the ComfyUI // GGUF repack differs only by appending ".weight" to the bare 3-D expert parameters. // Called with the "text_encoders." prefix already stripped, so the name still carries "llm.". std::string convert_llada2_moe_te_name(std::string name) { static const std::vector<std::pair<std::string, std::string>> name_map = { {"model.language_model.word_embeddings.", "model.embed_tokens."}, {"model.language_model.norm.", "model.norm."}, {"model.language_model.lm_head.", "lm_head."}, {"model.language_model.layers.", "model.layers."}, {"attention.query_key_value.", "self_attn.query_key_value."}, {"attention.dense.", "self_attn.o_proj."}, {"attention.query_layernorm.", "self_attn.q_norm."}, {"attention.key_layernorm.", "self_attn.k_norm."}, }; replace_with_name_map(name, name_map); // The HF checkpoint stores the stacked experts as bare nn.Parameters with no ".weight". static const std::vector<std::string> bare_expert_params = { "mlp.experts.gate_proj", "mlp.experts.up_proj", "mlp.experts.down_proj", }; for (const auto& suffix : bare_expert_params) { if (ends_with(name, suffix)) { name += ".weight"; break; } } return name; } // The attention projections keep their diffusers names (JointAttention's split_qkv mode), so // only the patch-size-keyed dicts need flattening. Latents arrive already patchified from the // Flux2 VAE, so the only patch key is 1-1. std::string convert_diffusers_dit_to_original_llada_image(std::string name) { static const std::vector<std::pair<std::string, std::string>> prefix_map = { {"all_x_embedder.1-1.", "x_embedder."}, {"all_final_layer.1-1.", "final_layer."}, }; replace_with_prefix_map(name, prefix_map); return name; } std::string convert_diffusers_dit_to_original_lumina2(std::string name) { int num_layers = 30; int num_refiner_layers = 2; static std::unordered_map<std::string, std::string> z_image_name_map; if (z_image_name_map.empty()) { z_image_name_map["all_x_embedder.2-1."] = "x_embedder."; z_image_name_map["all_final_layer.2-1."] = "final_layer."; z_image_name_map["all_x_embedder.16-1."] = "x_embedder."; // --- transformer blocks --- auto add_attention_map = [&](const std::string& prefix, int num) { for (int i = 0; i < num; ++i) { std::string block_prefix = prefix + std::to_string(i) + "."; std::string dst_prefix = prefix + std::to_string(i) + "."; z_image_name_map[block_prefix + "attention.norm_q."] = dst_prefix + "attention.q_norm."; z_image_name_map[block_prefix + "attention.norm_k."] = dst_prefix + "attention.k_norm."; z_image_name_map[block_prefix + "attention.to_out.0."] = dst_prefix + "attention.out."; z_image_name_map[block_prefix + "attention.to_q.weight"] = dst_prefix + "attention.qkv.weight"; z_image_name_map[block_prefix + "attention.to_q.bias"] = dst_prefix + "attention.qkv.bias"; z_image_name_map[block_prefix + "attention.to_k.weight"] = dst_prefix + "attention.qkv.weight.1"; z_image_name_map[block_prefix + "attention.to_k.bias"] = dst_prefix + "attention.qkv.bias.1"; z_image_name_map[block_prefix + "attention.to_v.weight"] = dst_prefix + "attention.qkv.weight.2"; z_image_name_map[block_prefix + "attention.to_v.bias"] = dst_prefix + "attention.qkv.bias.2"; } }; add_attention_map("noise_refiner.", num_refiner_layers); add_attention_map("context_refiner.", num_refiner_layers); add_attention_map("layers.", num_layers); } replace_with_prefix_map(name, z_image_name_map); return name; } std::string convert_other_dit_to_original_anima(std::string name) { static const std::string anima_net_prefix = "net."; if (!starts_with(name, anima_net_prefix)) { name = anima_net_prefix + name; } return name; } std::string convert_diffusers_dit_to_original_krea2(std::string name) { static const std::vector<std::pair<std::string, std::string>> prefix_map = { {"img_in.", "first."}, {"time_embed.linear_1.", "tmlp.0."}, {"time_embed.linear_2.", "tmlp.2."}, {"time_mod_proj.", "tproj.1."}, {"txt_in.linear_1.", "txtmlp.1."}, {"txt_in.linear_2.", "txtmlp.3."}, {"text_fusion.", "txtfusion."}, {"transformer_blocks.", "blocks."}, {"final_layer.", "last."}, }; static const std::vector<std::pair<std::string, std::string>> name_map = { {"attn.to_out.0.", "attn.wo."}, {"attn.to_out.", "attn.wo."}, {"attn.to_gate.", "attn.gate."}, {"attn.to_q.", "attn.wq."}, {"attn.to_k.", "attn.wk."}, {"attn.to_v.", "attn.wv."}, {"ff.gate.", "mlp.gate."}, {"ff.up.", "mlp.up."}, {"ff.down.", "mlp.down."}, {"txt_in.norm.", "txtmlp.0."}, {"last.norm.weight", "last.norm.scale"}, {"last.modulation.weight", "last.modulation.lin"}, }; replace_with_prefix_map(name, prefix_map); replace_with_name_map(name, name_map); return name; } // Convert a diffusers-format ControlNet tensor name to the original (LDM/lllyasviel) layout // declared by ControlNetBlock. Reuses the UNet down/mid conversion for the shared encoder // (down_blocks, mid_block, time_embedding, add_embedding, conv_in) and adds the ControlNet-only // mappings: input_hint_block, zero_convs, middle_block_out. std::string convert_diffusers_controlnet_to_original_sdxl(std::string name) { name = convert_diffusers_unet_to_original_sdxl(std::move(name)); static const std::vector<std::pair<std::string, std::string>> prefix_map = { {"controlnet_cond_embedding.conv_in.", "input_hint_block.0."}, {"controlnet_cond_embedding.blocks.0.", "input_hint_block.2."}, {"controlnet_cond_embedding.blocks.1.", "input_hint_block.4."}, {"controlnet_cond_embedding.blocks.2.", "input_hint_block.6."}, {"controlnet_cond_embedding.blocks.3.", "input_hint_block.8."}, {"controlnet_cond_embedding.blocks.4.", "input_hint_block.10."}, {"controlnet_cond_embedding.blocks.5.", "input_hint_block.12."}, {"controlnet_cond_embedding.conv_out.", "input_hint_block.14."}, {"controlnet_mid_block.", "middle_block_out.0."}, }; for (const auto& p : prefix_map) { if (starts_with(name, p.first)) { return p.second + name.substr(p.first.size()); } } static const std::string controlnet_down_prefix = "controlnet_down_blocks."; if (starts_with(name, controlnet_down_prefix)) { size_t rest_start = controlnet_down_prefix.size(); size_t dot = name.find('.', rest_start); if (dot != std::string::npos) { std::string idx = name.substr(rest_start, dot - rest_start); return "zero_convs." + idx + ".0" + name.substr(dot); } } return name; } static bool is_diffusers_controlnet_name(const std::string& name) { static const std::vector<std::string> heads = { "controlnet_cond_embedding.", "controlnet_down_blocks.", "controlnet_mid_block.", "down_blocks.", "mid_block.", "time_embedding.", "add_embedding.", "conv_in.", }; |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |