"vscode:/vscode.git/clone" did not exist on "72dfa96aebcdcd3178687b94923974287057683c"
sampling.cpp 23.1 KB
Newer Older
1
2
#include "sampling.h"

3
#include "common.h"
4
#include "log.h"
5

Daniel Hiltgen's avatar
Daniel Hiltgen committed
6
#include <algorithm>
7
#include <cmath>
Daniel Hiltgen's avatar
Daniel Hiltgen committed
8
#include <cstring>
9
#include <unordered_map>
10

11
12
13
14
15
// the ring buffer works similarly to std::deque, but with a fixed capacity
// TODO: deduplicate with llama-impl.h
template<typename T>
struct ring_buffer {
    ring_buffer(size_t cap) : capacity(cap), data(cap) {}
16

17
18
19
    T & front() {
        if (sz == 0) {
            throw std::runtime_error("ring buffer is empty");
20
        }
21
22
        return data[first];
    }
23

24
25
26
    const T & front() const {
        if (sz == 0) {
            throw std::runtime_error("ring buffer is empty");
27
        }
28
29
        return data[first];
    }
30

31
32
33
    T & back() {
        if (sz == 0) {
            throw std::runtime_error("ring buffer is empty");
34
        }
35
        return data[pos];
36
37
    }

38
39
40
41
42
    const T & back() const {
        if (sz == 0) {
            throw std::runtime_error("ring buffer is empty");
        }
        return data[pos];
43
44
    }

45
46
47
48
49
50
51
52
53
    void push_back(const T & value) {
        if (sz == capacity) {
            // advance the start when buffer is full
            first = (first + 1) % capacity;
        } else {
            sz++;
        }
        data[pos] = value;
        pos = (pos + 1) % capacity;
54
55
    }

56
57
58
59
60
61
62
63
64
    T pop_front() {
        if (sz == 0) {
            throw std::runtime_error("ring buffer is empty");
        }
        T value = data[first];
        first = (first + 1) % capacity;
        sz--;
        return value;
    }
65

66
67
68
    const T & rat(size_t i) const {
        if (i >= sz) {
            throw std::runtime_error("ring buffer: index out of bounds");
69
        }
70
        return data[(first + sz - i - 1) % capacity];
71
72
    }

73
74
75
76
77
78
79
80
    std::vector<T> to_vector() const {
        std::vector<T> result;
        result.reserve(sz);
        for (size_t i = 0; i < sz; i++) {
            result.push_back(data[(first + i) % capacity]);
        }
        return result;
    }
81

82
83
84
85
86
    void clear() {
        // here only reset the status of the buffer
        sz = 0;
        first = 0;
        pos = 0;
87
88
    }

89
90
    bool empty() const {
        return sz == 0;
91
92
    }

93
94
    size_t size() const {
        return sz;
95
96
    }

97
98
99
100
101
102
    size_t capacity = 0;
    size_t sz = 0;
    size_t first = 0;
    size_t pos = 0;
    std::vector<T> data;
};
103

104
105
struct common_sampler {
    common_params_sampling params;
106

107
    struct llama_sampler * chain;
108

109
110
    bool grammar;

111
    ring_buffer<llama_token> prev;
112

113
    std::vector<llama_token_data> cur;
114

115
    llama_token_data_array cur_p;
116

Daniel Hiltgen's avatar
Daniel Hiltgen committed
117
118
119
120
121
122
    void reset() {
        prev.clear();

        llama_sampler_reset(chain);
    }

123
124
    void set_logits(struct llama_context * ctx, int idx) {
        const auto * logits = llama_get_logits_ith(ctx, idx);
125

126
127
128
129
        const llama_model * model = llama_get_model(ctx);
        const llama_vocab * vocab = llama_model_get_vocab(model);

        const int n_vocab = llama_vocab_n_tokens(vocab);
130
131
132
133
134
135
136
137
138

        cur.resize(n_vocab);

        for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
            cur[token_id] = llama_token_data{token_id, logits[token_id], 0.0f};
        }

        cur_p = { cur.data(), cur.size(), -1, false };
    }
Daniel Hiltgen's avatar
Daniel Hiltgen committed
139
140
141
142
143
144

    common_time_meas tm() {
        return common_time_meas(t_total_us, params.no_perf);
    }

    mutable int64_t t_total_us = 0;
145
146
};

147
std::string common_params_sampling::print() const {
148
149
150
151
    char result[1024];

    snprintf(result, sizeof(result),
            "\trepeat_last_n = %d, repeat_penalty = %.3f, frequency_penalty = %.3f, presence_penalty = %.3f\n"
152
            "\tdry_multiplier = %.3f, dry_base = %.3f, dry_allowed_length = %d, dry_penalty_last_n = %d\n"
153
            "\ttop_k = %d, top_p = %.3f, min_p = %.3f, xtc_probability = %.3f, xtc_threshold = %.3f, typical_p = %.3f, top_n_sigma = %.3f, temp = %.3f\n"
154
            "\tmirostat = %d, mirostat_lr = %.3f, mirostat_ent = %.3f",
155
            penalty_last_n, penalty_repeat, penalty_freq, penalty_present,
156
            dry_multiplier, dry_base, dry_allowed_length, dry_penalty_last_n,
157
            top_k, top_p, min_p, xtc_probability, xtc_threshold, typ_p, top_n_sigma, temp,
158
            mirostat, mirostat_eta, mirostat_tau);
159
160
161
162

    return std::string(result);
}

163
struct common_sampler * common_sampler_init(const struct llama_model * model, const struct common_params_sampling & params) {
164
165
    const llama_vocab * vocab = llama_model_get_vocab(model);

166
167
168
169
    llama_sampler_chain_params lparams = llama_sampler_chain_default_params();

    lparams.no_perf = params.no_perf;

170
171
172
173
174
    llama_sampler * chain = llama_sampler_chain_init(lparams);

    bool grammar = false;
    std::vector<llama_sampler *> samplers;

175
176
    if (params.grammar.compare(0, 11, "%llguidance") == 0) {
#ifdef LLAMA_USE_LLGUIDANCE
177
178
        samplers.push_back(llama_sampler_init_llg(vocab, "lark", params.grammar.c_str()));
        grammar = true;
179
180
181
182
#else
        GGML_ABORT("llguidance (cmake -DLLAMA_LLGUIDANCE=ON) is not enabled");
#endif // LLAMA_USE_LLGUIDANCE
    } else {
183
        std::vector<std::string> trigger_patterns;
184
185
186
187
188
189
190
191
192
193
194
195
        std::vector<std::string> patterns_anywhere;
        std::vector<llama_token> trigger_tokens;
        for (const auto & trigger : params.grammar_triggers) {
            switch (trigger.type) {
                case COMMON_GRAMMAR_TRIGGER_TYPE_WORD:
                {
                    const auto & word = trigger.value;
                    patterns_anywhere.push_back(regex_escape(word));
                    break;
                }
                case COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN:
                {
196
197
198
199
200
201
                    patterns_anywhere.push_back(trigger.value);
                    break;
                }
                case COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN_FULL:
                {
                    trigger_patterns.push_back(trigger.value);
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
                    break;
                }
                case COMMON_GRAMMAR_TRIGGER_TYPE_TOKEN:
                {
                    const auto token = trigger.token;
                    trigger_tokens.push_back(token);
                    break;
                }
                default:
                    GGML_ASSERT(false && "unknown trigger type");
            }
        }

        if (!patterns_anywhere.empty()) {
            trigger_patterns.push_back("^[\\s\\S]*?(" + string_join(patterns_anywhere, "|") + ")[\\s\\S]*");
        }

        std::vector<const char *> trigger_patterns_c;
        trigger_patterns_c.reserve(trigger_patterns.size());
        for (const auto & regex : trigger_patterns) {
            trigger_patterns_c.push_back(regex.c_str());
223
224
        }

225
226
227
228
229
230
231
232
233
234
235
        if (!params.grammar.empty()) {
             if (params.grammar_lazy) {
                 samplers.push_back(
                         llama_sampler_init_grammar_lazy_patterns(vocab, params.grammar.c_str(), "root",
                             trigger_patterns_c.data(), trigger_patterns_c.size(),
                             trigger_tokens.data(),     trigger_tokens.size()));
             } else {
                 samplers.push_back(llama_sampler_init_grammar(vocab, params.grammar.c_str(), "root"));
             }

             grammar = true;
236
        }
237
238
    }

239
240
241
    if (params.has_logit_bias()) {
        samplers.push_back(llama_sampler_init_logit_bias(llama_vocab_n_tokens(vocab), params.logit_bias.size(), params.logit_bias.data()));
    }
242

243
    if (params.mirostat == 0) {
244
245
246
247
248
249
250
251
        for (const auto & cnstr : params.samplers) {
            switch (cnstr) {
                case COMMON_SAMPLER_TYPE_DRY:
                    {
                        std::vector<const char *> c_breakers;
                        c_breakers.reserve(params.dry_sequence_breakers.size());
                        for (const auto & str : params.dry_sequence_breakers) {
                            c_breakers.push_back(str.c_str());
252
                        }
253

254
                        samplers.push_back(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()));
255
256
257
                    }
                    break;
                case COMMON_SAMPLER_TYPE_TOP_K:
258
                    samplers.push_back(llama_sampler_init_top_k      (params.top_k));
259
260
                    break;
                case COMMON_SAMPLER_TYPE_TOP_P:
261
                    samplers.push_back(llama_sampler_init_top_p      (params.top_p, params.min_keep));
262
263
                    break;
                case COMMON_SAMPLER_TYPE_TOP_N_SIGMA:
264
                    samplers.push_back(llama_sampler_init_top_n_sigma(params.top_n_sigma));
265
266
                    break;
                case COMMON_SAMPLER_TYPE_MIN_P:
267
                    samplers.push_back(llama_sampler_init_min_p      (params.min_p, params.min_keep));
268
269
                    break;
                case COMMON_SAMPLER_TYPE_XTC:
270
                    samplers.push_back(llama_sampler_init_xtc        (params.xtc_probability, params.xtc_threshold, params.min_keep, params.seed));
271
272
                    break;
                case COMMON_SAMPLER_TYPE_TYPICAL_P:
273
                    samplers.push_back(llama_sampler_init_typical    (params.typ_p, params.min_keep));
274
275
                    break;
                case COMMON_SAMPLER_TYPE_TEMPERATURE:
276
                    samplers.push_back(llama_sampler_init_temp_ext   (params.temp, params.dynatemp_range, params.dynatemp_exponent));
277
278
                    break;
                case COMMON_SAMPLER_TYPE_INFILL:
279
                    samplers.push_back(llama_sampler_init_infill     (vocab));
280
281
                    break;
                case COMMON_SAMPLER_TYPE_PENALTIES:
282
                    samplers.push_back(llama_sampler_init_penalties  (params.penalty_last_n, params.penalty_repeat, params.penalty_freq, params.penalty_present));
283
284
285
                    break;
                default:
                    GGML_ASSERT(false && "unknown sampler type");
286
287
            }
        }
288
289

        samplers.push_back(llama_sampler_init_dist(params.seed));
290
    } else if (params.mirostat == 1) {
291
292
        samplers.push_back(llama_sampler_init_temp(params.temp));
        samplers.push_back(llama_sampler_init_mirostat(llama_vocab_n_tokens(vocab), params.seed, params.mirostat_tau, params.mirostat_eta, 100));
293
    } else if (params.mirostat == 2) {
294
295
        samplers.push_back(llama_sampler_init_temp(params.temp));
        samplers.push_back(llama_sampler_init_mirostat_v2(params.seed, params.mirostat_tau, params.mirostat_eta));
296
    } else {
297
        GGML_ASSERT(false && "unknown mirostat version");
298
299
    }

300
301
302
303
304
305
306
307
308
309
310
311
312
    for (auto * smpl : samplers) {
        llama_sampler_chain_add(chain, smpl);
    }

    auto * result = new common_sampler {
        /* .params  = */ params,
        /* .chain   = */ chain,
        /* .grammar = */ grammar,
        /* .prev    = */ ring_buffer<llama_token>(std::max(32, params.n_prev)),
        /* .cur     = */ {},
        /* .cur_p   = */ {},
    };

313
314
315
    return result;
}

316
void common_sampler_free(struct common_sampler * gsmpl) {
317
318
319
320
    if (gsmpl) {
        llama_sampler_free(gsmpl->chain);

        delete gsmpl;
321
322
323
    }
}

324
void common_sampler_accept(struct common_sampler * gsmpl, llama_token token, bool accept_grammar) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
325
326
    const auto tm = gsmpl->tm();

327
328
    if (gsmpl->grammar) {
        const int n_smpl = llama_sampler_chain_n(gsmpl->chain);
329

330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
        for (int i = 0; i < n_smpl; i++) {
            auto * smpl = llama_sampler_chain_get(gsmpl->chain, i);

            // the grammar sampler is always the first one
            if (i == 0) {
                if (accept_grammar) {
                    llama_sampler_accept(smpl, token);
                }
            } else {
                llama_sampler_accept(smpl, token);
            }
        }
    } else {
        llama_sampler_accept(gsmpl->chain, token);
    }
345

346
    gsmpl->prev.push_back(token);
347
348
}

349
void common_sampler_reset(struct common_sampler * gsmpl) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
350
    gsmpl->reset();
351
352
}

353
354
struct common_sampler * common_sampler_clone(common_sampler * gsmpl) {
    return new common_sampler {
355
356
357
358
359
360
        /* .params  = */ gsmpl->params,
        /* .chain   = */ llama_sampler_clone(gsmpl->chain),
        /* .grammar = */ gsmpl->grammar,
        /* .prev    = */ gsmpl->prev,
        /* .cur     = */ gsmpl->cur,
        /* .cur_p   = */ gsmpl->cur_p,
361
    };
362
363
}

364
void common_perf_print(const struct llama_context * ctx, const struct common_sampler * gsmpl) {
365
    // TODO: measure grammar performance
366

Daniel Hiltgen's avatar
Daniel Hiltgen committed
367
368
369
370
371
372
373
374
    const double t_sampling_ms = gsmpl ? 1e-3*gsmpl->t_total_us : 0;

    llama_perf_sampler_data data_smpl;
    llama_perf_context_data data_ctx;

    memset(&data_smpl, 0, sizeof(data_smpl));
    memset(&data_ctx,  0, sizeof(data_ctx));

375
    if (gsmpl) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
376
377
378
379
380
381
382
        auto & data = data_smpl;

        data = llama_perf_sampler(gsmpl->chain);

        // note: the sampling time includes the samplers time + extra time spent in common/sampling
        LOG_INF("%s:    sampling time = %10.2f ms\n", __func__, t_sampling_ms);
        LOG_INF("%s:    samplers time = %10.2f ms / %5d tokens\n", __func__, data.t_sample_ms, data.n_sample);
383
    }
Daniel Hiltgen's avatar
Daniel Hiltgen committed
384

385
    if (ctx) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
        auto & data = data_ctx;

        data = llama_perf_context(ctx);

        const double t_end_ms = 1e-3 * ggml_time_us();

        const double t_total_ms = t_end_ms - data.t_start_ms;
        const double t_unacc_ms = t_total_ms - (t_sampling_ms + data.t_p_eval_ms + data.t_eval_ms);
        const double t_unacc_pc = 100.0 * t_unacc_ms /  t_total_ms;

        LOG_INF("%s:        load time = %10.2f ms\n", __func__, data.t_load_ms);
        LOG_INF("%s: prompt eval time = %10.2f ms / %5d tokens (%8.2f ms per token, %8.2f tokens per second)\n",
                __func__, data.t_p_eval_ms, data.n_p_eval, data.t_p_eval_ms / data.n_p_eval, 1e3 / data.t_p_eval_ms * data.n_p_eval);
        LOG_INF("%s:        eval time = %10.2f ms / %5d runs   (%8.2f ms per token, %8.2f tokens per second)\n",
                __func__, data.t_eval_ms, data.n_eval, data.t_eval_ms / data.n_eval, 1e3 / data.t_eval_ms * data.n_eval);
        LOG_INF("%s:       total time = %10.2f ms / %5d tokens\n", __func__, (t_end_ms - data.t_start_ms), (data.n_p_eval + data.n_eval));
        LOG_INF("%s: unaccounted time = %10.2f ms / %5.1f %%      (total - sampling - prompt eval - eval) / (total)\n", __func__, t_unacc_ms, t_unacc_pc);
        LOG_INF("%s:    graphs reused = %10d\n", __func__, data.n_reused);

Daniel Hiltgen's avatar
Daniel Hiltgen committed
405
        llama_memory_breakdown_print(ctx);
406
407
    }
}
408

409
410
411
412
413
struct llama_sampler * common_sampler_get(const struct common_sampler * gsmpl) {
    return gsmpl->chain;
}

llama_token common_sampler_sample(struct common_sampler * gsmpl, struct llama_context * ctx, int idx) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
414
415
416
417
418
    llama_synchronize(ctx);

    // start measuring sampling time after the llama_context synchronization in order to not measure any ongoing async operations
    const auto tm = gsmpl->tm();

419
    llama_token id = LLAMA_TOKEN_NULL;
420

421
422
    auto & chain = gsmpl->chain;
    auto & cur_p = gsmpl->cur_p; // initialized by set_logits
423

424
    gsmpl->set_logits(ctx, idx);
425

426
    llama_sampler_apply(chain, &cur_p);
427

428
    GGML_ASSERT(cur_p.selected != -1 && "no selected token during sampling - check your sampling configuration");
429

430
    id = cur_p.data[cur_p.selected].id;
431

432
    return id;
433
}
434

435
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) {
436
437
438
439
440
441
442
    GGML_ASSERT(idxs.size() == draft.size() + 1 && "idxs.size() must be draft.size() + 1");

    std::vector<llama_token> result;
    result.reserve(idxs.size());

    size_t i = 0;
    for (; i < draft.size(); i++) {
443
        const llama_token id = common_sampler_sample(gsmpl, ctx, idxs[i]);
444
445
446
447
448
449
450
451
452
453
454

        common_sampler_accept(gsmpl, id, true);

        result.push_back(id);

        if (draft[i] != id) {
            break;
        }
    }

    if (i == draft.size()) {
455
        const llama_token id = common_sampler_sample(gsmpl, ctx, idxs[i]);
456
457
458
459
460
461
462
463
464

        common_sampler_accept(gsmpl, id, true);

        result.push_back(id);
    }

    return result;
}

465
std::vector<llama_token> common_sampler_sample_and_accept_n(struct common_sampler * gsmpl, struct llama_context * ctx, const llama_tokens & draft) {
466
467
468
469
470
    std::vector<int> idxs(draft.size() + 1);
    for (size_t i = 0; i < idxs.size(); ++i) {
        idxs[i] = i;
    }

471
    return common_sampler_sample_and_accept_n(gsmpl, ctx, idxs, draft);
472
473
474
}

uint32_t common_sampler_get_seed(const struct common_sampler * gsmpl) {
475
476
477
478
    return llama_sampler_get_seed(gsmpl->chain);
}

// helpers
479

Daniel Hiltgen's avatar
Daniel Hiltgen committed
480
llama_token_data_array * common_sampler_get_candidates(struct common_sampler * gsmpl, bool do_sort) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
481
482
    const auto tm = gsmpl->tm();

Daniel Hiltgen's avatar
Daniel Hiltgen committed
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
    auto * res = &gsmpl->cur_p;

    if (do_sort && !res->sorted) {
        // remember the selected token before sorting
        const llama_token id = res->data[res->selected].id;

        std::sort(res->data, res->data + res->size, [](const llama_token_data & a, const llama_token_data & b) {
            return a.p > b.p;
        });

        // restore the selected token after sorting
        for (size_t i = 0; i < res->size; ++i) {
            if (res->data[i].id == id) {
                res->selected = i;
                break;
            }
        }

        res->sorted = true;
    }

    return res;
505
}
506

507
llama_token common_sampler_last(const struct common_sampler * gsmpl) {
508
509
    return gsmpl->prev.rat(0);
}
510

511
std::string common_sampler_print(const struct common_sampler * gsmpl) {
512
    std::string result = "logits ";
513

514
515
    for (int i = 0; i < llama_sampler_chain_n(gsmpl->chain); i++) {
        const auto * smpl = llama_sampler_chain_get(gsmpl->chain, i);
516
517
        result += std::string("-> ");
        result += std::string(llama_sampler_name(smpl)) + " ";
518
519
    }

520
521
522
    return result;
}

523
std::string common_sampler_prev_str(common_sampler * gsmpl, llama_context * ctx_main, int n) {
524
525
526
527
    n = std::min(n, (int) gsmpl->prev.size());

    if (n <= 0) {
        return "";
528
529
    }

530
531
532
533
534
535
536
537
    std::string result;
    result.reserve(8*n); // 8 is the average length of a token [citation needed], TODO: compute this from the vocab

    for (int i = n - 1; i >= 0; i--) {
        const llama_token id = gsmpl->prev.rat(i);

        GGML_ASSERT(id != LLAMA_TOKEN_NULL && "null token in the sampling history - should not happen");

538
        result += common_token_to_piece(ctx_main, id);
539
540
    }

541
542
543
    return result;
}

544
char common_sampler_type_to_chr(enum common_sampler_type cnstr) {
545
    switch (cnstr) {
546
547
548
549
        case COMMON_SAMPLER_TYPE_DRY:         return 'd';
        case COMMON_SAMPLER_TYPE_TOP_K:       return 'k';
        case COMMON_SAMPLER_TYPE_TYPICAL_P:   return 'y';
        case COMMON_SAMPLER_TYPE_TOP_P:       return 'p';
550
        case COMMON_SAMPLER_TYPE_TOP_N_SIGMA: return 's';
551
552
553
554
        case COMMON_SAMPLER_TYPE_MIN_P:       return 'm';
        case COMMON_SAMPLER_TYPE_TEMPERATURE: return 't';
        case COMMON_SAMPLER_TYPE_XTC:         return 'x';
        case COMMON_SAMPLER_TYPE_INFILL:      return 'i';
555
        case COMMON_SAMPLER_TYPE_PENALTIES:   return 'e';
556
557
558
        default : return '?';
    }
}
559

560
std::string common_sampler_type_to_str(enum common_sampler_type cnstr) {
561
    switch (cnstr) {
562
563
564
565
        case COMMON_SAMPLER_TYPE_DRY:         return "dry";
        case COMMON_SAMPLER_TYPE_TOP_K:       return "top_k";
        case COMMON_SAMPLER_TYPE_TYPICAL_P:   return "typ_p";
        case COMMON_SAMPLER_TYPE_TOP_P:       return "top_p";
566
        case COMMON_SAMPLER_TYPE_TOP_N_SIGMA: return "top_n_sigma";
567
568
569
570
        case COMMON_SAMPLER_TYPE_MIN_P:       return "min_p";
        case COMMON_SAMPLER_TYPE_TEMPERATURE: return "temperature";
        case COMMON_SAMPLER_TYPE_XTC:         return "xtc";
        case COMMON_SAMPLER_TYPE_INFILL:      return "infill";
571
        case COMMON_SAMPLER_TYPE_PENALTIES:   return "penalties";
572
        default : return "";
573
    }
574
}
575

576
577
578
579
580
std::vector<common_sampler_type> common_sampler_types_from_names(const std::vector<std::string> & names, bool allow_alt_names) {
    std::unordered_map<std::string, common_sampler_type> sampler_canonical_name_map {
        { "dry",         COMMON_SAMPLER_TYPE_DRY },
        { "top_k",       COMMON_SAMPLER_TYPE_TOP_K },
        { "top_p",       COMMON_SAMPLER_TYPE_TOP_P },
581
        { "top_n_sigma", COMMON_SAMPLER_TYPE_TOP_N_SIGMA },
582
583
584
585
586
        { "typ_p",       COMMON_SAMPLER_TYPE_TYPICAL_P },
        { "min_p",       COMMON_SAMPLER_TYPE_MIN_P },
        { "temperature", COMMON_SAMPLER_TYPE_TEMPERATURE },
        { "xtc",         COMMON_SAMPLER_TYPE_XTC },
        { "infill",      COMMON_SAMPLER_TYPE_INFILL },
587
        { "penalties",   COMMON_SAMPLER_TYPE_PENALTIES },
588
    };
589

590
591
    // since samplers names are written multiple ways
    // make it ready for both system names and input names
592
593
594
    std::unordered_map<std::string, common_sampler_type> sampler_alt_name_map {
        { "top-k",       COMMON_SAMPLER_TYPE_TOP_K },
        { "top-p",       COMMON_SAMPLER_TYPE_TOP_P },
595
        { "top-n-sigma", COMMON_SAMPLER_TYPE_TOP_N_SIGMA },
596
597
598
599
600
601
602
        { "nucleus",     COMMON_SAMPLER_TYPE_TOP_P },
        { "typical-p",   COMMON_SAMPLER_TYPE_TYPICAL_P },
        { "typical",     COMMON_SAMPLER_TYPE_TYPICAL_P },
        { "typ-p",       COMMON_SAMPLER_TYPE_TYPICAL_P },
        { "typ",         COMMON_SAMPLER_TYPE_TYPICAL_P },
        { "min-p",       COMMON_SAMPLER_TYPE_MIN_P },
        { "temp",        COMMON_SAMPLER_TYPE_TEMPERATURE },
603
    };
604

605
    std::vector<common_sampler_type> samplers;
606
    samplers.reserve(names.size());
607

608
609
610
611
    for (const auto & name : names) {
        auto sampler = sampler_canonical_name_map.find(name);
        if (sampler != sampler_canonical_name_map.end()) {
            samplers.push_back(sampler->second);
612
613
614
615
616
617
618
            continue;
        }
        if (allow_alt_names) {
            sampler = sampler_alt_name_map.find(name);
            if (sampler != sampler_alt_name_map.end()) {
                samplers.push_back(sampler->second);
                continue;
619
620
            }
        }
621
        LOG_WRN("%s: unable to match sampler by name '%s'\n", __func__, name.c_str());
622
623
    }

624
    return samplers;
625
626
}

627
628
629
630
631
632
std::vector<common_sampler_type> common_sampler_types_from_chars(const std::string & chars) {
    std::unordered_map<char, common_sampler_type> sampler_name_map = {
        { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_DRY),         COMMON_SAMPLER_TYPE_DRY },
        { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TOP_K),       COMMON_SAMPLER_TYPE_TOP_K },
        { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TYPICAL_P),   COMMON_SAMPLER_TYPE_TYPICAL_P },
        { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TOP_P),       COMMON_SAMPLER_TYPE_TOP_P },
633
        { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TOP_N_SIGMA), COMMON_SAMPLER_TYPE_TOP_N_SIGMA },
634
635
636
637
        { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_MIN_P),       COMMON_SAMPLER_TYPE_MIN_P },
        { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TEMPERATURE), COMMON_SAMPLER_TYPE_TEMPERATURE },
        { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_XTC),         COMMON_SAMPLER_TYPE_XTC },
        { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_INFILL),      COMMON_SAMPLER_TYPE_INFILL },
638
        { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_PENALTIES),   COMMON_SAMPLER_TYPE_PENALTIES },
639
    };
640

641
    std::vector<common_sampler_type> samplers;
642
    samplers.reserve(chars.size());
643

644
645
646
647
    for (const auto & c : chars) {
        const auto sampler = sampler_name_map.find(c);
        if (sampler != sampler_name_map.end()) {
            samplers.push_back(sampler->second);
648
649
        } else {
            LOG_WRN("%s: unable to match sampler by char '%c'\n", __func__, c);
650
        }
651
    }
652
653

    return samplers;
654
}