Skip to content

Commit 54ef105

Browse files
committed
added tests and fixed nsigma impl
1 parent 8fb681b commit 54ef105

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/llama-sampling.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,36 +1655,32 @@ struct llama_sampler_top_n_sigma {
16551655
static const char * llama_sampler_top_n_sigma_name(const struct llama_sampler * /*smpl*/) {
16561656
return "top-n-sigma";
16571657
}
1658+
#include <iostream>
16581659

16591660
static void llama_sampler_top_n_sigma_apply(struct llama_sampler * smpl, llama_token_data_array * cur_p) {
16601661
const auto * ctx = (llama_sampler_top_n_sigma *) smpl->ctx;
1661-
// 1. Find max logit: M
1662-
// 2. Find standard deviation of logits: sig
1663-
// 3. Create a mask where m[i] = 1 if ith logit >= M - n (sig), else m[i] = 0
1664-
// 4. Apply mask: ith logit itself if m[i]==1, else ith logit = -inf
1665-
// 5. p = softmax(l)
16661662

16671663
// find max logit and calculate mean
1668-
int32_t max = cur_p->data[0].logit;
1669-
int32_t logits_sum = 0;
1664+
float max = cur_p->data[0].logit;
1665+
float logits_sum = 0;
16701666
for (size_t i = 0; i < cur_p->size; ++i) {
16711667
if(cur_p->data[i].logit > max){
16721668
max = cur_p->data[i].logit;
16731669
}
16741670
logits_sum += cur_p->data[i].logit;
16751671
}
1676-
int32_t mean = logits_sum/cur_p->size;
1672+
float mean = (float)logits_sum/cur_p->size;
16771673

16781674
// calculate standard deviation
1679-
int32_t acc = 0;
1675+
float acc = 0;
16801676
for(size_t i = 0; i < cur_p->size; ++i){
1681-
acc += (cur_p->data[i].logit - mean) * (cur_p->data[i].logit - mean);
1677+
acc += pow(cur_p->data[i].logit - mean, 2);
16821678
}
1683-
int32_t std = sqrt(acc/cur_p->size);
1684-
1679+
float std = sqrt((float)acc/cur_p->size);
1680+
16851681
//apply mask
16861682
for(size_t i = 0; i < cur_p->size; ++i){
1687-
if(cur_p->data[i].logit < max - (ctx->n * std)) {
1683+
if(cur_p->data[i].logit < max - ((float)ctx->n * std)) {
16881684
cur_p->data[i].logit = -INFINITY;
16891685
}
16901686
}

tests/test-sampling.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ static void test_dry(
182182
tester.check();
183183
}
184184

185+
static void test_top_n_sigma(const std::vector<float> & probs, const std::vector<float> & probs_expected, int n) {
186+
sampler_tester tester(probs, probs_expected);
187+
188+
DUMP(&tester.cur_p);
189+
tester.apply(llama_sampler_init_top_n_sigma(n));
190+
tester.apply(llama_sampler_init_dist (0));
191+
DUMP(&tester.cur_p);
192+
193+
tester.check();
194+
}
195+
185196
static void test_sampler_queue(const size_t n_vocab, const std::string & samplers_sequence, const int top_k, const float top_p, const float min_p
186197
) {
187198
sampler_tester tester(n_vocab);
@@ -349,6 +360,14 @@ int main(void) {
349360
test_dry({0.2f, 0.2f, 0.2f, 0.2f, 0.2f}, {0, 1, 2, 0, 1}, {0.241818f, 0.241818f, 0.241818f, 0.241818f, 0.032727f}, 2.0f, 1.1f, 2, 5, {});
350361
test_dry({0.2f, 0.2f, 0.2f, 0.2f, 0.2f}, {0, 1, 2, 3, 4, 0, 1}, {0.2f, 0.2f, 0.2f, 0.2f, 0.2f}, 1.0f, 1.1f, 4, 7, {});
351362

363+
test_top_n_sigma({0.1f, 0.2f, 0.3f, 0.4f}, {0.571429f, 0.428571f, 0.0f, 0.0f}, 1);
364+
test_top_n_sigma({0.1f, 0.2f, 0.3f, 0.4f}, {1.0f, 0.0f, 0.0f, 0.0f}, 0);
365+
test_top_n_sigma({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f, 0.3f, 0.2f, 0.1f}, 3);
366+
367+
// test_top_n_sigma({0.1f, 0.2f, 0.3f, 0.4f}, {0.44444f, 0.33333f, 0.22222f}, 3);
368+
// test_top_n_sigma({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f, 0.3f, 0.2f, 0.1f}, 4);
369+
// test_top_n_sigma({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f, 0.3f, 0.2f, 0.1f}, 0);
370+
352371
test_sampler_queue(10000, "k", 10000, 1.0f, 1.0f);
353372
test_sampler_queue(10000, "k", 1, 1.0f, 1.0f);
354373
test_sampler_queue(10000, "p", 10000, 1.0f, 1.0f);

0 commit comments

Comments
 (0)