cui-llama.rn 1.3.5 → 1.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/android/src/main/CMakeLists.txt +14 -8
  2. package/android/src/main/jni.cpp +38 -37
  3. package/cpp/common.cpp +43 -26
  4. package/cpp/common.h +18 -11
  5. package/cpp/ggml-backend-reg.cpp +5 -0
  6. package/cpp/ggml-backend.cpp +5 -2
  7. package/cpp/ggml-cpp.h +1 -0
  8. package/cpp/ggml-cpu-aarch64.cpp +6 -1
  9. package/cpp/ggml-cpu-quants.c +5 -1
  10. package/cpp/ggml-impl.h +11 -16
  11. package/cpp/ggml-metal.m +2 -2
  12. package/cpp/ggml.c +0 -1276
  13. package/cpp/ggml.h +0 -140
  14. package/cpp/gguf.cpp +1325 -0
  15. package/cpp/gguf.h +202 -0
  16. package/cpp/llama-adapter.cpp +346 -0
  17. package/cpp/llama-adapter.h +73 -0
  18. package/cpp/llama-arch.cpp +1434 -0
  19. package/cpp/llama-arch.h +395 -0
  20. package/cpp/llama-batch.cpp +368 -0
  21. package/cpp/llama-batch.h +88 -0
  22. package/cpp/llama-chat.cpp +567 -0
  23. package/cpp/llama-chat.h +51 -0
  24. package/cpp/llama-context.cpp +1771 -0
  25. package/cpp/llama-context.h +128 -0
  26. package/cpp/llama-cparams.cpp +1 -0
  27. package/cpp/llama-cparams.h +37 -0
  28. package/cpp/llama-cpp.h +30 -0
  29. package/cpp/llama-grammar.cpp +1 -0
  30. package/cpp/llama-grammar.h +3 -1
  31. package/cpp/llama-hparams.cpp +71 -0
  32. package/cpp/llama-hparams.h +140 -0
  33. package/cpp/llama-impl.cpp +167 -0
  34. package/cpp/llama-impl.h +16 -136
  35. package/cpp/llama-kv-cache.cpp +718 -0
  36. package/cpp/llama-kv-cache.h +218 -0
  37. package/cpp/llama-mmap.cpp +589 -0
  38. package/cpp/llama-mmap.h +67 -0
  39. package/cpp/llama-model-loader.cpp +1011 -0
  40. package/cpp/llama-model-loader.h +158 -0
  41. package/cpp/llama-model.cpp +2202 -0
  42. package/cpp/llama-model.h +391 -0
  43. package/cpp/llama-sampling.cpp +117 -4
  44. package/cpp/llama-vocab.cpp +21 -28
  45. package/cpp/llama-vocab.h +13 -1
  46. package/cpp/llama.cpp +8437 -19421
  47. package/cpp/llama.cpp.rej +23 -0
  48. package/cpp/llama.h +31 -6
  49. package/cpp/rn-llama.hpp +39 -37
  50. package/cpp/sgemm.cpp +776 -70
  51. package/cpp/unicode.cpp +6 -0
  52. package/package.json +1 -1
@@ -0,0 +1,391 @@
1
+ #pragma once
2
+
3
+ #include "llama.h"
4
+ #include "llama-arch.h"
5
+ #include "llama-hparams.h"
6
+ #include "llama-vocab.h"
7
+ #include "llama-mmap.h"
8
+
9
+ #include "ggml-cpp.h"
10
+
11
+ #include <vector>
12
+
13
+ // available models
14
+ // TODO: this enum does not follow the enum naming convention
15
+ enum llm_type {
16
+ MODEL_UNKNOWN,
17
+ MODEL_14M,
18
+ MODEL_17M,
19
+ MODEL_22M,
20
+ MODEL_33M,
21
+ MODEL_60M,
22
+ MODEL_70M,
23
+ MODEL_80M,
24
+ MODEL_109M,
25
+ MODEL_137M,
26
+ MODEL_160M,
27
+ MODEL_220M,
28
+ MODEL_250M,
29
+ MODEL_270M,
30
+ MODEL_335M,
31
+ MODEL_410M,
32
+ MODEL_450M,
33
+ MODEL_770M,
34
+ MODEL_780M,
35
+ MODEL_0_5B,
36
+ MODEL_1B,
37
+ MODEL_1_3B,
38
+ MODEL_1_4B,
39
+ MODEL_1_5B,
40
+ MODEL_1_6B,
41
+ MODEL_2B,
42
+ MODEL_2_8B,
43
+ MODEL_3B,
44
+ MODEL_4B,
45
+ MODEL_6B,
46
+ MODEL_6_9B,
47
+ MODEL_7B,
48
+ MODEL_8B,
49
+ MODEL_9B,
50
+ MODEL_11B,
51
+ MODEL_12B,
52
+ MODEL_13B,
53
+ MODEL_14B,
54
+ MODEL_15B,
55
+ MODEL_16B,
56
+ MODEL_20B,
57
+ MODEL_30B,
58
+ MODEL_32B,
59
+ MODEL_34B,
60
+ MODEL_35B,
61
+ MODEL_40B,
62
+ MODEL_65B,
63
+ MODEL_70B,
64
+ MODEL_236B,
65
+ MODEL_314B,
66
+ MODEL_671B,
67
+ MODEL_SMALL,
68
+ MODEL_MEDIUM,
69
+ MODEL_LARGE,
70
+ MODEL_XL,
71
+ MODEL_A1_7B,
72
+ MODEL_A2_7B,
73
+ MODEL_8x7B,
74
+ MODEL_8x22B,
75
+ MODEL_16x12B,
76
+ MODEL_10B_128x3_66B,
77
+ MODEL_57B_A14B,
78
+ MODEL_27B,
79
+ };
80
+
81
+ struct llama_layer_posnet {
82
+ // resnet
83
+ struct lm_ggml_tensor * norm1 = nullptr;
84
+ struct lm_ggml_tensor * norm1_b = nullptr;
85
+
86
+ struct lm_ggml_tensor * conv1 = nullptr;
87
+ struct lm_ggml_tensor * conv1_b = nullptr;
88
+
89
+ struct lm_ggml_tensor * norm2 = nullptr;
90
+ struct lm_ggml_tensor * norm2_b = nullptr;
91
+
92
+ struct lm_ggml_tensor * conv2 = nullptr;
93
+ struct lm_ggml_tensor * conv2_b = nullptr;
94
+
95
+ // attention
96
+ struct lm_ggml_tensor * attn_norm = nullptr;
97
+ struct lm_ggml_tensor * attn_norm_b = nullptr;
98
+
99
+ struct lm_ggml_tensor * attn_q = nullptr;
100
+ struct lm_ggml_tensor * attn_q_b = nullptr;
101
+
102
+ struct lm_ggml_tensor * attn_k = nullptr;
103
+ struct lm_ggml_tensor * attn_k_b = nullptr;
104
+
105
+ struct lm_ggml_tensor * attn_v = nullptr;
106
+ struct lm_ggml_tensor * attn_v_b = nullptr;
107
+
108
+ struct lm_ggml_tensor * attn_o = nullptr;
109
+ struct lm_ggml_tensor * attn_o_b = nullptr;
110
+
111
+ // normalize
112
+ struct lm_ggml_tensor * norm = nullptr;
113
+ struct lm_ggml_tensor * norm_b = nullptr;
114
+ };
115
+
116
+ struct llama_layer_convnext {
117
+ struct lm_ggml_tensor * dw = nullptr;
118
+ struct lm_ggml_tensor * dw_b = nullptr;
119
+
120
+ struct lm_ggml_tensor * norm = nullptr;
121
+ struct lm_ggml_tensor * norm_b = nullptr;
122
+
123
+ struct lm_ggml_tensor * pw1 = nullptr;
124
+ struct lm_ggml_tensor * pw1_b = nullptr;
125
+
126
+ struct lm_ggml_tensor * pw2 = nullptr;
127
+ struct lm_ggml_tensor * pw2_b = nullptr;
128
+
129
+ struct lm_ggml_tensor * gamma = nullptr;
130
+ };
131
+
132
+ struct llama_layer {
133
+ // normalization
134
+ struct lm_ggml_tensor * attn_norm = nullptr;
135
+ struct lm_ggml_tensor * attn_norm_b = nullptr;
136
+ struct lm_ggml_tensor * attn_norm_2 = nullptr;
137
+ struct lm_ggml_tensor * attn_norm_2_b = nullptr;
138
+ struct lm_ggml_tensor * attn_q_norm = nullptr;
139
+ struct lm_ggml_tensor * attn_q_norm_b = nullptr;
140
+ struct lm_ggml_tensor * attn_k_norm = nullptr;
141
+ struct lm_ggml_tensor * attn_k_norm_b = nullptr;
142
+ struct lm_ggml_tensor * attn_out_norm = nullptr;
143
+ struct lm_ggml_tensor * attn_out_norm_b = nullptr;
144
+ struct lm_ggml_tensor * attn_q_a_norm = nullptr;
145
+ struct lm_ggml_tensor * attn_kv_a_norm = nullptr;
146
+ struct lm_ggml_tensor * attn_sub_norm = nullptr;
147
+ struct lm_ggml_tensor * attn_post_norm = nullptr;
148
+ struct lm_ggml_tensor * ffn_sub_norm = nullptr;
149
+ struct lm_ggml_tensor * attn_norm_cross = nullptr;
150
+ struct lm_ggml_tensor * attn_norm_enc = nullptr;
151
+
152
+ // attention
153
+ struct lm_ggml_tensor * wq = nullptr;
154
+ struct lm_ggml_tensor * wk = nullptr;
155
+ struct lm_ggml_tensor * wv = nullptr;
156
+ struct lm_ggml_tensor * wo = nullptr;
157
+ struct lm_ggml_tensor * wqkv = nullptr;
158
+ struct lm_ggml_tensor * wq_a = nullptr;
159
+ struct lm_ggml_tensor * wq_b = nullptr;
160
+ struct lm_ggml_tensor * wkv_a_mqa = nullptr;
161
+ struct lm_ggml_tensor * wkv_b = nullptr;
162
+ struct lm_ggml_tensor * wq_cross = nullptr;
163
+ struct lm_ggml_tensor * wk_cross = nullptr;
164
+ struct lm_ggml_tensor * wv_cross = nullptr;
165
+ struct lm_ggml_tensor * wo_cross = nullptr;
166
+ struct lm_ggml_tensor * wq_enc = nullptr;
167
+ struct lm_ggml_tensor * wk_enc = nullptr;
168
+ struct lm_ggml_tensor * wv_enc = nullptr;
169
+ struct lm_ggml_tensor * wo_enc = nullptr;
170
+
171
+ // attention bias
172
+ struct lm_ggml_tensor * bq = nullptr;
173
+ struct lm_ggml_tensor * bk = nullptr;
174
+ struct lm_ggml_tensor * bv = nullptr;
175
+ struct lm_ggml_tensor * bo = nullptr;
176
+ struct lm_ggml_tensor * bqkv = nullptr;
177
+
178
+ // relative position bias
179
+ struct lm_ggml_tensor * attn_rel_b = nullptr;
180
+ struct lm_ggml_tensor * attn_rel_b_enc = nullptr;
181
+ struct lm_ggml_tensor * attn_rel_b_cross = nullptr;
182
+
183
+ // normalization
184
+ struct lm_ggml_tensor * ffn_norm = nullptr;
185
+ struct lm_ggml_tensor * ffn_norm_b = nullptr;
186
+ struct lm_ggml_tensor * ffn_post_norm = nullptr;
187
+ struct lm_ggml_tensor * layer_out_norm = nullptr;
188
+ struct lm_ggml_tensor * layer_out_norm_b = nullptr;
189
+ struct lm_ggml_tensor * ffn_norm_exps = nullptr;
190
+ struct lm_ggml_tensor * ffn_norm_enc = nullptr;
191
+
192
+ // ff
193
+ struct lm_ggml_tensor * ffn_gate = nullptr; // w1
194
+ struct lm_ggml_tensor * ffn_down = nullptr; // w2
195
+ struct lm_ggml_tensor * ffn_up = nullptr; // w3
196
+ struct lm_ggml_tensor * ffn_gate_enc = nullptr;
197
+ struct lm_ggml_tensor * ffn_down_enc = nullptr;
198
+ struct lm_ggml_tensor * ffn_up_enc = nullptr;
199
+
200
+ // ff MoE
201
+ struct lm_ggml_tensor * ffn_gate_inp = nullptr;
202
+ struct lm_ggml_tensor * ffn_gate_exps = nullptr;
203
+ struct lm_ggml_tensor * ffn_down_exps = nullptr;
204
+ struct lm_ggml_tensor * ffn_up_exps = nullptr;
205
+
206
+ // ff shared expert (shexp)
207
+ struct lm_ggml_tensor * ffn_gate_inp_shexp = nullptr;
208
+ struct lm_ggml_tensor * ffn_gate_shexp = nullptr;
209
+ struct lm_ggml_tensor * ffn_down_shexp = nullptr;
210
+ struct lm_ggml_tensor * ffn_up_shexp = nullptr;
211
+
212
+ // ff bias
213
+ struct lm_ggml_tensor * ffn_gate_b = nullptr;
214
+ struct lm_ggml_tensor * ffn_down_b = nullptr; // b2
215
+ struct lm_ggml_tensor * ffn_up_b = nullptr; // b3
216
+ struct lm_ggml_tensor * ffn_act = nullptr;
217
+ struct lm_ggml_tensor * ffn_exp_probs_b = nullptr;
218
+
219
+ // mamba proj
220
+ struct lm_ggml_tensor * ssm_in = nullptr;
221
+ struct lm_ggml_tensor * ssm_x = nullptr;
222
+ struct lm_ggml_tensor * ssm_dt = nullptr;
223
+ struct lm_ggml_tensor * ssm_out = nullptr;
224
+
225
+ // mamba
226
+ struct lm_ggml_tensor * ssm_conv1d = nullptr;
227
+ struct lm_ggml_tensor * ssm_a = nullptr;
228
+ struct lm_ggml_tensor * ssm_d = nullptr;
229
+
230
+ // mamba bias
231
+ struct lm_ggml_tensor * ssm_conv1d_b = nullptr;
232
+ struct lm_ggml_tensor * ssm_dt_b = nullptr;
233
+
234
+ // rwkv
235
+ struct lm_ggml_tensor * time_mix_w1 = nullptr;
236
+ struct lm_ggml_tensor * time_mix_w2 = nullptr;
237
+ struct lm_ggml_tensor * time_mix_lerp_x = nullptr;
238
+ struct lm_ggml_tensor * time_mix_lerp_w = nullptr;
239
+ struct lm_ggml_tensor * time_mix_lerp_k = nullptr;
240
+ struct lm_ggml_tensor * time_mix_lerp_v = nullptr;
241
+ struct lm_ggml_tensor * time_mix_lerp_r = nullptr;
242
+ struct lm_ggml_tensor * time_mix_lerp_g = nullptr;
243
+
244
+ struct lm_ggml_tensor * time_mix_first = nullptr;
245
+ struct lm_ggml_tensor * time_mix_decay = nullptr;
246
+ struct lm_ggml_tensor * time_mix_decay_w1 = nullptr;
247
+ struct lm_ggml_tensor * time_mix_decay_w2 = nullptr;
248
+ struct lm_ggml_tensor * time_mix_key = nullptr;
249
+ struct lm_ggml_tensor * time_mix_value = nullptr;
250
+ struct lm_ggml_tensor * time_mix_receptance = nullptr;
251
+ struct lm_ggml_tensor * time_mix_gate = nullptr;
252
+
253
+ struct lm_ggml_tensor * time_mix_ln = nullptr;
254
+ struct lm_ggml_tensor * time_mix_ln_b = nullptr;
255
+ struct lm_ggml_tensor * time_mix_output = nullptr;
256
+
257
+ struct lm_ggml_tensor * channel_mix_lerp_k = nullptr;
258
+ struct lm_ggml_tensor * channel_mix_lerp_r = nullptr;
259
+
260
+ struct lm_ggml_tensor * channel_mix_key = nullptr;
261
+ struct lm_ggml_tensor * channel_mix_receptance = nullptr;
262
+ struct lm_ggml_tensor * channel_mix_value = nullptr;
263
+
264
+ // long rope factors
265
+ struct lm_ggml_tensor * rope_long = nullptr;
266
+ struct lm_ggml_tensor * rope_short = nullptr;
267
+ struct lm_ggml_tensor * rope_freqs = nullptr;
268
+
269
+ // bitnet scale
270
+ struct lm_ggml_tensor * wq_scale = nullptr;
271
+ struct lm_ggml_tensor * wk_scale = nullptr;
272
+ struct lm_ggml_tensor * wv_scale = nullptr;
273
+ struct lm_ggml_tensor * wo_scale = nullptr;
274
+ struct lm_ggml_tensor * ffn_gate_scale = nullptr;
275
+ struct lm_ggml_tensor * ffn_up_scale = nullptr;
276
+ struct lm_ggml_tensor * ffn_down_scale = nullptr;
277
+
278
+ struct llama_layer_posnet posnet;
279
+
280
+ struct llama_layer_convnext convnext;
281
+ };
282
+
283
+ struct llama_model {
284
+ llm_type type = MODEL_UNKNOWN;
285
+ llm_arch arch = LLM_ARCH_UNKNOWN;
286
+
287
+ llama_ftype ftype = LLAMA_FTYPE_ALL_F32;
288
+
289
+ std::string name = "n/a";
290
+
291
+ llama_hparams hparams = {};
292
+ llama_vocab vocab;
293
+
294
+ struct lm_ggml_tensor * tok_embd = nullptr;
295
+ struct lm_ggml_tensor * type_embd = nullptr;
296
+ struct lm_ggml_tensor * pos_embd = nullptr;
297
+ struct lm_ggml_tensor * tok_norm = nullptr;
298
+ struct lm_ggml_tensor * tok_norm_b = nullptr;
299
+
300
+ struct lm_ggml_tensor * output_norm = nullptr;
301
+ struct lm_ggml_tensor * output_norm_b = nullptr;
302
+ struct lm_ggml_tensor * output = nullptr;
303
+ struct lm_ggml_tensor * output_b = nullptr;
304
+ struct lm_ggml_tensor * output_norm_enc = nullptr;
305
+
306
+ // classifier
307
+ struct lm_ggml_tensor * cls = nullptr;
308
+ struct lm_ggml_tensor * cls_b = nullptr;
309
+ struct lm_ggml_tensor * cls_out = nullptr;
310
+ struct lm_ggml_tensor * cls_out_b = nullptr;
311
+
312
+ struct lm_ggml_tensor * conv1d = nullptr;
313
+ struct lm_ggml_tensor * conv1d_b = nullptr;
314
+
315
+ std::vector<llama_layer> layers;
316
+
317
+ // gguf metadata
318
+ std::unordered_map<std::string, std::string> lm_gguf_kv;
319
+
320
+ llama_split_mode split_mode;
321
+ int main_gpu;
322
+ int n_gpu_layers;
323
+
324
+ std::vector<std::string> rpc_servers;
325
+
326
+ // list of devices used in this model
327
+ std::vector<lm_ggml_backend_dev_t> devices;
328
+
329
+
330
+ // lists of buffer types used for each layer
331
+ using buft_list_t = std::vector<std::pair<lm_ggml_backend_dev_t, lm_ggml_backend_buffer_type_t>>;
332
+ buft_list_t cpu_buft_list;
333
+ std::map<lm_ggml_backend_dev_t, buft_list_t> gpu_buft_list;
334
+
335
+ struct layer_dev {
336
+ lm_ggml_backend_dev_t dev;
337
+ buft_list_t * buft_list;
338
+ };
339
+
340
+ layer_dev dev_input = {};
341
+ layer_dev dev_output = {};
342
+ std::vector<layer_dev> dev_layer;
343
+
344
+ // contexts where the model tensors metadata is stored
345
+ std::vector<lm_ggml_context_ptr> ctxs;
346
+
347
+ // the model memory buffers for the tensor data
348
+ std::vector<lm_ggml_backend_buffer_ptr> bufs;
349
+
350
+ // model memory mapped files
351
+ llama_mmaps mappings;
352
+
353
+ // objects representing data potentially being locked in memory
354
+ llama_mlocks mlock_bufs;
355
+ llama_mlocks mlock_mmaps;
356
+
357
+ // for quantize-stats only
358
+ std::vector<std::pair<std::string, struct lm_ggml_tensor *>> tensors_by_name;
359
+
360
+ int64_t t_load_us = 0;
361
+ int64_t t_start_us = 0;
362
+
363
+ // total number of parameters in the model
364
+ uint64_t n_elements = 0;
365
+
366
+ // total size of all the tensors in the model in bytes
367
+ size_t n_bytes = 0;
368
+ };
369
+
370
+ const char * llm_type_name(llm_type type);
371
+
372
+ std::string llama_model_arch_name (const llama_model & model);
373
+ std::string llama_model_type_name (const llama_model & model);
374
+ std::string llama_model_ftype_name(const llama_model & model);
375
+
376
+ // used by llama_adapter_cvec
377
+ lm_ggml_backend_buffer_type_t llama_model_select_buft(const llama_model & model, int il);
378
+
379
+ // used by llama_adapter_lora
380
+ struct lm_ggml_tensor * llama_model_get_tensor(const struct llama_model & model, const char * name);
381
+
382
+ size_t llama_model_max_nodes(const llama_model & model);
383
+
384
+ struct llama_model_loader;
385
+
386
+ // TODO: become llama_model methods
387
+ void llm_load_stats (llama_model_loader & ml, llama_model & model);
388
+ void llm_load_arch (llama_model_loader & ml, llama_model & model);
389
+ void llm_load_hparams (llama_model_loader & ml, llama_model & model);
390
+ void llm_load_vocab (llama_model_loader & ml, llama_model & model);
391
+ void llm_load_print_meta(llama_model_loader & ml, llama_model & model);
@@ -1,5 +1,6 @@
1
1
  #include "llama-sampling.h"
2
2
 
3
+ #include "llama-impl.h"
3
4
  #include "llama-vocab.h"
4
5
  #include "llama-grammar.h"
5
6
 
@@ -14,6 +15,118 @@
14
15
  #include <numeric>
15
16
  #include <random>
16
17
  #include <unordered_map>
18
+ #include <stdexcept>
19
+
20
+ // the ring buffer works similarly to std::deque, but with a fixed capacity
21
+ template<typename T>
22
+ struct ring_buffer {
23
+ ring_buffer(size_t cap) : capacity(cap), data(cap) {}
24
+
25
+ T & front() {
26
+ if (sz == 0) {
27
+ throw std::runtime_error("ring buffer is empty");
28
+ }
29
+ return data[first];
30
+ }
31
+
32
+ const T & front() const {
33
+ if (sz == 0) {
34
+ throw std::runtime_error("ring buffer is empty");
35
+ }
36
+ return data[first];
37
+ }
38
+
39
+ T & back() {
40
+ if (sz == 0) {
41
+ throw std::runtime_error("ring buffer is empty");
42
+ }
43
+ return data[pos];
44
+ }
45
+
46
+ const T & back() const {
47
+ if (sz == 0) {
48
+ throw std::runtime_error("ring buffer is empty");
49
+ }
50
+ return data[pos];
51
+ }
52
+
53
+ void push_back(const T & value) {
54
+ if (capacity == 0) {
55
+ throw std::runtime_error("ring buffer: capacity is zero");
56
+ }
57
+
58
+ if (sz == capacity) {
59
+ // advance the start when buffer is full
60
+ first = (first + 1) % capacity;
61
+ } else {
62
+ sz++;
63
+ }
64
+ data[pos] = value;
65
+ pos = (pos + 1) % capacity;
66
+ }
67
+
68
+ T pop_front() {
69
+ if (sz == 0) {
70
+ throw std::runtime_error("ring buffer is empty");
71
+ }
72
+ T value = data[first];
73
+ first = (first + 1) % capacity;
74
+ sz--;
75
+ return value;
76
+ }
77
+
78
+ //T & operator[](size_t i) {
79
+ // if (i >= sz) {
80
+ // throw std::runtime_error("ring buffer: index out of bounds");
81
+ // }
82
+ // return data[(first + i) % capacity];
83
+ //}
84
+
85
+ //const T & at(size_t i) const {
86
+ // if (i >= sz) {
87
+ // throw std::runtime_error("ring buffer: index out of bounds");
88
+ // }
89
+ // return data[(first + i) % capacity];
90
+ //}
91
+
92
+ const T & rat(size_t i) const {
93
+ if (i >= sz) {
94
+ throw std::runtime_error("ring buffer: index out of bounds");
95
+ }
96
+ return data[(first + sz - i - 1) % capacity];
97
+ }
98
+
99
+ std::vector<T> to_vector() const {
100
+ std::vector<T> result;
101
+ result.reserve(sz);
102
+ for (size_t i = 0; i < sz; i++) {
103
+ result.push_back(data[(first + i) % capacity]);
104
+ }
105
+ return result;
106
+ }
107
+
108
+ void clear() {
109
+ // here only reset the status of the buffer
110
+ sz = 0;
111
+ first = 0;
112
+ pos = 0;
113
+ }
114
+
115
+ bool empty() const {
116
+ return sz == 0;
117
+ }
118
+
119
+ size_t size() const {
120
+ return sz;
121
+ }
122
+
123
+ size_t capacity = 0;
124
+ size_t sz = 0;
125
+ size_t first = 0;
126
+ size_t pos = 0;
127
+
128
+ std::vector<T> data;
129
+ };
17
130
 
18
131
  static int llama_sample_dist(llama_token_data_array * cur_p, std::mt19937 & rng) {
19
132
  // iterator for the probabilities
@@ -144,7 +257,7 @@ static void llama_sampler_top_k_impl(llama_token_data_array * cur_p, int32_t k)
144
257
  for (int i = 0; i < (int)cur_p->size; ++i) {
145
258
  const float val = cur_p->data[i].logit;
146
259
  int ib = int(bucket_scale * val + bucket_inter); //nbuckets * (val - bucket_low) / (bucket_high - bucket_low);
147
- ib = std::max(0, std::min(nbuckets-1, ib));
260
+ ib = std::max(0, std::min(nbuckets - 1, ib));
148
261
  bucket_idx[i] = ib;
149
262
  ++histo[ib];
150
263
  }
@@ -167,13 +280,13 @@ static void llama_sampler_top_k_impl(llama_token_data_array * cur_p, int32_t k)
167
280
  for (int i = 0; i < (int)cur_p->size; ++i) {
168
281
  int j = bucket_idx[i];
169
282
  if (j >= ib) {
170
- *bucket_ptrs[nbuckets-1-j]++ = cur_p->data[i];
283
+ *bucket_ptrs[nbuckets - 1 - j]++ = cur_p->data[i];
171
284
  }
172
285
  }
173
286
 
174
287
  ptr = tmp_tokens.data();
175
288
  int ndone = 0;
176
- for (int j = nbuckets-1; j > ib; --j) {
289
+ for (int j = nbuckets - 1; j > ib; --j) {
177
290
  std::sort(ptr, ptr + histo[j], comp);
178
291
  ptr += histo[j];
179
292
  ndone += histo[j];
@@ -1720,7 +1833,7 @@ static void llama_sampler_dry_apply(struct llama_sampler * smpl, llama_token_dat
1720
1833
  ctx->dry_repeat_count[last - k] = std::min(n, rep_limit);
1721
1834
  if (n > 0) {
1722
1835
  lt = k;
1723
- rt = k+n-1;
1836
+ rt = k + n - 1;
1724
1837
  }
1725
1838
  } else {
1726
1839
  // If k is inside the current Z-box, consider two cases.
@@ -1,5 +1,7 @@
1
1
  #include "llama-vocab.h"
2
2
 
3
+ #include "llama-impl.h"
4
+
3
5
  #include "unicode.h"
4
6
 
5
7
  #include <algorithm>
@@ -16,22 +18,6 @@
16
18
  // helpers
17
19
  //
18
20
 
19
- LLAMA_ATTRIBUTE_FORMAT(1, 2)
20
- static std::string format(const char * fmt, ...) {
21
- va_list ap;
22
- va_list ap2;
23
- va_start(ap, fmt);
24
- va_copy(ap2, ap);
25
- int size = vsnprintf(NULL, 0, fmt, ap);
26
- LM_GGML_ASSERT(size >= 0 && size < INT_MAX); // NOLINT
27
- std::vector<char> buf(size + 1);
28
- int size2 = vsnprintf(buf.data(), size + 1, fmt, ap2);
29
- LM_GGML_ASSERT(size2 == size);
30
- va_end(ap2);
31
- va_end(ap);
32
- return std::string(buf.data(), size);
33
- }
34
-
35
21
  struct naive_trie {
36
22
  naive_trie() : has_value(false), value(0) {
37
23
  }
@@ -396,6 +382,13 @@ struct llm_tokenizer_bpe : llm_tokenizer {
396
382
  "\\p{N}+",
397
383
  };
398
384
  break;
385
+ case LLAMA_VOCAB_PRE_TYPE_DEEPSEEK3_LLM:
386
+ regex_exprs = {
387
+ "\\p{N}{1,3}",
388
+ "[一-龥぀-ゟ゠-ヿ]+",
389
+ "[!\"#$%&'()*+,\\-./:;<=>?@\\[\\\\\\]^_`{|}~][A-Za-z]+|[^\r\n\\p{L}\\p{P}\\p{S}]?[\\p{L}\\p{M}]+| ?[\\p{P}\\p{S}]+[\r\n]*|\\s*[\r\n]+|\\s+(?!\\S)|\\s+",
390
+ };
391
+ break;
399
392
  case LLAMA_VOCAB_PRE_TYPE_DEEPSEEK_CODER:
400
393
  regex_exprs = {
401
394
  "[\r\n]",
@@ -504,7 +497,7 @@ struct llm_tokenizer_bpe_session {
504
497
 
505
498
  bool append_bos(std::vector<llama_vocab::id> & output) const {
506
499
  if (vocab.tokenizer_add_bos) {
507
- LM_GGML_ASSERT(vocab.special_bos_id != -1);
500
+ LM_GGML_ASSERT(vocab.special_bos_id != LLAMA_TOKEN_NULL);
508
501
  output.push_back(vocab.special_bos_id);
509
502
  return true;
510
503
  }
@@ -513,7 +506,7 @@ struct llm_tokenizer_bpe_session {
513
506
 
514
507
  bool append_eos(std::vector<llama_vocab::id> & output) const {
515
508
  if (vocab.tokenizer_add_eos) {
516
- LM_GGML_ASSERT(vocab.special_eos_id != -1);
509
+ LM_GGML_ASSERT(vocab.special_eos_id != LLAMA_TOKEN_NULL);
517
510
  output.push_back(vocab.special_eos_id);
518
511
  return true;
519
512
  }
@@ -1410,7 +1403,7 @@ static void tokenizer_st_partition(const llama_vocab & vocab, std::forward_list<
1410
1403
  if (source == 0) {
1411
1404
  buffer.erase_after(buffer.before_begin());
1412
1405
  } else {
1413
- buffer.erase_after(std::next(buffer.begin(), (source-1)));
1406
+ buffer.erase_after(std::next(buffer.begin(), (source - 1)));
1414
1407
  }
1415
1408
 
1416
1409
  // repeat for the right side
@@ -1424,7 +1417,7 @@ static void tokenizer_st_partition(const llama_vocab & vocab, std::forward_list<
1424
1417
  if (source == 0) {
1425
1418
  buffer.erase_after(buffer.before_begin());
1426
1419
  } else {
1427
- buffer.erase_after(std::next(buffer.begin(), (source-1)));
1420
+ buffer.erase_after(std::next(buffer.begin(), (source - 1)));
1428
1421
  }
1429
1422
  break;
1430
1423
  }
@@ -1461,7 +1454,7 @@ std::vector<llama_vocab::id> llama_tokenize_internal(
1461
1454
  bool is_prev_special = true; // prefix with space if first token
1462
1455
 
1463
1456
  if (add_special && vocab.tokenizer_add_bos) {
1464
- LM_GGML_ASSERT(vocab.special_bos_id != -1);
1457
+ LM_GGML_ASSERT(vocab.special_bos_id != LLAMA_TOKEN_NULL);
1465
1458
  output.push_back(vocab.special_bos_id);
1466
1459
  is_prev_special = true;
1467
1460
  }
@@ -1496,7 +1489,7 @@ std::vector<llama_vocab::id> llama_tokenize_internal(
1496
1489
  }
1497
1490
 
1498
1491
  if (add_special && vocab.tokenizer_add_eos) {
1499
- LM_GGML_ASSERT(vocab.special_eos_id != -1);
1492
+ LM_GGML_ASSERT(vocab.special_eos_id != LLAMA_TOKEN_NULL);
1500
1493
  output.push_back(vocab.special_eos_id);
1501
1494
  }
1502
1495
  } break;
@@ -1529,7 +1522,7 @@ std::vector<llama_vocab::id> llama_tokenize_internal(
1529
1522
  case LLAMA_VOCAB_TYPE_WPM:
1530
1523
  {
1531
1524
  if (add_special) {
1532
- LM_GGML_ASSERT(vocab.special_cls_id != -1);
1525
+ LM_GGML_ASSERT(vocab.special_cls_id != LLAMA_TOKEN_NULL);
1533
1526
  output.push_back(vocab.special_cls_id);
1534
1527
  }
1535
1528
 
@@ -1549,14 +1542,14 @@ std::vector<llama_vocab::id> llama_tokenize_internal(
1549
1542
  }
1550
1543
 
1551
1544
  if (add_special) {
1552
- LM_GGML_ASSERT(vocab.special_sep_id != -1);
1545
+ LM_GGML_ASSERT(vocab.special_sep_id != LLAMA_TOKEN_NULL);
1553
1546
  output.push_back(vocab.special_sep_id);
1554
1547
  }
1555
1548
  } break;
1556
1549
  case LLAMA_VOCAB_TYPE_UGM:
1557
1550
  {
1558
1551
  if (add_special && vocab.tokenizer_add_bos) {
1559
- LM_GGML_ASSERT(vocab.special_bos_id != -1);
1552
+ LM_GGML_ASSERT(vocab.special_bos_id != LLAMA_TOKEN_NULL);
1560
1553
  output.push_back(vocab.special_bos_id);
1561
1554
  }
1562
1555
  llm_tokenizer_ugm_session session(vocab);
@@ -1581,7 +1574,7 @@ std::vector<llama_vocab::id> llama_tokenize_internal(
1581
1574
  }
1582
1575
 
1583
1576
  if (add_special && vocab.tokenizer_add_eos) {
1584
- LM_GGML_ASSERT(vocab.special_eos_id != -1);
1577
+ LM_GGML_ASSERT(vocab.special_eos_id != LLAMA_TOKEN_NULL);
1585
1578
  output.push_back(vocab.special_eos_id);
1586
1579
  }
1587
1580
  } break;
@@ -1649,7 +1642,7 @@ llama_token_attr llama_token_get_attr_impl(const struct llama_vocab & vocab, lla
1649
1642
  }
1650
1643
 
1651
1644
  bool llama_token_is_eog_impl(const struct llama_vocab & vocab, llama_token token) {
1652
- return token != -1 && vocab.special_eog_ids.count(token) > 0;
1645
+ return token != LLAMA_TOKEN_NULL && vocab.special_eog_ids.count(token) > 0;
1653
1646
  }
1654
1647
 
1655
1648
  bool llama_token_is_control_impl(const struct llama_vocab & vocab, llama_token token) {
@@ -1888,7 +1881,7 @@ int32_t llama_detokenize_impl(
1888
1881
  }
1889
1882
 
1890
1883
  if (remove_special && vocab.tokenizer_add_eos) {
1891
- if (n_tokens > 0 && tokens[n_tokens-1] == vocab.special_eos_id) {
1884
+ if (n_tokens > 0 && tokens[n_tokens - 1] == vocab.special_eos_id) {
1892
1885
  n_tokens--;
1893
1886
  }
1894
1887
  }