Skip to content
Navigation Menu
{{ message }}
This repository was archived by the owner on Mar 10, 2024. It is now read-only.
forked from iamlemec/bert.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbert.cpp
More file actions
1008 lines (852 loc) · 36.4 KB
/
Copy pathbert.cpp
File metadata and controls
1008 lines (852 loc) · 36.4 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
/*
BERT inference in GGML
Forked with gratitude from:
https://github.com/skeskinen/bert.cpp
https://github.com/xyzhang626/embeddings.cpp
*/
#include "bert.h"
#include "ggml.h"
#ifdef GGML_USE_CUBLAS
#include "ggml-cuda.h"
#endif
#ifdef GGML_USE_METAL
#include "ggml-metal.h"
#endif
#include <cmath>
#include <fstream>
#include <map>
#include <string>
#include <vector>
#include <cstring>
#include <algorithm>
#define BERT_MAX_NODES 4096
const int verbosity = 1;
//
// utilities to get data from a gguf file
//
static int get_key_idx(const gguf_context * ctx, const char * key) {
int i = gguf_find_key(ctx, key);
if (i == -1) {
fprintf(stderr, "%s: key %s not found in file\n", __func__, key);
throw;
}
return i;
}
static int32_t get_i32(const gguf_context * ctx, const std::string & key) {
const int i = get_key_idx(ctx, key.c_str());
return gguf_get_val_i32(ctx, i);
}
static uint32_t get_u32(const gguf_context * ctx, const std::string & key) {
const int i = get_key_idx(ctx, key.c_str());
return gguf_get_val_u32(ctx, i);
}
static float get_f32(const gguf_context * ctx, const std::string & key) {
const int i = get_key_idx(ctx, key.c_str());
return gguf_get_val_f32(ctx, i);
}
static std::string get_str(const gguf_context * ctx, const std::string & key, const std::string & def = "") {
const int i = gguf_find_key(ctx, key.c_str());
if (i == -1) {
return def;
}
return gguf_get_val_str(ctx, i);
}
static struct ggml_tensor * get_tensor(struct ggml_context * ctx, const std::string & name) {
struct ggml_tensor * cur = ggml_get_tensor(ctx, name.c_str());
if (!cur) {
fprintf(stderr, "%s: unable to find tensor %s\n", __func__, name.c_str());
throw;
}
return cur;
}
static std::string get_ftype(int ftype) {
return ggml_type_name(static_cast<ggml_type>(ftype));
}
static void tensor_stats(ggml_tensor * t) {
int32_t src0 = t->src[0] ? t->src[0]->backend : -1;
int32_t src1 = t->src[1] ? t->src[1]->backend : -1;
fprintf(stderr,
"type = %s, dims = %d, shape = (%ld, %ld, %ld, %ld), backend = %d, src0 = %d, src1 = %d\n",
ggml_type_name(t->type), ggml_n_dims(t), t->ne[0], t->ne[1], t->ne[2], t->ne[3], t->backend, src0, src1
);
}
//
// tokenizing
//
static size_t utf8_len(char src) {
const size_t lookup[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4};
uint8_t highbits = static_cast<uint8_t>(src) >> 4;
return lookup[highbits];
}
std::string strip_accents(const std::string &inputString) {
std::string resultString;
std::map<std::string, char> accentMap = {{"À", 'A'},{"Á", 'A'},
{"Â", 'A'},{"Ã", 'A'},{"Ä", 'A'},{"Å", 'A'},{"à", 'a'},{"á", 'a'},
{"â", 'a'},{"ã", 'a'},{"ä", 'a'},{"å", 'a'},{"È", 'E'},{"É", 'E'},
{"Ê", 'E'},{"Ë", 'E'},{"è", 'e'},{"é", 'e'},{"ê", 'e'},{"ë", 'e'},
{"Ì", 'I'},{"Í", 'I'},{"Î", 'I'},{"Ï", 'I'},{"ì", 'i'},{"í", 'i'},
{"î", 'i'},{"ï", 'i'},{"Ò", 'O'},{"Ó", 'O'},{"Ô", 'O'},{"Õ", 'O'},
{"Ö", 'O'},{"ò", 'o'},{"ó", 'o'},{"ô", 'o'},{"õ", 'o'},{"ö", 'o'},
{"Ù", 'U'},{"Ú", 'U'},{"Û", 'U'},{"Ü", 'U'},{"ù", 'u'},{"ú", 'u'},
{"û", 'u'},{"ü", 'u'},{"Ý", 'Y'},{"ý", 'y'},{"Ç", 'C'},{"ç", 'c'},
{"Ñ", 'N'},{"ñ", 'n'},
};
for (size_t i = 0; i < inputString.length();)
{
int len = utf8_len(inputString[i]);
std::string curChar = inputString.substr(i, len);
auto iter = accentMap.find(curChar);
if (iter != accentMap.end())
{
resultString += iter->second;
}
else
{
resultString += curChar;
}
i += len;
}
return resultString;
}
std::string bert_normalize_prompt(const std::string &text) {
// TODO: handle chinese characters? https://github.com/huggingface/tokenizers/blob/ef5f50605ddf9f8caef1598c0e4853862b9707a7/tokenizers/src/normalizers/bert.rs#L98
std::string text2 = strip_accents(text);
for (size_t i = 0; i < text2.size(); i += utf8_len(text2[i]))
{
char c = text2[i];
if (c >= 'A' && c <= 'Z')
text2[i] = c - 'A' + 'a';
}
return text2;
}
bool is_chinese_char(const std::string& str) {
int len = str.length();
unsigned int codepoint = 0;
int num_bytes = 0;
int i = 0;
unsigned char ch = static_cast<unsigned char>(str[i]);
if (ch <= 0x7f) {
codepoint = ch;
num_bytes = 1;
} else if ((ch >> 5) == 0x06) {
codepoint = ch & 0x1f;
num_bytes = 2;
} else if ((ch >> 4) == 0x0e) {
codepoint = ch & 0x0f;
num_bytes = 3;
} else if ((ch >> 3) == 0x1e) {
codepoint = ch & 0x07;
num_bytes = 4;
}
for (int j = 1; j < num_bytes; ++j) {
if (i + j >= len) {
return false; // incomplete UTF-8 character
}
unsigned char next_ch = static_cast<unsigned char>(str[i + j]);
if ((next_ch >> 6) != 0x02) {
return false; // invalid trailing byte
}
codepoint = (codepoint << 6) | (next_ch & 0x3f);
}
if ((codepoint >= 0x4E00 && codepoint <= 0x9FFF) ||
(codepoint >= 0x3400 && codepoint <= 0x4DBF) ||
(codepoint >= 0x20000 && codepoint <= 0x2A6DF) ||
(codepoint >= 0x2A700 && codepoint <= 0x2B73F) ||
(codepoint >= 0x2B740 && codepoint <= 0x2B81F) ||
(codepoint >= 0x2B920 && codepoint <= 0x2CEAF) || // this should be 0x2B820 but in hf rust code it is 0x2B920
(codepoint >= 0xF900 && codepoint <= 0xFAFF) ||
(codepoint >= 0x2F800 && codepoint <= 0x2FA1F) ||
(codepoint >= 0x3000 && codepoint <= 0x303F) ||
(codepoint >= 0xFF00 && codepoint <= 0xFFEF)) {
return true;
}
return false;
}
const char* bert_vocab_id_to_token(bert_ctx * ctx, bert_token id) {
bert_vocab & vocab = ctx->vocab;
auto it = vocab._id_to_token.find(id);
if (it != vocab._id_to_token.end()) {
return it->second.c_str();
}
it = vocab._id_to_subword_token.find(id);
if (it != vocab._id_to_subword_token.end()) {
return it->second.c_str();
}
return "[UNK]";
}
bert_tokens bert_tokenize(struct bert_ctx * ctx, bert_string text, uint64_t n_max_tokens) {
const bert_vocab &vocab = ctx->vocab;
const bert_token bos_id = vocab.bos_id;
const bert_token eos_id = vocab.eos_id;
const bert_token unk_id = vocab.unk_id;
std::string ori_str = text;
ori_str = bert_normalize_prompt(ori_str);
uint64_t ori_size = ori_str.size();
// single punct / single symbol / single digit
// baseline: add whitespace on the left and right of punct and chinese characters
std::vector<std::string> words;
std::string new_str = "";
uint64_t i = 0;
while (i < ori_size) {
int utf_char_len = utf8_len(ori_str[i]);
if ((utf_char_len == 1) && ispunct(ori_str[i])) {
new_str += " ";
new_str += ori_str[i];
new_str += " ";
i += 1;
}
else if ((utf_char_len == 3) && is_chinese_char(ori_str.substr(i, 3))) {
new_str += " ";
new_str += ori_str.substr(i, 3);
new_str += " ";
i += 3;
}
else {
new_str += ori_str[i];
i += 1;
}
}
// split by whitespace
uint64_t l = 0;
uint64_t r = 0;
while (r < new_str.size()) {
// if is whitespace
if (isspace(new_str[r])) {
if (r > l) words.push_back(new_str.substr(l, (r - l)));
l = r + 1;
r = l;
}
else {
r += 1;
}
}
if (r > l) {
words.push_back(new_str.substr(l, (r - l)));
}
// start with a cls token
bert_tokens tokens;
tokens.push_back(bos_id);
// find the longest tokens that form the words
for (const std::string &word : words) {
// skip empty words
int n = word.size();
if (n == 0) continue;
// we're at the start of a new word
int i = 0;
bool match = false;
auto * token_map = &vocab.token_to_id;
loop:
// check for max tokens
if (tokens.size() >= n_max_tokens - 1) {
break;
}
// move through character position in word
while (i < n) {
// loop through possible match length
for (int j = n; j > i; j--) {
auto it = token_map->find(word.substr(i, j - i));
if (it != token_map->end()) {
tokens.push_back(it->second);
match = true;
i = j;
token_map = &vocab.subword_token_to_id;
goto loop;
}
}
// we didn't find a match at this length
// fprintf(stderr, "%s: unknown token '%s'\n", __func__, word.substr(i, 1).data());
token_map = &vocab.subword_token_to_id;
i++;
}
// we didn't find any matches for this word
if (!match) {
// fprintf(stderr, "%s: unknown token '%s'\n", __func__, word.data());
tokens.push_back(unk_id);
}
}
// append terminate token
tokens.push_back(eos_id);
// return tokens
return tokens;
}
bert_string bert_detokenize(struct bert_ctx * ctx, bert_tokens tokens, bool debug = false) {
const bert_token bos_id = ctx->vocab.bos_id;
const bert_token eos_id = ctx->vocab.eos_id;
const std::string word_prefix = ctx->vocab.word_prefix;
const std::string subword_prefix = ctx->vocab.subword_prefix;
const uint32_t word_prefix_len = word_prefix.size();
const uint32_t subword_prefix_len = subword_prefix.size();
bert_string str = "";
for (const uint64_t &t : tokens) {
std::string token = bert_vocab_id_to_token(ctx, t);
bool subword = (
(subword_prefix_len > 0 && token.find(subword_prefix) == 0) ||
(word_prefix_len > 0 && token.find(word_prefix) != 0)
);
if (debug) {
if ((str.size() > 0) && !subword) {
str += " ";
}
str += token;
} else {
if (t == bos_id || t == eos_id) {
continue;
}
if (subword) {
str += token.substr(subword_prefix_len);
} else {
if (str.size() > 0) {
str += " ";
}
str += token.substr(word_prefix_len);
}
}
}
return str;
}
uint64_t bert_detokenize_c(struct bert_ctx * ctx, int32_t * tokens, char * output, uint64_t n_tokens, uint64_t n_output, bool debug) {
bert_tokens tokens2(tokens, tokens + n_tokens);
bert_string str = bert_detokenize(ctx, tokens2, debug);
memcpy(output, str.c_str(), std::min(n_output, str.size()));
return str.size();
}
// c-string interface to tokenizer
uint64_t bert_tokenize_c(struct bert_ctx * ctx, const char * text, int32_t * output, uint64_t n_max_tokens) {
bert_string str(text);
bert_tokens tokens = bert_tokenize(ctx, str, n_max_tokens);
memcpy(output, tokens.data(), tokens.size() * sizeof(int32_t));
return tokens.size();
}
//
// bert model
//
int32_t bert_n_embd(bert_ctx * ctx) {
return ctx->model.hparams.n_embd;
}
int32_t bert_n_max_tokens(bert_ctx * ctx) {
return ctx->model.hparams.n_max_tokens;
}
//
// loading and setup
//
struct bert_ctx * bert_load_from_file(const char *fname, bool use_cpu) {
struct ggml_context * ctx_ggml = NULL;
struct gguf_init_params gguf_params = {
/*.no_alloc = */ true,
/*.ctx = */ &ctx_ggml,
};
// open gguf file
struct gguf_context * ctx_gguf = gguf_init_from_file(fname, gguf_params);
if (!ctx_gguf) {
fprintf(stderr, "%s: failed to load BERT model from %s. Does this file exist?\n", __func__, fname);
return nullptr;
}
// get generic model info
if (verbosity >= 1) {
const int n_tensors = gguf_get_n_tensors(ctx_gguf);
const int n_kv = gguf_get_n_kv(ctx_gguf);
const int ftype = get_u32(ctx_gguf, KEY_FTYPE);
const int alignment = gguf_get_alignment(ctx_gguf);
const int version = gguf_get_version(ctx_gguf);
const std::string ftype_str = get_ftype(ftype);
const std::string description = get_str(ctx_gguf, KEY_DESCRIPTION);
const std::string name = get_str(ctx_gguf, KEY_NAME);
fprintf(stderr, "\n");
fprintf(stderr, "%s: GGUF\n", __func__);
fprintf(stderr, "%s: model name: %s\n", __func__, name.c_str());
fprintf(stderr, "%s: description: %s\n", __func__, description.c_str());
fprintf(stderr, "%s: GGUF version: %d\n", __func__, version);
fprintf(stderr, "%s: alignment: %d\n", __func__, alignment);
fprintf(stderr, "%s: n_tensors: %d\n", __func__, n_tensors);
fprintf(stderr, "%s: n_kv: %d\n", __func__, n_kv);
fprintf(stderr, "%s: ftype: %s\n", __func__, ftype_str.c_str());
fprintf(stderr, "\n");
}
const int n_tensors = gguf_get_n_tensors(ctx_gguf);
// create model object
bert_ctx * new_bert = new bert_ctx;
bert_model & model = new_bert->model;
bert_vocab & vocab = new_bert->vocab;
bert_hparams & hparams = model.hparams;
// load hparams
{
hparams.n_vocab = get_u32(ctx_gguf, "vocab_size");
hparams.n_max_tokens = get_u32(ctx_gguf, "max_position_embedding");
hparams.n_embd = get_u32(ctx_gguf, "hidden_size");
hparams.n_intermediate = get_u32(ctx_gguf, "intermediate_size");
hparams.n_head = get_u32(ctx_gguf, "num_attention_heads");
hparams.n_layer = get_u32(ctx_gguf, "num_hidden_layers");
hparams.layer_norm_eps = get_f32(ctx_gguf, "layer_norm_eps");
if (verbosity >= 1) {
fprintf(stderr, "%s: MODEL\n", __func__);
fprintf(stderr, "%s: n_vocab = %d\n", __func__, hparams.n_vocab);
fprintf(stderr, "%s: n_max_tokens = %d\n", __func__, hparams.n_max_tokens);
fprintf(stderr, "%s: n_embd = %d\n", __func__, hparams.n_embd);
fprintf(stderr, "%s: n_intermediate = %d\n", __func__, hparams.n_intermediate);
fprintf(stderr, "%s: n_head = %d\n", __func__, hparams.n_head);
fprintf(stderr, "%s: n_layer = %d\n", __func__, hparams.n_layer);
fprintf(stderr, "%s: layer_norm_eps = %g\n", __func__, hparams.layer_norm_eps);
fprintf(stderr, "\n");
}
}
// load vocab
{
vocab.pad_id = get_i32(ctx_gguf, KEY_PAD_ID);
vocab.unk_id = get_i32(ctx_gguf, KEY_UNK_ID);
vocab.bos_id = get_i32(ctx_gguf, KEY_BOS_ID);
vocab.eos_id = get_i32(ctx_gguf, KEY_EOS_ID);
vocab.word_prefix = get_str(ctx_gguf, KEY_WORD_PREFIX);
vocab.subword_prefix = get_str(ctx_gguf, KEY_SUBWORD_PREFIX);
uint32_t word_prefix_len = vocab.word_prefix.size();
uint32_t subword_prefix_len = vocab.subword_prefix.size();
const int token_idx = gguf_find_key(ctx_gguf, KEY_TOKEN_LIST);
const int n_vocab = gguf_get_arr_n(ctx_gguf, token_idx);
for (int i = 0; i < n_vocab; i++) {
std::string word = gguf_get_arr_str(ctx_gguf, token_idx, i);
vocab.tokens.push_back(word);
bool subword = (
(subword_prefix_len > 0 && word.find(vocab.subword_prefix) == 0) ||
(word_prefix_len > 0 && word.find(vocab.word_prefix) != 0)
);
if (subword) {
vocab.subword_token_to_id[word.substr(subword_prefix_len)] = i;
vocab._id_to_subword_token[i] = word;
} else {
vocab.token_to_id[word.substr(word_prefix_len)] = i;
vocab._id_to_token[i] = word;
}
}
if (verbosity >= 1) {
fprintf(stderr, "%s: TOKENIZER\n", __func__);
fprintf(stderr, "%s: vocab size: %d\n", __func__, n_vocab);
fprintf(stderr, "%s: word_prefix: %s\n", __func__, vocab.word_prefix.c_str());
fprintf(stderr, "%s: subword_prefix: %s\n", __func__, vocab.subword_prefix.c_str());
fprintf(stderr, "%s: pad_id = %d\n", __func__, vocab.pad_id);
fprintf(stderr, "%s: unk_id = %d\n", __func__, vocab.unk_id);
fprintf(stderr, "%s: bos_id = %d\n", __func__, vocab.bos_id);
fprintf(stderr, "%s: eos_id = %d\n", __func__, vocab.eos_id);
fprintf(stderr, "\n");
}
}
// model tensor sizing
size_t buffer_size = 32*1024; // need some extra room??
{
for (int i = 0; i < n_tensors; ++i) {
const char * name = gguf_get_tensor_name(ctx_gguf, i);
const size_t offset = gguf_get_tensor_offset(ctx_gguf, i);
struct ggml_tensor * cur = ggml_get_tensor(ctx_ggml, name);
size_t tensor_size = ggml_nbytes(cur);
buffer_size += tensor_size;
if (verbosity >= 2) {
fprintf(stderr, "%s: tensor[%d]: type = %s, n_dims = %d, name = %s, offset=%zu, type=%d\n", __func__, i,
ggml_type_name(cur->type), ggml_n_dims(cur), cur->name, offset, cur->type);
}
}
}
// initialize advanced backend
#ifdef GGML_USE_CUBLAS
if (!use_cpu) {
new_bert->backend = ggml_backend_cuda_init(0);
if (!new_bert->backend) {
fprintf(stderr, "%s: ggml_backend_cuda_init() failed\n", __func__);
} else {
fprintf(stderr, "%s: using CUDA backend\n", __func__);
}
}
#endif
#ifdef GGML_USE_METAL
if (!use_cpu) {
new_bert->backend = ggml_backend_metal_init();
if (!new_bert->backend) {
fprintf(stderr, "%s: ggml_backend_metal_init() failed\n", __func__);
} else {
fprintf(stderr, "%s: using Metal backend\n", __func__);
}
}
#endif
// fall back to CPU backend
if (!new_bert->backend) {
new_bert->backend = ggml_backend_cpu_init();
fprintf(stderr, "%s: using CPU backend\n", __func__);
}
// load tensors
{
// host buffer for CUDA loading
std::vector<uint8_t> read_buf;
// context params for tensors
struct ggml_init_params ggml_params = {
/*.mem_size =*/ (n_tensors + 1) * ggml_tensor_overhead(),
/*.mem_buffer =*/ NULL,
/*.no_alloc =*/ true,
};
// create context for tensors
new_bert->ctx_data = ggml_init(ggml_params);
if (!new_bert->ctx_data) {
fprintf(stderr, "%s: ggml_init() failed\n", __func__);
delete new_bert;
return nullptr;
}
// open model gguf file
auto fin = std::ifstream(fname, std::ios::binary);
if (!fin) {
fprintf(stderr, "cannot open model file for loading tensors\n");
delete new_bert;
return nullptr;
}
// add tensors to our context
for (int i = 0; i < n_tensors; ++i) {
const char * name = gguf_get_tensor_name(ctx_gguf, i);
struct ggml_tensor * ten = ggml_get_tensor(ctx_ggml, name);
struct ggml_tensor * cur = ggml_dup_tensor(new_bert->ctx_data, ten);
ggml_set_name(cur, name);
}
// create params buffer and allocr
new_bert->weights_buffer = ggml_backend_alloc_buffer(new_bert->backend, buffer_size);
ggml_allocr * alloc = ggml_allocr_new_from_buffer(new_bert->weights_buffer);
// loop over tensors and load in
for (int i = 0; i < n_tensors; ++i) {
// do the actual allocation on the backend
const char * name = gguf_get_tensor_name(ctx_gguf, i);
struct ggml_tensor * cur = ggml_get_tensor(new_bert->ctx_data, name);
ggml_allocr_alloc(alloc, cur);
// seek to the tensor data in the file
const size_t offset = gguf_get_data_offset(ctx_gguf) + gguf_get_tensor_offset(ctx_gguf, i);
fin.seekg(offset, std::ios::beg);
if (!fin) {
fprintf(stderr, "%s: failed to seek for tensor %s\n", __func__, name);
bert_free(new_bert);
return nullptr;
}
// read in data and copy to device if needed
int num_bytes = ggml_nbytes(cur);
if (ggml_backend_buffer_is_host(new_bert->weights_buffer)) {
// for the CPU and Metal backend, we can read directly into the tensor
fin.read(reinterpret_cast<char *>(cur->data), num_bytes);
} else {
// read into a temporary buffer first, then copy to device memory
read_buf.resize(num_bytes);
fin.read(reinterpret_cast<char *>(read_buf.data()), num_bytes);
ggml_backend_tensor_set(cur, read_buf.data(), 0, num_bytes);
}
}
// bye bye allocr
ggml_allocr_free(alloc);
}
// use get_tensors to populate bert_model
{
// embeddings weights
model.word_embeddings = get_tensor(new_bert->ctx_data, "embeddings.word_embeddings.weight");
model.token_type_embeddings = get_tensor(new_bert->ctx_data, "embeddings.token_type_embeddings.weight");
model.position_embeddings = get_tensor(new_bert->ctx_data, "embeddings.position_embeddings.weight");
model.ln_e_w = get_tensor(new_bert->ctx_data, "embeddings.LayerNorm.weight");
model.ln_e_b = get_tensor(new_bert->ctx_data, "embeddings.LayerNorm.bias");
// layers
model.layers.resize(hparams.n_layer);
for (int i = 0; i < hparams.n_layer; ++i) {
bert_layer & layer = model.layers[i];
std::string pre = "encoder.layer." + std::to_string(i) + ".";
// attention
layer.q_w = get_tensor(new_bert->ctx_data, pre + "attention.self.query.weight");
layer.q_b = get_tensor(new_bert->ctx_data, pre + "attention.self.query.bias");
layer.k_w = get_tensor(new_bert->ctx_data, pre + "attention.self.key.weight");
layer.k_b = get_tensor(new_bert->ctx_data, pre + "attention.self.key.bias");
layer.v_w = get_tensor(new_bert->ctx_data, pre + "attention.self.value.weight");
layer.v_b = get_tensor(new_bert->ctx_data, pre + "attention.self.value.bias");
layer.o_w = get_tensor(new_bert->ctx_data, pre + "attention.output.dense.weight");
layer.o_b = get_tensor(new_bert->ctx_data, pre + "attention.output.dense.bias");
layer.ln_att_w = get_tensor(new_bert->ctx_data, pre + "attention.output.LayerNorm.weight");
layer.ln_att_b = get_tensor(new_bert->ctx_data, pre + "attention.output.LayerNorm.bias");
// ff
layer.ff_i_w = get_tensor(new_bert->ctx_data, pre + "intermediate.dense.weight");
layer.ff_i_b = get_tensor(new_bert->ctx_data, pre + "intermediate.dense.bias");
layer.ff_o_w = get_tensor(new_bert->ctx_data, pre + "output.dense.weight");
layer.ff_o_b = get_tensor(new_bert->ctx_data, pre + "output.dense.bias");
layer.ln_out_w = get_tensor(new_bert->ctx_data, pre + "output.LayerNorm.weight");
layer.ln_out_b = get_tensor(new_bert->ctx_data, pre + "output.LayerNorm.bias");
}
}
// free metadata
ggml_free(ctx_ggml);
gguf_free(ctx_gguf);
// return context
return new_bert;
}
// measure and allocate comptue buffers
void bert_allocate_buffers(bert_ctx * ctx, int32_t n_max_tokens, int32_t batch_size) {
// deallocate if already allocated
bert_deallocate_buffers(ctx);
// get measuring allocr for backend
ctx->buf_compute_meta.resize(GGML_DEFAULT_GRAPH_SIZE * ggml_tensor_overhead() + ggml_graph_overhead());
ctx->compute_alloc = ggml_allocr_new_measure_from_backend(ctx->backend);
// construct batch and compute graph
bert_tokens tokens(n_max_tokens);
bert_batch batch;
for (int i = 0; i < batch_size; ++i) {
batch.push_back(tokens);
}
ggml_cgraph * gf = bert_build_graph(ctx, batch, true);
// do computing graph measurement
size_t compute_memory_buffer_size = ggml_allocr_alloc_graph(ctx->compute_alloc, gf);
ggml_allocr_free(ctx->compute_alloc);
// now that we know the compute size, create a buffer and allocr
ctx->compute_buffer = ggml_backend_alloc_buffer(ctx->backend, compute_memory_buffer_size);
ctx->compute_alloc = ggml_allocr_new_from_buffer(ctx->compute_buffer);
if (verbosity >= 1) {
fprintf(stderr, "%s: compute allocated memory: %.2f MB\n\n", __func__, compute_memory_buffer_size / 1024.0 / 1024.0);
}
}
void bert_deallocate_buffers(bert_ctx * ctx) {
if (ctx->compute_buffer) {
ggml_backend_buffer_free(ctx->compute_buffer);
ctx->compute_buffer = NULL;
}
if (ctx->compute_alloc) {
ggml_allocr_free(ctx->compute_alloc);
ctx->compute_alloc = NULL;
}
}
void bert_free(bert_ctx * ctx) {
// free compute buffers
bert_deallocate_buffers(ctx);
// free weights buffer
if (ctx->weights_buffer) {
ggml_backend_buffer_free(ctx->weights_buffer);
ctx->weights_buffer = NULL;
}
// free tensor context
if (ctx->ctx_data) {
ggml_free(ctx->ctx_data);
ctx->ctx_data = NULL;
}
// free backend
if (ctx->backend) {
ggml_backend_free(ctx->backend);
ctx->backend = NULL;
}
delete ctx;
}
//
// model execution
//
ggml_cgraph * bert_build_graph(bert_ctx * ctx, bert_batch batch, bool normalize) {
// vocab params
const bert_vocab & vocab = ctx->vocab;
const bert_token pad_id = vocab.pad_id;
// model params
const bert_model & model = ctx->model;
const bert_hparams & hparams = model.hparams;
// extract model params
const int n_embd = hparams.n_embd;
const int n_layer = hparams.n_layer;
const int n_max_tokens = hparams.n_max_tokens;
const int n_head = hparams.n_head;
const float layer_norm_eps = hparams.layer_norm_eps;
const int d_head = n_embd / n_head; // E = D * H
// get the max length of the batch
int n_batch_size = batch.size();
int cur_max_len = 0;
for (uint64_t ba = 0; ba < batch.size(); ba++) {
int n = batch[ba].size();
if (n > cur_max_len)
cur_max_len = n;
}
// check for token overflow
if (cur_max_len > n_max_tokens) {
fprintf(stderr, "Too many tokens, maximum is %d, got %d\n", n_max_tokens, cur_max_len);
return nullptr;
}
// params for graph data
struct ggml_init_params params = {
/*.mem_size =*/ ctx->buf_compute_meta.size(),
/*.mem_buffer =*/ ctx->buf_compute_meta.data(),
/*.no_alloc =*/ true,
};
// initialze computational graph
struct ggml_context * ctx0 = ggml_init(params);
struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, BERT_MAX_NODES, false);
// embeddings = word_embeddings + token_type_embeddings + position_embeddings
struct ggml_tensor * token_layer = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, cur_max_len * n_batch_size);
struct ggml_tensor * token_types = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, cur_max_len * n_batch_size);
struct ggml_tensor * pad_mask = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, 1, cur_max_len, 1, n_batch_size);
struct ggml_tensor * positions = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, cur_max_len * n_batch_size);
struct ggml_tensor * sum = ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, cur_max_len, 1, n_batch_size); // the avg pooler
struct ggml_tensor * minus_one = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, 1); // for attention mask
ggml_allocr_alloc(ctx->compute_alloc, token_layer);
ggml_allocr_alloc(ctx->compute_alloc, token_types);
ggml_allocr_alloc(ctx->compute_alloc, pad_mask);
ggml_allocr_alloc(ctx->compute_alloc, positions);
ggml_allocr_alloc(ctx->compute_alloc, sum);
ggml_allocr_alloc(ctx->compute_alloc, minus_one);
// avoid writing input embeddings in memory measure mode
if (!ggml_allocr_is_measure(ctx->compute_alloc)) {
int32_t * token_layer_data = (int32_t*)malloc(ggml_nbytes(token_layer));
int32_t * token_types_data = (int32_t*)malloc(ggml_nbytes(token_types));
float * pad_mask_data = (float*)malloc(ggml_nbytes(pad_mask));
int32_t * pos_data = (int32_t*)malloc(ggml_nbytes(positions));
float * sum_data = (float*)malloc(ggml_nbytes(sum));
float m1 = -1.0f;
for (int ba = 0; ba < n_batch_size; ba++) {
for (int i = 0; i < cur_max_len; i++) {
int cur_len = batch[ba].size();
if (i < cur_len) {
token_layer_data[ba * cur_max_len + i] = batch[ba][i];
pad_mask_data[ba * cur_max_len + i] = 1.0f;
sum_data[ba * cur_max_len + i] = 1 / (float)cur_len;
}
else {
token_layer_data[ba * cur_max_len + i] = pad_id; // padding
pad_mask_data[ba * cur_max_len + i] = 0.0f;
sum_data[ba * cur_max_len + i] = 0.0f;
}
token_types_data[ba * cur_max_len + i] = 0;
pos_data[ba * cur_max_len + i] = i;
}
}
ggml_backend_tensor_set(token_layer, token_layer_data, 0, ggml_nbytes(token_layer));
ggml_backend_tensor_set(token_types, token_types_data, 0, ggml_nbytes(token_types));
ggml_backend_tensor_set(pad_mask, pad_mask_data, 0, ggml_nbytes(pad_mask));
ggml_backend_tensor_set(positions, pos_data, 0, ggml_nbytes(positions));
ggml_backend_tensor_set(sum, sum_data, 0, ggml_nbytes(sum));
ggml_backend_tensor_set(minus_one, &m1, 0, sizeof(m1));
free(token_layer_data);
free(token_types_data);
free(pad_mask_data);
free(pos_data);
free(sum_data);
}
// outer product the padding mask to kill off outside
struct ggml_tensor * attn_mask = ggml_mul_mat(ctx0, pad_mask, pad_mask); // [L, L, 1, B]
attn_mask = ggml_add(ctx0, attn_mask, minus_one); // result -0
attn_mask = ggml_scale_inplace(ctx0, attn_mask, 100000.0f); // BUG: 1e3 will cause overflow?
// get various embedding components
struct ggml_tensor *inpL = ggml_get_rows(ctx0, model.word_embeddings, token_layer); // [E, L * B]
inpL = ggml_add(ctx0, ggml_get_rows(ctx0, model.token_type_embeddings, token_types), inpL);
inpL = ggml_add(ctx0, ggml_get_rows(ctx0, model.position_embeddings, positions), inpL);
inpL = ggml_reshape_3d(ctx0, inpL, n_embd, cur_max_len, n_batch_size); // [E, L, B]
// embed layer norm
inpL = ggml_norm_inplace(ctx0, inpL, layer_norm_eps);
inpL = ggml_add(ctx0, ggml_mul(ctx0, inpL, model.ln_e_w), model.ln_e_b); // [E, L, B]
// layers
for (int il = 0; il < n_layer; il++) {
struct ggml_tensor * cur = inpL;
// self-attention
{
// extract Q
struct ggml_tensor * Q = cur;
Q = ggml_add(ctx0, ggml_mul_mat(ctx0, model.layers[il].q_w, Q), model.layers[il].q_b); // [E, L, B]
Q = ggml_reshape_4d(ctx0, Q, d_head, n_head, cur_max_len, n_batch_size); // [D, H, L, B]
Q = ggml_cont(ctx0, ggml_permute(ctx0, Q, 0, 2, 1, 3)); // [D, L, H, B]
// extract K
struct ggml_tensor * K = cur;
K = ggml_add(ctx0, ggml_mul_mat(ctx0, model.layers[il].k_w, K), model.layers[il].k_b); // [E, L, B]
K = ggml_reshape_4d(ctx0, K, d_head, n_head, cur_max_len, n_batch_size); // [D, H, L, B]
K = ggml_cont(ctx0, ggml_permute(ctx0, K, 0, 2, 1, 3)); // [D, L, H, B]
// extract V
struct ggml_tensor * V = cur;
V = ggml_add(ctx0, ggml_mul_mat(ctx0, model.layers[il].v_w, V), model.layers[il].v_b); // [E, L, B]
V = ggml_reshape_4d(ctx0, V, d_head, n_head, cur_max_len, n_batch_size); // [D, H, L, B]
V = ggml_cont(ctx0, ggml_permute(ctx0, V, 1, 2, 0, 3)); // [L, D, H, B]
// scaled attention
struct ggml_tensor * KQ = ggml_mul_mat(ctx0, K, Q); // -> [L, L, H, B]
KQ = ggml_scale_inplace(ctx0, KQ, 1.0f / sqrt((float)d_head));
KQ = ggml_add(ctx0, KQ, attn_mask);
KQ = ggml_soft_max(ctx0, KQ);
// get weighted values
struct ggml_tensor * KQV = ggml_mul_mat(ctx0, V, KQ); // -> [D, L, H, B]
KQV = ggml_cont(ctx0, ggml_permute(ctx0, KQV, 0, 2, 1, 3)); // -> [D, H, L, B]
// copy back to input (E = D * H)
cur = ggml_reshape_3d(ctx0, KQV, n_embd, cur_max_len, n_batch_size); // [E, L, B]
}
// attention output
cur = ggml_add(ctx0, ggml_mul_mat(ctx0, model.layers[il].o_w, cur), model.layers[il].o_b);
// residual connection
cur = ggml_add(ctx0, cur, inpL);
// attention layer norm
cur = ggml_norm_inplace(ctx0, cur, layer_norm_eps);
cur = ggml_add(ctx0, ggml_mul(ctx0, cur, model.layers[il].ln_att_w), model.layers[il].ln_att_b);
// store for later
struct ggml_tensor * att_output = cur;
// feed forward steps
cur = ggml_add(ctx0, ggml_mul_mat(ctx0, model.layers[il].ff_i_w, cur), model.layers[il].ff_i_b);
cur = ggml_gelu(ctx0, cur);
cur = ggml_add(ctx0, ggml_mul_mat(ctx0, model.layers[il].ff_o_w, cur), model.layers[il].ff_o_b);
// attentions bypass the intermediate layer
cur = ggml_add(ctx0, att_output, cur);
// output layer norm
cur = ggml_norm_inplace(ctx0, cur, layer_norm_eps);
cur = ggml_add(ctx0, ggml_mul(ctx0, cur, model.layers[il].ln_out_w), model.layers[il].ln_out_b);
// on to next layer
inpL = cur;
}
// pooling (sum = [L, 1, B])
inpL = ggml_mul_mat(ctx0, ggml_cont(ctx0, ggml_transpose(ctx0, inpL)), sum); // [E, 1, B]
inpL = ggml_reshape_2d(ctx0, inpL, n_embd, n_batch_size); // [E, B]
// l2 normalize
if (normalize) {
inpL = ggml_rms_norm(ctx0, inpL, layer_norm_eps); // [E, B]
inpL = ggml_scale_inplace(ctx0, inpL, 1.0f / sqrt((float)n_embd)); // [E, B] (since rms_norm does mean instead of sum)
}
// final output
ggml_tensor * output = inpL;
// build the graph
ggml_build_forward_expand(gf, output);
// free context
ggml_free(ctx0);
// return complete graph
return gf;
}
void bert_forward_batch(bert_ctx * ctx, bert_batch batch, float * embeddings, bool normalize, int32_t n_threads) {
// reset alloc buffer to clean the memory from previous invocations
ggml_allocr_reset(ctx->compute_alloc);
// build the compute graph
ggml_cgraph * gf = bert_build_graph(ctx, batch, normalize);
if (gf == nullptr) {
fprintf(stderr, "%s: failed to build compute graph\n", __func__);
return;
}
// allocate memory for the graph
ggml_allocr_alloc_graph(ctx->compute_alloc, gf);
// print timing information per ggml operation (for debugging purposes)
if (verbosity >= 3) {
ggml_graph_print(gf);
}
if (ggml_backend_is_cpu(ctx->backend)) {
ggml_backend_cpu_set_n_threads(ctx->backend, n_threads);
}
#ifdef GGML_USE_METAL
if (ggml_backend_is_metal(ctx->backend)) {
ggml_backend_metal_set_n_cb(ctx->backend, n_threads);
}
#endif
// execute the graph
ggml_backend_graph_compute(ctx->backend, gf);
// the last node is the embedding tensor
struct ggml_tensor * output = gf->nodes[gf->n_nodes - 1];
// copy the embeddings to the location passed by the user
ggml_backend_tensor_get(output, embeddings, 0, ggml_nbytes(output));
}
void bert_encode_batch(struct bert_ctx * ctx, bert_strings texts, float * embeddings, bool normalize, int32_t n_threads) {
int32_t N = bert_n_max_tokens(ctx);
int32_t n_input = texts.size();
bert_batch batch;
for (int i = 0; i < n_input; i++) {
bert_tokens tokens = bert_tokenize(ctx, texts[i], N);
batch.push_back(tokens);
}
bert_forward_batch(ctx, batch, embeddings, normalize, n_threads);
}
void bert_encode_batch_c(struct bert_ctx * ctx, const char ** texts, float * embeddings, int32_t n_input, bool normalize, int32_t n_threads) {
bert_strings strings;
for (int i = 0; i < n_input; i++) {
strings.push_back(texts[i]);
}
bert_encode_batch(ctx, strings, embeddings, normalize, n_threads);
}
void bert_forward(struct bert_ctx * ctx, bert_tokens tokens, float * embeddings, bool normalize, int32_t n_threads) {
You can’t perform that action at this time.
