ngram-cache.cpp 11 KB

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