test-sampling.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #include "ggml.h"
  2. #include "llama.h"
  3. #ifdef NDEBUG
  4. #undef NDEBUG
  5. #endif
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <string>
  9. #include <vector>
  10. static void dump(const llama_token_data_array * candidates) {
  11. for (size_t i = 0; i < candidates->size; i++) {
  12. printf("%d: %f (%f)\n", candidates->data[i].id, candidates->data[i].p, candidates->data[i].logit);
  13. }
  14. }
  15. #define DUMP(__candidates) do { printf("%s:%d (%s)\n", __FILE__, __LINE__, __func__); dump((__candidates)); printf("-\n"); } while(0)
  16. static void test_top_k(const std::vector<float> & probs, const std::vector<float> & expected_probs, int k) {
  17. const size_t n_vocab = probs.size();
  18. std::vector<llama_token_data> candidates;
  19. candidates.reserve(n_vocab);
  20. for (llama_token token_id = 0; token_id < (llama_token)n_vocab; token_id++) {
  21. const float logit = logf(probs[token_id]);
  22. candidates.emplace_back(llama_token_data{token_id, logit, 0.0f});
  23. }
  24. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  25. llama_sample_softmax(nullptr, &candidates_p);
  26. DUMP(&candidates_p);
  27. llama_sample_top_k(nullptr, &candidates_p, k, 1);
  28. DUMP(&candidates_p);
  29. GGML_ASSERT(candidates_p.size == expected_probs.size());
  30. for (size_t i = 0; i < candidates_p.size; i++) {
  31. GGML_ASSERT(fabs(candidates_p.data[i].p - expected_probs[i]) < 1e-5);
  32. }
  33. }
  34. static void test_top_p(const std::vector<float> & probs, const std::vector<float> & expected_probs, float p) {
  35. const size_t n_vocab = probs.size();
  36. std::vector<llama_token_data> candidates;
  37. candidates.reserve(n_vocab);
  38. for (llama_token token_id = 0; token_id < (llama_token)n_vocab; token_id++) {
  39. const float logit = logf(probs[token_id]);
  40. candidates.emplace_back(llama_token_data{token_id, logit, 0.0f});
  41. }
  42. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  43. llama_sample_softmax(nullptr, &candidates_p);
  44. DUMP(&candidates_p);
  45. llama_sample_top_p(nullptr, &candidates_p, p, 1);
  46. DUMP(&candidates_p);
  47. GGML_ASSERT(candidates_p.size == expected_probs.size());
  48. for (size_t i = 0; i < candidates_p.size; i++) {
  49. GGML_ASSERT(fabs(candidates_p.data[i].p - expected_probs[i]) < 1e-3);
  50. }
  51. }
  52. static void test_tfs(const std::vector<float> & probs, const std::vector<float> & expected_probs, float z) {
  53. const size_t n_vocab = probs.size();
  54. std::vector<llama_token_data> candidates;
  55. candidates.reserve(n_vocab);
  56. for (llama_token token_id = 0; token_id < (llama_token)n_vocab; token_id++) {
  57. const float logit = logf(probs[token_id]);
  58. candidates.emplace_back(llama_token_data{token_id, logit, 0.0f});
  59. }
  60. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  61. DUMP(&candidates_p);
  62. llama_sample_tail_free(nullptr, &candidates_p, z, 1);
  63. DUMP(&candidates_p);
  64. GGML_ASSERT(candidates_p.size == expected_probs.size());
  65. for (size_t i = 0; i < candidates_p.size; i++) {
  66. GGML_ASSERT(fabs(candidates_p.data[i].p - expected_probs[i]) < 1e-3);
  67. }
  68. }
  69. static void test_min_p(const std::vector<float> & probs, const std::vector<float> & expected_probs, float p) {
  70. const size_t n_vocab = probs.size();
  71. std::vector<llama_token_data> candidates;
  72. candidates.reserve(n_vocab);
  73. for (llama_token token_id = 0; token_id < (llama_token)n_vocab; token_id++) {
  74. const float logit = logf(probs[token_id]);
  75. candidates.emplace_back(llama_token_data{token_id, logit, 0.0f});
  76. }
  77. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  78. DUMP(&candidates_p);
  79. llama_sample_min_p(nullptr, &candidates_p, p, 1);
  80. DUMP(&candidates_p);
  81. llama_sample_softmax(nullptr, &candidates_p);
  82. GGML_ASSERT(candidates_p.size == expected_probs.size());
  83. for (size_t i = 0; i < candidates_p.size; i++) {
  84. GGML_ASSERT(fabs(candidates_p.data[i].p - expected_probs[i]) < 1e-3);
  85. }
  86. }
  87. static void test_typical(const std::vector<float> & probs, const std::vector<float> & expected_probs, float p) {
  88. const size_t n_vocab = probs.size();
  89. std::vector<llama_token_data> candidates;
  90. candidates.reserve(n_vocab);
  91. for (llama_token token_id = 0; token_id < (llama_token)n_vocab; token_id++) {
  92. const float logit = logf(probs[token_id]);
  93. candidates.emplace_back(llama_token_data{token_id, logit, 0.0f});
  94. }
  95. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  96. DUMP(&candidates_p);
  97. llama_sample_typical(nullptr, &candidates_p, p, 1);
  98. DUMP(&candidates_p);
  99. GGML_ASSERT(candidates_p.size == expected_probs.size());
  100. for (size_t i = 0; i < candidates_p.size; i++) {
  101. GGML_ASSERT(fabs(candidates_p.data[i].p - expected_probs[i]) < 1e-3);
  102. }
  103. }
  104. static void test_repetition_penalties(
  105. const std::vector<float> & probs, const std::vector<llama_token> & last_tokens,
  106. const std::vector<float> & expected_probs, float repeat_penalty, float alpha_frequency, float alpha_presence
  107. ) {
  108. GGML_ASSERT(probs.size() == expected_probs.size());
  109. const size_t n_vocab = probs.size();
  110. std::vector<llama_token_data> candidates;
  111. candidates.reserve(n_vocab);
  112. for (llama_token token_id = 0; token_id < (llama_token)n_vocab; token_id++) {
  113. const float logit = logf(probs[token_id]);
  114. candidates.emplace_back(llama_token_data{token_id, logit, 0.0f});
  115. }
  116. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  117. llama_sample_softmax(nullptr, &candidates_p);
  118. DUMP(&candidates_p);
  119. llama_sample_repetition_penalties(nullptr, &candidates_p, (const llama_token *) last_tokens.data(), last_tokens.size(), repeat_penalty, alpha_frequency, alpha_presence);
  120. llama_sample_softmax(nullptr, &candidates_p);
  121. DUMP(&candidates_p);
  122. GGML_ASSERT(candidates_p.size == expected_probs.size());
  123. for (size_t i = 0; i < candidates_p.size; i++) {
  124. GGML_ASSERT(fabs(candidates_p.data[i].p - expected_probs[i]) < 1e-3);
  125. }
  126. }
  127. static void test_sampler_queue(
  128. const size_t n_vocab, const std::string samplers_sequence, const int top_k, const float top_p, const float min_p
  129. ) {
  130. std::vector<llama_token_data> candidates;
  131. candidates.reserve(n_vocab);
  132. for (llama_token token_id = 0; token_id < (llama_token)n_vocab; token_id++) {
  133. const float logit = logf(token_id);
  134. candidates.emplace_back(llama_token_data{token_id, logit, 0.0f});
  135. }
  136. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  137. llama_token min_token_id = 0;
  138. const llama_token max_token_id = n_vocab-1;
  139. for (auto s : samplers_sequence) {
  140. switch (s){
  141. case 'k': llama_sample_top_k (nullptr, &candidates_p, top_k, 1); break;
  142. case 'f': GGML_ABORT("tail_free test not implemented"); break;
  143. case 'y': GGML_ABORT("typical test not implemented"); break;
  144. case 'p': llama_sample_top_p (nullptr, &candidates_p, top_p, 1); break;
  145. case 'm': llama_sample_min_p (nullptr, &candidates_p, min_p, 1); break;
  146. case 't': GGML_ABORT("temperature test not implemented"); break;
  147. default : GGML_ABORT("Unknown sampler"); break;
  148. }
  149. llama_sample_softmax(nullptr, &candidates_p); // make sure tokens are sorted for tests
  150. const int size = candidates_p.size;
  151. if (s == 'k') {
  152. const int expected_size = std::min(size, top_k);
  153. min_token_id = std::max(min_token_id, (llama_token)(n_vocab - top_k));
  154. GGML_ASSERT(size == expected_size);
  155. GGML_ASSERT(candidates_p.data[0].id == max_token_id);
  156. GGML_ASSERT(candidates_p.data[expected_size-1].id == min_token_id);
  157. } else if (s == 'p') {
  158. const int softmax_divisor = n_vocab * (n_vocab-1) / 2 - min_token_id * (min_token_id-1) / 2;
  159. const int softmax_numerator_target = ceilf(top_p * softmax_divisor);
  160. min_token_id = n_vocab;
  161. int expected_size = 0;
  162. int cumsum = 0;
  163. do { // do-while because always at least one token is sampled
  164. min_token_id--;
  165. expected_size++;
  166. cumsum += min_token_id;
  167. } while (cumsum < softmax_numerator_target);
  168. // token 0 has p == 0, need special consideration for cumsum because top_p immediately returns
  169. if (min_token_id == 1) {
  170. min_token_id--;
  171. expected_size += 1;
  172. }
  173. GGML_ASSERT(size == expected_size);
  174. GGML_ASSERT(candidates_p.data[0].id == max_token_id);
  175. GGML_ASSERT(candidates_p.data[expected_size-1].id == min_token_id);
  176. } else if (s == 'm') {
  177. int expected_size = ceilf((1.0f-min_p) * n_vocab);
  178. expected_size = std::max(expected_size, 1);
  179. expected_size = std::min(expected_size, size);
  180. min_token_id = floorf(min_p * n_vocab);
  181. min_token_id = std::max(min_token_id, 1);
  182. min_token_id = std::max(min_token_id, (llama_token)(n_vocab - size));
  183. min_token_id = std::min(min_token_id, (llama_token)(n_vocab - 1));
  184. GGML_ASSERT(size == expected_size);
  185. GGML_ASSERT(candidates_p.data[0].id == max_token_id);
  186. GGML_ASSERT(candidates_p.data[expected_size-1].id == min_token_id);
  187. } else {
  188. GGML_ABORT("fatal error");
  189. }
  190. }
  191. printf("Sampler queue %3s OK with n_vocab=%05ld top_k=%05d top_p=%f min_p=%f\n",
  192. samplers_sequence.c_str(), n_vocab, top_k, top_p, min_p);
  193. }
  194. int main(void) {
  195. ggml_time_init();
  196. test_top_k({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f}, 1);
  197. test_top_k({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f, 0.3f, 0.2f}, 3);
  198. test_top_k({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f, 0.3f, 0.2f, 0.1f}, 4);
  199. test_top_k({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f, 0.3f, 0.2f, 0.1f}, 0);
  200. test_top_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f}, 0);
  201. test_top_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f, 0.3f}, 0.7f);
  202. test_top_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f, 0.3f, 0.2f}, 0.8f);
  203. test_top_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f, 0.3f, 0.2f, 0.1f}, 1);
  204. test_min_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f/1.0f, 0.3f/1.0f, 0.2f/1.0f, 0.1f/1.0f}, 0.00f);
  205. test_min_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f/1.0f, 0.3f/1.0f, 0.2f/1.0f, 0.1f/1.0f}, 0.24f);
  206. test_min_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f/0.9f, 0.3f/0.9f, 0.2f/0.9f}, 0.26f);
  207. test_min_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f/0.9f, 0.3f/0.9f, 0.2f/0.9f}, 0.49f);
  208. test_min_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f/0.7f, 0.3f/0.7f}, 0.51f);
  209. test_min_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f/0.7f, 0.3f/0.7f}, 0.74f);
  210. test_min_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f/0.4f}, 0.76f);
  211. test_min_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f/0.4f}, 1.00f);
  212. test_tfs({0.1f, 0.15f, 0.2f, 0.25f, 0.3f}, {0.3f}, 0.25f);
  213. test_tfs({0.1f, 0.15f, 0.2f, 0.25f, 0.3f}, {0.3f, 0.25f}, 0.75f);
  214. test_tfs({0.1f, 0.15f, 0.2f, 0.25f, 0.3f}, {0.3f, 0.25f}, 0.99f);
  215. test_typical({0.97f, 0.01f, 0.01f, 0.01f}, {0.97f}, 0.5f);
  216. test_typical({0.4f, 0.2f, 0.2f, 0.2f}, {0.2f, 0.2f, 0.2f}, 0.5f);
  217. test_repetition_penalties({0.2f, 0.2f, 0.2f, 0.2f, 0.2f}, {0}, {0.25f, 0.25f, 0.25f, 0.25f, 0}, 50.0f, 0.0f, 0.0f);
  218. test_repetition_penalties({0.2f, 0.2f, 0.2f, 0.2f, 0.2f}, {0, 1, 2}, {0.5f, 0.5f, 0, 0, 0}, 50.0f, 0.0f, 0.0f);
  219. test_repetition_penalties({0.2f, 0.2f, 0.2f, 0.2f, 0.2f}, {0, 1, 2, 0, 0}, {0.5f, 0.5f, 0, 0, 0}, 50.0f, 0.0f, 0.0f);
  220. test_repetition_penalties({0.2f, 0.2f, 0.2f, 0.2f, 0.2f}, {0}, {0.249997f, 0.249997f, 0.249997f, 0.249997f, 0.000011f}, 1.0f, 5.0f, 5.0f);
  221. test_repetition_penalties({0.2f, 0.2f, 0.2f, 0.2f, 0.2f}, {0, 1, 2}, {0.499966f, 0.499966f, 0.000023f, 0.000023f, 0.000023f}, 1.0f, 5.0f, 5.0f);
  222. test_repetition_penalties({0.2f, 0.2f, 0.2f, 0.2f, 0.2f}, {0, 1, 2, 0, 0}, {0.499977f, 0.499977f, 0.000023f, 0.000023f, 0.000000f}, 1.0f, 5.0f, 5.0f);
  223. test_sampler_queue(10000, "k", 10000, 1.0f, 1.0f);
  224. test_sampler_queue(10000, "k", 1, 1.0f, 1.0f);
  225. test_sampler_queue(10000, "p", 10000, 1.0f, 1.0f);
  226. test_sampler_queue(10000, "p", 10000, 0.0f, 1.0f);
  227. test_sampler_queue(10000, "m", 10000, 1.0f, 1.0f);
  228. test_sampler_queue(10000, "m", 10000, 1.0f, 1e-12);
  229. test_sampler_queue(10000, "k", 100, 1.0000f, 1.0f);
  230. test_sampler_queue(10000, "p", 10000, 0.0002f, 1.0f);
  231. test_sampler_queue(10000, "p", 10000, 0.8000f, 1.0f);
  232. test_sampler_queue(10000, "m", 10000, 1.0000f, 9997.9f/9999.0f);
  233. test_sampler_queue(10000, "m", 10000, 1.0000f, 0.1f);
  234. test_sampler_queue(10000, "kp", 100, 0.8f, 0.1f);
  235. test_sampler_queue(10000, "km", 100, 0.8f, 0.1f);
  236. test_sampler_queue(10000, "pk", 100, 0.8f, 0.1f);
  237. test_sampler_queue(10000, "pm", 100, 0.8f, 0.1f);
  238. test_sampler_queue(10000, "mk", 100, 0.8f, 0.1f);
  239. test_sampler_queue(10000, "mp", 100, 0.8f, 9997.9f/9999.0f);
  240. test_sampler_queue(10000, "mp", 100, 0.8f, 0.1f);
  241. test_sampler_queue(10000, "kpm", 100, 0.8f, 0.1f);
  242. test_sampler_queue(10000, "kmp", 100, 0.8f, 0.1f);
  243. test_sampler_queue(10000, "pkm", 100, 0.8f, 0.1f);
  244. test_sampler_queue(10000, "pmk", 100, 0.8f, 0.1f);
  245. test_sampler_queue(10000, "mkp", 100, 0.8f, 0.1f);
  246. test_sampler_queue(10000, "mpk", 100, 0.8f, 0.1f);
  247. printf("OK\n");
  248. return 0;
  249. }