@@ -21,15 +21,15 @@ static void llama_log_softmax(float * array, size_t size) {
21
21
}
22
22
}
23
23
24
- void llama_set_rng_seed (struct llama_sampling * smpl, uint32_t seed) {
24
+ void llama_set_rng_seed_impl (struct llama_sampling * smpl, uint32_t seed) {
25
25
if (seed == LLAMA_DEFAULT_SEED) {
26
26
seed = time (NULL );
27
27
}
28
28
29
29
smpl->rng .seed (seed);
30
30
}
31
31
32
- void llama_sample_softmax (struct llama_sampling * smpl, llama_token_data_array * candidates) {
32
+ void llama_sample_softmax_impl (struct llama_sampling * smpl, llama_token_data_array * candidates) {
33
33
GGML_ASSERT (candidates->size > 0 );
34
34
35
35
const int64_t t_start_sample_us = ggml_time_us ();
@@ -58,7 +58,7 @@ void llama_sample_softmax(struct llama_sampling * smpl, llama_token_data_array *
58
58
}
59
59
}
60
60
61
- void llama_sample_top_k (struct llama_sampling * smpl, llama_token_data_array * candidates, int32_t k, size_t min_keep) {
61
+ void llama_sample_top_k_impl (struct llama_sampling * smpl, llama_token_data_array * candidates, int32_t k, size_t min_keep) {
62
62
// TODO: move bucket sort to separate function so that top_p/tail_free/typical/softmax first is equally fast
63
63
// if (k >= (int32_t)candidates->size) {
64
64
// return;
@@ -139,12 +139,12 @@ void llama_sample_top_k(struct llama_sampling * smpl, llama_token_data_array * c
139
139
}
140
140
}
141
141
142
- void llama_sample_top_p (struct llama_sampling * smpl, llama_token_data_array * candidates, float p, size_t min_keep) {
142
+ void llama_sample_top_p_impl (struct llama_sampling * smpl, llama_token_data_array * candidates, float p, size_t min_keep) {
143
143
if (p >= 1 .0f ) {
144
144
return ;
145
145
}
146
146
147
- llama_sample_softmax (smpl, candidates);
147
+ llama_sample_softmax_impl (smpl, candidates);
148
148
149
149
const int64_t t_start_sample_us = ggml_time_us ();
150
150
@@ -171,7 +171,7 @@ void llama_sample_top_p(struct llama_sampling * smpl, llama_token_data_array * c
171
171
}
172
172
}
173
173
174
- void llama_sample_min_p (struct llama_sampling * smpl, llama_token_data_array * candidates, float p, size_t min_keep) {
174
+ void llama_sample_min_p_impl (struct llama_sampling * smpl, llama_token_data_array * candidates, float p, size_t min_keep) {
175
175
if (p <= 0 .0f || !candidates->size ) {
176
176
return ;
177
177
}
@@ -232,12 +232,12 @@ void llama_sample_min_p(struct llama_sampling * smpl, llama_token_data_array * c
232
232
}
233
233
}
234
234
235
- void llama_sample_tail_free (struct llama_sampling * smpl, llama_token_data_array * candidates, float z, size_t min_keep) {
235
+ void llama_sample_tail_free_impl (struct llama_sampling * smpl, llama_token_data_array * candidates, float z, size_t min_keep) {
236
236
if (z >= 1 .0f || candidates->size <= 2 ) {
237
237
return ;
238
238
}
239
239
240
- llama_sample_softmax ((struct llama_sampling *) nullptr , candidates);
240
+ llama_sample_softmax_impl ((struct llama_sampling *) nullptr , candidates);
241
241
const int64_t t_start_sample_us = ggml_time_us ();
242
242
243
243
// Compute the first and second derivatives
@@ -291,15 +291,15 @@ void llama_sample_tail_free(struct llama_sampling * smpl, llama_token_data_array
291
291
}
292
292
}
293
293
294
- void llama_sample_typical (struct llama_sampling * smpl, llama_token_data_array * candidates, float p, size_t min_keep) {
294
+ void llama_sample_typical_impl (struct llama_sampling * smpl, llama_token_data_array * candidates, float p, size_t min_keep) {
295
295
// Reference implementation:
296
296
// https://github.com/huggingface/transformers/compare/main...cimeister:typical-sampling:typical-pr
297
297
if (p >= 1 .0f ) {
298
298
return ;
299
299
}
300
300
301
301
// Compute the softmax of logits and calculate entropy
302
- llama_sample_softmax ((struct llama_sampling *) nullptr , candidates);
302
+ llama_sample_softmax_impl ((struct llama_sampling *) nullptr , candidates);
303
303
304
304
const int64_t t_start_sample_us = ggml_time_us ();
305
305
@@ -355,7 +355,7 @@ void llama_sample_typical(struct llama_sampling * smpl, llama_token_data_array *
355
355
}
356
356
}
357
357
358
- void llama_sample_entropy (struct llama_sampling * smpl, llama_token_data_array * candidates, float min_temp, float max_temp, float exponent_val) {
358
+ void llama_sample_entropy_impl (struct llama_sampling * smpl, llama_token_data_array * candidates, float min_temp, float max_temp, float exponent_val) {
359
359
const int64_t t_start_sample_us = ggml_time_us ();
360
360
361
361
// no need to do anything if there is only one (or zero) candidates
@@ -366,7 +366,7 @@ void llama_sample_entropy(struct llama_sampling * smpl, llama_token_data_array *
366
366
// Calculate maximum possible entropy
367
367
float max_entropy = -logf (1 .0f / candidates->size );
368
368
369
- llama_sample_softmax ((struct llama_sampling *) nullptr , candidates);
369
+ llama_sample_softmax_impl ((struct llama_sampling *) nullptr , candidates);
370
370
371
371
// Calculate entropy of the softmax probabilities
372
372
float entropy = 0 .0f ;
@@ -422,7 +422,7 @@ void llama_sample_entropy(struct llama_sampling * smpl, llama_token_data_array *
422
422
}
423
423
}
424
424
425
- void llama_sample_temp (struct llama_sampling * smpl, llama_token_data_array * candidates, float temp) {
425
+ void llama_sample_temp_impl (struct llama_sampling * smpl, llama_token_data_array * candidates, float temp) {
426
426
const int64_t t_start_sample_us = ggml_time_us ();
427
427
428
428
for (size_t i = 0 ; i < candidates->size ; ++i) {
@@ -434,7 +434,7 @@ void llama_sample_temp(struct llama_sampling * smpl, llama_token_data_array * ca
434
434
}
435
435
}
436
436
437
- void llama_sample_repetition_penalties (
437
+ void llama_sample_repetition_penalties_impl (
438
438
struct llama_sampling * smpl,
439
439
llama_token_data_array * candidates,
440
440
const llama_token * last_tokens,
@@ -481,7 +481,7 @@ void llama_sample_repetition_penalties(
481
481
}
482
482
}
483
483
484
- void llama_sample_apply_guidance (
484
+ void llama_sample_apply_guidance_impl (
485
485
struct llama_sampling * smpl,
486
486
float * logits,
487
487
float * logits_guidance,
@@ -504,14 +504,14 @@ void llama_sample_apply_guidance(
504
504
smpl->t_sample_us += ggml_time_us () - t_start_sample_us;
505
505
}
506
506
507
- llama_token llama_sample_token_mirostat (struct llama_sampling * smpl, llama_token_data_array * candidates, float tau, float eta, int32_t m, float * mu) {
507
+ llama_token llama_sample_token_mirostat_impl (struct llama_sampling * smpl, llama_token_data_array * candidates, float tau, float eta, int32_t m, float * mu) {
508
508
GGML_ASSERT (smpl);
509
509
510
510
const int32_t n_vocab = float (smpl->n_vocab );
511
511
512
512
int64_t t_start_sample_us = ggml_time_us ();
513
513
514
- llama_sample_softmax ((struct llama_sampling *) nullptr , candidates);
514
+ llama_sample_softmax_impl ((struct llama_sampling *) nullptr , candidates);
515
515
516
516
// Estimate s_hat using the most probable m tokens
517
517
float s_hat = 0.0 ;
@@ -530,9 +530,9 @@ llama_token llama_sample_token_mirostat(struct llama_sampling * smpl, llama_toke
530
530
float k = powf ((epsilon_hat * powf (2 , *mu)) / (1 - powf (n_vocab, -epsilon_hat)), 1 / s_hat);
531
531
532
532
// Sample the next word X using top-k sampling
533
- llama_sample_top_k ((struct llama_sampling *) nullptr , candidates, int (k), 1 );
533
+ llama_sample_top_k_impl ((struct llama_sampling *) nullptr , candidates, int (k), 1 );
534
534
smpl->t_sample_us += ggml_time_us () - t_start_sample_us;
535
- llama_token X = llama_sample_token (smpl, candidates);
535
+ llama_token X = llama_sample_token_impl (smpl, candidates);
536
536
t_start_sample_us = ggml_time_us ();
537
537
538
538
// Compute error as the difference between observed surprise and target surprise value
@@ -549,11 +549,11 @@ llama_token llama_sample_token_mirostat(struct llama_sampling * smpl, llama_toke
549
549
return X;
550
550
}
551
551
552
- llama_token llama_sample_token_mirostat_v2 (struct llama_sampling * smpl, llama_token_data_array * candidates, float tau, float eta, float * mu) {
552
+ llama_token llama_sample_token_mirostat_v2_impl (struct llama_sampling * smpl, llama_token_data_array * candidates, float tau, float eta, float * mu) {
553
553
int64_t t_start_sample_us;
554
554
t_start_sample_us = ggml_time_us ();
555
555
556
- llama_sample_softmax (smpl, candidates);
556
+ llama_sample_softmax_impl (smpl, candidates);
557
557
558
558
// Truncate the words with surprise values greater than mu
559
559
candidates->size = std::distance (candidates->data , std::find_if (candidates->data , candidates->data + candidates->size , [&](const llama_token_data & candidate) {
@@ -569,10 +569,10 @@ llama_token llama_sample_token_mirostat_v2(struct llama_sampling * smpl, llama_t
569
569
}
570
570
571
571
// Normalize the probabilities of the remaining words
572
- llama_sample_softmax (smpl, candidates);
572
+ llama_sample_softmax_impl (smpl, candidates);
573
573
574
574
// Sample the next word X from the remaining words
575
- llama_token X = llama_sample_token (smpl, candidates);
575
+ llama_token X = llama_sample_token_impl (smpl, candidates);
576
576
t_start_sample_us = ggml_time_us ();
577
577
578
578
// Compute error as the difference between observed surprise and target surprise value
@@ -591,7 +591,7 @@ llama_token llama_sample_token_mirostat_v2(struct llama_sampling * smpl, llama_t
591
591
return X;
592
592
}
593
593
594
- llama_token llama_sample_token_greedy (struct llama_sampling * smpl, llama_token_data_array * candidates) {
594
+ llama_token llama_sample_token_greedy_impl (struct llama_sampling * smpl, llama_token_data_array * candidates) {
595
595
const int64_t t_start_sample_us = ggml_time_us ();
596
596
597
597
// Find max element
@@ -607,11 +607,11 @@ llama_token llama_sample_token_greedy(struct llama_sampling * smpl, llama_token_
607
607
return result;
608
608
}
609
609
610
- llama_token llama_sample_token_with_rng (struct llama_sampling * smpl, llama_token_data_array * candidates, std::mt19937 & rng) {
610
+ llama_token llama_sample_token_with_rng_impl (struct llama_sampling * smpl, llama_token_data_array * candidates, std::mt19937 & rng) {
611
611
GGML_ASSERT (smpl);
612
612
613
613
const int64_t t_start_sample_us = ggml_time_us ();
614
- llama_sample_softmax ((struct llama_sampling *) nullptr , candidates);
614
+ llama_sample_softmax_impl ((struct llama_sampling *) nullptr , candidates);
615
615
616
616
std::vector<float > probs;
617
617
probs.reserve (candidates->size );
@@ -630,6 +630,6 @@ llama_token llama_sample_token_with_rng(struct llama_sampling * smpl, llama_toke
630
630
return result;
631
631
}
632
632
633
- llama_token llama_sample_token (struct llama_sampling * smpl, llama_token_data_array * candidates) {
634
- return llama_sample_token_with_rng (smpl, candidates, smpl->rng );
633
+ llama_token llama_sample_token_impl (struct llama_sampling * smpl, llama_token_data_array * candidates) {
634
+ return llama_sample_token_with_rng_impl (smpl, candidates, smpl->rng );
635
635
}
0 commit comments