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/model_loader.cpp at master · noctrex/stable-diffusion.cpp Expand file tree/ Copy pathmodel_loader.cppMore file actions 1704 lines (1516 loc) · 68.9 KB / Copy pathmodel_loader.cppFile metadata and controls1704 lines (1516 loc) · 68.9 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 <algorithm> #include <atomic> #include <chrono> #include <cinttypes> #include <cstdarg> #include <cstdlib> #include <filesystem> #include <fstream> #include <functional> #include <mutex> #include <regex> #include <set> #include <stdexcept> #include <string> #include <thread> #include <unordered_map> #include <vector> #include "core/util.h" #include "model_io/gguf_io.h" #include "model_io/safetensors_io.h" #include "model_io/torch_legacy_io.h" #include "model_io/torch_zip_io.h" #include "model_loader.h" #include "runtime/imatrix.h" #include "stable-diffusion.h" #include "core/ggml_extend_backend.h" #include "ggml-alloc.h" #include "ggml-backend.h" #include "ggml.h" #include "json.hpp" #include "zip.h" #include "name_conversion.h" /*================================================= Preprocess ==================================================*/ #ifdef SD_USE_UPSTREAM_GGML uint16_t f8_e4m3_to_f16(uint8_t f8) { const uint32_t exponent_bias = 7; if (f8 == 0xff) { return ggml_fp32_to_fp16(-NAN); } else if (f8 == 0x7f) { return ggml_fp32_to_fp16(NAN); } uint32_t sign = f8 & 0x80; uint32_t exponent = (f8 & 0x78) >> 3; uint32_t mantissa = f8 & 0x07; uint32_t result = sign << 24; if (exponent == 0) { if (mantissa > 0) { exponent = 0x7f - exponent_bias; // yes, 2 times if ((mantissa & 0x04) == 0) { mantissa &= 0x03; mantissa <<= 1; exponent -= 1; } if ((mantissa & 0x04) == 0) { mantissa &= 0x03; mantissa <<= 1; exponent -= 1; } result |= (mantissa & 0x03) << 21; result |= exponent << 23; } } else { result |= mantissa << 20; exponent += 0x7f - exponent_bias; result |= exponent << 23; } return ggml_fp32_to_fp16(*reinterpret_cast<const float*>(&result)); } uint16_t f8_e5m2_to_f16(uint8_t fp8) { return static_cast<uint16_t>(fp8) << 8; } void f8_e4m3_to_f16_vec(uint8_t* src, uint16_t* dst, int64_t n) { // support inplace op for (int64_t i = n - 1; i >= 0; i--) { dst[i] = f8_e4m3_to_f16(src[i]); } } void f8_e5m2_to_f16_vec(uint8_t* src, uint16_t* dst, int64_t n) { // support inplace op for (int64_t i = n - 1; i >= 0; i--) { dst[i] = f8_e5m2_to_f16(src[i]); } } #endif void f64_to_f32_vec(double* src, float* dst, int64_t n) { // support inplace op for (int64_t i = 0; i < n; i++) { dst[i] = (float)src[i]; } } void i64_to_i32_vec(int64_t* src, int32_t* dst, int64_t n) { // support inplace op for (int64_t i = 0; i < n; i++) { dst[i] = (int32_t)src[i]; } } void convert_tensor(void* src, ggml_type src_type, void* dst, ggml_type dst_type, int nrows, int n_per_row, std::vector<float> imatrix) { int n = nrows * n_per_row; if (src_type == dst_type) { size_t nbytes = n * ggml_type_size(src_type) / ggml_blck_size(src_type); memcpy(((char*)dst), ((char*)src), nbytes); } else if (src_type == GGML_TYPE_F32) { if (dst_type == GGML_TYPE_F16) { ggml_fp32_to_fp16_row((float*)src, (ggml_fp16_t*)dst, n); } else { imatrix.resize(n_per_row, 1.0f); const float* im = imatrix.data(); ggml_quantize_chunk(dst_type, (float*)src, dst, 0, nrows, n_per_row, im); } } else if (dst_type == GGML_TYPE_F32) { if (src_type == GGML_TYPE_F16) { ggml_fp16_to_fp32_row((ggml_fp16_t*)src, (float*)dst, n); } else if (src_type == GGML_TYPE_I32) { for (int i = 0; i < n; i++) { ((float*)dst)[i] = static_cast<float>(((int32_t*)src)[i]); } } else { auto qtype = ggml_get_type_traits(src_type); if (qtype->to_float == nullptr) { throw std::runtime_error(sd_format("type %s unsupported for integer quantization: no dequantization available", ggml_type_name(src_type))); } qtype->to_float(src, (float*)dst, n); } } else { // src_type == GGML_TYPE_F16 => dst_type is quantized // src_type is quantized => dst_type == GGML_TYPE_F16 or dst_type is quantized auto qtype = ggml_get_type_traits(src_type); if (qtype->to_float == nullptr) { throw std::runtime_error(sd_format("type %s unsupported for integer quantization: no dequantization available", ggml_type_name(src_type))); } std::vector<char> buf; buf.resize(sizeof(float) * n); char* src_data_f32 = buf.data(); qtype->to_float(src, (float*)src_data_f32, n); if (dst_type == GGML_TYPE_F16) { ggml_fp32_to_fp16_row((float*)src_data_f32, (ggml_fp16_t*)dst, n); } else { imatrix.resize(n_per_row, 1.0f); const float* im = imatrix.data(); ggml_quantize_chunk(dst_type, (float*)src_data_f32, dst, 0, nrows, n_per_row, im); } } } /*================================================= ModelLoader ==================================================*/ ModelLoader::ModelLoader() : n_threads_(sd_get_num_physical_cores()) { } size_t ModelLoader::add_file_path(const std::string& file_path) { auto it = std::find(file_paths_.begin(), file_paths_.end(), file_path); if (it != file_paths_.end()) { return static_cast<size_t>(it - file_paths_.begin()); } invalidate_file_data(); file_paths_.push_back(file_path); return file_paths_.size() - 1; } void ModelLoader::add_tensor_storage(const TensorStorage& tensor_storage) { if (tensor_storage_map.count(tensor_storage.name) != 0) { throw std::runtime_error("duplicate tensor in model source: " + tensor_storage.name); } tensor_storage_map[tensor_storage.name] = tensor_storage; } void ModelLoader::set_n_threads(int n_threads) { n_threads_ = n_threads > 0 ? n_threads : sd_get_num_physical_cores(); LOG_VERBOSE("using %d threads for model loading", n_threads_); } bool ModelLoader::init_from_file(const std::string& file_path, const std::string& prefix) { return add_file(file_path, prefix); } bool ModelLoader::parse_file(const std::string& file_path, const std::string& prefix) { FileStamp stamp; if (!read_file_stamp(file_path, stamp)) { return false; } parsed_dependencies_.push_back(stamp); if (is_directory(file_path)) { const std::string diffusers_index_path = path_join(file_path, "model_index.json"); const std::string diffusers_unet_path = path_join(file_path, "unet/diffusion_pytorch_model.safetensors"); const bool has_diffusers_layout = file_exists(diffusers_index_path) || file_exists(diffusers_unet_path); const std::string safetensors_index_path = path_join(file_path, "model.safetensors.index.json"); if (!has_diffusers_layout && file_exists(safetensors_index_path)) { LOG_INFO("load %s using root safetensors index", file_path.c_str()); return parse_file(safetensors_index_path, prefix); } LOG_INFO("load %s using diffusers format", file_path.c_str()); return init_from_diffusers_file(file_path, prefix); } else if (is_gguf_file(file_path)) { LOG_INFO("load %s using gguf format", file_path.c_str()); return init_from_gguf_file(file_path, prefix); } else if (ends_with(file_path, ".json")) { LOG_INFO("load %s using safetensors index format", file_path.c_str()); return init_from_safetensors_index_file(file_path, prefix); } else if (is_safetensors_file(file_path)) { LOG_INFO("load %s using safetensors format", file_path.c_str()); return init_from_safetensors_file(file_path, prefix); } else if (is_torch_zip_file(file_path)) { LOG_INFO("load %s using torch zip format", file_path.c_str()); return init_from_torch_zip_file(file_path, prefix); } else if (init_from_torch_legacy_file(file_path, prefix)) { LOG_INFO("load %s using torch legacy format", file_path.c_str()); return true; } else { if (file_exists(file_path)) { LOG_WARN("unknown format %s", file_path.c_str()); } else { LOG_WARN("file %s not found", file_path.c_str()); } return false; } } void ModelLoader::convert_tensors_name() { if (names_converted_) { return; } names_converted_ = true; rebuild_catalog(); } bool ModelLoader::init_from_file_and_convert_name(const std::string& file_path, const std::string& prefix, SDVersion version) { if (version_ == VERSION_COUNT && version != VERSION_COUNT) { version_ = version; } if (!init_from_file(file_path, prefix)) { return false; } convert_tensors_name(); return true; } /*================================================= GGUFModelLoader ==================================================*/ bool ModelLoader::init_from_gguf_file(const std::string& file_path, const std::string& prefix) { LOG_VERBOSE("init from '%s'", file_path.c_str()); std::vector<TensorStorage> tensor_storages; std::string error; if (!read_gguf_file(file_path, tensor_storages, &error)) { LOG_ERROR("%s", error.c_str()); return false; } size_t file_index = add_file_path(file_path); for (auto& tensor_storage : tensor_storages) { // LOG_VERBOSE("%s", tensor_storage.name.c_str()); if (!starts_with(tensor_storage.name, prefix)) { tensor_storage.name = prefix + tensor_storage.name; } tensor_storage.file_index = file_index; add_tensor_storage(tensor_storage); } return true; } /*================================================= SafeTensorsModelLoader ==================================================*/ bool ModelLoader::init_from_safetensors_file(const std::string& file_path, const std::string& prefix) { LOG_VERBOSE("init from '%s', prefix = '%s'", file_path.c_str(), prefix.c_str()); std::vector<TensorStorage> tensor_storages; std::string error; if (!read_safetensors_file(file_path, tensor_storages, &error, &metadata_, &parsed_tensor_names_[file_path])) { LOG_ERROR("%s", error.c_str()); return false; } size_t file_index = add_file_path(file_path); for (auto& tensor_storage : tensor_storages) { if (!starts_with(tensor_storage.name, prefix)) { tensor_storage.name = prefix + tensor_storage.name; } tensor_storage.file_index = file_index; add_tensor_storage(tensor_storage); // LOG_VERBOSE("%s", tensor_storage.to_string().c_str()); } return true; } bool ModelLoader::init_from_safetensors_index_file(const std::string& file_path, const std::string& prefix) { LOG_VERBOSE("init from safetensors index '%s', prefix = '%s'", file_path.c_str(), prefix.c_str()); std::vector<std::string> shard_paths; std::string error; if (!read_safetensors_index_file(file_path, shard_paths, &error)) { LOG_ERROR("%s", error.c_str()); return false; } for (const std::string& shard_path : shard_paths) { FileStamp stamp; if (!read_file_stamp(shard_path, stamp)) { return false; } parsed_dependencies_.push_back(stamp); LOG_INFO("load %s using safetensors format", shard_path.c_str()); if (!init_from_safetensors_file(shard_path, prefix)) { return false; } } std::ifstream index_file(std::filesystem::u8path(file_path)); const auto index = nlohmann::json::parse(index_file); for (const auto& entry : index.at("weight_map").items()) { const auto expected = (std::filesystem::u8path(file_path).parent_path() / std::filesystem::u8path(entry.value().get<std::string>())) .lexically_normal(); bool found = false; for (const auto& shard : parsed_tensor_names_) { if (std::filesystem::u8path(shard.first).lexically_normal() == expected) { found = shard.second.count(entry.key()) != 0; break; } } if (!found) { LOG_ERROR("safetensors index tensor '%s' is missing from its declared shard", entry.key().c_str()); return false; } } return true; } /*================================================= TorchLegacyModelLoader ==================================================*/ bool ModelLoader::init_from_torch_legacy_file(const std::string& file_path, const std::string& prefix) { LOG_VERBOSE("init from torch legacy '%s'", file_path.c_str()); std::vector<TensorStorage> tensor_storages; std::string error; if (!read_torch_legacy_file(file_path, tensor_storages, &error)) { if ((!error.empty()) && (ends_with(file_path, ".pt") || ends_with(file_path, ".pth"))) { LOG_WARN("%s", error.c_str()); } return false; } size_t file_index = add_file_path(file_path); for (auto& tensor_storage : tensor_storages) { if (!starts_with(tensor_storage.name, prefix)) { tensor_storage.name = prefix + tensor_storage.name; } tensor_storage.file_index = file_index; add_tensor_storage(tensor_storage); } return true; } /*================================================= TorchZipModelLoader ==================================================*/ bool ModelLoader::init_from_torch_zip_file(const std::string& file_path, const std::string& prefix) { LOG_VERBOSE("init from '%s'", file_path.c_str()); std::vector<TensorStorage> tensor_storages; std::string error; if (!read_torch_zip_file(file_path, tensor_storages, &error)) { LOG_ERROR("%s", error.c_str()); return false; } size_t file_index = add_file_path(file_path); for (auto& tensor_storage : tensor_storages) { if (!starts_with(tensor_storage.name, prefix)) { tensor_storage.name = prefix + tensor_storage.name; } tensor_storage.file_index = file_index; add_tensor_storage(tensor_storage); // LOG_VERBOSE("%s", tensor_storage.to_string().c_str()); } return true; } /*================================================= DiffusersModelLoader ==================================================*/ bool ModelLoader::init_from_diffusers_file(const std::string& file_path, const std::string& prefix) { std::string unet_path = path_join(file_path, "unet/diffusion_pytorch_model.safetensors"); std::string vae_path = path_join(file_path, "vae/diffusion_pytorch_model.safetensors"); std::string clip_path = path_join(file_path, "text_encoder/model.safetensors"); std::string clip_g_path = path_join(file_path, "text_encoder_2/model.safetensors"); if (!parse_file(unet_path, prefix + "unet.")) { return false; } if (file_exists(vae_path) && !parse_file(vae_path, prefix + "vae.")) { return false; } if (file_exists(clip_path) && !parse_file(clip_path, prefix + "te.")) { return false; } if (file_exists(clip_g_path) && !parse_file(clip_g_path, prefix + "te.1.")) { return false; } return true; } SDVersion ModelLoader::get_sd_version() const { TensorStorage token_embedding_weight, input_block_weight, context_ebedding_weight; if (tensor_storage_map.count("model.diffusion_model.perceiver.latent_queries")) { return VERSION_FIBO2; } if (tensor_storage_map.find("model.diffusion_model.emb_in.image.weight") != tensor_storage_map.end() && tensor_storage_map.find("model.diffusion_model.txt_mode_blocks.0.q_proj.weight") != tensor_storage_map.end()) { return VERSION_FLUX3; } if (tensor_storage_map.count("model.diffusion_model.video_embedder.weight") && tensor_storage_map.count("model.diffusion_model.shared_block_modulation.weight")) { return VERSION_MOSS_IMAGE; } bool has_multiple_encoders = false; bool is_unet = false; bool is_xl = false; bool is_flux = false; bool is_flux2 = false; bool has_single_block_47 = false; bool is_wan = false; bool is_s2v = false; int64_t patch_embedding_channels = 0; bool has_img_emb = false; bool has_middle_block_1 = false; bool has_output_block_311 = false; bool has_output_block_71 = false; bool has_attn_1024 = false; bool has_distilled_guidance = false; bool has_sdxl_add_embedding = false; for (auto& [name, tensor_storage] : tensor_storage_map) { if (tensor_storage.name.find("model.diffusion_model.double_blocks.") != std::string::npos || tensor_storage.name.find("model.diffusion_model.single_transformer_blocks.") != std::string::npos) { is_flux = true; } if (tensor_storage.name.find("model.diffusion_model.net.lq_proj.latent_proj.0.weight") != std::string::npos) { return VERSION_PID; } if (tensor_storage.name.find("embed_image_indicator.weight") != std::string::npos) { return VERSION_IDEOGRAM4; } if (tensor_storage.name.find("model.diffusion_model.txtfusion.projector.weight") != std::string::npos || tensor_storage.name.find("model.diffusion_model.text_fusion.projector.weight") != std::string::npos) { return VERSION_KREA2; } if (tensor_storage.name.find("model.diffusion_model.nerf_final_layer_conv.") != std::string::npos) { return VERSION_CHROMA_RADIANCE; } if (tensor_storage.name.find("distilled_guidance_layer.in_proj.weight") != std::string::npos) { has_distilled_guidance = true; } if (tensor_storage.name.find("model.diffusion_model.joint_blocks.") != std::string::npos) { return VERSION_SD3; } if (tensor_storage.name.find("model.x_embedder.proj1.weight") != std::string::npos && tensor_storage_map.find("model.language_model.layers.0.self_attn.q_proj.weight") != tensor_storage_map.end()) { return VERSION_HIDREAM_O1; } if (tensor_storage.name.find("model.diffusion_model.transformer_blocks.0.attn.norm_added_q.weight") != std::string::npos && tensor_storage_map.find("model.diffusion_model.transformer_blocks.0.img_mlp.w1.weight") != tensor_storage_map.end()) { return VERSION_LENS; } if (tensor_storage.name.find("net.img_embedder.proj1.weight") != std::string::npos) { return VERSION_MINIT2I; } if (tensor_storage.name.find("language_model.model.layers.0.self_attn.q_proj_mot_gen.weight") != std::string::npos) { return VERSION_SENSENOVA_U1_5; } if (tensor_storage.name == "model.diffusion_model.txt_in.text_norm.weight") { return VERSION_QWEN_IMAGE_2_1; } if (tensor_storage.name.find("model.diffusion_model.transformer_blocks.0.img_mod.1.weight") != std::string::npos) { auto img_in = tensor_storage_map.find("model.diffusion_model.img_in.weight"); if (img_in != tensor_storage_map.end() && img_in->second.logical_ne0() == 128) { return VERSION_MAGE_FLOW; } if (tensor_storage_map.find("model.diffusion_model.time_text_embed.addition_t_embedding.weight") != tensor_storage_map.end()) { return VERSION_QWEN_IMAGE_LAYERED; } return VERSION_QWEN_IMAGE; } if (tensor_storage.name.find("model.diffusion_model.txt_in.individual_token_refiner.blocks.0.adaLN_modulation.1.weight") != std::string::npos) { return VERSION_HUNYUAN_VIDEO; } if (tensor_storage.name.find("llm_adapter.blocks.0.cross_attn.q_proj.weight") != std::string::npos) { return VERSION_ANIMA; } if (tensor_storage.name.find("model.diffusion_model.double_stream_modulation_img.lin.weight") != std::string::npos) { is_flux2 = true; } if (tensor_storage.name.find("dual_time_embed.semantic_embedder.linear_1.weight") != std::string::npos) { return VERSION_SEFI_IMAGE; } if (tensor_storage.name.find("single_blocks.47.linear1.weight") != std::string::npos) { has_single_block_47 = true; } if (tensor_storage.name.find("model.diffusion_model.double_blocks.0.img_mlp.gate_proj.weight") != std::string::npos) { return VERSION_OVIS_IMAGE; } if (tensor_storage.name.find("model.diffusion_model.sigvq_embedder.1.weight") != std::string::npos) { return VERSION_LLADA_IMAGE; } if (tensor_storage.name.find("model.diffusion_model.cap_embedder.0.weight") != std::string::npos) { if (tensor_storage_map.find("text_encoders.llm.connector.layers.0.self_attn.q_proj.weight") != tensor_storage_map.end()) { return VERSION_MING_IMAGE; } if (tensor_storage_map.find("model.diffusion_model.local_decoder.out_conv.weight") != tensor_storage_map.end()) { return VERSION_Z_IMAGE_L2P; } return VERSION_Z_IMAGE; } if (tensor_storage.name.find("double_stream_layers.0.img_instruct_attn.processor.img_to_q.weight") != std::string::npos) { return VERSION_BOOGU_IMAGE; } if (tensor_storage.name.find("model.diffusion_model.layers.0.adaLN_sa_ln.weight") != std::string::npos) { return VERSION_ERNIE_IMAGE; } if (tensor_storage.name.find("model.diffusion_model.t_block.1.weight") != std::string::npos && tensor_storage_map.find("model.diffusion_model.x_embedder.proj.weight") != tensor_storage_map.end() && tensor_storage_map.find("model.diffusion_model.audio_patchify_proj.weight") == tensor_storage_map.end()) { return VERSION_PIXART; } if (tensor_storage.name.find("model.diffusion_model.adaln_single.emb.timestep_embedder.linear_1.bias") != std::string::npos) { // PixArt shares this timestep embedding with LTX-AV. if (tensor_storage_map.find("model.diffusion_model.pos_embed.proj.weight") != tensor_storage_map.end() && tensor_storage_map.find("model.diffusion_model.adaln_single.linear.weight") != tensor_storage_map.end() && tensor_storage_map.find("model.diffusion_model.audio_patchify_proj.weight") == tensor_storage_map.end()) { return VERSION_PIXART; } return VERSION_LTXAV; } if (tensor_storage.name.find("model.diffusion_model.video_patch_proj.weight") != std::string::npos && tensor_storage_map.find("model.diffusion_model.condition_proj.weight") != tensor_storage_map.end()) { if (tensor_storage_map.count("model.diffusion_model.audio_patch_proj.weight") == 0) { return VERSION_MINIMAX_H3_IMAGE; } // The connector is shared with H3 video; the single-frame VAE identifies P-H3. auto decoder = tensor_storage_map.find("first_stage_model.decoder.proj_out.weight"); if (decoder == tensor_storage_map.end()) { decoder = tensor_storage_map.find("vae.decoder.proj_out.weight"); } if (decoder != tensor_storage_map.end() && decoder->second.ne[1] == 768) { return VERSION_P_H3_IMAGE; } return VERSION_MINIMAX_H3; } if (name == "model.diffusion_model.y_embedder.layer_pool.weight" && tensor_storage_map.count("model.diffusion_model.modulation_cores.adaln_img.weight")) { return VERSION_IRIS; } if (tensor_storage.name.find("model.diffusion_model.blocks.0.cross_attn.norm_k.weight") != std::string::npos) { is_wan = true; } if (tensor_storage.name.find("casual_audio_encoder.weights") != std::string::npos || tensor_storage.name.find("audio_injector.injector.0.q.weight") != std::string::npos) { // S2V and T2V-14B share patch_embedding shapes. is_s2v = true; } if (tensor_storage.name.find("model.diffusion_model.patch_embedder.weight") != std::string::npos) { return VERSION_LINGBOT_VIDEO; } if (tensor_storage.name.find("model.diffusion_model.patch_embedding.weight") != std::string::npos) { patch_embedding_channels = tensor_storage.ne[3]; } if (tensor_storage.name.find("model.diffusion_model.img_emb") != std::string::npos) { has_img_emb = true; } if (tensor_storage.name.find("model.diffusion_model.input_blocks.") != std::string::npos || tensor_storage.name.find("model.diffusion_model.down_blocks.") != std::string::npos || tensor_storage.name.find("unet.down_blocks.") != std::string::npos) { is_unet = true; if (has_multiple_encoders) { is_xl = true; } } if (tensor_storage.name.find("conditioner.embedders.1") != std::string::npos || tensor_storage.name.find("cond_stage_model.1") != std::string::npos || tensor_storage.name.find("te.1") != std::string::npos) { has_multiple_encoders = true; if (is_unet) { is_xl = true; } } // A standalone SDXL UNet (--diffusion-model) identifies the family by its added // text/time embedding; its clip_g never carries a checkpoint prefix. if (tensor_storage.name.find("model.diffusion_model.add_embedding.") != std::string::npos || tensor_storage.name.find("model.diffusion_model.label_emb.0.0.") != std::string::npos) { has_sdxl_add_embedding = true; } if (tensor_storage.name.find("model.diffusion_model.input_blocks.8.0.time_mixer.mix_factor") != std::string::npos) { return VERSION_SVD; } if (tensor_storage.name.find("model.diffusion_model.middle_block.1.") != std::string::npos || tensor_storage.name.find("model.diffusion_model.mid_block.resnets.1.") != std::string::npos || tensor_storage.name.find("unet.mid_block.resnets.1.") != std::string::npos) { has_middle_block_1 = true; } if (tensor_storage.name.find("model.diffusion_model.output_blocks.3.1.transformer_blocks.1") != std::string::npos || tensor_storage.name.find("model.diffusion_model.up_blocks.1.attentions.0.transformer_blocks.1") != std::string::npos || tensor_storage.name.find("unet.up_blocks.1.attentions.0.transformer_blocks.1") != std::string::npos) { has_output_block_311 = true; } if (tensor_storage.name.find("model.diffusion_model.output_blocks.7.1") != std::string::npos || tensor_storage.name.find("model.diffusion_model.up_blocks.2.attentions.1") != std::string::npos || tensor_storage.name.find("unet.up_blocks.2.attentions.1") != std::string::npos) { has_output_block_71 = true; if (tensor_storage.name.find("model.diffusion_model.output_blocks.7.1.transformer_blocks.0.attn1.to_k.weight") != std::string::npos) { if (tensor_storage.logical_ne0() == 1024) has_attn_1024 = true; } } if (tensor_storage.name == "cond_stage_model.transformer.text_model.embeddings.token_embedding.weight" || tensor_storage.name == "cond_stage_model.model.token_embedding.weight" || tensor_storage.name == "text_model.embeddings.token_embedding.weight" || tensor_storage.name == "te.text_model.embeddings.token_embedding.weight" || tensor_storage.name == "conditioner.embedders.0.model.token_embedding.weight" || tensor_storage.name == "conditioner.embedders.0.transformer.text_model.embeddings.token_embedding.weight") { token_embedding_weight = tensor_storage; // break; } if (tensor_storage.name == "model.diffusion_model.input_blocks.0.0.weight" || tensor_storage.name == "model.diffusion_model.conv_in.weight" || tensor_storage.name == "model.diffusion_model.img_in.weight" || tensor_storage.name == "unet.conv_in.weight") { input_block_weight = tensor_storage; } if (tensor_storage.name == "model.diffusion_model.txt_in.weight" || tensor_storage.name == "model.diffusion_model.context_embedder.weight") { context_ebedding_weight = tensor_storage; } } if (is_wan) { LOG_DEBUG("patch_embedding_channels %d", patch_embedding_channels); if (is_s2v) { return VERSION_WAN2_2_S2V; } if (patch_embedding_channels == 184320 && !has_img_emb) { return VERSION_WAN2_2_I2V; } if (patch_embedding_channels == 147456 && !has_img_emb) { return VERSION_WAN2_2_TI2V; } return VERSION_WAN2; } bool is_inpaint = input_block_weight.ne[2] == 9; bool is_ip2p = input_block_weight.ne[2] == 8; if (is_unet && has_sdxl_add_embedding) { is_xl = true; } if (is_xl) { if (is_inpaint) { return VERSION_SDXL_INPAINT; } if (is_ip2p) { return VERSION_SDXL_PIX2PIX; } if (!has_middle_block_1) { if (!has_output_block_311) { return VERSION_SDXL_VEGA; } return VERSION_SDXL_SSD1B; } return VERSION_SDXL; } if (is_flux && !is_flux2) { if (has_distilled_guidance) { return VERSION_CHROMA; } if (context_ebedding_weight.logical_ne0() == 3584) { return VERSION_LONGCAT; } else { if (input_block_weight.logical_ne0() == 384) { return VERSION_FLUX_FILL; } if (input_block_weight.logical_ne0() == 128) { return VERSION_FLUX_CONTROLS; } if (input_block_weight.logical_ne0() == 196) { return VERSION_FLEX_2; } return VERSION_FLUX; } } if (is_flux2) { if (has_single_block_47) { return VERSION_FLUX2; } return VERSION_FLUX2_KLEIN; } if (token_embedding_weight.ne[0] == 768) { if (is_inpaint) { return VERSION_SD1_INPAINT; } if (is_ip2p) { return VERSION_SD1_PIX2PIX; } if (!has_middle_block_1) { if (!has_output_block_71) { return VERSION_SDXS_512_DS; } return VERSION_SD1_TINY_UNET; } return VERSION_SD1; } else if (token_embedding_weight.ne[0] == 1024) { if (is_inpaint) { return VERSION_SD2_INPAINT; } if (!has_middle_block_1) { return has_attn_1024 ? VERSION_SDXS_09 : VERSION_SD2_TINY_UNET; } return VERSION_SD2; } return VERSION_COUNT; } std::map<ggml_type, uint32_t> ModelLoader::get_wtype_stat() const { std::map<ggml_type, uint32_t> wtype_stat; for (auto& [name, tensor_storage] : tensor_storage_map) { auto iter = wtype_stat.find(tensor_storage.type); if (iter != wtype_stat.end()) { iter->second++; } else { wtype_stat[tensor_storage.type] = 1; } } return wtype_stat; } std::map<ggml_type, uint32_t> ModelLoader::get_conditioner_wtype_stat() const { std::map<ggml_type, uint32_t> wtype_stat; for (auto& [name, tensor_storage] : tensor_storage_map) { if ((tensor_storage.name.find("text_encoders") == std::string::npos && tensor_storage.name.find("cond_stage_model") == std::string::npos && tensor_storage.name.find("te.text_model.") == std::string::npos && tensor_storage.name.find("conditioner") == std::string::npos)) { continue; } auto iter = wtype_stat.find(tensor_storage.type); if (iter != wtype_stat.end()) { iter->second++; } else { wtype_stat[tensor_storage.type] = 1; } } return wtype_stat; } std::map<ggml_type, uint32_t> ModelLoader::get_diffusion_model_wtype_stat() const { std::map<ggml_type, uint32_t> wtype_stat; for (auto& [name, tensor_storage] : tensor_storage_map) { if (tensor_storage.name.find("model.diffusion_model.") == std::string::npos && tensor_storage.name.find("unet.") == std::string::npos) { continue; } auto iter = wtype_stat.find(tensor_storage.type); if (iter != wtype_stat.end()) { iter->second++; } else { wtype_stat[tensor_storage.type] = 1; } } return wtype_stat; } std::map<ggml_type, uint32_t> ModelLoader::get_vae_wtype_stat() const { std::map<ggml_type, uint32_t> wtype_stat; for (auto& [name, tensor_storage] : tensor_storage_map) { if (tensor_storage.name.find("vae.") == std::string::npos && tensor_storage.name.find("first_stage_model") == std::string::npos) { continue; } auto iter = wtype_stat.find(tensor_storage.type); if (iter != wtype_stat.end()) { iter->second++; } else { wtype_stat[tensor_storage.type] = 1; } } return wtype_stat; } TensorTypeRules parse_tensor_type_rules(const std::string& tensor_type_rules) { TensorTypeRules result; for (const auto& item : split_string(tensor_type_rules, ',')) { if (item.size() == 0) continue; std::string::size_type pos = item.find('='); if (pos == std::string::npos) { LOG_WARN("ignoring invalid quant override \"%s\"", item.c_str()); continue; } std::string tensor_pattern = item.substr(0, pos); std::string type_name = item.substr(pos + 1); ggml_type tensor_type = GGML_TYPE_COUNT; if (type_name == "f32") { tensor_type = GGML_TYPE_F32; } else { for (size_t i = 0; i < GGML_TYPE_COUNT; i++) { auto trait = ggml_get_type_traits((ggml_type)i); if (trait->to_float && trait->type_size && type_name == trait->type_name) { tensor_type = (ggml_type)i; } } } if (tensor_type != GGML_TYPE_COUNT) { result.emplace_back(tensor_pattern, tensor_type); } else { LOG_WARN("ignoring invalid quant override \"%s\"", item.c_str()); } } return result; } void ModelLoader::set_wtype_override(ggml_type wtype, std::string tensor_type_rules) { wtype_override_ = wtype; tensor_type_rules_ = tensor_type_rules; auto map_rules = parse_tensor_type_rules(tensor_type_rules); for (auto& [name, tensor_storage] : tensor_storage_map) { tensor_storage.expected_type = GGML_TYPE_COUNT; ggml_type dst_type = wtype; for (const auto& tensor_type_rule : map_rules) { std::regex pattern(tensor_type_rule.first); if (std::regex_search(name, pattern)) { dst_type = tensor_type_rule.second; break; } } if (dst_type == GGML_TYPE_COUNT) { continue; } if (!tensor_should_be_converted(tensor_storage, dst_type)) { continue; } tensor_storage.expected_type = dst_type; } invalidate_file_data(); ++revision_; } void ModelLoader::process_model_files(bool enable_mmap, bool writable_mmap) { if (model_files_processed) { return; } std::vector<TensorStorage> processed_tensor_storages; for (const auto& [name, tensor_storage] : tensor_storage_map) { processed_tensor_storages.push_back(tensor_storage); } for (size_t file_index = 0; file_index < file_paths_.size(); file_index++) { std::string file_path = file_paths_[file_index]; std::vector<TensorStorage> file_tensors; for (const auto& ts : processed_tensor_storages) { if (ts.file_index == file_index) { file_tensors.push_back(ts); } } if (file_tensors.empty()) { continue; } bool is_zip = false; for (auto const& ts : file_tensors) { if (ts.index_in_zip >= 0) { is_zip = true; break; } } ModelFileData fdata = {}; fdata.path = file_path; fdata.is_zip = is_zip; fdata.tensors = std::move(file_tensors); if (enable_mmap && !is_zip) { LOG_VERBOSE("using mmap for I/O"); std::unique_ptr<MmapWrapper> mmapped = MmapWrapper::create(file_path, writable_mmap); if (mmapped) { uint8_t* mmap_data = static_cast<uint8_t*>(mmapped->writable_data()); ggml_backend_buffer_t buf_mmap = ggml_backend_cpu_buffer_from_ptr(mmap_data, mmapped->size()); if (buf_mmap) { LOG_INFO("using mmap for '%s'", file_path.c_str()); fdata.mmbuffer = std::shared_ptr<struct ggml_backend_buffer>(buf_mmap, ggml_backend_buffer_free); } else { LOG_WARN("mmap: failed to create backend buffer for file %s", fdata.path.c_str()); } fdata.mmapped = std::shared_ptr<MmapWrapper>(std::move(mmapped)); } else { LOG_WARN("failed to memory-map '%s' (falling back to read())", file_path.c_str()); } } file_data.push_back(std::move(fdata)); } model_files_processed = true; } std::vector<MmapTensorStore> ModelLoader::mmap_tensors(std::map<std::string, ggml_tensor*>& tensors, std::set<std::string> ignore_tensors, bool writable_mmap, ggml_backend_dev_t device) { std::set<std::string> names; for (const auto& entry : tensors) { names.insert(entry.first); } if (!validate_sources(&names)) { return {}; } process_model_files(true, writable_mmap); std::vector<MmapTensorStore> result; uint64_t mapped_bytes = 0; size_t mapped_tensors = 0; LOG_VERBOSE("memory-mapping tensors..."); int64_t t_start = ggml_time_ms(); for (auto& fdata : file_data) { if (!fdata.mmbuffer) continue; // Wrapped on first use: a device buffer makes the whole file resident on that device. std::shared_ptr<struct ggml_backend_buffer> file_buffer = device == nullptr ? fdata.mmbuffer : nullptr; bool file_unmappable = false; auto buffer_for_file = [&]() -> ggml_backend_buffer_t { if (file_buffer || file_unmappable) { return file_buffer.get(); } auto cached = fdata.device_mmbuffers.find(device); if (cached != fdata.device_mmbuffers.end()) { file_buffer = cached->second; return file_buffer.get(); } size_t max_tensor_size = 0; for (const auto& ts : fdata.tensors) { max_tensor_size = std::max(max_tensor_size, static_cast<size_t>(ts.nbytes())); } ggml_backend_buffer_t buf = sd_backend_dev_buffer_from_host_ptr(device, fdata.mmapped->writable_data(), fdata.mmapped->size(), max_tensor_size); if (buf == nullptr) { |
| 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. |