cui-llama.rn 1.4.0 → 1.4.2

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 (108) hide show
  1. package/README.md +4 -23
  2. package/android/build.gradle +12 -3
  3. package/android/src/main/CMakeLists.txt +13 -7
  4. package/android/src/main/java/com/rnllama/LlamaContext.java +27 -20
  5. package/android/src/main/java/com/rnllama/RNLlama.java +5 -1
  6. package/android/src/main/jni.cpp +15 -12
  7. package/android/src/main/jniLibs/arm64-v8a/librnllama.so +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8.so +0 -0
  9. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2.so +0 -0
  10. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod.so +0 -0
  11. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod_i8mm.so +0 -0
  12. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_i8mm.so +0 -0
  13. package/android/src/main/jniLibs/x86_64/librnllama.so +0 -0
  14. package/android/src/main/jniLibs/x86_64/librnllama_x86_64.so +0 -0
  15. package/cpp/README.md +1 -1
  16. package/cpp/common.cpp +158 -267
  17. package/cpp/common.h +46 -12
  18. package/cpp/ggml-alloc.c +1042 -1037
  19. package/cpp/ggml-backend-impl.h +255 -256
  20. package/cpp/ggml-backend-reg.cpp +582 -582
  21. package/cpp/ggml-backend.cpp +2002 -2002
  22. package/cpp/ggml-backend.h +354 -352
  23. package/cpp/ggml-common.h +1853 -1853
  24. package/cpp/ggml-cpp.h +39 -39
  25. package/cpp/ggml-cpu-aarch64.cpp +4247 -4247
  26. package/cpp/ggml-cpu-aarch64.h +8 -8
  27. package/cpp/ggml-cpu-impl.h +386 -386
  28. package/cpp/ggml-cpu-quants.c +10920 -10839
  29. package/cpp/ggml-cpu-traits.cpp +36 -36
  30. package/cpp/ggml-cpu-traits.h +38 -38
  31. package/cpp/ggml-cpu.c +329 -60
  32. package/cpp/ggml-cpu.cpp +10 -2
  33. package/cpp/ggml-cpu.h +135 -135
  34. package/cpp/ggml-impl.h +567 -567
  35. package/cpp/ggml-metal-impl.h +17 -17
  36. package/cpp/ggml-metal.m +4884 -4884
  37. package/cpp/ggml-quants.c +5238 -5238
  38. package/cpp/ggml-threading.h +14 -14
  39. package/cpp/ggml.c +6514 -6448
  40. package/cpp/ggml.h +2194 -2163
  41. package/cpp/gguf.cpp +1329 -1325
  42. package/cpp/gguf.h +202 -202
  43. package/cpp/json-schema-to-grammar.cpp +1045 -1045
  44. package/cpp/json-schema-to-grammar.h +8 -8
  45. package/cpp/json.hpp +24766 -24766
  46. package/cpp/llama-adapter.cpp +347 -346
  47. package/cpp/llama-adapter.h +74 -73
  48. package/cpp/llama-arch.cpp +1487 -1434
  49. package/cpp/llama-arch.h +400 -395
  50. package/cpp/llama-batch.cpp +368 -368
  51. package/cpp/llama-batch.h +88 -88
  52. package/cpp/llama-chat.cpp +578 -567
  53. package/cpp/llama-chat.h +52 -51
  54. package/cpp/llama-context.cpp +1775 -1771
  55. package/cpp/llama-context.h +128 -128
  56. package/cpp/llama-cparams.cpp +1 -1
  57. package/cpp/llama-cparams.h +37 -37
  58. package/cpp/llama-cpp.h +30 -30
  59. package/cpp/llama-grammar.cpp +1139 -1139
  60. package/cpp/llama-grammar.h +143 -143
  61. package/cpp/llama-hparams.cpp +71 -71
  62. package/cpp/llama-hparams.h +139 -140
  63. package/cpp/llama-impl.cpp +167 -167
  64. package/cpp/llama-impl.h +61 -61
  65. package/cpp/llama-kv-cache.cpp +718 -718
  66. package/cpp/llama-kv-cache.h +218 -218
  67. package/cpp/llama-mmap.cpp +2 -1
  68. package/cpp/llama-mmap.h +67 -67
  69. package/cpp/llama-model-loader.cpp +1124 -1011
  70. package/cpp/llama-model-loader.h +167 -158
  71. package/cpp/llama-model.cpp +3997 -2202
  72. package/cpp/llama-model.h +370 -391
  73. package/cpp/llama-sampling.cpp +2408 -2406
  74. package/cpp/llama-sampling.h +32 -48
  75. package/cpp/llama-vocab.cpp +3247 -1982
  76. package/cpp/llama-vocab.h +125 -182
  77. package/cpp/llama.cpp +416 -2886
  78. package/cpp/llama.h +1323 -1285
  79. package/cpp/log.cpp +401 -401
  80. package/cpp/log.h +121 -121
  81. package/cpp/rn-llama.cpp +822 -0
  82. package/cpp/rn-llama.h +123 -0
  83. package/cpp/rn-llama.hpp +18 -12
  84. package/cpp/sampling.cpp +505 -500
  85. package/cpp/sgemm.cpp +2597 -2597
  86. package/cpp/speculative.cpp +277 -274
  87. package/cpp/speculative.h +28 -28
  88. package/cpp/unicode.cpp +2 -3
  89. package/ios/CMakeLists.txt +99 -0
  90. package/ios/RNLlama.h +5 -1
  91. package/ios/RNLlama.mm +2 -2
  92. package/ios/RNLlamaContext.h +8 -1
  93. package/ios/RNLlamaContext.mm +15 -11
  94. package/ios/rnllama.xcframework/Info.plist +74 -0
  95. package/jest/mock.js +3 -2
  96. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  97. package/lib/commonjs/index.js +4 -2
  98. package/lib/commonjs/index.js.map +1 -1
  99. package/lib/module/NativeRNLlama.js.map +1 -1
  100. package/lib/module/index.js +4 -2
  101. package/lib/module/index.js.map +1 -1
  102. package/lib/typescript/NativeRNLlama.d.ts +5 -1
  103. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  104. package/lib/typescript/index.d.ts.map +1 -1
  105. package/llama-rn.podspec +8 -2
  106. package/package.json +5 -2
  107. package/src/NativeRNLlama.ts +5 -1
  108. package/src/index.ts +9 -2
package/cpp/sampling.cpp CHANGED
@@ -1,500 +1,505 @@
1
- #include "sampling.h"
2
-
3
- #include "common.h"
4
-
5
- #include <cmath>
6
- #include <unordered_map>
7
-
8
- // the ring buffer works similarly to std::deque, but with a fixed capacity
9
- // TODO: deduplicate with llama-impl.h
10
- template<typename T>
11
- struct ring_buffer {
12
- ring_buffer(size_t cap) : capacity(cap), data(cap) {}
13
-
14
- T & front() {
15
- if (sz == 0) {
16
- throw std::runtime_error("ring buffer is empty");
17
- }
18
- return data[first];
19
- }
20
-
21
- const T & front() const {
22
- if (sz == 0) {
23
- throw std::runtime_error("ring buffer is empty");
24
- }
25
- return data[first];
26
- }
27
-
28
- T & back() {
29
- if (sz == 0) {
30
- throw std::runtime_error("ring buffer is empty");
31
- }
32
- return data[pos];
33
- }
34
-
35
- const T & back() const {
36
- if (sz == 0) {
37
- throw std::runtime_error("ring buffer is empty");
38
- }
39
- return data[pos];
40
- }
41
-
42
- void push_back(const T & value) {
43
- if (sz == capacity) {
44
- // advance the start when buffer is full
45
- first = (first + 1) % capacity;
46
- } else {
47
- sz++;
48
- }
49
- data[pos] = value;
50
- pos = (pos + 1) % capacity;
51
- }
52
-
53
- T pop_front() {
54
- if (sz == 0) {
55
- throw std::runtime_error("ring buffer is empty");
56
- }
57
- T value = data[first];
58
- first = (first + 1) % capacity;
59
- sz--;
60
- return value;
61
- }
62
-
63
- const T & rat(size_t i) const {
64
- if (i >= sz) {
65
- throw std::runtime_error("ring buffer: index out of bounds");
66
- }
67
- return data[(first + sz - i - 1) % capacity];
68
- }
69
-
70
- std::vector<T> to_vector() const {
71
- std::vector<T> result;
72
- result.reserve(sz);
73
- for (size_t i = 0; i < sz; i++) {
74
- result.push_back(data[(first + i) % capacity]);
75
- }
76
- return result;
77
- }
78
-
79
- void clear() {
80
- // here only reset the status of the buffer
81
- sz = 0;
82
- first = 0;
83
- pos = 0;
84
- }
85
-
86
- bool empty() const {
87
- return sz == 0;
88
- }
89
-
90
- size_t size() const {
91
- return sz;
92
- }
93
-
94
- size_t capacity = 0;
95
- size_t sz = 0;
96
- size_t first = 0;
97
- size_t pos = 0;
98
- std::vector<T> data;
99
- };
100
-
101
- struct common_sampler {
102
- common_params_sampling params;
103
-
104
- struct llama_sampler * grmr;
105
- struct llama_sampler * chain;
106
-
107
- ring_buffer<llama_token> prev;
108
-
109
- std::vector<llama_token_data> cur;
110
-
111
- llama_token_data_array cur_p;
112
-
113
- void set_logits(struct llama_context * ctx, int idx) {
114
- const auto * logits = llama_get_logits_ith(ctx, idx);
115
-
116
- const int n_vocab = llama_n_vocab(llama_get_model(ctx));
117
-
118
- cur.resize(n_vocab);
119
-
120
- for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
121
- cur[token_id] = llama_token_data{token_id, logits[token_id], 0.0f};
122
- }
123
-
124
- cur_p = { cur.data(), cur.size(), -1, false };
125
- }
126
- };
127
-
128
- std::string common_params_sampling::print() const {
129
- char result[1024];
130
-
131
- snprintf(result, sizeof(result),
132
- "\trepeat_last_n = %d, repeat_penalty = %.3f, frequency_penalty = %.3f, presence_penalty = %.3f\n"
133
- "\tdry_multiplier = %.3f, dry_base = %.3f, dry_allowed_length = %d, dry_penalty_last_n = %d\n"
134
- "\ttop_k = %d, top_p = %.3f, min_p = %.3f, xtc_probability = %.3f, xtc_threshold = %.3f, typical_p = %.3f, temp = %.3f\n"
135
- "\tmirostat = %d, mirostat_lr = %.3f, mirostat_ent = %.3f",
136
- penalty_last_n, penalty_repeat, penalty_freq, penalty_present,
137
- dry_multiplier, dry_base, dry_allowed_length, dry_penalty_last_n,
138
- top_k, top_p, min_p, xtc_probability, xtc_threshold, typ_p, temp,
139
- mirostat, mirostat_eta, mirostat_tau);
140
-
141
- return std::string(result);
142
- }
143
-
144
- struct common_sampler * common_sampler_init(const struct llama_model * model, const struct common_params_sampling & params) {
145
- llama_sampler_chain_params lparams = llama_sampler_chain_default_params();
146
-
147
- lparams.no_perf = params.no_perf;
148
-
149
- auto * result = new common_sampler {
150
- /* .params = */ params,
151
- /* .grmr = */ llama_sampler_init_grammar(model, params.grammar.c_str(), "root"),
152
- /* .chain = */ llama_sampler_chain_init(lparams),
153
- /* .prev = */ ring_buffer<llama_token>(std::max(32, params.n_prev)),
154
- /* .cur = */ {},
155
- /* .cur_p = */ {},
156
- };
157
-
158
- llama_sampler_chain_add(result->chain,
159
- llama_sampler_init_logit_bias(
160
- llama_n_vocab(model),
161
- params.logit_bias.size(),
162
- params.logit_bias.data()));
163
-
164
- if (params.mirostat == 0) {
165
- for (const auto & cnstr : params.samplers) {
166
- switch (cnstr) {
167
- case COMMON_SAMPLER_TYPE_DRY:
168
- {
169
- std::vector<const char *> c_breakers;
170
- c_breakers.reserve(params.dry_sequence_breakers.size());
171
- for (const auto & str : params.dry_sequence_breakers) {
172
- c_breakers.push_back(str.c_str());
173
- }
174
-
175
- llama_sampler_chain_add(result->chain, llama_sampler_init_dry (model, params.dry_multiplier, params.dry_base, params.dry_allowed_length, params.dry_penalty_last_n, c_breakers.data(), c_breakers.size()));
176
- }
177
- break;
178
- case COMMON_SAMPLER_TYPE_TOP_K:
179
- llama_sampler_chain_add(result->chain, llama_sampler_init_top_k (params.top_k));
180
- break;
181
- case COMMON_SAMPLER_TYPE_TOP_P:
182
- llama_sampler_chain_add(result->chain, llama_sampler_init_top_p (params.top_p, params.min_keep));
183
- break;
184
- case COMMON_SAMPLER_TYPE_MIN_P:
185
- llama_sampler_chain_add(result->chain, llama_sampler_init_min_p (params.min_p, params.min_keep));
186
- break;
187
- case COMMON_SAMPLER_TYPE_XTC:
188
- llama_sampler_chain_add(result->chain, llama_sampler_init_xtc (params.xtc_probability, params.xtc_threshold, params.min_keep, params.seed));
189
- break;
190
- case COMMON_SAMPLER_TYPE_TYPICAL_P:
191
- llama_sampler_chain_add(result->chain, llama_sampler_init_typical (params.typ_p, params.min_keep));
192
- break;
193
- case COMMON_SAMPLER_TYPE_TEMPERATURE:
194
- llama_sampler_chain_add(result->chain, llama_sampler_init_temp_ext (params.temp, params.dynatemp_range, params.dynatemp_exponent));
195
- break;
196
- case COMMON_SAMPLER_TYPE_INFILL:
197
- llama_sampler_chain_add(result->chain, llama_sampler_init_infill (model));
198
- break;
199
- case COMMON_SAMPLER_TYPE_PENALTIES:
200
- llama_sampler_chain_add(result->chain, llama_sampler_init_penalties(params.penalty_last_n, params.penalty_repeat, params.penalty_freq, params.penalty_present));
201
- break;
202
- default:
203
- LM_GGML_ASSERT(false && "unknown sampler type");
204
- }
205
- }
206
- llama_sampler_chain_add(result->chain, llama_sampler_init_dist(params.seed));
207
- } else if (params.mirostat == 1) {
208
- llama_sampler_chain_add(result->chain, llama_sampler_init_temp(params.temp));
209
- llama_sampler_chain_add(result->chain, llama_sampler_init_mirostat(llama_n_vocab(model), params.seed, params.mirostat_tau, params.mirostat_eta, 100));
210
- } else if (params.mirostat == 2) {
211
- llama_sampler_chain_add(result->chain, llama_sampler_init_temp(params.temp));
212
- llama_sampler_chain_add(result->chain, llama_sampler_init_mirostat_v2(params.seed, params.mirostat_tau, params.mirostat_eta));
213
- } else {
214
- LM_GGML_ASSERT(false && "unknown mirostat version");
215
- }
216
-
217
- return result;
218
- }
219
-
220
- void common_sampler_free(struct common_sampler * gsmpl) {
221
- if (gsmpl) {
222
- llama_sampler_free(gsmpl->grmr);
223
-
224
- llama_sampler_free(gsmpl->chain);
225
-
226
- delete gsmpl;
227
- }
228
- }
229
-
230
- void common_sampler_accept(struct common_sampler * gsmpl, llama_token token, bool accept_grammar) {
231
- if (accept_grammar) {
232
- llama_sampler_accept(gsmpl->grmr, token);
233
- }
234
-
235
- llama_sampler_accept(gsmpl->chain, token);
236
-
237
- gsmpl->prev.push_back(token);
238
- }
239
-
240
- void common_sampler_reset(struct common_sampler * gsmpl) {
241
- llama_sampler_reset(gsmpl->grmr);
242
-
243
- llama_sampler_reset(gsmpl->chain);
244
- }
245
-
246
- struct common_sampler * common_sampler_clone(common_sampler * gsmpl) {
247
- return new common_sampler {
248
- /* .params = */ gsmpl->params,
249
- /* .grmr = */ llama_sampler_clone(gsmpl->grmr),
250
- /* .chain = */ llama_sampler_clone(gsmpl->chain),
251
- /* .prev = */ gsmpl->prev,
252
- /* .cur = */ gsmpl->cur,
253
- /* .cur_p = */ gsmpl->cur_p,
254
- };
255
- }
256
-
257
- void common_perf_print(const struct llama_context * ctx, const struct common_sampler * gsmpl) {
258
- // TODO: measure grammar performance
259
-
260
- if (gsmpl) {
261
- llama_perf_sampler_print(gsmpl->chain);
262
- }
263
- if (ctx) {
264
- llama_perf_context_print(ctx);
265
- }
266
- }
267
-
268
- llama_token common_sampler_sample(struct common_sampler * gsmpl, struct llama_context * ctx, int idx, bool grammar_first) {
269
- gsmpl->set_logits(ctx, idx);
270
-
271
- auto & grmr = gsmpl->grmr;
272
- auto & chain = gsmpl->chain;
273
- auto & cur_p = gsmpl->cur_p; // initialized by set_logits
274
-
275
- if (grammar_first) {
276
- llama_sampler_apply(grmr, &cur_p);
277
- }
278
-
279
- llama_sampler_apply(chain, &cur_p);
280
-
281
- LM_GGML_ASSERT(cur_p.selected != -1 && "no selected token during sampling - check your sampling configuration");
282
-
283
- const llama_token id = cur_p.data[cur_p.selected].id;
284
-
285
- if (grammar_first) {
286
- return id;
287
- }
288
-
289
- // check if it the sampled token fits the grammar
290
- {
291
- llama_token_data single_token_data = { id, 1.0f, 0.0f };
292
- llama_token_data_array single_token_data_array = { &single_token_data, 1, -1, false };
293
-
294
- llama_sampler_apply(grmr, &single_token_data_array);
295
-
296
- const bool is_valid = single_token_data_array.data[0].logit != -INFINITY;
297
- if (is_valid) {
298
- return id;
299
- }
300
- }
301
-
302
- // resampling:
303
- // if the token is not valid, sample again, but first apply the grammar sampler and then the sampling chain
304
- gsmpl->set_logits(ctx, idx);
305
-
306
- llama_sampler_apply(grmr, &cur_p);
307
- llama_sampler_apply(chain, &cur_p);
308
-
309
- LM_GGML_ASSERT(cur_p.selected != -1 && "no selected token during re-sampling - check your sampling configuration");
310
-
311
- return cur_p.data[cur_p.selected].id;
312
- }
313
-
314
- std::vector<llama_token> common_sampler_sample_and_accept_n(struct common_sampler * gsmpl, struct llama_context * ctx, const std::vector<int> & idxs, const llama_tokens & draft, bool grammar_first) {
315
- LM_GGML_ASSERT(idxs.size() == draft.size() + 1 && "idxs.size() must be draft.size() + 1");
316
-
317
- std::vector<llama_token> result;
318
- result.reserve(idxs.size());
319
-
320
- size_t i = 0;
321
- for (; i < draft.size(); i++) {
322
- const llama_token id = common_sampler_sample(gsmpl, ctx, idxs[i], grammar_first);
323
-
324
- common_sampler_accept(gsmpl, id, true);
325
-
326
- result.push_back(id);
327
-
328
- if (draft[i] != id) {
329
- break;
330
- }
331
- }
332
-
333
- if (i == draft.size()) {
334
- const llama_token id = common_sampler_sample(gsmpl, ctx, idxs[i], grammar_first);
335
-
336
- common_sampler_accept(gsmpl, id, true);
337
-
338
- result.push_back(id);
339
- }
340
-
341
- return result;
342
- }
343
-
344
- std::vector<llama_token> common_sampler_sample_and_accept_n(struct common_sampler * gsmpl, struct llama_context * ctx, const llama_tokens & draft, bool grammar_first) {
345
- std::vector<int> idxs(draft.size() + 1);
346
- for (size_t i = 0; i < idxs.size(); ++i) {
347
- idxs[i] = i;
348
- }
349
-
350
- return common_sampler_sample_and_accept_n(gsmpl, ctx, idxs, draft, grammar_first);
351
- }
352
-
353
- uint32_t common_sampler_get_seed(const struct common_sampler * gsmpl) {
354
- return llama_sampler_get_seed(gsmpl->chain);
355
- }
356
-
357
- // helpers
358
-
359
- llama_token_data_array * common_sampler_get_candidates(struct common_sampler * gsmpl) {
360
- return &gsmpl->cur_p;
361
- }
362
-
363
- llama_token common_sampler_last(const struct common_sampler * gsmpl) {
364
- return gsmpl->prev.rat(0);
365
- }
366
-
367
- std::string common_sampler_print(const struct common_sampler * gsmpl) {
368
- std::string result = "logits ";
369
-
370
- for (int i = 0; i < llama_sampler_chain_n(gsmpl->chain); i++) {
371
- const auto * smpl = llama_sampler_chain_get(gsmpl->chain, i);
372
- result += std::string("-> ") + llama_sampler_name(smpl) + " ";
373
- }
374
-
375
- return result;
376
- }
377
-
378
- std::string common_sampler_prev_str(common_sampler * gsmpl, llama_context * ctx_main, int n) {
379
- n = std::min(n, (int) gsmpl->prev.size());
380
-
381
- if (n <= 0) {
382
- return "";
383
- }
384
-
385
- std::string result;
386
- result.reserve(8*n); // 8 is the average length of a token [citation needed], TODO: compute this from the vocab
387
-
388
- for (int i = n - 1; i >= 0; i--) {
389
- const llama_token id = gsmpl->prev.rat(i);
390
-
391
- LM_GGML_ASSERT(id != LLAMA_TOKEN_NULL && "null token in the sampling history - should not happen");
392
-
393
- result += common_token_to_piece(ctx_main, id);
394
- }
395
-
396
- return result;
397
- }
398
-
399
- char common_sampler_type_to_chr(enum common_sampler_type cnstr) {
400
- switch (cnstr) {
401
- case COMMON_SAMPLER_TYPE_DRY: return 'd';
402
- case COMMON_SAMPLER_TYPE_TOP_K: return 'k';
403
- case COMMON_SAMPLER_TYPE_TYPICAL_P: return 'y';
404
- case COMMON_SAMPLER_TYPE_TOP_P: return 'p';
405
- case COMMON_SAMPLER_TYPE_MIN_P: return 'm';
406
- case COMMON_SAMPLER_TYPE_TEMPERATURE: return 't';
407
- case COMMON_SAMPLER_TYPE_XTC: return 'x';
408
- case COMMON_SAMPLER_TYPE_INFILL: return 'i';
409
- case COMMON_SAMPLER_TYPE_PENALTIES: return 'e';
410
- default : return '?';
411
- }
412
- }
413
-
414
- std::string common_sampler_type_to_str(enum common_sampler_type cnstr) {
415
- switch (cnstr) {
416
- case COMMON_SAMPLER_TYPE_DRY: return "dry";
417
- case COMMON_SAMPLER_TYPE_TOP_K: return "top_k";
418
- case COMMON_SAMPLER_TYPE_TYPICAL_P: return "typ_p";
419
- case COMMON_SAMPLER_TYPE_TOP_P: return "top_p";
420
- case COMMON_SAMPLER_TYPE_MIN_P: return "min_p";
421
- case COMMON_SAMPLER_TYPE_TEMPERATURE: return "temperature";
422
- case COMMON_SAMPLER_TYPE_XTC: return "xtc";
423
- case COMMON_SAMPLER_TYPE_INFILL: return "infill";
424
- case COMMON_SAMPLER_TYPE_PENALTIES: return "penalties";
425
- default : return "";
426
- }
427
- }
428
-
429
- std::vector<common_sampler_type> common_sampler_types_from_names(const std::vector<std::string> & names, bool allow_alt_names) {
430
- std::unordered_map<std::string, common_sampler_type> sampler_canonical_name_map {
431
- { "dry", COMMON_SAMPLER_TYPE_DRY },
432
- { "top_k", COMMON_SAMPLER_TYPE_TOP_K },
433
- { "top_p", COMMON_SAMPLER_TYPE_TOP_P },
434
- { "typ_p", COMMON_SAMPLER_TYPE_TYPICAL_P },
435
- { "min_p", COMMON_SAMPLER_TYPE_MIN_P },
436
- { "temperature", COMMON_SAMPLER_TYPE_TEMPERATURE },
437
- { "xtc", COMMON_SAMPLER_TYPE_XTC },
438
- { "infill", COMMON_SAMPLER_TYPE_INFILL },
439
- { "penalties", COMMON_SAMPLER_TYPE_PENALTIES },
440
- };
441
-
442
- // since samplers names are written multiple ways
443
- // make it ready for both system names and input names
444
- std::unordered_map<std::string, common_sampler_type> sampler_alt_name_map {
445
- { "top-k", COMMON_SAMPLER_TYPE_TOP_K },
446
- { "top-p", COMMON_SAMPLER_TYPE_TOP_P },
447
- { "nucleus", COMMON_SAMPLER_TYPE_TOP_P },
448
- { "typical-p", COMMON_SAMPLER_TYPE_TYPICAL_P },
449
- { "typical", COMMON_SAMPLER_TYPE_TYPICAL_P },
450
- { "typ-p", COMMON_SAMPLER_TYPE_TYPICAL_P },
451
- { "typ", COMMON_SAMPLER_TYPE_TYPICAL_P },
452
- { "min-p", COMMON_SAMPLER_TYPE_MIN_P },
453
- { "temp", COMMON_SAMPLER_TYPE_TEMPERATURE },
454
- };
455
-
456
- std::vector<common_sampler_type> samplers;
457
- samplers.reserve(names.size());
458
-
459
- for (const auto & name : names) {
460
- auto sampler = sampler_canonical_name_map.find(name);
461
- if (sampler != sampler_canonical_name_map.end()) {
462
- samplers.push_back(sampler->second);
463
- } else {
464
- if (allow_alt_names) {
465
- sampler = sampler_alt_name_map.find(name);
466
- if (sampler != sampler_alt_name_map.end()) {
467
- samplers.push_back(sampler->second);
468
- }
469
- }
470
- }
471
- }
472
-
473
- return samplers;
474
- }
475
-
476
- std::vector<common_sampler_type> common_sampler_types_from_chars(const std::string & chars) {
477
- std::unordered_map<char, common_sampler_type> sampler_name_map = {
478
- { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_DRY), COMMON_SAMPLER_TYPE_DRY },
479
- { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TOP_K), COMMON_SAMPLER_TYPE_TOP_K },
480
- { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TYPICAL_P), COMMON_SAMPLER_TYPE_TYPICAL_P },
481
- { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TOP_P), COMMON_SAMPLER_TYPE_TOP_P },
482
- { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_MIN_P), COMMON_SAMPLER_TYPE_MIN_P },
483
- { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TEMPERATURE), COMMON_SAMPLER_TYPE_TEMPERATURE },
484
- { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_XTC), COMMON_SAMPLER_TYPE_XTC },
485
- { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_INFILL), COMMON_SAMPLER_TYPE_INFILL },
486
- { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_PENALTIES), COMMON_SAMPLER_TYPE_PENALTIES },
487
- };
488
-
489
- std::vector<common_sampler_type> samplers;
490
- samplers.reserve(chars.size());
491
-
492
- for (const auto & c : chars) {
493
- const auto sampler = sampler_name_map.find(c);
494
- if (sampler != sampler_name_map.end()) {
495
- samplers.push_back(sampler->second);
496
- }
497
- }
498
-
499
- return samplers;
500
- }
1
+ #include "sampling.h"
2
+
3
+ #include "common.h"
4
+
5
+ #include <cmath>
6
+ #include <unordered_map>
7
+
8
+ // the ring buffer works similarly to std::deque, but with a fixed capacity
9
+ // TODO: deduplicate with llama-impl.h
10
+ template<typename T>
11
+ struct ring_buffer {
12
+ ring_buffer(size_t cap) : capacity(cap), data(cap) {}
13
+
14
+ T & front() {
15
+ if (sz == 0) {
16
+ throw std::runtime_error("ring buffer is empty");
17
+ }
18
+ return data[first];
19
+ }
20
+
21
+ const T & front() const {
22
+ if (sz == 0) {
23
+ throw std::runtime_error("ring buffer is empty");
24
+ }
25
+ return data[first];
26
+ }
27
+
28
+ T & back() {
29
+ if (sz == 0) {
30
+ throw std::runtime_error("ring buffer is empty");
31
+ }
32
+ return data[pos];
33
+ }
34
+
35
+ const T & back() const {
36
+ if (sz == 0) {
37
+ throw std::runtime_error("ring buffer is empty");
38
+ }
39
+ return data[pos];
40
+ }
41
+
42
+ void push_back(const T & value) {
43
+ if (sz == capacity) {
44
+ // advance the start when buffer is full
45
+ first = (first + 1) % capacity;
46
+ } else {
47
+ sz++;
48
+ }
49
+ data[pos] = value;
50
+ pos = (pos + 1) % capacity;
51
+ }
52
+
53
+ T pop_front() {
54
+ if (sz == 0) {
55
+ throw std::runtime_error("ring buffer is empty");
56
+ }
57
+ T value = data[first];
58
+ first = (first + 1) % capacity;
59
+ sz--;
60
+ return value;
61
+ }
62
+
63
+ const T & rat(size_t i) const {
64
+ if (i >= sz) {
65
+ throw std::runtime_error("ring buffer: index out of bounds");
66
+ }
67
+ return data[(first + sz - i - 1) % capacity];
68
+ }
69
+
70
+ std::vector<T> to_vector() const {
71
+ std::vector<T> result;
72
+ result.reserve(sz);
73
+ for (size_t i = 0; i < sz; i++) {
74
+ result.push_back(data[(first + i) % capacity]);
75
+ }
76
+ return result;
77
+ }
78
+
79
+ void clear() {
80
+ // here only reset the status of the buffer
81
+ sz = 0;
82
+ first = 0;
83
+ pos = 0;
84
+ }
85
+
86
+ bool empty() const {
87
+ return sz == 0;
88
+ }
89
+
90
+ size_t size() const {
91
+ return sz;
92
+ }
93
+
94
+ size_t capacity = 0;
95
+ size_t sz = 0;
96
+ size_t first = 0;
97
+ size_t pos = 0;
98
+ std::vector<T> data;
99
+ };
100
+
101
+ struct common_sampler {
102
+ common_params_sampling params;
103
+
104
+ struct llama_sampler * grmr;
105
+ struct llama_sampler * chain;
106
+
107
+ ring_buffer<llama_token> prev;
108
+
109
+ std::vector<llama_token_data> cur;
110
+
111
+ llama_token_data_array cur_p;
112
+
113
+ void set_logits(struct llama_context * ctx, int idx) {
114
+ const auto * logits = llama_get_logits_ith(ctx, idx);
115
+
116
+ const llama_model * model = llama_get_model(ctx);
117
+ const llama_vocab * vocab = llama_model_get_vocab(model);
118
+
119
+ const int n_vocab = llama_vocab_n_tokens(vocab);
120
+
121
+ cur.resize(n_vocab);
122
+
123
+ for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
124
+ cur[token_id] = llama_token_data{token_id, logits[token_id], 0.0f};
125
+ }
126
+
127
+ cur_p = { cur.data(), cur.size(), -1, false };
128
+ }
129
+ };
130
+
131
+ std::string common_params_sampling::print() const {
132
+ char result[1024];
133
+
134
+ snprintf(result, sizeof(result),
135
+ "\trepeat_last_n = %d, repeat_penalty = %.3f, frequency_penalty = %.3f, presence_penalty = %.3f\n"
136
+ "\tdry_multiplier = %.3f, dry_base = %.3f, dry_allowed_length = %d, dry_penalty_last_n = %d\n"
137
+ "\ttop_k = %d, top_p = %.3f, min_p = %.3f, xtc_probability = %.3f, xtc_threshold = %.3f, typical_p = %.3f, temp = %.3f\n"
138
+ "\tmirostat = %d, mirostat_lr = %.3f, mirostat_ent = %.3f",
139
+ penalty_last_n, penalty_repeat, penalty_freq, penalty_present,
140
+ dry_multiplier, dry_base, dry_allowed_length, dry_penalty_last_n,
141
+ top_k, top_p, min_p, xtc_probability, xtc_threshold, typ_p, temp,
142
+ mirostat, mirostat_eta, mirostat_tau);
143
+
144
+ return std::string(result);
145
+ }
146
+
147
+ struct common_sampler * common_sampler_init(const struct llama_model * model, const struct common_params_sampling & params) {
148
+ const llama_vocab * vocab = llama_model_get_vocab(model);
149
+
150
+ llama_sampler_chain_params lparams = llama_sampler_chain_default_params();
151
+
152
+ lparams.no_perf = params.no_perf;
153
+
154
+ auto * result = new common_sampler {
155
+ /* .params = */ params,
156
+ /* .grmr = */ llama_sampler_init_grammar(vocab, params.grammar.c_str(), "root"),
157
+ /* .chain = */ llama_sampler_chain_init(lparams),
158
+ /* .prev = */ ring_buffer<llama_token>(std::max(32, params.n_prev)),
159
+ /* .cur = */ {},
160
+ /* .cur_p = */ {},
161
+ };
162
+
163
+ llama_sampler_chain_add(result->chain,
164
+ llama_sampler_init_logit_bias(
165
+ llama_vocab_n_tokens(vocab),
166
+ params.logit_bias.size(),
167
+ params.logit_bias.data()));
168
+
169
+ if (params.mirostat == 0) {
170
+ for (const auto & cnstr : params.samplers) {
171
+ switch (cnstr) {
172
+ case COMMON_SAMPLER_TYPE_DRY:
173
+ {
174
+ std::vector<const char *> c_breakers;
175
+ c_breakers.reserve(params.dry_sequence_breakers.size());
176
+ for (const auto & str : params.dry_sequence_breakers) {
177
+ c_breakers.push_back(str.c_str());
178
+ }
179
+
180
+ llama_sampler_chain_add(result->chain, llama_sampler_init_dry (vocab, llama_model_n_ctx_train(model), params.dry_multiplier, params.dry_base, params.dry_allowed_length, params.dry_penalty_last_n, c_breakers.data(), c_breakers.size()));
181
+ }
182
+ break;
183
+ case COMMON_SAMPLER_TYPE_TOP_K:
184
+ llama_sampler_chain_add(result->chain, llama_sampler_init_top_k (params.top_k));
185
+ break;
186
+ case COMMON_SAMPLER_TYPE_TOP_P:
187
+ llama_sampler_chain_add(result->chain, llama_sampler_init_top_p (params.top_p, params.min_keep));
188
+ break;
189
+ case COMMON_SAMPLER_TYPE_MIN_P:
190
+ llama_sampler_chain_add(result->chain, llama_sampler_init_min_p (params.min_p, params.min_keep));
191
+ break;
192
+ case COMMON_SAMPLER_TYPE_XTC:
193
+ llama_sampler_chain_add(result->chain, llama_sampler_init_xtc (params.xtc_probability, params.xtc_threshold, params.min_keep, params.seed));
194
+ break;
195
+ case COMMON_SAMPLER_TYPE_TYPICAL_P:
196
+ llama_sampler_chain_add(result->chain, llama_sampler_init_typical (params.typ_p, params.min_keep));
197
+ break;
198
+ case COMMON_SAMPLER_TYPE_TEMPERATURE:
199
+ llama_sampler_chain_add(result->chain, llama_sampler_init_temp_ext (params.temp, params.dynatemp_range, params.dynatemp_exponent));
200
+ break;
201
+ case COMMON_SAMPLER_TYPE_INFILL:
202
+ llama_sampler_chain_add(result->chain, llama_sampler_init_infill (vocab));
203
+ break;
204
+ case COMMON_SAMPLER_TYPE_PENALTIES:
205
+ llama_sampler_chain_add(result->chain, llama_sampler_init_penalties(params.penalty_last_n, params.penalty_repeat, params.penalty_freq, params.penalty_present));
206
+ break;
207
+ default:
208
+ LM_GGML_ASSERT(false && "unknown sampler type");
209
+ }
210
+ }
211
+ llama_sampler_chain_add(result->chain, llama_sampler_init_dist(params.seed));
212
+ } else if (params.mirostat == 1) {
213
+ llama_sampler_chain_add(result->chain, llama_sampler_init_temp(params.temp));
214
+ llama_sampler_chain_add(result->chain, llama_sampler_init_mirostat(llama_vocab_n_tokens(vocab), params.seed, params.mirostat_tau, params.mirostat_eta, 100));
215
+ } else if (params.mirostat == 2) {
216
+ llama_sampler_chain_add(result->chain, llama_sampler_init_temp(params.temp));
217
+ llama_sampler_chain_add(result->chain, llama_sampler_init_mirostat_v2(params.seed, params.mirostat_tau, params.mirostat_eta));
218
+ } else {
219
+ LM_GGML_ASSERT(false && "unknown mirostat version");
220
+ }
221
+
222
+ return result;
223
+ }
224
+
225
+ void common_sampler_free(struct common_sampler * gsmpl) {
226
+ if (gsmpl) {
227
+ llama_sampler_free(gsmpl->grmr);
228
+
229
+ llama_sampler_free(gsmpl->chain);
230
+
231
+ delete gsmpl;
232
+ }
233
+ }
234
+
235
+ void common_sampler_accept(struct common_sampler * gsmpl, llama_token token, bool accept_grammar) {
236
+ if (accept_grammar) {
237
+ llama_sampler_accept(gsmpl->grmr, token);
238
+ }
239
+
240
+ llama_sampler_accept(gsmpl->chain, token);
241
+
242
+ gsmpl->prev.push_back(token);
243
+ }
244
+
245
+ void common_sampler_reset(struct common_sampler * gsmpl) {
246
+ llama_sampler_reset(gsmpl->grmr);
247
+
248
+ llama_sampler_reset(gsmpl->chain);
249
+ }
250
+
251
+ struct common_sampler * common_sampler_clone(common_sampler * gsmpl) {
252
+ return new common_sampler {
253
+ /* .params = */ gsmpl->params,
254
+ /* .grmr = */ llama_sampler_clone(gsmpl->grmr),
255
+ /* .chain = */ llama_sampler_clone(gsmpl->chain),
256
+ /* .prev = */ gsmpl->prev,
257
+ /* .cur = */ gsmpl->cur,
258
+ /* .cur_p = */ gsmpl->cur_p,
259
+ };
260
+ }
261
+
262
+ void common_perf_print(const struct llama_context * ctx, const struct common_sampler * gsmpl) {
263
+ // TODO: measure grammar performance
264
+
265
+ if (gsmpl) {
266
+ llama_perf_sampler_print(gsmpl->chain);
267
+ }
268
+ if (ctx) {
269
+ llama_perf_context_print(ctx);
270
+ }
271
+ }
272
+
273
+ llama_token common_sampler_sample(struct common_sampler * gsmpl, struct llama_context * ctx, int idx, bool grammar_first) {
274
+ gsmpl->set_logits(ctx, idx);
275
+
276
+ auto & grmr = gsmpl->grmr;
277
+ auto & chain = gsmpl->chain;
278
+ auto & cur_p = gsmpl->cur_p; // initialized by set_logits
279
+
280
+ if (grammar_first) {
281
+ llama_sampler_apply(grmr, &cur_p);
282
+ }
283
+
284
+ llama_sampler_apply(chain, &cur_p);
285
+
286
+ LM_GGML_ASSERT(cur_p.selected != -1 && "no selected token during sampling - check your sampling configuration");
287
+
288
+ const llama_token id = cur_p.data[cur_p.selected].id;
289
+
290
+ if (grammar_first) {
291
+ return id;
292
+ }
293
+
294
+ // check if it the sampled token fits the grammar
295
+ {
296
+ llama_token_data single_token_data = { id, 1.0f, 0.0f };
297
+ llama_token_data_array single_token_data_array = { &single_token_data, 1, -1, false };
298
+
299
+ llama_sampler_apply(grmr, &single_token_data_array);
300
+
301
+ const bool is_valid = single_token_data_array.data[0].logit != -INFINITY;
302
+ if (is_valid) {
303
+ return id;
304
+ }
305
+ }
306
+
307
+ // resampling:
308
+ // if the token is not valid, sample again, but first apply the grammar sampler and then the sampling chain
309
+ gsmpl->set_logits(ctx, idx);
310
+
311
+ llama_sampler_apply(grmr, &cur_p);
312
+ llama_sampler_apply(chain, &cur_p);
313
+
314
+ LM_GGML_ASSERT(cur_p.selected != -1 && "no selected token during re-sampling - check your sampling configuration");
315
+
316
+ return cur_p.data[cur_p.selected].id;
317
+ }
318
+
319
+ std::vector<llama_token> common_sampler_sample_and_accept_n(struct common_sampler * gsmpl, struct llama_context * ctx, const std::vector<int> & idxs, const llama_tokens & draft, bool grammar_first) {
320
+ LM_GGML_ASSERT(idxs.size() == draft.size() + 1 && "idxs.size() must be draft.size() + 1");
321
+
322
+ std::vector<llama_token> result;
323
+ result.reserve(idxs.size());
324
+
325
+ size_t i = 0;
326
+ for (; i < draft.size(); i++) {
327
+ const llama_token id = common_sampler_sample(gsmpl, ctx, idxs[i], grammar_first);
328
+
329
+ common_sampler_accept(gsmpl, id, true);
330
+
331
+ result.push_back(id);
332
+
333
+ if (draft[i] != id) {
334
+ break;
335
+ }
336
+ }
337
+
338
+ if (i == draft.size()) {
339
+ const llama_token id = common_sampler_sample(gsmpl, ctx, idxs[i], grammar_first);
340
+
341
+ common_sampler_accept(gsmpl, id, true);
342
+
343
+ result.push_back(id);
344
+ }
345
+
346
+ return result;
347
+ }
348
+
349
+ std::vector<llama_token> common_sampler_sample_and_accept_n(struct common_sampler * gsmpl, struct llama_context * ctx, const llama_tokens & draft, bool grammar_first) {
350
+ std::vector<int> idxs(draft.size() + 1);
351
+ for (size_t i = 0; i < idxs.size(); ++i) {
352
+ idxs[i] = i;
353
+ }
354
+
355
+ return common_sampler_sample_and_accept_n(gsmpl, ctx, idxs, draft, grammar_first);
356
+ }
357
+
358
+ uint32_t common_sampler_get_seed(const struct common_sampler * gsmpl) {
359
+ return llama_sampler_get_seed(gsmpl->chain);
360
+ }
361
+
362
+ // helpers
363
+
364
+ llama_token_data_array * common_sampler_get_candidates(struct common_sampler * gsmpl) {
365
+ return &gsmpl->cur_p;
366
+ }
367
+
368
+ llama_token common_sampler_last(const struct common_sampler * gsmpl) {
369
+ return gsmpl->prev.rat(0);
370
+ }
371
+
372
+ std::string common_sampler_print(const struct common_sampler * gsmpl) {
373
+ std::string result = "logits ";
374
+
375
+ for (int i = 0; i < llama_sampler_chain_n(gsmpl->chain); i++) {
376
+ const auto * smpl = llama_sampler_chain_get(gsmpl->chain, i);
377
+ result += std::string("-> ") + llama_sampler_name(smpl) + " ";
378
+ }
379
+
380
+ return result;
381
+ }
382
+
383
+ std::string common_sampler_prev_str(common_sampler * gsmpl, llama_context * ctx_main, int n) {
384
+ n = std::min(n, (int) gsmpl->prev.size());
385
+
386
+ if (n <= 0) {
387
+ return "";
388
+ }
389
+
390
+ std::string result;
391
+ result.reserve(8*n); // 8 is the average length of a token [citation needed], TODO: compute this from the vocab
392
+
393
+ for (int i = n - 1; i >= 0; i--) {
394
+ const llama_token id = gsmpl->prev.rat(i);
395
+
396
+ LM_GGML_ASSERT(id != LLAMA_TOKEN_NULL && "null token in the sampling history - should not happen");
397
+
398
+ result += common_token_to_piece(ctx_main, id);
399
+ }
400
+
401
+ return result;
402
+ }
403
+
404
+ char common_sampler_type_to_chr(enum common_sampler_type cnstr) {
405
+ switch (cnstr) {
406
+ case COMMON_SAMPLER_TYPE_DRY: return 'd';
407
+ case COMMON_SAMPLER_TYPE_TOP_K: return 'k';
408
+ case COMMON_SAMPLER_TYPE_TYPICAL_P: return 'y';
409
+ case COMMON_SAMPLER_TYPE_TOP_P: return 'p';
410
+ case COMMON_SAMPLER_TYPE_MIN_P: return 'm';
411
+ case COMMON_SAMPLER_TYPE_TEMPERATURE: return 't';
412
+ case COMMON_SAMPLER_TYPE_XTC: return 'x';
413
+ case COMMON_SAMPLER_TYPE_INFILL: return 'i';
414
+ case COMMON_SAMPLER_TYPE_PENALTIES: return 'e';
415
+ default : return '?';
416
+ }
417
+ }
418
+
419
+ std::string common_sampler_type_to_str(enum common_sampler_type cnstr) {
420
+ switch (cnstr) {
421
+ case COMMON_SAMPLER_TYPE_DRY: return "dry";
422
+ case COMMON_SAMPLER_TYPE_TOP_K: return "top_k";
423
+ case COMMON_SAMPLER_TYPE_TYPICAL_P: return "typ_p";
424
+ case COMMON_SAMPLER_TYPE_TOP_P: return "top_p";
425
+ case COMMON_SAMPLER_TYPE_MIN_P: return "min_p";
426
+ case COMMON_SAMPLER_TYPE_TEMPERATURE: return "temperature";
427
+ case COMMON_SAMPLER_TYPE_XTC: return "xtc";
428
+ case COMMON_SAMPLER_TYPE_INFILL: return "infill";
429
+ case COMMON_SAMPLER_TYPE_PENALTIES: return "penalties";
430
+ default : return "";
431
+ }
432
+ }
433
+
434
+ std::vector<common_sampler_type> common_sampler_types_from_names(const std::vector<std::string> & names, bool allow_alt_names) {
435
+ std::unordered_map<std::string, common_sampler_type> sampler_canonical_name_map {
436
+ { "dry", COMMON_SAMPLER_TYPE_DRY },
437
+ { "top_k", COMMON_SAMPLER_TYPE_TOP_K },
438
+ { "top_p", COMMON_SAMPLER_TYPE_TOP_P },
439
+ { "typ_p", COMMON_SAMPLER_TYPE_TYPICAL_P },
440
+ { "min_p", COMMON_SAMPLER_TYPE_MIN_P },
441
+ { "temperature", COMMON_SAMPLER_TYPE_TEMPERATURE },
442
+ { "xtc", COMMON_SAMPLER_TYPE_XTC },
443
+ { "infill", COMMON_SAMPLER_TYPE_INFILL },
444
+ { "penalties", COMMON_SAMPLER_TYPE_PENALTIES },
445
+ };
446
+
447
+ // since samplers names are written multiple ways
448
+ // make it ready for both system names and input names
449
+ std::unordered_map<std::string, common_sampler_type> sampler_alt_name_map {
450
+ { "top-k", COMMON_SAMPLER_TYPE_TOP_K },
451
+ { "top-p", COMMON_SAMPLER_TYPE_TOP_P },
452
+ { "nucleus", COMMON_SAMPLER_TYPE_TOP_P },
453
+ { "typical-p", COMMON_SAMPLER_TYPE_TYPICAL_P },
454
+ { "typical", COMMON_SAMPLER_TYPE_TYPICAL_P },
455
+ { "typ-p", COMMON_SAMPLER_TYPE_TYPICAL_P },
456
+ { "typ", COMMON_SAMPLER_TYPE_TYPICAL_P },
457
+ { "min-p", COMMON_SAMPLER_TYPE_MIN_P },
458
+ { "temp", COMMON_SAMPLER_TYPE_TEMPERATURE },
459
+ };
460
+
461
+ std::vector<common_sampler_type> samplers;
462
+ samplers.reserve(names.size());
463
+
464
+ for (const auto & name : names) {
465
+ auto sampler = sampler_canonical_name_map.find(name);
466
+ if (sampler != sampler_canonical_name_map.end()) {
467
+ samplers.push_back(sampler->second);
468
+ } else {
469
+ if (allow_alt_names) {
470
+ sampler = sampler_alt_name_map.find(name);
471
+ if (sampler != sampler_alt_name_map.end()) {
472
+ samplers.push_back(sampler->second);
473
+ }
474
+ }
475
+ }
476
+ }
477
+
478
+ return samplers;
479
+ }
480
+
481
+ std::vector<common_sampler_type> common_sampler_types_from_chars(const std::string & chars) {
482
+ std::unordered_map<char, common_sampler_type> sampler_name_map = {
483
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_DRY), COMMON_SAMPLER_TYPE_DRY },
484
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TOP_K), COMMON_SAMPLER_TYPE_TOP_K },
485
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TYPICAL_P), COMMON_SAMPLER_TYPE_TYPICAL_P },
486
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TOP_P), COMMON_SAMPLER_TYPE_TOP_P },
487
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_MIN_P), COMMON_SAMPLER_TYPE_MIN_P },
488
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TEMPERATURE), COMMON_SAMPLER_TYPE_TEMPERATURE },
489
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_XTC), COMMON_SAMPLER_TYPE_XTC },
490
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_INFILL), COMMON_SAMPLER_TYPE_INFILL },
491
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_PENALTIES), COMMON_SAMPLER_TYPE_PENALTIES },
492
+ };
493
+
494
+ std::vector<common_sampler_type> samplers;
495
+ samplers.reserve(chars.size());
496
+
497
+ for (const auto & c : chars) {
498
+ const auto sampler = sampler_name_map.find(c);
499
+ if (sampler != sampler_name_map.end()) {
500
+ samplers.push_back(sampler->second);
501
+ }
502
+ }
503
+
504
+ return samplers;
505
+ }