forked from balisujohn/tortoise.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
6439 lines (4990 loc) · 243 KB
/
main.cpp
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 "ggml/ggml-alloc.h"
#include "ggml/ggml-backend.h"
#include "ggml/ggml.h"
#ifdef GGML_USE_CUBLAS
#include "ggml-cuda.h"
#endif
#ifdef GGML_USE_METAL
#include "ggml-metal.h"
#endif
#include "common.h"
#include <algorithm>
#include <assert.h>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <random>
#include <string>
#include <vector>
#if defined(_MSC_VER)
#pragma warning(disable : 4244 4267) // possible loss of data
#endif
#define GPT2_MAX_NODES 4096
int32_t NUM_RETURN_SEQUENCES =
4; // hardcoding this for now, analagous to "num_return_sequences arugment
// to inference_speech"
auto now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();
auto milliseconds =
std::chrono::duration_cast<std::chrono::milliseconds>(duration);
// Use the milliseconds count as a seed for the random number generator
unsigned time_seed = milliseconds.count();
std::mt19937 generator(time_seed);
std::uniform_real_distribution<float> distribution(0.0, 1.0);
std::normal_distribution<double> normal_distribution(0.0, 1.0);
void localAssert(bool condition) {
if (!condition) {
std::cout << "failure" << std::endl;
exit(0);
}
}
// clang-format off
/*
██████╗ ██████╗ ████████╗ ██████╗
██╔════╝ ██╔══██╗╚══██╔══╝ ╚════██╗
██║ ███╗██████╔╝ ██║█████╗ █████╔╝
██║ ██║██╔═══╝ ██║╚════╝██╔═══╝
╚██████╔╝██║ ██║ ███████╗
╚═════╝ ╚═╝ ╚═╝ ╚══════╝
████████╗███████╗███╗ ██╗███████╗ ██████╗ ██████╗
╚══██╔══╝██╔════╝████╗ ██║██╔════╝██╔═══██╗██╔══██╗
██║ █████╗ ██╔██╗ ██║███████╗██║ ██║██████╔╝
██║ ██╔══╝ ██║╚██╗██║╚════██║██║ ██║██╔══██╗
██║ ███████╗██║ ╚████║███████║╚██████╔╝██║ ██║
╚═╝ ╚══════╝╚═╝ ╚═══╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝
███╗ ███╗ █████╗ ███╗ ██╗██╗███████╗███████╗███████╗████████╗
████╗ ████║██╔══██╗████╗ ██║██║██╔════╝██╔════╝██╔════╝╚══██╔══╝
██╔████╔██║███████║██╔██╗ ██║██║█████╗ █████╗ ███████╗ ██║
██║╚██╔╝██║██╔══██║██║╚██╗██║██║██╔══╝ ██╔══╝ ╚════██║ ██║
██║ ╚═╝ ██║██║ ██║██║ ╚████║██║██║ ███████╗███████║ ██║
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝╚═╝ ╚══════╝╚══════╝ ╚═╝
*/
// clang-format on
// derived from ggml gpt2 reference implementation
struct gpt2_layer {
struct ggml_tensor *layer_norm_1_weights;
struct ggml_tensor *layer_norm_1_bias;
struct ggml_tensor *layer_norm_2_weights;
struct ggml_tensor *layer_norm_2_bias;
// attention
struct ggml_tensor *c_attention_attention_weights;
struct ggml_tensor *c_attention_attention_bias;
struct ggml_tensor *c_attention_projection_weights;
struct ggml_tensor *c_attention_projection_bias;
// mlp
struct ggml_tensor *c_multi_layer_perceptron_fully_connected_weights;
struct ggml_tensor *c_multi_layer_perceptron_fully_connected_bias;
struct ggml_tensor *c_multi_layer_perceptron_projection_weights;
struct ggml_tensor *c_multi_layer_perceptron_projection_bias;
};
struct autoregressive_model {
struct ggml_tensor *embedding;
std::map<std::string, struct ggml_tensor *> tensors;
struct ggml_tensor *text_embedding_weights;
struct ggml_tensor *text_position_embedding_weights;
struct ggml_tensor *mel_embedding_weights;
struct ggml_tensor *mel_position_embedding_weights;
struct ggml_tensor *final_layer_norm_weights;
struct ggml_tensor *final_layer_norm_bias;
struct ggml_tensor *language_model_head_layer_norm_weights;
struct ggml_tensor *language_model_head_layer_norm_bias;
struct ggml_tensor *language_model_head_linear_weights;
struct ggml_tensor *language_model_head_linear_bias;
struct ggml_tensor *memory_key;
struct ggml_tensor *memory_value;
std::vector<gpt2_layer> layers;
struct ggml_context *ctx;
ggml_backend_buffer_t buffer_w;
ggml_backend_t backend = NULL;
};
// clang-format off
/*
██████╗ ██╗███████╗███████╗██╗ ██╗███████╗██╗ ██████╗ ███╗ ██╗
██╔══██╗██║██╔════╝██╔════╝██║ ██║██╔════╝██║██╔═══██╗████╗ ██║
██║ ██║██║█████╗ █████╗ ██║ ██║███████╗██║██║ ██║██╔██╗ ██║
██║ ██║██║██╔══╝ ██╔══╝ ██║ ██║╚════██║██║██║ ██║██║╚██╗██║
██████╔╝██║██║ ██║ ╚██████╔╝███████║██║╚██████╔╝██║ ╚████║
╚═════╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═══╝
████████╗███████╗███╗ ██╗███████╗ ██████╗ ██████╗
╚══██╔══╝██╔════╝████╗ ██║██╔════╝██╔═══██╗██╔══██╗
██║ █████╗ ██╔██╗ ██║███████╗██║ ██║██████╔╝
██║ ██╔══╝ ██║╚██╗██║╚════██║██║ ██║██╔══██╗
██║ ███████╗██║ ╚████║███████║╚██████╔╝██║ ██║
╚═╝ ╚══════╝╚═╝ ╚═══╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝
███╗ ███╗ █████╗ ███╗ ██╗██╗███████╗███████╗███████╗████████╗
████╗ ████║██╔══██╗████╗ ██║██║██╔════╝██╔════╝██╔════╝╚══██╔══╝
██╔████╔██║███████║██╔██╗ ██║██║█████╗ █████╗ ███████╗ ██║
██║╚██╔╝██║██╔══██║██║╚██╗██║██║██╔══╝ ██╔══╝ ╚════██║ ██║
██║ ╚═╝ ██║██║ ██║██║ ╚████║██║██║ ███████╗███████║ ██║
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝╚═╝ ╚══════╝╚══════╝ ╚═╝
*/
// clang-format on
struct latent_conditioner_attention_block {
struct ggml_tensor *norm_weight;
struct ggml_tensor *norm_bias;
struct ggml_tensor *qkv_weight;
struct ggml_tensor *qkv_bias;
struct ggml_tensor *projection_out_weight;
struct ggml_tensor *projection_out_bias;
struct ggml_tensor
*relative_position_embeddings_relative_attention_bias_weight;
};
struct residual_block {
struct ggml_tensor *in_layers_0_weight;
struct ggml_tensor *in_layers_0_bias;
struct ggml_tensor *in_layers_2_weight;
struct ggml_tensor *in_layers_2_bias;
struct ggml_tensor *emb_layers_1_weight;
struct ggml_tensor *emb_layers_1_bias;
struct ggml_tensor *out_layers_0_weight;
struct ggml_tensor *out_layers_0_bias;
struct ggml_tensor *out_layers_3_weight;
struct ggml_tensor *out_layers_3_bias;
};
struct diffusion_layer {
struct ggml_tensor *resblock_in_layers_0_weight;
struct ggml_tensor *resblock_in_layers_0_bias;
struct ggml_tensor *resblock_in_layers_2_weight;
struct ggml_tensor *resblock_in_layers_2_bias;
struct ggml_tensor *resblock_emb_layers_1_weight;
struct ggml_tensor *resblock_emb_layers_1_bias;
struct ggml_tensor *resblock_out_layers_0_weight;
struct ggml_tensor *resblock_out_layers_0_bias;
struct ggml_tensor *resblock_out_layers_3_weight;
struct ggml_tensor *resblock_out_layers_3_bias;
struct ggml_tensor *attn_norm_weight;
struct ggml_tensor *attn_norm_bias;
struct ggml_tensor *attn_qkv_weight;
struct ggml_tensor *attn_qkv_bias;
struct ggml_tensor *attn_proj_out_weight;
struct ggml_tensor *attn_proj_out_bias;
struct ggml_tensor
*attn_relative_pos_embeddings_relative_attention_bias_weight;
};
struct diffusion_model {
struct ggml_tensor *diffusion_conditioning_latent;
struct ggml_tensor *latent_conditioner_convolution_weight;
struct ggml_tensor *latent_conditioner_convolution_bias;
std::vector<latent_conditioner_attention_block>
latent_conditioner_attention_blocks;
struct ggml_tensor *code_norm_weight;
struct ggml_tensor *code_norm_bias;
struct ggml_tensor *time_emb_linear_0_weight;
struct ggml_tensor *time_emb_linear_0_bias;
struct ggml_tensor *time_emb_linear_1_weight;
struct ggml_tensor *time_emb_layer_norm_1_bias;
std::vector<diffusion_layer>
timestep_conditioning_integrator_diffusion_layers;
struct ggml_tensor *inp_block_weight;
struct ggml_tensor *inp_block_bias;
struct ggml_tensor *integrating_conv_weight;
struct ggml_tensor *integrating_conv_bias;
std::vector<diffusion_layer> main_diffusion_layers;
std::vector<residual_block> main_residual_blocks;
struct ggml_tensor *out_group_norm_weight;
struct ggml_tensor *out_group_norm_bias;
struct ggml_tensor *out_convolution_weight;
struct ggml_tensor *out_convolution_bias;
struct ggml_tensor *unconditioned_embedding;
std::map<std::string, struct ggml_tensor *> tensors;
struct ggml_context *ctx;
ggml_backend_buffer_t buffer_w;
ggml_backend_t backend = NULL;
};
// clang-format off
/*
██╗ ██╗ ██████╗ ██████╗ ██████╗ ██████╗ ███████╗██████╗
██║ ██║██╔═══██╗██╔════╝██╔═══██╗██╔══██╗██╔════╝██╔══██╗
██║ ██║██║ ██║██║ ██║ ██║██║ ██║█████╗ ██████╔╝
╚██╗ ██╔╝██║ ██║██║ ██║ ██║██║ ██║██╔══╝ ██╔══██╗
╚████╔╝ ╚██████╔╝╚██████╗╚██████╔╝██████╔╝███████╗██║ ██║
╚═══╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝
████████╗███████╗███╗ ██╗███████╗ ██████╗ ██████╗
╚══██╔══╝██╔════╝████╗ ██║██╔════╝██╔═══██╗██╔══██╗
██║ █████╗ ██╔██╗ ██║███████╗██║ ██║██████╔╝
██║ ██╔══╝ ██║╚██╗██║╚════██║██║ ██║██╔══██╗
██║ ███████╗██║ ╚████║███████║╚██████╔╝██║ ██║
╚═╝ ╚══════╝╚═╝ ╚═══╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝
███╗ ███╗ █████╗ ███╗ ██╗██╗███████╗███████╗███████╗████████╗
████╗ ████║██╔══██╗████╗ ██║██║██╔════╝██╔════╝██╔════╝╚══██╔══╝
██╔████╔██║███████║██╔██╗ ██║██║█████╗ █████╗ ███████╗ ██║
██║╚██╔╝██║██╔══██║██║╚██╗██║██║██╔══╝ ██╔══╝ ╚════██║ ██║
██║ ╚═╝ ██║██║ ██║██║ ╚████║██║██║ ███████╗███████║ ██║
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝╚═╝ ╚══════╝╚══════╝ ╚═╝
*/
// clang-format on
struct residual_conv_block {
struct ggml_tensor *residual_convs_1_bias;
struct ggml_tensor *residual_convs_1_weight;
struct ggml_tensor *residual_convs_3_bias;
struct ggml_tensor *residual_convs_3_weight;
};
struct conv_block {
struct ggml_tensor *conv_block_1_bias;
struct ggml_tensor *conv_block_1_weight;
};
struct vocoder_residual_block {
struct ggml_tensor *kernel_predictor_input_convolution_weight;
struct ggml_tensor *kernel_predictor_input_convolution_bias;
std::vector<residual_conv_block> kernel_predictor_residual_conv_blocks;
struct ggml_tensor *kernel_predictor_kernel_convolution_weight;
struct ggml_tensor *kernel_predictor_kernel_convolution_bias;
struct ggml_tensor *kernel_predictor_bias_convolution_weight;
struct ggml_tensor *kernel_predictor_bias_convolution_bias;
struct ggml_tensor *convolution_t_pre_weight;
struct ggml_tensor *convolution_t_pre_bias;
std::vector<conv_block> conv_blocks;
};
// model tether
struct vocoder_model {
struct ggml_tensor *convolution_pre_weight;
struct ggml_tensor *convolution_pre_bias;
std::vector<vocoder_residual_block> residual_stack;
struct ggml_tensor *convolution_post_weight;
struct ggml_tensor *convolution_post_bias;
std::map<std::string, struct ggml_tensor *> tensors;
struct ggml_context *ctx;
ggml_backend_buffer_t buffer_w;
ggml_backend_t backend = NULL;
};
void save_f32_tensor(ggml_tensor *tensor, std::string path_name) {
std::ofstream stream;
stream.open(path_name, std::ios::out | std::ios::binary);
int elements = tensor->ne[0] * tensor->ne[1] * tensor->ne[2] * tensor->ne[3];
std::vector<float> data_read(elements);
ggml_backend_tensor_get(tensor, data_read.data(), 0,
sizeof(float) * elements);
stream.write(reinterpret_cast<const char *>(data_read.data()),
elements * sizeof(float));
stream.close();
}
void compare_to_saved_tensor_with_name(ggml_tensor *tensor) {
std::string filename = "./logs/" + std::string(tensor->name) + ".txt";
int nBytes = tensor->ne[0] * tensor->ne[1] * tensor->ne[2] * tensor->ne[3] *
sizeof(float);
std::ifstream file(filename, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Error: Unable to open file " << filename << std::endl;
return;
}
// Calculate number of floats to read based on number of bytes
size_t numFloats = nBytes / sizeof(float);
std::vector<float> floats(numFloats);
// Read floats from file
file.read(reinterpret_cast<char *>(floats.data()), nBytes);
file.close();
std::vector<float> tensor_floats(numFloats);
ggml_backend_tensor_get(tensor, tensor_floats.data(), 0, nBytes);
std::cout << "starting comparison" << std::endl;
bool write_flag = true;
int lastIndex = -1;
for (int i = 0; i < floats.size(); i++) {
// std::cout << floats[i] << std::endl;
if (abs(floats[i] - tensor_floats[i]) > .01) {
if (write_flag) {
std::cout << i << ": " << floats[i] << "," << tensor_floats[i]
<< std::endl;
}
lastIndex = i;
write_flag = false;
}
}
if (lastIndex != -1) {
std::cout << "last index " << lastIndex << ": " << floats[lastIndex] << ","
<< tensor_floats[lastIndex] << std::endl;
}
std::cout << "done with comparison" << std::endl;
}
void extract_tensor_to_vector(ggml_tensor *tensor, std::vector<float> &vector) {
int elements = tensor->ne[0] * tensor->ne[1] * tensor->ne[2] * tensor->ne[3];
vector.resize(elements);
ggml_backend_tensor_get(tensor, vector.data(), 0, sizeof(float) * elements);
}
// clang-format off
/*
██████╗ ██████╗ ████████╗ ██████╗
██╔════╝ ██╔══██╗╚══██╔══╝ ╚════██╗
██║ ███╗██████╔╝ ██║█████╗ █████╔╝
██║ ██║██╔═══╝ ██║╚════╝██╔═══╝
╚██████╔╝██║ ██║ ███████╗
╚═════╝ ╚═╝ ╚═╝ ╚══════╝
████████╗███████╗███╗ ██╗███████╗ ██████╗ ██████╗
╚══██╔══╝██╔════╝████╗ ██║██╔════╝██╔═══██╗██╔══██╗
██║ █████╗ ██╔██╗ ██║███████╗██║ ██║██████╔╝
██║ ██╔══╝ ██║╚██╗██║╚════██║██║ ██║██╔══██╗
██║ ███████╗██║ ╚████║███████║╚██████╔╝██║ ██║
╚═╝ ╚══════╝╚═╝ ╚═══╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝
██╗ ██████╗ █████╗ ██████╗
██║ ██╔═══██╗██╔══██╗██╔══██╗
██║ ██║ ██║███████║██║ ██║
██║ ██║ ██║██╔══██║██║ ██║
███████╗╚██████╔╝██║ ██║██████╔╝
╚══════╝ ╚═════╝ ╚═╝ ╚═╝╚═════╝
*/
// clang-format on
// derived from gpt2_model_load(const std::string & fname, gpt2_model & model,
// gpt_vocab & vocab, int n_ctx, int n_gpu_layers) {
bool autoregressive_model_load(const std::string &fname,
autoregressive_model &model) {
printf("%s: loading model from '%s'\n", __func__, fname.c_str());
auto fin = std::ifstream(fname, std::ios::binary);
if (!fin) {
fprintf(stderr, "%s: failed to open '%s'\n", __func__, fname.c_str());
return false;
}
// verify magic
{
uint32_t magic;
fin.read((char *)&magic, sizeof(magic));
if (magic != GGML_FILE_MAGIC) {
fprintf(stderr, "%s: invalid model file '%s' (bad magic)\n", __func__,
fname.c_str());
return false;
}
}
// load hparams
/*
{
auto & hparams = model.hparams;
int32_t max_mel_tokens;
int32_t max_text_tokens;
int32_t max_conditioning_inputs;
int32_t layers;
int32_t model_dim;
int32_t heads;
int32_t number_text_tokens;
int32_t start_text_token;
int32_t num_embeddings;
fin.read((char *) &hparams.max_mel_tokens,
sizeof(hparams.max_mel_tokens)); fin.read((char *) &hparams.max_text_tokens,
sizeof(hparams.max_text_tokens)); fin.read((char *)
&hparams.max_conditioning_inputs, sizeof(hparams.max_conditioning_inputs));
fin.read((char *) &hparams.layers, sizeof(hparams.layers));
fin.read((char *) &hparams.model_dim, sizeof(hparams.model_dim));
fin.read((char *) &hparams.heads, sizeof(hparams.heads));
fin.read((char *) &hparams.number_text_tokens,
sizeof(hparams.number_text_tokens)); fin.read((char *)
&hparams.start_text_token, sizeof(hparams.start_text_token)); fin.read((char
*) &hparams.num_embeddings, sizeof(hparams.num_embeddings));
printf("%s: max_mel_tokens = %d\n", __func__, hparams.max_mel_tokens);
printf("%s: max_text_tokens = %d\n", __func__, hparams.max_text_tokens);
printf("%s: max_conditioning_inputs = %d\n", __func__,
hparams.max_conditioning_inputs); printf("%s: layers = %d\n", __func__,
hparams.layers); printf("%s: model_dim = %d\n", __func__, hparams.model_dim);
printf("%s: heads = %d\n", __func__, hparams.heads);
printf("%s: number_text_tokens = %d\n", __func__,
hparams.number_text_tokens); printf("%s: start_text_token = %d\n", __func__,
hparams.start_text_token); printf("%s: num_embeddings = %d\n", __func__,
hparams.num_embeddings);
}
*/
size_t buffer_size = 0;
buffer_size +=
256 * 1024 * ggml_type_sizef(GGML_TYPE_F32); // text embedding weights
buffer_size +=
404 * 1024 *
ggml_type_sizef(GGML_TYPE_F32); // text position embedding weights
buffer_size +=
1 * 1024 * ggml_type_sizef(GGML_TYPE_F32); // conditioning latent
buffer_size +=
8194 * 1024 * ggml_type_sizef(GGML_TYPE_F32); // mel embedding weight
buffer_size +=
608 * 1024 *
ggml_type_sizef(GGML_TYPE_F32); // mel position embedding weight
for (int i = 0; i < 30; i++) {
// todo fix this
buffer_size += 1024 * ggml_type_sizef(
GGML_TYPE_F32); // inference model linear 1 weight
buffer_size +=
1024 * ggml_type_sizef(GGML_TYPE_F32); // inference model linear 1 bias
buffer_size +=
1024 * 3072 *
ggml_type_sizef(GGML_TYPE_F32); // inference model attention weight
buffer_size +=
3072 * ggml_type_sizef(GGML_TYPE_F32); // inference model attention bias
buffer_size +=
1024 * 1024 *
ggml_type_sizef(
GGML_TYPE_F32); // inference model attention projection weight
buffer_size +=
1024 * ggml_type_sizef(
GGML_TYPE_F32); // inference model attention projection bias
buffer_size += 1024 * ggml_type_sizef(
GGML_TYPE_F32); // inference model linear 2 weight
buffer_size +=
1024 * ggml_type_sizef(GGML_TYPE_F32); // inference model linear 2 bias
buffer_size +=
1024 * 4096 *
ggml_type_sizef(GGML_TYPE_F32); // inference model multi layer
// perceptron fully connected weight
buffer_size += 4096 * ggml_type_sizef(
GGML_TYPE_F32); // inference model multi layer
// perceptron fully connected bais
buffer_size +=
4096 * 1024 *
ggml_type_sizef(GGML_TYPE_F32); // inference model multi layer
// perceptron projection weight
buffer_size +=
1024 * ggml_type_sizef(GGML_TYPE_F32); // inference model multi layer
// perceptron projection bais
}
buffer_size += 404 * 30 * ggml_type_sizef(GGML_TYPE_F32) * 1024 *
4; // key cache (memory_key)
buffer_size += 404 * 30 * ggml_type_sizef(GGML_TYPE_F32) * 1024 *
4; // value cache (memory_value)
buffer_size +=
1024 * ggml_type_sizef(GGML_TYPE_F32); // final layer norm weight
buffer_size += 1024 * ggml_type_sizef(GGML_TYPE_F32); // final layer norm bias
buffer_size +=
1024 *
ggml_type_sizef(GGML_TYPE_F32); // language model head layer norm weight
buffer_size +=
1024 *
ggml_type_sizef(GGML_TYPE_F32); // language model head layer norm bias
buffer_size +=
1024 * 8194 *
ggml_type_sizef(GGML_TYPE_F32); // language model head linear weight
buffer_size +=
8194 * ggml_type_sizef(GGML_TYPE_F32); // language model head linear bias
printf("%s: ggml tensor size = %d bytes\n", __func__,
(int)sizeof(ggml_tensor));
printf("%s: backend buffer size = %6.2f MB\n", __func__,
buffer_size / (1024.0 * 1024.0));
struct ggml_init_params params = {
/*.mem_size =*/ggml_tensor_overhead() * (size_t)(5 + 12 * 30 + 8),
/*.mem_buffer =*/NULL,
/*.no_alloc =*/true,
};
model.ctx = ggml_init(params);
if (!model.ctx) {
fprintf(stderr, "%s: ggml_init() failed\n", __func__);
return false;
}
// initialize the backend
#ifdef GGML_USE_CUBLAS
fprintf(stderr, "%s: using CUDA backend\n", __func__);
model.backend = ggml_backend_cuda_init(0);
if (!model.backend) {
fprintf(stderr, "%s: ggml_backend_cuda_init() failed\n", __func__);
}
#endif
#ifdef GGML_USE_METAL
fprintf(stderr, "%s: using Metal backend\n", __func__);
// ggml_metal_log_set_callback(ggml_log_callback_default, nullptr);
model.backend = ggml_backend_metal_init();
if (!model.backend) {
fprintf(stderr, "%s: ggml_backend_metal_init() failed\n", __func__);
}
#endif
if (!model.backend) {
// fallback to CPU backend
fprintf(stderr, "%s: using CPU backend\n", __func__);
model.backend = ggml_backend_cpu_init();
}
if (!model.backend) {
fprintf(stderr, "%s: ggml_backend_cpu_init() failed\n", __func__);
return false;
}
// model.buffer_w = ggml_backend_alloc_buffer(model.backend, buffer_size);
auto &ctx = model.ctx;
model.text_embedding_weights =
ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 1024, 256);
model.text_position_embedding_weights =
ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 1024, 404);
model.mel_embedding_weights =
ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 1024, 8194);
model.mel_position_embedding_weights =
ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 1024, 608);
model.final_layer_norm_weights = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1024);
model.final_layer_norm_bias = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1024);
model.language_model_head_layer_norm_weights =
ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1024);
model.language_model_head_layer_norm_bias =
ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1024);
model.language_model_head_linear_weights =
ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 1024, 8194);
model.language_model_head_linear_bias =
ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 8194);
model.layers.resize(30);
for (int i = 0; i < 30; i++) {
auto &layer = model.layers[i];
layer.layer_norm_1_weights = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1024);
layer.layer_norm_1_bias = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1024);
layer.c_attention_attention_weights =
ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 3072, 1024);
layer.c_attention_attention_bias =
ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 3072);
layer.c_attention_projection_weights =
ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 1024, 1024);
layer.c_attention_projection_bias =
ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1024);
layer.layer_norm_2_weights = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1024);
layer.layer_norm_2_bias = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1024);
layer.c_multi_layer_perceptron_fully_connected_weights =
ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 4096, 1024);
layer.c_multi_layer_perceptron_fully_connected_bias =
ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 4096);
layer.c_multi_layer_perceptron_projection_weights =
ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 1024, 4096);
layer.c_multi_layer_perceptron_projection_bias =
ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1024);
model.tensors["inference_model.transformer.h." + std::to_string(i) +
".ln_1.weight"] = layer.layer_norm_1_weights;
model.tensors["inference_model.transformer.h." + std::to_string(i) +
".ln_1.bias"] = layer.layer_norm_1_bias;
model.tensors["inference_model.transformer.h." + std::to_string(i) +
".attn.c_attn.weight"] = layer.c_attention_attention_weights;
model.tensors["inference_model.transformer.h." + std::to_string(i) +
".attn.c_attn.bias"] = layer.c_attention_attention_bias;
model.tensors["inference_model.transformer.h." + std::to_string(i) +
".attn.c_proj.weight"] = layer.c_attention_projection_weights;
model.tensors["inference_model.transformer.h." + std::to_string(i) +
".attn.c_proj.bias"] = layer.c_attention_projection_bias;
model.tensors["inference_model.transformer.h." + std::to_string(i) +
".ln_2.weight"] = layer.layer_norm_2_weights;
model.tensors["inference_model.transformer.h." + std::to_string(i) +
".ln_2.bias"] = layer.layer_norm_2_bias;
model.tensors["inference_model.transformer.h." + std::to_string(i) +
".mlp.c_fc.weight"] =
layer.c_multi_layer_perceptron_fully_connected_weights;
model.tensors["inference_model.transformer.h." + std::to_string(i) +
".mlp.c_fc.bias"] =
layer.c_multi_layer_perceptron_fully_connected_bias;
model.tensors["inference_model.transformer.h." + std::to_string(i) +
".mlp.c_proj.weight"] =
layer.c_multi_layer_perceptron_projection_weights;
model.tensors["inference_model.transformer.h." + std::to_string(i) +
".mlp.c_proj.bias"] =
layer.c_multi_layer_perceptron_projection_bias;
}
// model.init_conv_bias = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 1,1024);
// model.init_conv_weights = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 80,1024);
// model.tensors["conditioning_encoder.init.bias"] = model.init_conv_bias;
// model.tensors["conditioning_encoder.init.weight"] =
// model.init_conv_weights;
model.tensors["inference_model.lm_head.0.weight"] =
model.language_model_head_layer_norm_weights;
model.tensors["inference_model.lm_head.0.bias"] =
model.language_model_head_layer_norm_bias;
model.tensors["inference_model.lm_head.1.weight"] =
model.language_model_head_linear_weights;
model.tensors["inference_model.lm_head.1.bias"] =
model.language_model_head_linear_bias;
model.tensors["inference_model.transformer.ln_f.weight"] =
model.final_layer_norm_weights;
model.tensors["inference_model.transformer.ln_f.bias"] =
model.final_layer_norm_bias;
model.tensors["text_embedding.weight"] = model.text_embedding_weights;
model.tensors["text_pos_embedding.emb.weight"] =
model.text_position_embedding_weights;
model.tensors["mel_embedding.weight"] = model.mel_embedding_weights;
model.tensors["mel_pos_embedding.emb.weight"] =
model.mel_position_embedding_weights;
model.memory_key =
ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 4 * 404 * 30 * 1024);
model.memory_value =
ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 4 * 404 * 30 * 1024);
// ggml_allocr * alloc = ggml_allocr_new_from_buffer(model.buffer_w);
model.buffer_w = ggml_backend_alloc_ctx_tensors(ctx, model.backend);
// load weights
{
size_t total_size = 0;
bool has_lm_head = false;
std::vector<char> read_buf;
while (true) {
int32_t n_dims;
int32_t length;
int32_t ttype;
fin.read(reinterpret_cast<char *>(&n_dims), sizeof(n_dims));
fin.read(reinterpret_cast<char *>(&length), sizeof(length));
fin.read(reinterpret_cast<char *>(&ttype), sizeof(ttype));
if (fin.eof()) {
break;
}
int32_t nelements = 1;
int32_t ne[4] = {1, 1, 1, 1};
for (int i = 0; i < n_dims; ++i) {
fin.read(reinterpret_cast<char *>(&ne[i]), sizeof(ne[i]));
nelements *= ne[i];
}
std::string name(length, 0);
fin.read(&name[0], length);
if (model.tensors.find(name) == model.tensors.end()) {
fprintf(stderr, "%s: unknown tensor '%s' in model file\n", __func__,
name.c_str());
return false;
}
auto tensor = model.tensors[name];
ggml_set_name(tensor, name.c_str());
if (ggml_nelements(tensor) != nelements) {
fprintf(stderr, "%s: tensor '%s' has wrong size in model file\n",
__func__, name.c_str());
return false;
}
if (tensor->ne[0] != ne[0] || tensor->ne[1] != ne[1]) {
fprintf(stderr,
"%s: tensor '%s' has wrong shape in model file: got [%d, %d], "
"expected [%d, %d]\n",
__func__, name.c_str(), (int)tensor->ne[0], (int)tensor->ne[1],
ne[0], ne[1]);
return false;
}
// for debugging
if (0) {
printf("%24s - [%5d, %5d], type = %6s, %6.2f MB, %9zu bytes\n",
name.c_str(), ne[0], ne[1], ggml_type_name(ggml_type(ttype)),
ggml_nbytes(tensor) / 1024.0 / 1024.0, ggml_nbytes(tensor));
}
const size_t bpe = ggml_type_size(ggml_type(ttype));
if ((nelements * bpe) / ggml_blck_size(tensor->type) !=
ggml_nbytes(tensor)) {
fprintf(stderr,
"%s: tensor '%s' has wrong size in model file: got %zu, "
"expected %zu\n",
__func__, name.c_str(), ggml_nbytes(tensor), nelements * bpe);
return false;
}
if (ggml_backend_buffer_is_host(model.buffer_w)) {
// for some backends such as CPU and Metal, the tensor data is in system
// memory and we can read directly into it
fin.read(reinterpret_cast<char *>(tensor->data), ggml_nbytes(tensor));
} else {
// read into a temporary buffer first, then copy to device memory
read_buf.resize(ggml_nbytes(tensor));
fin.read(read_buf.data(), ggml_nbytes(tensor));
ggml_backend_tensor_set(tensor, read_buf.data(), 0,
ggml_nbytes(tensor));
}
total_size += ggml_nbytes(tensor);
}
printf("%s: model size = %8.2f MB\n", __func__,
total_size / 1024.0 / 1024.0);
}
fin.close();
return true;
}
// clang-format off
/*
██████╗ ██╗███████╗███████╗██╗ ██╗███████╗██╗ ██████╗ ███╗ ██╗
██╔══██╗██║██╔════╝██╔════╝██║ ██║██╔════╝██║██╔═══██╗████╗ ██║
██║ ██║██║█████╗ █████╗ ██║ ██║███████╗██║██║ ██║██╔██╗ ██║
██║ ██║██║██╔══╝ ██╔══╝ ██║ ██║╚════██║██║██║ ██║██║╚██╗██║
██████╔╝██║██║ ██║ ╚██████╔╝███████║██║╚██████╔╝██║ ╚████║
╚═════╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═══╝
████████╗███████╗███╗ ██╗███████╗ ██████╗ ██████╗
╚══██╔══╝██╔════╝████╗ ██║██╔════╝██╔═══██╗██╔══██╗
██║ █████╗ ██╔██╗ ██║███████╗██║ ██║██████╔╝
██║ ██╔══╝ ██║╚██╗██║╚════██║██║ ██║██╔══██╗
██║ ███████╗██║ ╚████║███████║╚██████╔╝██║ ██║
╚═╝ ╚══════╝╚═╝ ╚═══╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝
██╗ ██████╗ █████╗ ██████╗
██║ ██╔═══██╗██╔══██╗██╔══██╗
██║ ██║ ██║███████║██║ ██║
██║ ██║ ██║██╔══██║██║ ██║
███████╗╚██████╔╝██║ ██║██████╔╝
╚══════╝ ╚═════╝ ╚═╝ ╚═╝╚═════╝
*/
// clang-format on
// derived from gpt2_model_load(const std::string & fname, gpt2_model & model,
// gpt_vocab & vocab, int n_ctx, int n_gpu_layers) {
bool diffusion_model_load(const std::string &fname, diffusion_model &model) {
printf("%s: loading model from '%s'\n", __func__, fname.c_str());
auto fin = std::ifstream(fname, std::ios::binary);
if (!fin) {
fprintf(stderr, "%s: failed to open '%s'\n", __func__, fname.c_str());
return false;
}
// verify magic
{
uint32_t magic;
fin.read((char *)&magic, sizeof(magic));
if (magic != GGML_FILE_MAGIC) {
fprintf(stderr, "%s: invalid model file '%s' (bad magic)\n", __func__,
fname.c_str());
return false;
}
}
size_t buffer_size = 0;
buffer_size +=
1 * 2048 * ggml_type_sizef(GGML_TYPE_F32); // conditioning latent
buffer_size += 1024 * 1024 * 3 *
ggml_type_sizef(GGML_TYPE_F32); // latent conditioning weight
buffer_size +=
1024 * ggml_type_sizef(GGML_TYPE_F32); // latent conditioning bias
for (int i = 0; i < 4; i++) {
buffer_size +=
1024 *
ggml_type_sizef(
GGML_TYPE_F32); // latent conditioner attention block norm weight
buffer_size +=
1024 *
ggml_type_sizef(
GGML_TYPE_F32); // latent conditioner attention block norm bias
buffer_size +=
3072 * 1024 *
ggml_type_sizef(
GGML_TYPE_F32); // latent conditioner key value query weight
buffer_size +=
3072 * ggml_type_sizef(
GGML_TYPE_F32); // latent conditioner key value query bias
buffer_size +=
1024 * 1024 *
ggml_type_sizef(
GGML_TYPE_F32); // latent conditioner projection out weight
buffer_size +=
1024 * ggml_type_sizef(
GGML_TYPE_F32); // latent conditioner projection out bias
buffer_size +=
16 * 32 *
ggml_type_sizef(
GGML_TYPE_F32); // latent conditioner relative position embeddings
// relative attention bias weight
}
buffer_size += 1024 * ggml_type_sizef(GGML_TYPE_F32); // code norm weight
buffer_size += 1024 * ggml_type_sizef(GGML_TYPE_F32); // code norm bias
buffer_size += 1024 * 1024 *
ggml_type_sizef(GGML_TYPE_F32); // time embed linear 0 weight
buffer_size +=
1024 * ggml_type_sizef(GGML_TYPE_F32); // time embed linear 0 bias
buffer_size += 1024 * 1024 *
ggml_type_sizef(GGML_TYPE_F32); // time embed linear 1 weight