1
0

ngram-cache.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include "ngram-cache.h"
  2. #include "common.h"
  3. #include "log.h"
  4. #include <cinttypes>
  5. #include <cstdint>
  6. #include <cstdio>
  7. #include <fstream>
  8. #include <thread>
  9. void llama_ngram_cache_update(llama_ngram_cache & ngram_cache, int ngram_min, int ngram_max,
  10. std::vector<llama_token> & inp, int nnew, bool print_progress) {
  11. const int64_t t_start_ms = ggml_time_ms();
  12. const int64_t inp_size = inp.size();
  13. const int64_t n_todo = inp_size * (ngram_max - ngram_min + 1);
  14. int64_t n_done = 0;
  15. for (int64_t ngram_size = ngram_min; ngram_size <= ngram_max; ++ngram_size) {
  16. const int64_t i_start = std::max(inp_size - nnew, ngram_size);
  17. for (int64_t i = i_start; i < inp_size; ++i) {
  18. const int64_t ngram_start = i - ngram_size;
  19. llama_ngram ngram(&inp[ngram_start], ngram_size);
  20. const llama_token token = inp[i];
  21. llama_ngram_cache::iterator part_it = ngram_cache.find(ngram);
  22. if (part_it == ngram_cache.end()) {
  23. llama_ngram_cache_part part;
  24. part.emplace(token, 1);
  25. ngram_cache.emplace(ngram, part);
  26. } else {
  27. llama_ngram_cache_part::iterator token_count_it = part_it->second.find(token);
  28. if (token_count_it == part_it->second.end()) {
  29. part_it->second.emplace(token, 1);
  30. } else {
  31. token_count_it->second++;
  32. }
  33. }
  34. ++n_done;
  35. if (print_progress && n_done % 10000000 == 0) {
  36. const int64_t t_now_ms = ggml_time_ms();
  37. const int64_t eta_ms = (inp_size*(ngram_max-ngram_min+1) - n_done) * (t_now_ms - t_start_ms) / n_done;
  38. const int64_t eta_min = eta_ms / (60*1000);
  39. const int64_t eta_s = (eta_ms - 60*1000*eta_min) / 1000;
  40. fprintf(stderr, "%s: %" PRId64 "/%" PRId64 " done, ETA: %02" PRId64 ":%02" PRId64 "\n", __func__, n_done, n_todo, eta_min, eta_s);
  41. }
  42. }
  43. }
  44. }
  45. // Helper function to get a token from the combined, speculative sequence of inp and draft.
  46. static llama_token get_token(const std::vector<llama_token> & inp, const std::vector<llama_token> & draft, const size_t i) {
  47. return i < inp.size() ? inp[i] : draft[1 + i - inp.size()];
  48. }
  49. // If sample size or percentage are below these thresholds the draft is aborted early:
  50. constexpr int draft_min_sample_size_lax[LLAMA_NGRAM_MAX] = { 2, 2, 1, 1};
  51. constexpr int draft_min_percent_lax[LLAMA_NGRAM_MAX] = {66, 50, 50, 50};
  52. constexpr int draft_min_sample_size_strict[LLAMA_NGRAM_MAX] = { 4, 3, 2, 2};
  53. constexpr int draft_min_percent_strict[LLAMA_NGRAM_MAX] = {75, 66, 66, 66};
  54. // Helper function that tries to draft a token from only the static ngram cache:
  55. static llama_token try_draft(llama_ngram_cache & nc_static, const llama_ngram ngram_static) {
  56. llama_ngram_cache::iterator part_static_it = nc_static.find(ngram_static);
  57. if (part_static_it == nc_static.end()) {
  58. return -1;
  59. }
  60. const llama_ngram_cache_part part_static = part_static_it->second;
  61. int max_count_static = 0;
  62. int sum_count_static = 0;
  63. llama_token max_token = -1;
  64. for (std::pair<llama_token, int> token_count_static : part_static) {
  65. const llama_token token = token_count_static.first;
  66. const int32_t count_static = token_count_static.second;
  67. if (count_static > max_count_static) {
  68. max_token = token;
  69. max_count_static = count_static;
  70. }
  71. sum_count_static += count_static;
  72. }
  73. if (sum_count_static < draft_min_sample_size_lax[LLAMA_NGRAM_STATIC-1]) {
  74. return -1;
  75. }
  76. if (100*max_count_static < draft_min_percent_lax[LLAMA_NGRAM_STATIC-1]*sum_count_static) {
  77. return -1;
  78. }
  79. return max_token;
  80. }
  81. // Try to draft a token from primary cache (context/dynamic), validate with static cache:
  82. static llama_token try_draft(
  83. llama_ngram_cache & nc_primary, const std::vector<llama_ngram> & ngrams_primary, llama_ngram_cache_part & part_static,
  84. const int * min_sample_size, const int * min_percent) {
  85. llama_token drafted_token = -1;
  86. for (int i = ngrams_primary.size()-1; i >= 0 && drafted_token == -1; --i) {
  87. const llama_ngram ngram_primary = ngrams_primary[i];
  88. llama_ngram_cache::iterator part_primary_it = nc_primary.find(ngram_primary);
  89. if (part_primary_it == nc_primary.end()) {
  90. continue;
  91. }
  92. const llama_ngram_cache_part part_primary = part_primary_it->second;
  93. int max_count_primary = 0;
  94. int max_count_static = 0;
  95. int sum_count_primary = 0;
  96. llama_token max_token = -1;
  97. for (std::pair<llama_token, int> token_count_primary : part_primary) {
  98. const llama_token token = token_count_primary.first;
  99. llama_ngram_cache_part::iterator token_count_static_it = part_static.find(token);
  100. const int32_t count_primary = token_count_primary.second;
  101. const int32_t count_static = token_count_static_it != part_static.end() ? 100*token_count_static_it->second : 1;
  102. if (count_primary*count_static > max_count_primary*max_count_static) {
  103. max_token = token;
  104. max_count_primary = count_primary;
  105. max_count_static = count_static;
  106. }
  107. sum_count_primary += count_primary;
  108. }
  109. if (sum_count_primary < min_sample_size[i]) {
  110. continue;
  111. }
  112. if (100*max_count_primary < min_percent[i]*sum_count_primary) {
  113. continue;;
  114. }
  115. drafted_token = max_token;
  116. }
  117. return drafted_token;
  118. }
  119. void llama_ngram_cache_draft(
  120. std::vector<llama_token> & inp, std::vector<llama_token> & draft, int n_draft, int ngram_min, int ngram_max,
  121. llama_ngram_cache & nc_context, llama_ngram_cache & nc_dynamic, llama_ngram_cache & nc_static
  122. ) {
  123. GGML_ASSERT(draft.size() == 1);
  124. const int inp_size = inp.size();
  125. if (inp_size < LLAMA_NGRAM_STATIC) {
  126. return;
  127. }
  128. while ((int) draft.size()-1 < n_draft) {
  129. llama_token drafted_token = -1;
  130. const int ngram_start_static = inp_size-LLAMA_NGRAM_STATIC + draft.size()-1;
  131. llama_ngram ngram_static;
  132. for (int j = ngram_start_static; j < ngram_start_static + LLAMA_NGRAM_STATIC; ++j) {
  133. ngram_static.tokens[j-ngram_start_static] = get_token(inp, draft, j);
  134. }
  135. llama_ngram_cache::iterator part_static_it = nc_static.find(ngram_static);
  136. llama_ngram_cache_part part_static;
  137. if (part_static_it != nc_static.end()) {
  138. part_static = part_static_it->second;
  139. }
  140. // cd = context + dynamic
  141. std::vector<llama_ngram> ngrams_cd;
  142. for (int ngram_size_cd = ngram_min; ngram_size_cd <= ngram_max; ++ngram_size_cd) {
  143. const int ngram_start_cd = inp_size-ngram_size_cd + draft.size()-1;
  144. llama_ngram ngram_cd;
  145. for (int j = ngram_start_cd; j < ngram_start_cd + ngram_size_cd; ++j) {
  146. ngram_cd.tokens[j-ngram_start_cd] = get_token(inp, draft, j);
  147. }
  148. ngrams_cd.push_back(ngram_cd);
  149. }
  150. if (drafted_token == -1) {
  151. drafted_token = try_draft(nc_context, ngrams_cd, part_static, draft_min_sample_size_lax, draft_min_percent_lax);
  152. }
  153. if (drafted_token == -1) {
  154. drafted_token = try_draft(nc_dynamic, ngrams_cd, part_static, draft_min_sample_size_strict, draft_min_percent_strict);
  155. }
  156. if (drafted_token == -1) {
  157. drafted_token = try_draft(nc_static, ngram_static);
  158. }
  159. if (drafted_token == -1) {
  160. break;
  161. }
  162. LOG(" - draft candidate: token=%d\n", drafted_token);
  163. draft.push_back(drafted_token);
  164. }
  165. }
  166. void llama_ngram_cache_save(llama_ngram_cache & ngram_cache, std::string & filename) {
  167. std::ofstream file_out(filename, std::ios::binary);
  168. for (std::pair<llama_ngram, llama_ngram_cache_part> item : ngram_cache) {
  169. const llama_ngram ngram = item.first;
  170. llama_ngram_cache_part token_counts = item.second;
  171. GGML_ASSERT(!token_counts.empty());
  172. const int32_t ntokens = token_counts.size();
  173. GGML_ASSERT(ntokens > 0);
  174. file_out.write(reinterpret_cast<const char *>(&ngram), sizeof(llama_ngram));
  175. file_out.write(reinterpret_cast<const char *>(&ntokens), sizeof(int32_t));
  176. for (std::pair<llama_token, int32_t> item2 : token_counts) {
  177. const llama_token token = item2.first;
  178. const int32_t count = item2.second;
  179. GGML_ASSERT(count > 0);
  180. file_out.write(reinterpret_cast<const char *>(&token), sizeof(llama_token));
  181. file_out.write(reinterpret_cast<const char *>(&count), sizeof(int32_t));
  182. }
  183. }
  184. }
  185. llama_ngram_cache llama_ngram_cache_load(std::string & filename) {
  186. std::ifstream hashmap_file(filename, std::ios::binary);
  187. if (!hashmap_file) {
  188. throw std::ifstream::failure("Unable to open file " + filename);
  189. }
  190. llama_ngram_cache ngram_cache;
  191. llama_ngram ngram;
  192. int32_t ntokens;
  193. llama_token token;
  194. int32_t count;
  195. char * ngramc = reinterpret_cast<char*>(&ngram);
  196. char * ntokensc = reinterpret_cast<char*>(&ntokens);
  197. char * tokenc = reinterpret_cast<char*>(&token);
  198. char * countc = reinterpret_cast<char*>(&count);
  199. while(hashmap_file.read(ngramc, sizeof(llama_ngram))) {
  200. GGML_ASSERT(!hashmap_file.eof());
  201. GGML_ASSERT(hashmap_file.read(ntokensc, sizeof(int32_t)));
  202. GGML_ASSERT(ntokens > 0);
  203. llama_ngram_cache_part token_counts;
  204. for (int i = 0; i < ntokens; ++i) {
  205. GGML_ASSERT(!hashmap_file.eof());
  206. GGML_ASSERT(hashmap_file.read(tokenc, sizeof(llama_token)));
  207. GGML_ASSERT(!hashmap_file.eof());
  208. GGML_ASSERT(hashmap_file.read(countc, sizeof(int32_t)));
  209. GGML_ASSERT(count > 0);
  210. token_counts.emplace(token, count);
  211. }
  212. ngram_cache.emplace(ngram, token_counts);
  213. }
  214. GGML_ASSERT(hashmap_file.eof());
  215. return ngram_cache;
  216. }
  217. void llama_ngram_cache_merge(llama_ngram_cache & ngram_cache_target, llama_ngram_cache & ngram_cache_add) {
  218. for (std::pair<llama_ngram, llama_ngram_cache_part> ngram_part : ngram_cache_add) {
  219. const llama_ngram ngram = ngram_part.first;
  220. llama_ngram_cache_part part = ngram_part.second;
  221. llama_ngram_cache::iterator part_merged_it = ngram_cache_target.find(ngram);
  222. if (part_merged_it == ngram_cache_target.end()) {
  223. ngram_cache_target.emplace(ngram, part);
  224. continue;
  225. }
  226. for (std::pair<llama_token, int32_t> token_count : part) {
  227. const llama_token token = token_count.first;
  228. const int32_t count = token_count.second;
  229. GGML_ASSERT(count > 0);
  230. llama_ngram_cache_part::iterator token_count_merged_it = part_merged_it->second.find(token);
  231. if (token_count_merged_it == part_merged_it->second.end()) {
  232. part_merged_it->second.emplace(token, count);
  233. continue;
  234. }
  235. token_count_merged_it->second += count;
  236. }
  237. }
  238. }