-
Notifications
You must be signed in to change notification settings - Fork 335
/
chatglm.h
1383 lines (1088 loc) · 51.3 KB
/
chatglm.h
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
#pragma once
#include <cmath>
#include <ggml.h>
#include <ggml/ggml-backend.h>
#include <iomanip>
#include <re2/re2.h>
#include <sentencepiece_processor.h>
#include <sstream>
#include <unordered_map>
namespace chatglm {
// ===== common =====
class LogMessageFatal {
public:
LogMessageFatal(const char *file, int line) { oss_ << file << ':' << line << ' '; }
[[noreturn]] ~LogMessageFatal() noexcept(false) { throw std::runtime_error(oss_.str()); }
std::ostringstream &stream() { return oss_; }
private:
std::ostringstream oss_;
};
#define CHATGLM_THROW ::chatglm::LogMessageFatal(__FILE__, __LINE__).stream()
#define CHATGLM_CHECK(cond) \
if (!(cond)) \
CHATGLM_THROW << "check failed (" #cond ") "
#define CHATGLM_CHECK_CUDA(call) \
do { \
cudaError_t error = (call); \
CHATGLM_CHECK(error == cudaSuccess) << "CUDA error: " << cudaGetErrorString(error); \
} while (0)
std::string to_string(ggml_tensor *tensor, bool with_data = true);
enum class ModelType {
CHATGLM = 1,
CHATGLM2 = 2,
CHATGLM3 = 3,
CHATGLM4 = 4,
CHATGLM4V = 1004,
};
std::string to_string(ModelType model_type);
// For compatibility
struct ModelConfigRecordV1 {
// common attributes
ggml_type dtype;
int vocab_size;
int hidden_size;
int num_attention_heads;
int num_hidden_layers;
int intermediate_size;
// for sequence generation
int max_length;
// for tokenizer
int bos_token_id;
int eos_token_id;
int pad_token_id;
int sep_token_id;
};
// For compatibility
struct ModelConfigRecordV1GQA {
// ModelConfigRecordV1
ggml_type dtype;
int vocab_size;
int hidden_size;
int num_attention_heads;
int num_hidden_layers;
int intermediate_size;
int max_length;
int bos_token_id;
int eos_token_id;
int pad_token_id;
int sep_token_id;
// GQA
int num_key_value_heads;
};
// TODO: use json to serialize config
struct ModelConfigRecordV2 {
ggml_type dtype;
int vocab_size;
int hidden_size;
int num_attention_heads;
int num_key_value_heads;
int num_hidden_layers;
int intermediate_size;
float norm_eps;
int num_virtual_tokens;
float rope_theta;
int max_length;
int eos_token_id;
int pad_token_id;
};
enum class ActivationType {
GELU,
SILU,
};
enum class RopeType {
GPTJ = 0,
NEOX = 2,
CHATGLM = 4,
CHATGLM2 = 8,
DISABLED = 10000,
};
enum class AttentionMaskType {
BIDIRECTIONAL,
CAUSAL,
CHATGLM,
};
struct VisionModelConfigRecord {
ggml_type dtype;
int hidden_size;
int image_size;
int in_channels;
int intermediate_size;
float norm_eps;
int num_attention_heads;
int num_hidden_layers;
int num_positions;
int patch_size;
float scaling_factor;
};
struct VisionModelConfig {
ggml_type dtype;
ActivationType hidden_act;
int hidden_size;
int image_size;
int in_channels;
int intermediate_size;
float norm_eps;
int num_attention_heads;
int num_hidden_layers;
int num_positions;
int patch_size;
float scaling_factor;
VisionModelConfig() = default;
VisionModelConfig(ggml_type dtype, ActivationType hidden_act, int hidden_size, int image_size, int in_channels,
int intermediate_size, float norm_eps, int num_attention_heads, int num_hidden_layers,
int num_positions, int patch_size, float scaling_factor)
: dtype(dtype), hidden_act(hidden_act), hidden_size(hidden_size), image_size(image_size),
in_channels(in_channels), intermediate_size(intermediate_size), norm_eps(norm_eps),
num_attention_heads(num_attention_heads), num_hidden_layers(num_hidden_layers), num_positions(num_positions),
patch_size(patch_size), scaling_factor(scaling_factor) {}
VisionModelConfig(const VisionModelConfigRecord &rec)
: VisionModelConfig(rec.dtype, ActivationType::GELU, rec.hidden_size, rec.image_size, rec.in_channels,
rec.intermediate_size, rec.norm_eps, rec.num_attention_heads, rec.num_hidden_layers,
rec.num_positions, rec.patch_size, rec.scaling_factor) {}
friend std::ostream &operator<<(std::ostream &os, const VisionModelConfig &self) {
return os << "VisionModelConfig(dtype=" << self.dtype << ", hidden_act=" << (int)self.hidden_act
<< ", hidden_size=" << self.hidden_size << ", image_size=" << self.image_size
<< ", in_channels=" << self.in_channels << ", intermediate_size=" << self.intermediate_size
<< ", norm_eps=" << self.norm_eps << ", num_attention_heads=" << self.num_attention_heads
<< ", num_hidden_layers=" << self.num_hidden_layers << ", num_positions="
<< ", patch_size=" << self.patch_size << ", scaling_factor=" << self.scaling_factor << ")";
}
};
// Should save kv record of ModelConfig in the future
class ModelConfig {
public:
ModelConfig() = default;
ModelConfig(ModelType model_type, ggml_type dtype, int vocab_size, int hidden_size, int num_attention_heads,
int num_key_value_heads, int num_hidden_layers, int intermediate_size, float norm_eps, float rope_theta,
int num_virtual_tokens, int max_length, int bos_token_id, int eos_token_id, int pad_token_id,
int sep_token_id, int boi_token_id, int eoi_token_id, std::vector<int> extra_eos_token_ids,
const VisionModelConfig &vision)
: model_type(model_type), dtype(dtype), vocab_size(vocab_size), hidden_size(hidden_size),
num_attention_heads(num_attention_heads), num_key_value_heads(num_key_value_heads),
num_hidden_layers(num_hidden_layers), intermediate_size(intermediate_size), norm_eps(norm_eps),
rope_theta(rope_theta), num_virtual_tokens(num_virtual_tokens), max_length(max_length),
bos_token_id(bos_token_id), eos_token_id(eos_token_id), pad_token_id(pad_token_id),
sep_token_id(sep_token_id), boi_token_id(boi_token_id), eoi_token_id(eoi_token_id),
extra_eos_token_ids(std::move(extra_eos_token_ids)), vision(vision) {
if (model_type == ModelType::CHATGLM) {
hidden_act = ActivationType::GELU;
use_qkv_bias = true;
use_dense_bias = true;
interleaved_qkv = true;
tie_word_embeddings = true;
rope_type = RopeType::CHATGLM;
} else {
hidden_act = ActivationType::SILU;
use_qkv_bias = true;
use_dense_bias = false;
interleaved_qkv = false;
tie_word_embeddings = false;
rope_type = RopeType::CHATGLM2;
}
}
ModelConfig(ModelType model_type, const ModelConfigRecordV1 &rec, float norm_eps, float rope_theta,
int num_virtual_tokens)
: ModelConfig(model_type, rec.dtype, rec.vocab_size, rec.hidden_size, rec.num_attention_heads,
rec.num_attention_heads, rec.num_hidden_layers, rec.intermediate_size, norm_eps, rope_theta,
num_virtual_tokens, rec.max_length, rec.bos_token_id, rec.eos_token_id, rec.pad_token_id,
rec.sep_token_id, -1, -1, {}, {}) {}
ModelConfig(ModelType model_type, const ModelConfigRecordV1GQA &rec, float norm_eps, float rope_theta,
int num_virtual_tokens)
: ModelConfig(model_type, rec.dtype, rec.vocab_size, rec.hidden_size, rec.num_attention_heads,
rec.num_key_value_heads, rec.num_hidden_layers, rec.intermediate_size, norm_eps, rope_theta,
num_virtual_tokens, rec.max_length, rec.bos_token_id, rec.eos_token_id, rec.pad_token_id,
rec.sep_token_id, -1, -1, {}, {}) {}
ModelConfig(ModelType model_type, const ModelConfigRecordV2 &rec)
: ModelConfig(model_type, rec.dtype, rec.vocab_size, rec.hidden_size, rec.num_attention_heads,
rec.num_key_value_heads, rec.num_hidden_layers, rec.intermediate_size, rec.norm_eps,
rec.rope_theta, rec.num_virtual_tokens, rec.max_length, -1, rec.eos_token_id, rec.pad_token_id,
-1, -1, -1, {}, {}) {}
std::string model_type_name() const { return to_string(model_type); }
friend std::ostream &operator<<(std::ostream &os, const ModelConfig &self) {
os << "ModelConfig(model_type=" << (int)self.model_type << ", dtype=" << self.dtype
<< ", vocab_size=" << self.vocab_size << ", hidden_size=" << self.hidden_size
<< ", num_attention_heads=" << self.num_attention_heads
<< ", num_key_value_heads=" << self.num_key_value_heads << ", num_hidden_layers=" << self.num_hidden_layers
<< ", intermediate_size=" << self.intermediate_size << ", norm_eps=" << self.norm_eps
<< ", hidden_act=" << (int)self.hidden_act << ", use_qkv_bias=" << self.use_qkv_bias
<< ", use_dense_bias=" << self.use_dense_bias << ", interleaved_qkv=" << self.interleaved_qkv
<< ", tie_word_embeddings=" << self.tie_word_embeddings << ", rope_type=" << (int)self.rope_type
<< ", rope_theta=" << self.rope_theta << ", num_virtual_tokens=" << self.num_virtual_tokens
<< ", max_length=" << self.max_length << ", bos_token_id=" << self.bos_token_id
<< ", eos_token_id=" << self.eos_token_id << ", pad_token_id=" << self.pad_token_id
<< ", sep_token_id=" << self.sep_token_id << ", extra_eos_token_ids={";
for (size_t i = 0; i < self.extra_eos_token_ids.size(); i++) {
os << (i > 0 ? ", " : "") << self.extra_eos_token_ids[i];
}
return os << "}, vision=" << self.vision << ")";
}
public:
ModelType model_type;
ggml_type dtype;
int vocab_size;
int hidden_size;
int num_attention_heads;
int num_key_value_heads;
int num_hidden_layers;
int intermediate_size;
float norm_eps;
ActivationType hidden_act;
bool use_qkv_bias;
bool use_dense_bias;
bool interleaved_qkv;
bool tie_word_embeddings;
RopeType rope_type;
float rope_theta;
int num_virtual_tokens;
int max_length;
int bos_token_id;
int eos_token_id;
int pad_token_id;
int sep_token_id;
int boi_token_id;
int eoi_token_id;
std::vector<int> extra_eos_token_ids;
VisionModelConfig vision;
};
struct FunctionMessage {
std::string name;
std::string arguments;
FunctionMessage() = default;
FunctionMessage(std::string name, std::string arguments) : name(std::move(name)), arguments(std::move(arguments)) {}
friend std::ostream &operator<<(std::ostream &os, const FunctionMessage &self) {
return os << "FunctionMessage(name=" << std::quoted(self.name) << ", arguments=" << std::quoted(self.arguments)
<< ")";
}
};
struct CodeMessage {
std::string input;
CodeMessage() = default;
CodeMessage(std::string input) : input(std::move(input)) {}
friend std::ostream &operator<<(std::ostream &os, const CodeMessage &self) {
return os << "CodeMessage(input=" << std::quoted(self.input) << ")";
}
};
struct ToolCallMessage {
std::string type;
FunctionMessage function;
CodeMessage code;
static const std::string TYPE_FUNCTION;
static const std::string TYPE_CODE;
ToolCallMessage(FunctionMessage function) : type(TYPE_FUNCTION), function(std::move(function)) {}
ToolCallMessage(CodeMessage code) : type(TYPE_CODE), code(std::move(code)) {}
friend std::ostream &operator<<(std::ostream &os, const ToolCallMessage &self) {
return os << "ToolCallMessage(type=" << std::quoted(self.type) << ", function=" << self.function
<< ", code=" << self.code << ")";
}
};
struct Image {
size_t width = 0;
size_t height = 0;
size_t channels = 0;
std::vector<uint8_t> pixels;
Image() = default;
Image(size_t width, size_t height, size_t channels)
: width(width), height(height), channels(channels), pixels(width * height * channels) {}
Image(size_t width, size_t height, size_t channels, uint8_t *data)
: width(width), height(height), channels(channels), pixels(data, data + width * height * channels) {}
Image(const Image &other) = default;
Image(Image &&other) { *this = std::move(other); }
Image &operator=(const Image &other) = default;
Image &operator=(Image &&other) {
width = other.width;
height = other.height;
channels = other.channels;
pixels = std::move(other.pixels);
other.clear();
return *this;
}
static Image open(const std::string &path);
Image resize(size_t new_width, size_t new_height) const;
void clear() {
width = height = channels = 0;
pixels.clear();
}
friend std::ostream &operator<<(std::ostream &os, const Image &self) {
return os << "Image(mode=RGB, size=" << self.width << "x" << self.height << ")";
}
};
struct ChatMessage {
std::string role;
std::string content;
std::optional<Image> image;
std::vector<ToolCallMessage> tool_calls;
static const std::string ROLE_USER;
static const std::string ROLE_ASSISTANT;
static const std::string ROLE_SYSTEM;
static const std::string ROLE_OBSERVATION;
ChatMessage() = default;
ChatMessage(std::string role, std::string content, std::optional<Image> image = std::nullopt,
std::vector<ToolCallMessage> tool_calls = {})
: role(std::move(role)), content(std::move(content)), image(std::move(image)),
tool_calls(std::move(tool_calls)) {}
friend std::ostream &operator<<(std::ostream &os, const ChatMessage &self) {
os << "ChatMessage(role=" << std::quoted(self.role) << ", content=" << std::quoted(self.content);
if (self.image.has_value()) {
os << ", image=" << *self.image;
}
os << ", tool_calls=[";
for (size_t i = 0; i < self.tool_calls.size(); i++) {
os << (i > 0 ? ", " : "") << self.tool_calls[i];
}
return os << "])";
}
};
class BaseTokenizer {
public:
virtual ~BaseTokenizer() = default;
virtual std::vector<int> encode(const std::string &text, int max_length) const = 0;
virtual std::string decode(const std::vector<int> &ids, bool skip_special_tokens = true) const = 0;
virtual std::vector<int> apply_chat_template(const std::vector<ChatMessage> &messages, int max_length) const = 0;
virtual ChatMessage decode_message(const std::vector<int> &ids) const {
return {ChatMessage::ROLE_ASSISTANT, decode(ids)};
}
protected:
static void check_chat_messages(const std::vector<ChatMessage> &messages);
static std::vector<ChatMessage> filter_user_assistant_messages(const std::vector<ChatMessage> &messages);
};
struct ggml_context_deleter_t {
void operator()(ggml_context *ctx) const noexcept { ggml_free(ctx); }
};
using unique_ggml_context_t = std::unique_ptr<ggml_context, ggml_context_deleter_t>;
inline unique_ggml_context_t make_unique_ggml_context(size_t mem_size, void *mem_buffer, bool no_alloc) {
return unique_ggml_context_t(ggml_init({mem_size, mem_buffer, no_alloc}));
}
struct ggml_gallocr_deleter_t {
void operator()(ggml_gallocr *galloc) const noexcept { ggml_gallocr_free(galloc); }
};
using unique_ggml_gallocr_t = std::unique_ptr<ggml_gallocr, ggml_gallocr_deleter_t>;
struct ggml_backend_deleter_t {
void operator()(ggml_backend_t backend) const noexcept { ggml_backend_free(backend); }
};
using unique_ggml_backend_t = std::unique_ptr<ggml_backend, ggml_backend_deleter_t>;
struct ggml_backend_buffer_deleter_t {
void operator()(ggml_backend_buffer_t buffer) const noexcept { ggml_backend_buffer_free(buffer); }
};
using unique_ggml_backend_buffer_t = std::unique_ptr<ggml_backend_buffer, ggml_backend_buffer_deleter_t>;
// reference: https://github.com/ggerganov/llama.cpp/blob/master/llama.cpp
template <typename T>
struct no_init {
T value;
no_init() { /* do nothing */
}
};
struct ModelContext {
std::vector<no_init<char>> compute_meta;
unique_ggml_context_t ctx_w; // weight
unique_ggml_context_t ctx_kv; // kv cache
unique_ggml_context_t ctx_b; // buffer
ggml_cgraph *gf;
unique_ggml_backend_t backend;
unique_ggml_gallocr_t allocr;
unique_ggml_backend_buffer_t buf_w;
unique_ggml_backend_buffer_t buf_kv;
ModelContext();
};
class Embedding {
public:
Embedding() = default;
Embedding(ModelContext *mctx, ggml_type dtype, int num_embeddings, int embedding_dim)
: weight(ggml_new_tensor_2d(mctx->ctx_w.get(), dtype, embedding_dim, num_embeddings)) {}
int num_embeddings() const { return weight->ne[1]; }
int embedding_dim() const { return weight->ne[0]; }
ggml_tensor *forward(ModelContext *mctx, ggml_tensor *input) const;
public:
ggml_tensor *weight = nullptr;
};
class Linear {
public:
Linear() = default;
Linear(ModelContext *mctx, ggml_type dtype, int in_features, int out_features, bool use_bias = true)
: weight(ggml_new_tensor_2d(mctx->ctx_w.get(), dtype, in_features, out_features)),
bias(use_bias ? ggml_new_tensor_1d(mctx->ctx_w.get(), GGML_TYPE_F32, out_features) : nullptr) {}
int in_features() const { return weight->ne[0]; }
int out_features() const { return weight->ne[1]; }
ggml_tensor *forward(ModelContext *mctx, ggml_tensor *input) const;
public:
ggml_tensor *weight = nullptr; // [out_features, in_features]
ggml_tensor *bias = nullptr; // [out_features]
};
class LayerNorm {
public:
LayerNorm() = default;
LayerNorm(ModelContext *mctx, int normalized_shape, float eps = 1e-5f)
: weight(ggml_new_tensor_1d(mctx->ctx_w.get(), GGML_TYPE_F32, normalized_shape)),
bias(ggml_new_tensor_1d(mctx->ctx_w.get(), GGML_TYPE_F32, normalized_shape)), eps(eps) {}
ggml_tensor *forward(ModelContext *mctx, ggml_tensor *input) const;
public:
ggml_tensor *weight = nullptr; // [normalized_shape]
ggml_tensor *bias = nullptr; // [normalized_shape]
float eps = 0.f;
};
class RMSNorm {
public:
RMSNorm() = default;
RMSNorm(ModelContext *mctx, int normalized_shape, float eps = 1e-5f)
: weight(ggml_new_tensor_1d(mctx->ctx_w.get(), GGML_TYPE_F32, normalized_shape)), eps(eps) {}
ggml_tensor *forward(ModelContext *mctx, ggml_tensor *input) const;
public:
ggml_tensor *weight = nullptr; // [normalized_shape]
float eps = 0.f;
};
class BasicMLP {
public:
BasicMLP() = default;
BasicMLP(ModelContext *mctx, ggml_type dtype, int hidden_size, int intermediate_size, ActivationType hidden_act)
: dense_h_to_4h(mctx, dtype, hidden_size, intermediate_size),
dense_4h_to_h(mctx, dtype, intermediate_size, hidden_size), hidden_act(hidden_act) {}
ggml_tensor *forward(ModelContext *mctx, ggml_tensor *hidden_states) const;
public:
Linear dense_h_to_4h;
Linear dense_4h_to_h;
ActivationType hidden_act;
};
class BasicGLU {
public:
BasicGLU() = default;
BasicGLU(ModelContext *mctx, ggml_type dtype, int hidden_size, int intermediate_size, ActivationType hidden_act)
: gate_proj(mctx, dtype, hidden_size, intermediate_size, false),
up_proj(mctx, dtype, hidden_size, intermediate_size, false),
down_proj(mctx, dtype, intermediate_size, hidden_size, false), hidden_act(hidden_act) {}
ggml_tensor *forward(ModelContext *mctx, ggml_tensor *hidden_states) const;
public:
Linear gate_proj;
Linear up_proj;
Linear down_proj;
ActivationType hidden_act;
};
class BasicAttention {
public:
BasicAttention() = default;
BasicAttention(ModelContext *mctx, ggml_type dtype, int hidden_size, int num_attention_heads,
int num_key_value_heads, int max_length, bool use_qkv_bias, bool use_dense_bias,
bool interleaved_qkv, RopeType rope_type, float rope_theta, int num_virtual_tokens, bool use_cache)
: num_attention_heads(num_attention_heads), num_key_value_heads(num_key_value_heads),
interleaved_qkv(interleaved_qkv), rope_type(rope_type), rope_theta(rope_theta),
num_virtual_tokens(num_virtual_tokens),
query_key_value(mctx, dtype, hidden_size,
hidden_size + 2 * (hidden_size / num_attention_heads) * num_key_value_heads, use_qkv_bias),
dense(mctx, dtype, hidden_size, hidden_size, use_dense_bias),
k_cache(use_cache ? ggml_new_tensor_3d(mctx->ctx_kv.get(), GGML_TYPE_F16, hidden_size / num_attention_heads,
max_length + num_virtual_tokens, num_key_value_heads)
: nullptr),
v_cache(use_cache ? ggml_new_tensor_3d(mctx->ctx_kv.get(), GGML_TYPE_F16, max_length + num_virtual_tokens,
hidden_size / num_attention_heads, num_key_value_heads)
: nullptr) {}
ggml_tensor *forward(ModelContext *mctx, ggml_tensor *hidden_states, ggml_tensor *attention_mask,
ggml_tensor *position_ids, int n_past) const;
public:
int num_attention_heads;
int num_key_value_heads;
bool interleaved_qkv;
RopeType rope_type;
float rope_theta;
int num_virtual_tokens;
Linear query_key_value;
Linear dense;
ggml_tensor *k_cache = nullptr; // [#kvh, s, d]
ggml_tensor *v_cache = nullptr; // [#kvh, d, s]
};
template <typename Norm, typename MLP>
class BasicBlock {
public:
BasicBlock() = default;
BasicBlock(ModelContext *mctx, ggml_type dtype, int hidden_size, int num_attention_heads, int num_key_value_heads,
int intermediate_size, int max_length, float norm_eps, ActivationType hidden_act, bool use_qkv_bias,
bool use_dense_bias, bool interleaved_qkv, RopeType rope_type, float rope_theta, int num_virtual_tokens,
bool use_cache)
: input_layernorm(mctx, hidden_size, norm_eps),
attention(mctx, dtype, hidden_size, num_attention_heads, num_key_value_heads, max_length, use_qkv_bias,
use_dense_bias, interleaved_qkv, rope_type, rope_theta, num_virtual_tokens, use_cache),
post_attention_layernorm(mctx, hidden_size, norm_eps),
mlp(mctx, dtype, hidden_size, intermediate_size, hidden_act) {}
ggml_tensor *forward(ModelContext *mctx, ggml_tensor *hidden_states, ggml_tensor *attention_mask,
ggml_tensor *position_ids, int n_past) const {
ggml_context *ctx = mctx->ctx_b.get();
ggml_tensor *residual = hidden_states;
hidden_states = input_layernorm.forward(mctx, hidden_states);
hidden_states = attention.forward(mctx, hidden_states, attention_mask, position_ids, n_past);
hidden_states = ggml_add_inplace(ctx, hidden_states, residual);
residual = hidden_states;
hidden_states = post_attention_layernorm.forward(mctx, hidden_states);
hidden_states = mlp.forward(mctx, hidden_states);
hidden_states = ggml_add_inplace(ctx, hidden_states, residual);
return hidden_states;
}
protected:
BasicBlock(Norm input_layernorm, BasicAttention attention, Norm post_attention_layernorm, MLP mlp)
: input_layernorm(input_layernorm), attention(attention), post_attention_layernorm(post_attention_layernorm),
mlp(mlp) {}
public:
Norm input_layernorm;
BasicAttention attention;
Norm post_attention_layernorm;
MLP mlp;
};
struct NoopPositionIdsAllocator {
ggml_tensor *operator()(ggml_context *ctx, int qlen) const { return nullptr; }
};
struct BasicPositionIdsAllocator {
ggml_tensor *operator()(ggml_context *ctx, int qlen) const { return ggml_new_tensor_1d(ctx, GGML_TYPE_I32, qlen); }
};
struct GLMPositionIdsAllocator {
ggml_tensor *operator()(ggml_context *ctx, int qlen) const {
return ggml_new_tensor_1d(ctx, GGML_TYPE_I32, qlen * 2);
}
};
template <typename Block, typename Norm, typename PositionIdsAllocator>
class BasicModel {
public:
BasicModel() = default;
BasicModel(Embedding word_embeddings, std::vector<Block> layers, Norm final_layernorm)
: word_embeddings(word_embeddings), layers(std::move(layers)), final_layernorm(final_layernorm) {}
BasicModel(ModelContext *mctx, const ModelConfig &config)
: word_embeddings(mctx, config.dtype, config.vocab_size, config.hidden_size),
layers(build_layers(mctx, config)), final_layernorm(mctx, config.hidden_size) {}
ggml_tensor *forward(ModelContext *mctx, ggml_tensor *input_ids, ggml_tensor *images,
const std::vector<int> &input_ids_vec, int n_past) const {
ggml_context *ctx = mctx->ctx_b.get();
ggml_tensor *hidden_states = forward_embeddings(mctx, input_ids, images, input_ids_vec, n_past);
const int qlen = hidden_states->ne[1];
const int kvlen = layers.front().attention.num_virtual_tokens + n_past + qlen;
ggml_tensor *position_ids = pos_ids_alloc_(ctx, qlen);
if (position_ids) {
ggml_set_name(position_ids, "position_ids");
ggml_set_input(position_ids);
}
ggml_tensor *attention_mask = nullptr;
if (n_past == 0) {
attention_mask = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, kvlen, qlen);
ggml_set_name(attention_mask, "attention_mask");
ggml_set_input(attention_mask);
}
for (const auto &layer : layers) {
hidden_states = layer.forward(mctx, hidden_states, attention_mask, position_ids, n_past);
}
hidden_states = final_layernorm.forward(mctx, hidden_states);
return hidden_states;
}
virtual ggml_tensor *forward_embeddings(ModelContext *mctx, ggml_tensor *input_ids, ggml_tensor *images,
const std::vector<int> &input_ids_vec, int n_past) const {
CHATGLM_CHECK(images == nullptr) << "unimplemented";
return word_embeddings.forward(mctx, input_ids);
}
void load_prefix_cache(const ModelConfig &config, ggml_tensor *past_key_values) {
// past_key_values: [l * 2, #h, v, d]
ModelContext mctx;
ggml_tensor *backend_past_key_values = ggml_new_tensor(mctx.ctx_kv.get(), past_key_values->type,
ggml_n_dims(past_key_values), past_key_values->ne);
auto buf_kv =
unique_ggml_backend_buffer_t(ggml_backend_alloc_ctx_tensors(mctx.ctx_kv.get(), mctx.backend.get()));
ggml_backend_tensor_set(backend_past_key_values, past_key_values->data, 0, ggml_nbytes(past_key_values));
past_key_values = backend_past_key_values;
const int head_size = config.hidden_size / config.num_attention_heads;
for (size_t i = 0; i < layers.size(); i++) {
auto &attn = layers[i].attention;
ggml_tensor *virtual_key =
ggml_view_3d(mctx.ctx_b.get(), past_key_values, head_size, config.num_virtual_tokens,
config.num_key_value_heads, past_key_values->nb[1], past_key_values->nb[2],
i * 2 * past_key_values->nb[3]); // [#h, v, d]
ggml_tensor *k_cache_view =
ggml_view_3d(mctx.ctx_b.get(), attn.k_cache, head_size, config.num_virtual_tokens,
config.num_key_value_heads, attn.k_cache->nb[1], attn.k_cache->nb[2], 0); // [#h, v, d]
ggml_build_forward_expand(mctx.gf, ggml_cpy(mctx.ctx_b.get(), virtual_key, k_cache_view));
ggml_tensor *virtual_value = ggml_view_3d(
mctx.ctx_b.get(), past_key_values, head_size, config.num_virtual_tokens, config.num_key_value_heads,
past_key_values->nb[1], past_key_values->nb[2], (i * 2 + 1) * past_key_values->nb[3]); // [#h, v, d]
virtual_value = ggml_permute(mctx.ctx_b.get(), virtual_value, 1, 0, 2, 3); // [#h, d, v]
ggml_tensor *v_cache_view =
ggml_view_3d(mctx.ctx_b.get(), attn.v_cache, config.num_virtual_tokens, head_size,
config.num_key_value_heads, attn.v_cache->nb[1], attn.v_cache->nb[2], 0); // [#h, d, v]
ggml_build_forward_expand(mctx.gf, ggml_cpy(mctx.ctx_b.get(), virtual_value, v_cache_view));
}
CHATGLM_CHECK(ggml_gallocr_alloc_graph(mctx.allocr.get(), mctx.gf));
CHATGLM_CHECK(ggml_backend_graph_compute(mctx.backend.get(), mctx.gf) == GGML_STATUS_SUCCESS);
}
private:
std::vector<Block> build_layers(ModelContext *mctx, const ModelConfig &config) {
std::vector<Block> layers;
layers.reserve(config.num_hidden_layers);
for (int layer_id = 0; layer_id < config.num_hidden_layers; layer_id++) {
layers.emplace_back(mctx, config.dtype, config.hidden_size, config.num_attention_heads,
config.num_key_value_heads, config.intermediate_size, config.max_length,
config.norm_eps, config.hidden_act, config.use_qkv_bias, config.use_dense_bias,
config.interleaved_qkv, config.rope_type, config.rope_theta, config.num_virtual_tokens,
true);
}
mctx->buf_kv =
unique_ggml_backend_buffer_t(ggml_backend_alloc_ctx_tensors(mctx->ctx_kv.get(), mctx->backend.get()));
return layers;
}
public:
Embedding word_embeddings;
std::vector<Block> layers;
Norm final_layernorm;
private:
PositionIdsAllocator pos_ids_alloc_;
};
class BaseStreamer {
public:
virtual ~BaseStreamer() = default;
virtual void put(const std::vector<int> &output_ids) = 0;
virtual void end() = 0;
};
class StreamerGroup : public BaseStreamer {
public:
StreamerGroup(std::vector<std::shared_ptr<BaseStreamer>> streamers) : streamers_(std::move(streamers)) {}
void put(const std::vector<int> &output_ids) override;
void end() override;
private:
std::vector<std::shared_ptr<BaseStreamer>> streamers_;
};
// reference: https://github.com/huggingface/transformers/blob/main/src/transformers/generation/streamers.py
class TextStreamer : public BaseStreamer {
public:
TextStreamer(std::ostream &os, BaseTokenizer *tokenizer)
: os_(os), tokenizer_(tokenizer), is_prompt_(true), is_first_line_(true), print_len_(0) {}
void put(const std::vector<int> &output_ids) override;
void end() override;
private:
std::ostream &os_;
BaseTokenizer *tokenizer_;
bool is_prompt_;
bool is_first_line_;
std::vector<int> token_cache_;
int print_len_;
};
class PerfStreamer : public BaseStreamer {
public:
PerfStreamer() : start_us_(0), prompt_us_(0), end_us_(0), num_prompt_tokens_(0), num_output_tokens_(0) {}
void put(const std::vector<int> &output_ids) override;
void end() override { end_us_ = ggml_time_us(); }
void reset();
std::string to_string() const;
int64_t num_prompt_tokens() const { return num_prompt_tokens_; }
int64_t prompt_total_time_us() const { return prompt_us_ - start_us_; }
int64_t prompt_token_time_us() const {
return num_prompt_tokens() ? prompt_total_time_us() / num_prompt_tokens() : 0;
}
int64_t num_output_tokens() const { return num_output_tokens_; }
int64_t output_total_time_us() const { return end_us_ - prompt_us_; }
int64_t output_token_time_us() const {
return num_output_tokens() ? output_total_time_us() / num_output_tokens() : 0;
}
private:
int64_t start_us_;
int64_t prompt_us_;
int64_t end_us_;
int64_t num_prompt_tokens_;
int64_t num_output_tokens_;
};
class MappedFile {
public:
MappedFile(const std::string &path);
~MappedFile();
public:
char *data;
size_t size;
};
struct StateDict {
unique_ggml_context_t ctx;
unique_ggml_backend_buffer_t buf;
std::unordered_map<std::string, ggml_tensor *> kv;
};
class ModelLoader {
public:
ModelLoader(char *data, size_t size) : data(data), size(size), ptr(data) {}
int64_t tell() const { return ptr - data; }
void seek(int64_t offset, int whence);
template <typename T>
T read_basic() {
T obj = *(T *)ptr;
ptr += sizeof(T);
return obj;
}
std::string read_string(size_t length);
StateDict read_state_dict();
private:
char *data;
size_t size;
char *ptr;
};
// ===== generation =====
struct GenerationConfig {
int max_length;
int max_new_tokens;
int max_context_length;
bool do_sample;
int top_k;
float top_p;
float temperature;
float repetition_penalty;
GenerationConfig(int max_length = 2048, int max_new_tokens = -1, int max_context_length = 512,
bool do_sample = true, int top_k = 0, float top_p = 0.7, float temperature = 0.95,
float repetition_penalty = 1.f)
: max_length(max_length), max_new_tokens(max_new_tokens), max_context_length(max_context_length),
do_sample(do_sample), top_k(top_k), top_p(top_p), temperature(temperature),
repetition_penalty(repetition_penalty) {}
};
struct TokenIdScore {
int id;
float score;
TokenIdScore() = default;
TokenIdScore(int id, float score) : id(id), score(score) {}
bool operator<(const TokenIdScore &other) const { return score < other.score; }
bool operator>(const TokenIdScore &other) const { return score > other.score; }
friend std::ostream &operator<<(std::ostream &os, const TokenIdScore &self) {
return os << "TokenIdScore(id=" << self.id << ", score=" << self.score << ")";
}
};
class BaseModelForCausalLM {
public:
BaseModelForCausalLM(ModelConfig config);
virtual ~BaseModelForCausalLM() = default;
virtual void load_state_dict(const StateDict &sd) = 0;
virtual ggml_tensor *forward(ModelContext *mctx, ggml_tensor *input_ids, ggml_tensor *images,
const std::vector<int> &input_ids_vec, int n_past, bool is_decoding) const = 0;
virtual void set_graph_inputs(const std::vector<int> &input_ids, const std::optional<Image> &image, int n_past,
int n_ctx) const = 0;
virtual int count_tokens(const std::vector<int> &input_ids, const std::optional<Image> &image) const = 0;
ggml_tensor *forward_graph_compute(const std::vector<int> &input_ids, const std::optional<Image> &image, int n_past,
int n_ctx, bool is_decoding);
std::vector<int> generate(const std::vector<int> &input_ids, const std::optional<Image> &image,
const GenerationConfig &gen_config, BaseStreamer *streamer = nullptr);
int generate_next_token(const std::vector<int> &input_ids, const std::optional<Image> &image,
const GenerationConfig &gen_config, int n_past, int n_ctx);
// logits processor
static void sampling_repetition_penalty(float *first, float *last, const std::vector<int> &input_ids,
float penalty);
// logits warper
static void sampling_temperature(float *first, float *last, float temp);
static void sampling_top_k(TokenIdScore *first, TokenIdScore *kth, TokenIdScore *last);
static TokenIdScore *sampling_top_p(TokenIdScore *first, TokenIdScore *last, float top_p);
static void sampling_softmax_inplace(TokenIdScore *first, TokenIdScore *last);
public:
ModelConfig config;
protected:
std::unique_ptr<ModelContext> mctx_;
};
template <typename Model>
class BasicModelForCausalLM : public BaseModelForCausalLM {
protected:
BasicModelForCausalLM(const ModelConfig &config)
: BaseModelForCausalLM(config), transformer(mctx_.get(), config),
lm_head(mctx_.get(), config.dtype, config.hidden_size, config.vocab_size, false) {
if (config.tie_word_embeddings) {
lm_head.weight = transformer.word_embeddings.weight;
}
}
public:
ggml_tensor *forward(ModelContext *mctx, ggml_tensor *input_ids, ggml_tensor *images,
const std::vector<int> &input_ids_vec, int n_past, bool is_decoding) const override {
ggml_tensor *transformer_outputs = transformer.forward(mctx, input_ids, images, input_ids_vec, n_past);
// NOTE: only compute next token logits for decoding
if (is_decoding && transformer_outputs->ne[1] > 1) {
transformer_outputs = ggml_view_1d(mctx->ctx_b.get(), transformer_outputs, transformer_outputs->ne[0],
(transformer_outputs->ne[1] - 1) * transformer_outputs->nb[1]);
}
ggml_tensor *lm_logits = lm_head.forward(mctx, transformer_outputs);
return lm_logits;
}
void set_graph_inputs(const std::vector<int> &input_ids, const std::optional<Image> &image, int n_past,
int n_ctx) const override {
transformer.set_graph_inputs(mctx_->gf, input_ids, image, n_past, n_ctx);
}
int count_tokens(const std::vector<int> &input_ids, const std::optional<Image> &image) const override {
CHATGLM_CHECK(!image) << "unimplemented";
return input_ids.size();
}
void load_prefix_cache(ggml_tensor *past_key_values) { transformer.load_prefix_cache(config, past_key_values); }
public:
Model transformer;
Linear lm_head;
};
// ===== ChatGLM-6B =====
class ChatGLMTokenizer : public BaseTokenizer {
public:
ChatGLMTokenizer(std::string_view serialized_model_proto);
std::vector<int> encode(const std::string &text, int max_length) const override;
std::string decode(const std::vector<int> &ids, bool skip_special_tokens = true) const override;
std::vector<int> apply_chat_template(const std::vector<ChatMessage> &messages, int max_length) const override;