test-sampling.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 * cur_p) {
  11. for (size_t i = 0; i < cur_p->size; i++) {
  12. printf("%d: %f (%f)\n", cur_p->data[i].id, cur_p->data[i].p, cur_p->data[i].logit);
  13. }
  14. }
  15. #define DUMP(__cur_p) do { printf("%s:%d (%s)\n", __FILE__, __LINE__, __func__); dump((__cur_p)); printf("-\n"); } while(0)
  16. struct sampler_tester {
  17. sampler_tester(size_t n_vocab) {
  18. cur.reserve(n_vocab);
  19. for (llama_token token_id = 0; token_id < (llama_token)n_vocab; token_id++) {
  20. const float logit = logf(token_id);
  21. cur.emplace_back(llama_token_data{token_id, logit, 0.0f});
  22. }
  23. cur_p = llama_token_data_array { cur.data(), cur.size(), -1, false };
  24. }
  25. sampler_tester(const std::vector<float> & probs, const std::vector<float> & probs_expected) : probs_expected(probs_expected) {
  26. cur.reserve(probs.size());
  27. for (llama_token token_id = 0; token_id < (llama_token)probs.size(); token_id++) {
  28. const float logit = logf(probs[token_id]);
  29. cur.emplace_back(llama_token_data{token_id, logit, probs[token_id]});
  30. }
  31. cur_p = llama_token_data_array { cur.data(), cur.size(), -1, false };
  32. }
  33. void apply(llama_sampler * sampler) {
  34. llama_sampler_apply(sampler, &cur_p);
  35. llama_sampler_free(sampler);
  36. }
  37. void check() {
  38. GGML_ASSERT(cur_p.size == probs_expected.size());
  39. for (size_t i = 0; i < cur_p.size; i++) {
  40. GGML_ASSERT(fabs(cur_p.data[i].p - probs_expected[i]) < 1e-5);
  41. }
  42. }
  43. llama_token_data_array cur_p;
  44. private:
  45. const std::vector<float> probs_expected;
  46. std::vector<llama_token_data> cur;
  47. };
  48. static void test_temp(const std::vector<float> & probs, const std::vector<float> & probs_expected, float temp) {
  49. sampler_tester tester(probs, probs_expected);
  50. DUMP(&tester.cur_p);
  51. tester.apply(llama_sampler_init_temp(temp));
  52. tester.apply(llama_sampler_init_dist(0));
  53. DUMP(&tester.cur_p);
  54. tester.check();
  55. }
  56. static void test_temp_ext(const std::vector<float> & probs, const std::vector<float> & probs_expected, float temp, float delta, float exponent) {
  57. sampler_tester tester(probs, probs_expected);
  58. DUMP(&tester.cur_p);
  59. tester.apply(llama_sampler_init_temp_ext(temp, delta, exponent));
  60. tester.apply(llama_sampler_init_dist (0));
  61. DUMP(&tester.cur_p);
  62. tester.check();
  63. }
  64. static void test_top_k(const std::vector<float> & probs, const std::vector<float> & probs_expected, int k) {
  65. sampler_tester tester(probs, probs_expected);
  66. DUMP(&tester.cur_p);
  67. tester.apply(llama_sampler_init_top_k(k));
  68. tester.apply(llama_sampler_init_dist (0));
  69. DUMP(&tester.cur_p);
  70. tester.check();
  71. }
  72. static void test_top_p(const std::vector<float> & probs, const std::vector<float> & probs_expected, float p) {
  73. sampler_tester tester(probs, probs_expected);
  74. DUMP(&tester.cur_p);
  75. tester.apply(llama_sampler_init_top_p(p, 1));
  76. tester.apply(llama_sampler_init_dist (0));
  77. DUMP(&tester.cur_p);
  78. tester.check();
  79. }
  80. static void test_tfs(const std::vector<float> & probs, const std::vector<float> & probs_expected, float z) {
  81. sampler_tester tester(probs, probs_expected);
  82. DUMP(&tester.cur_p);
  83. tester.apply(llama_sampler_init_tail_free(z, 1));
  84. DUMP(&tester.cur_p);
  85. tester.check();
  86. }
  87. static void test_min_p(const std::vector<float> & probs, const std::vector<float> & probs_expected, float p) {
  88. sampler_tester tester(probs, probs_expected);
  89. DUMP(&tester.cur_p);
  90. tester.apply(llama_sampler_init_min_p(p, 1));
  91. tester.apply(llama_sampler_init_dist (0));
  92. DUMP(&tester.cur_p);
  93. tester.check();
  94. }
  95. static void test_xtc(const std::vector<float> & probs, const std::vector<float> & probs_expected, float p, float t) {
  96. sampler_tester tester(probs, probs_expected);
  97. DUMP(&tester.cur_p);
  98. tester.apply(llama_sampler_init_xtc(p, t, 0, 0));
  99. DUMP(&tester.cur_p);
  100. tester.check();
  101. }
  102. static void test_typical(const std::vector<float> & probs, const std::vector<float> & probs_expected, float p) {
  103. sampler_tester tester(probs, probs_expected);
  104. DUMP(&tester.cur_p);
  105. tester.apply(llama_sampler_init_typical(p, 1));
  106. DUMP(&tester.cur_p);
  107. tester.check();
  108. }
  109. static void test_penalties(
  110. const std::vector<float> & probs, const std::vector<llama_token> & last_tokens,
  111. const std::vector<float> & probs_expected, float repeat_penalty, float alpha_frequency, float alpha_presence
  112. ) {
  113. GGML_ASSERT(probs.size() == probs_expected.size());
  114. sampler_tester tester(probs, probs_expected);
  115. const size_t n_vocab = probs.size();
  116. auto * sampler = llama_sampler_init_penalties(n_vocab, LLAMA_TOKEN_NULL, LLAMA_TOKEN_NULL, last_tokens.size(), repeat_penalty, alpha_frequency, alpha_presence, false, false);
  117. for (size_t i = 0; i < last_tokens.size(); i++) {
  118. llama_sampler_accept(sampler, last_tokens[i]);
  119. }
  120. DUMP(&tester.cur_p);
  121. tester.apply(sampler);
  122. tester.apply(llama_sampler_init_dist(0));
  123. DUMP(&tester.cur_p);
  124. tester.check();
  125. }
  126. 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
  127. ) {
  128. sampler_tester tester(n_vocab);
  129. llama_token min_token_id = 0;
  130. const llama_token max_token_id = n_vocab-1;
  131. for (auto s : samplers_sequence) {
  132. switch (s){
  133. case 'k': tester.apply(llama_sampler_init_top_k(top_k)); break;
  134. case 'f': GGML_ABORT("tail_free test not implemented");
  135. case 'y': GGML_ABORT("typical test not implemented");
  136. case 'p': tester.apply(llama_sampler_init_top_p(top_p, 1)); break;
  137. case 'm': tester.apply(llama_sampler_init_min_p(min_p, 1)); break;
  138. case 't': GGML_ABORT("temperature test not implemented");
  139. default : GGML_ABORT("Unknown sampler");
  140. }
  141. tester.apply(llama_sampler_init_dist(0));
  142. auto & cur_p = tester.cur_p;
  143. const int size = cur_p.size;
  144. if (s == 'k') {
  145. const int expected_size = std::min(size, top_k);
  146. min_token_id = std::max(min_token_id, (llama_token)(n_vocab - top_k));
  147. GGML_ASSERT(size == expected_size);
  148. GGML_ASSERT(cur_p.data[0].id == max_token_id);
  149. GGML_ASSERT(cur_p.data[expected_size-1].id == min_token_id);
  150. } else if (s == 'p') {
  151. const int softmax_divisor = n_vocab * (n_vocab-1) / 2 - min_token_id * (min_token_id-1) / 2;
  152. const int softmax_numerator_target = ceilf(top_p * softmax_divisor);
  153. min_token_id = n_vocab;
  154. int expected_size = 0;
  155. int cumsum = 0;
  156. do { // do-while because always at least one token is sampled
  157. min_token_id--;
  158. expected_size++;
  159. cumsum += min_token_id;
  160. } while (cumsum < softmax_numerator_target);
  161. // token 0 has p == 0, need special consideration for cumsum because top_p immediately returns
  162. if (min_token_id == 1) {
  163. min_token_id--;
  164. expected_size += 1;
  165. }
  166. GGML_ASSERT(size == expected_size);
  167. GGML_ASSERT(cur_p.data[0].id == max_token_id);
  168. GGML_ASSERT(cur_p.data[expected_size-1].id == min_token_id);
  169. } else if (s == 'm') {
  170. int expected_size = ceilf((1.0f-min_p) * n_vocab);
  171. expected_size = std::max(expected_size, 1);
  172. expected_size = std::min(expected_size, size);
  173. min_token_id = floorf(min_p * n_vocab);
  174. min_token_id = std::max(min_token_id, 1);
  175. min_token_id = std::max(min_token_id, (llama_token)(n_vocab - size));
  176. min_token_id = std::min(min_token_id, (llama_token)(n_vocab - 1));
  177. GGML_ASSERT(size == expected_size);
  178. GGML_ASSERT(cur_p.data[0].id == max_token_id);
  179. GGML_ASSERT(cur_p.data[expected_size-1].id == min_token_id);
  180. } else {
  181. GGML_ABORT("fatal error");
  182. }
  183. }
  184. printf("Sampler queue %3s OK with n_vocab=%05zu top_k=%05d top_p=%f min_p=%f\n",
  185. samplers_sequence.c_str(), n_vocab, top_k, top_p, min_p);
  186. }
  187. static void bench(llama_sampler * cnstr, const char * cnstr_name, const std::vector<llama_token_data> & data, int n_iter) {
  188. std::vector<llama_token_data> cur(data.size());
  189. std::copy(data.begin(), data.end(), cur.begin());
  190. llama_token_data_array cur_p = { cur.data(), cur.size(), -1, false };
  191. llama_sampler_apply(cnstr, &cur_p);
  192. llama_sampler_reset(cnstr);
  193. const int64_t t_start = ggml_time_us();
  194. for (int i = 0; i < n_iter; i++) {
  195. std::copy(data.begin(), data.end(), cur.begin());
  196. llama_token_data_array cur_p = { cur.data(), cur.size(), -1, false };
  197. llama_sampler_apply(cnstr, &cur_p);
  198. llama_sampler_reset(cnstr);
  199. }
  200. const int64_t t_end = ggml_time_us();
  201. llama_sampler_free(cnstr);
  202. printf("%-43s: %8.3f us/iter\n", cnstr_name, (t_end - t_start) / (float)n_iter);
  203. }
  204. #define BENCH(__cnstr, __data, __n_iter) bench((__cnstr), #__cnstr, (__data), (__n_iter))
  205. static void test_perf() {
  206. const int n_vocab = 1 << 17;
  207. std::vector<llama_token_data> data;
  208. data.reserve(n_vocab);
  209. for (int i = 0; i < n_vocab; i++) {
  210. const float logit = 2.0f*((float)(rand())/RAND_MAX - 0.5f);
  211. data.emplace_back(llama_token_data{i, logit, 0.0f});
  212. }
  213. BENCH(llama_sampler_init_top_k (40), data, 32);
  214. BENCH(llama_sampler_init_top_p (0.8f, 1), data, 32);
  215. BENCH(llama_sampler_init_min_p (0.2f, 1), data, 32);
  216. BENCH(llama_sampler_init_tail_free(0.5f, 1), data, 32);
  217. BENCH(llama_sampler_init_typical (0.5f, 1), data, 32);
  218. BENCH(llama_sampler_init_xtc (1.0f, 0.1f, 1, 1), data, 32);
  219. }
  220. int main(void) {
  221. ggml_time_init();
  222. test_temp({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f, 0.3f, 0.2f, 0.1f}, 1.0f);
  223. test_temp({0.1f, 0.2f, 0.3f, 0.4f}, {1.0f, 0.0f, 0.0f, 0.0f}, 0.0f);
  224. test_temp_ext({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f, 0.3f, 0.2f, 0.1f}, 1.0f, 0.0f, 1.0f);
  225. test_temp_ext({0.1f, 0.2f, 0.3f, 0.4f}, {1.0f, 0.0f, 0.0f, 0.0f}, 0.0f, 0.0f, 1.0f);
  226. test_top_k({0.1f, 0.2f, 0.3f, 0.4f}, {1.0f}, 1);
  227. test_top_k({0.1f, 0.2f, 0.3f, 0.4f}, {0.44444f, 0.33333f, 0.22222f}, 3);
  228. test_top_k({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f, 0.3f, 0.2f, 0.1f}, 4);
  229. test_top_k({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f, 0.3f, 0.2f, 0.1f}, 0);
  230. test_top_p({0.1f, 0.2f, 0.3f, 0.4f}, {1.0f}, 0);
  231. test_top_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.571429f, 0.428571f}, 0.7f);
  232. test_top_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.44444f, 0.33333f, 0.22222f}, 0.8f);
  233. test_top_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f, 0.3f, 0.2f, 0.1f}, 1.0f);
  234. 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);
  235. 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);
  236. 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);
  237. 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);
  238. test_min_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f/0.7f, 0.3f/0.7f}, 0.51f);
  239. test_min_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f/0.7f, 0.3f/0.7f}, 0.74f);
  240. test_min_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f/0.4f}, 0.76f);
  241. test_min_p({0.1f, 0.2f, 0.3f, 0.4f}, {0.4f/0.4f}, 1.00f);
  242. printf("XTC should:\n");
  243. test_xtc({0.4f, 0.3f, 0.2f, 0.1f}, {0.1f}, 0.99f, 0.09f);
  244. test_xtc({0.4f, 0.3f, 0.2f, 0.1f}, {0.2f, 0.1f}, 0.99f, 0.19f);
  245. test_xtc({0.4f, 0.3f, 0.2f, 0.1f}, {0.3f, 0.2f, 0.1f}, 0.99f, 0.29f);
  246. printf("XTC should not:\n");
  247. test_xtc({0.4f, 0.3f, 0.2f, 0.1f}, {0.4f, 0.3f, 0.2f, 0.1f}, 0.99f, 0.39f);
  248. test_tfs({0.1f, 0.15f, 0.2f, 0.25f, 0.3f}, {0.3f}, 0.25f);
  249. test_tfs({0.1f, 0.15f, 0.2f, 0.25f, 0.3f}, {0.3f, 0.25f}, 0.75f);
  250. test_tfs({0.1f, 0.15f, 0.2f, 0.25f, 0.3f}, {0.3f, 0.25f}, 0.99f);
  251. test_typical({0.97f, 0.01f, 0.01f, 0.01f}, {0.97f}, 0.5f);
  252. test_typical({0.4f, 0.2f, 0.2f, 0.2f}, {0.2f, 0.2f, 0.2f}, 0.5f);
  253. test_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);
  254. test_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);
  255. test_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);
  256. test_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);
  257. test_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);
  258. test_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);
  259. test_sampler_queue(10000, "k", 10000, 1.0f, 1.0f);
  260. test_sampler_queue(10000, "k", 1, 1.0f, 1.0f);
  261. test_sampler_queue(10000, "p", 10000, 1.0f, 1.0f);
  262. test_sampler_queue(10000, "p", 10000, 0.0f, 1.0f);
  263. test_sampler_queue(10000, "m", 10000, 1.0f, 1.0f);
  264. test_sampler_queue(10000, "m", 10000, 1.0f, 1e-12);
  265. test_sampler_queue(10000, "k", 100, 1.0000f, 1.0f);
  266. test_sampler_queue(10000, "p", 10000, 0.0002f, 1.0f);
  267. test_sampler_queue(10000, "p", 10000, 0.8000f, 1.0f);
  268. test_sampler_queue(10000, "m", 10000, 1.0000f, 9997.9f/9999.0f);
  269. test_sampler_queue(10000, "m", 10000, 1.0000f, 0.1f);
  270. test_sampler_queue(10000, "kp", 100, 0.8f, 0.1f);
  271. test_sampler_queue(10000, "km", 100, 0.8f, 0.1f);
  272. test_sampler_queue(10000, "pk", 100, 0.8f, 0.1f);
  273. test_sampler_queue(10000, "pm", 100, 0.8f, 0.1f);
  274. test_sampler_queue(10000, "mk", 100, 0.8f, 0.1f);
  275. test_sampler_queue(10000, "mp", 100, 0.8f, 9997.9f/9999.0f);
  276. test_sampler_queue(10000, "mp", 100, 0.8f, 0.1f);
  277. test_sampler_queue(10000, "kpm", 100, 0.8f, 0.1f);
  278. test_sampler_queue(10000, "kmp", 100, 0.8f, 0.1f);
  279. test_sampler_queue(10000, "pkm", 100, 0.8f, 0.1f);
  280. test_sampler_queue(10000, "pmk", 100, 0.8f, 0.1f);
  281. test_sampler_queue(10000, "mkp", 100, 0.8f, 0.1f);
  282. test_sampler_queue(10000, "mpk", 100, 0.8f, 0.1f);
  283. printf("OK\n");
  284. test_perf();
  285. return 0;
  286. }