FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
stable-diffusion.cpp/src/model_loader.cpp at master · unslothai/stable-diffusion.cpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
unslothai
/
stable-diffusion.cpp
Public
forked from
leejet/stable-diffusion.cpp
Notifications
You must be signed in to change notification settings
Fork
2
Star
10
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
stable-diffusion.cpp
/
src
/
model_loader.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1574 lines (1384 loc) · 61.5 KB
Breadcrumbs
stable-diffusion.cpp
/
src
/
model_loader.cpp
Copy path
File metadata and controls
1574 lines (1384 loc) · 61.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<
fstream
>
#
include
<
functional
>
#
include
<
mutex
>
#
include
<
regex
>
#
include
<
set
>
#
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
"
zip.h
"
#
include
"
name_conversion.h
"
/*
================================================= Preprocess ==================================================
*/
const
char
* unused_tensors[] = {
"
betas
"
,
"
alphas_cumprod_prev
"
,
"
sqrt_alphas_cumprod
"
,
"
sqrt_one_minus_alphas_cumprod
"
,
"
log_one_minus_alphas_cumprod
"
,
"
sqrt_recip_alphas_cumprod
"
,
"
sqrt_recipm1_alphas_cumprod
"
,
"
posterior_variance
"
,
"
posterior_log_variance_clipped
"
,
"
posterior_mean_coef1
"
,
"
posterior_mean_coef2
"
,
"
cond_stage_model.transformer.text_model.embeddings.position_ids
"
,
"
cond_stage_model.1.model.text_model.embeddings.position_ids
"
,
"
cond_stage_model.transformer.vision_model.embeddings.position_ids
"
,
"
cond_stage_model.model.logit_scale
"
,
"
conditioner.embedders.0.transformer.text_model.embeddings.position_ids
"
,
"
conditioner.embedders.0.model.logit_scale
"
,
"
conditioner.embedders.1.model.logit_scale
"
,
"
model.diffusion_model.time_embedding.cond_proj.weight
"
,
"
unet.time_embedding.cond_proj.weight
"
,
"
model_ema.decay
"
,
"
model_ema.num_updates
"
,
"
model_ema.diffusion_model
"
,
"
embedding_manager
"
,
"
denoiser.sigmas
"
,
"
text_encoders.t5xxl.transformer.encoder.embed_tokens.weight
"
,
//
only used during training
"
ztsnr
"
,
//
Found in some SDXL vpred models
"
edm_vpred.sigma_min
"
,
//
Found in CosXL
//
TODO: find another way to avoid the "unknown tensor" for these two
//
"edm_vpred.sigma_max", // Used to detect CosXL
//
"v_pred", // Used to detect SDXL vpred models
"
text_encoders.llm.output.weight
"
,
"
text_encoders.llm.lm_head.
"
,
};
bool
is_unused_tensor
(
const
std::string& name) {
for
(
size_t
i =
0
; i <
sizeof
(unused_tensors) /
sizeof
(
const
char
*); i++) {
if
(
starts_with
(name, unused_tensors[i])) {
return
true
;
}
}
return
false
;
}
uint16_t
f8_e4m3_to_f16
(
uint8_t
f8
) {
//
do we need to support uz?
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]);
}
}
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
{
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) {
if
(model_files_processed) {
file_data.
clear
();
model_files_processed =
false
;
}
file_paths_.
push_back
(file_path);
return
file_paths_.
size
() -
1
;
}
void
ModelLoader::add_tensor_storage
(
const
TensorStorage& tensor_storage) {
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_DEBUG
(
"
using %d threads for model loading
"
, n_threads_);
}
bool
ModelLoader::init_from_file
(
const
std::string& file_path,
const
std::string& prefix) {
if
(
is_directory
(file_path)) {
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
() {
SDVersion version = (version_ ==
VERSION_COUNT
) ?
get_sd_version
() : version_;
String2TensorStorage new_map;
for
(
auto
& [_, tensor_storage] : tensor_storage_map) {
auto
new_name =
convert_tensor_name
(tensor_storage.
name
, version);
//
LOG_DEBUG("%s -> %s", tensor_storage.name.c_str(), new_name.c_str());
tensor_storage.
name
= new_name;
new_map[new_name] =
std::move
(tensor_storage);
}
tensor_storage_map.
swap
(new_map);
}
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_DEBUG
(
"
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_DEBUG("%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_DEBUG
(
"
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_)) {
LOG_ERROR
(
"
%s
"
, error.
c_str
());
return
false
;
}
size_t
file_index =
add_file_path
(file_path);
for
(
auto
& tensor_storage : tensor_storages) {
if
(
is_unused_tensor
(tensor_storage.
name
)) {
continue
;
}
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_DEBUG("%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_DEBUG
(
"
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) {
if
(!
init_from_file
(shard_path, prefix)) {
return
false
;
}
}
return
true
;
}
/*
================================================= TorchLegacyModelLoader ==================================================
*/
bool
ModelLoader::init_from_torch_legacy_file
(
const
std::string& file_path,
const
std::string& prefix) {
LOG_DEBUG
(
"
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
(
is_unused_tensor
(tensor_storage.
name
)) {
continue
;
}
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_DEBUG
(
"
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_DEBUG("%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
(!
init_from_safetensors_file
(unet_path,
"
unet.
"
)) {
return
false
;
}
if
(!
init_from_safetensors_file
(vae_path,
"
vae.
"
)) {
LOG_WARN
(
"
Couldn't find working VAE in %s
"
, file_path.
c_str
());
//
return false;
}
if
(!
init_from_safetensors_file
(clip_path,
"
te.
"
)) {
LOG_WARN
(
"
Couldn't find working text encoder in %s
"
, file_path.
c_str
());
//
return false;
}
if
(!
init_from_safetensors_file
(clip_g_path,
"
te.1.
"
)) {
LOG_DEBUG
(
"
Couldn't find working second text encoder in %s
"
, file_path.
c_str
());
}
return
true
;
}
SDVersion
ModelLoader::get_sd_version
() {
TensorStorage token_embedding_weight, input_block_weight, context_ebedding_weight;
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
;
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
;
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
(
"
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
(
"
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
.
ne
[
0
] ==
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.cap_embedder.0.weight
"
) != std::string::npos) {
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.adaln_single.emb.timestep_embedder.linear_1.bias
"
) != std::string::npos) {
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.audio_patch_proj.weight
"
) != tensor_storage_map.
end
()) {
return
VERSION_MINIMAX_H3
;
}
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
(
"
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
(
"
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
;
}
}
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
(
"
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
(
"
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
(
"
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.
ne
[
0
] ==
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.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
(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_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
(context_ebedding_weight.
ne
[
0
] ==
3584
) {
return
VERSION_LONGCAT
;
}
else
{
if
(input_block_weight.
ne
[
0
] ==
384
) {
return
VERSION_FLUX_FILL
;
}
if
(input_block_weight.
ne
[
0
] ==
128
) {
return
VERSION_FLUX_CONTROLS
;
}
if
(input_block_weight.
ne
[
0
] ==
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
() {
std::map<ggml_type,
uint32_t
> wtype_stat;
for
(
auto
& [name, tensor_storage] : tensor_storage_map) {
if
(
is_unused_tensor
(tensor_storage.
name
)) {
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_conditioner_wtype_stat
() {
std::map<ggml_type,
uint32_t
> wtype_stat;
for
(
auto
& [name, tensor_storage] : tensor_storage_map) {
if
(
is_unused_tensor
(tensor_storage.
name
)) {
continue
;
}
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
() {
std::map<ggml_type,
uint32_t
> wtype_stat;
for
(
auto
& [name, tensor_storage] : tensor_storage_map) {
if
(
is_unused_tensor
(tensor_storage.
name
)) {
continue
;
}
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
() {
std::map<ggml_type,
uint32_t
> wtype_stat;
for
(
auto
& [name, tensor_storage] : tensor_storage_map) {
if
(
is_unused_tensor
(tensor_storage.
name
)) {
continue
;
}
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) {
auto
map_rules =
parse_tensor_type_rules
(tensor_type_rules);
for
(
auto
& [name, tensor_storage] : tensor_storage_map) {
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;
}
}
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) {
if
(
is_unused_tensor
(tensor_storage.
name
)) {
continue
;
}
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_DEBUG
(
"
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) {
process_model_files
(
true
, writable_mmap);
std::vector<MmapTensorStore> result;
uint64_t
mapped_bytes =
0
;
size_t
mapped_tensors =
0
;
LOG_DEBUG
(
"
memory-mapping tensors...
"
);
int64_t
t_start =
ggml_time_ms
();
for
(
auto
& fdata : file_data) {
if
(!fdata.
mmbuffer
)
continue
;
const
std::vector<TensorStorage>& file_tensors = fdata.
tensors
;
size_t
file_mapped_bytes =
0
;
size_t
file_mapped_tensors =
0
;
for
(
const
auto
& tensor_storage : file_tensors) {
const
std::string& name = tensor_storage.
name
;
bool
is_ignored =
false
;
for
(
const
auto
& ignore_prefix : ignore_tensors) {
if
(
starts_with
(name, ignore_prefix)) {
is_ignored =
true
;
break
;
}
}
if
(is_ignored)
continue
;
auto
it = tensors.
find
(name);
if
(it == tensors.
end
())
continue
;
ggml_tensor* dst_tensor = it->
second
;
if
(dst_tensor ==
nullptr
)
continue
;
if
(tensor_storage.
is_f8_e4m3
||
tensor_storage.
is_f8_e5m2
||
tensor_storage.
is_f64
||
tensor_storage.
is_i64
||
tensor_storage.
type
!= dst_tensor->
type
) {
continue
;
}
size_t
tensor_size = tensor_storage.
nbytes
();
size_t
tensor_offset = tensor_storage.
offset
;
if
(tensor_storage.
ne
[
0
] != dst_tensor->
ne
[
0
] ||
tensor_storage.
ne
[
1
] != dst_tensor->
ne
[
1
] ||
tensor_storage.
ne
[
2
] != dst_tensor->
ne
[
2
] ||
tensor_storage.
ne
[
3
] != dst_tensor->
ne
[
3
] ||
tensor_size !=
ggml_nbytes
(dst_tensor)) {
//
let load_tensors worry about this
continue
;
}
ggml_backend_buffer_t
buf_mmap = fdata.
mmbuffer
.
get
();
uint8_t
* mmap_data =
static_cast
<
uint8_t
*>(
ggml_backend_buffer_get_base
(buf_mmap));
dst_tensor->
buffer
= buf_mmap;
dst_tensor->
data
= mmap_data + tensor_offset;
file_mapped_bytes += tensor_size;
file_mapped_tensors++;
}
if
(file_mapped_bytes >
0
) {
mapped_tensors += file_mapped_tensors;
mapped_bytes += file_mapped_bytes;
result.
push_back
({fdata.
mmapped
, fdata.
mmbuffer
});
}
}
int64_t
t_end =
ggml_time_ms
();
int64_t
duration_ms = t_end - t_start;
LOG_INFO
(
"
memory-mapped %zu tensors in %zu files (%.2f MB), taking %.2fs
"
,
mapped_tensors,
result.
size
(),
mapped_bytes / (
1024.0
*
1024.0
),
duration_ms /
1000.0
);
return
result;
}
bool
ModelLoader::load_tensors
(
on_new_tensor_cb_t
on_new_tensor_cb,
bool
enable_mmap,
const
std::set<std::string>* target_tensor_names,
bool
log_progress) {
process_model_files
(enable_mmap,
false
);
std::atomic<
int64_t
>
read_time_ms
(
0
);
std::atomic<
int64_t
>
memcpy_time_ms
(
0
);
std::atomic<
int64_t
>
copy_to_backend_time_ms
(
0
);
std::atomic<
int64_t
>
convert_time_ms
(
0
);
std::atomic<
uint64_t
>
bytes_processed
(
0
);
int
num_threads_to_use = n_threads_;
int64_t
start_time =
ggml_time_ms
();
size_t
total_tensors_to_process =
0
;
std::vector<
size_t
> file_tensors_to_process_counts;
file_tensors_to_process_counts.
reserve
(file_data.
size
());
for
(
const
auto
& fdata : file_data) {
size_t
file_tensors_to_process =
0
;
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL