1
0

ngram-cache.cpp 11 KB

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