cui-llama.rn 1.3.4 → 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.
- package/android/src/main/CMakeLists.txt +14 -8
- package/android/src/main/jni.cpp +38 -37
- package/cpp/common.cpp +50 -30
- package/cpp/common.h +32 -13
- package/cpp/ggml-alloc.c +0 -1
- package/cpp/ggml-backend-reg.cpp +79 -49
- package/cpp/ggml-backend.cpp +5 -2
- package/cpp/ggml-cpp.h +1 -0
- package/cpp/ggml-cpu-aarch64.cpp +57 -72
- package/cpp/ggml-cpu-quants.c +5 -1
- package/cpp/ggml-cpu.c +6 -6
- package/cpp/ggml-cpu.cpp +9 -0
- package/cpp/ggml-impl.h +11 -0
- package/cpp/ggml-metal.m +2 -2
- package/cpp/ggml.c +129 -1388
- package/cpp/ggml.h +29 -152
- package/cpp/gguf.cpp +1325 -0
- package/cpp/gguf.h +202 -0
- package/cpp/llama-adapter.cpp +346 -0
- package/cpp/llama-adapter.h +73 -0
- package/cpp/llama-arch.cpp +1434 -0
- package/cpp/llama-arch.h +395 -0
- package/cpp/llama-batch.cpp +368 -0
- package/cpp/llama-batch.h +88 -0
- package/cpp/llama-chat.cpp +567 -0
- package/cpp/llama-chat.h +51 -0
- package/cpp/llama-context.cpp +1771 -0
- package/cpp/llama-context.h +128 -0
- package/cpp/llama-cparams.cpp +1 -0
- package/cpp/llama-cparams.h +37 -0
- package/cpp/llama-cpp.h +30 -0
- package/cpp/llama-grammar.cpp +16 -15
- package/cpp/llama-grammar.h +5 -6
- package/cpp/llama-hparams.cpp +71 -0
- package/cpp/llama-hparams.h +140 -0
- package/cpp/llama-impl.cpp +167 -0
- package/cpp/llama-impl.h +16 -136
- package/cpp/llama-kv-cache.cpp +718 -0
- package/cpp/llama-kv-cache.h +218 -0
- package/cpp/llama-mmap.cpp +589 -0
- package/cpp/llama-mmap.h +67 -0
- package/cpp/llama-model-loader.cpp +1011 -0
- package/cpp/llama-model-loader.h +158 -0
- package/cpp/llama-model.cpp +2202 -0
- package/cpp/llama-model.h +391 -0
- package/cpp/llama-sampling.cpp +117 -4
- package/cpp/llama-vocab.cpp +26 -29
- package/cpp/llama-vocab.h +14 -2
- package/cpp/llama.cpp +8839 -19131
- package/cpp/llama.cpp.rej +23 -0
- package/cpp/llama.h +31 -9
- package/cpp/rn-llama.hpp +39 -37
- package/cpp/sgemm.cpp +1091 -378
- package/cpp/sgemm.h +2 -2
- package/cpp/unicode.cpp +6 -0
- 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);
|
package/cpp/llama-sampling.cpp
CHANGED
@@ -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.
|