MCPcopy Create free account
hub / github.com/antirez/llama.cpp-deepseek-v4-flash / common_ngram_map_draft

Function common_ngram_map_draft

common/ngram-map.cpp:215–510  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

213}
214
215void common_ngram_map_draft(common_ngram_map & map,
216 const llama_tokens & inp, llama_token sampled,
217 llama_tokens & draft) {
218 // reset last key and value.
219 map.last_draft_created = false;
220 map.last_draft_key_idx = 0;
221 map.last_draft_value_idx = 0;
222
223 const size_t cur_len = inp.size();
224 const uint16_t n = map.size_key;
225 const uint16_t m = map.size_value;
226 if (cur_len < static_cast<size_t>(2 * n + m)) {
227 return;
228 }
229 if (cur_len >= static_cast<size_t>(UINT32_MAX)) {
230 // key_map uses uint32_t instead of size_t.
231 GGML_ABORT("%s: cur_len exceeds UINT32_MAX: %zu", __func__, cur_len);
232 }
233
234 if (map.idx_last_check > cur_len) {
235 // Should not happen because of common_ngram_map_begin().
236 GGML_ABORT("%s: map.idx_last_check > cur_len: %zu > %zu", __func__, map.idx_last_check, cur_len);
237 }
238 map.idx_last_check = cur_len;
239
240 // search pattern, the key n-gram
241 std::vector<llama_token> key_tokens;
242 key_tokens.reserve(n);
243 for (size_t j = cur_len - n + 1; j < cur_len; ++j) {
244 key_tokens.push_back(inp[j]);
245 }
246 key_tokens.push_back(sampled);
247
248 // search for the key in the map
249 size_t match_pos = 0;
250 if (map.size_last_begin > cur_len) {
251 GGML_ABORT("%s: map.size_last_begin > cur_len: %zu > %zu", __func__, map.size_last_begin, cur_len);
252 }
253 if (!map.key_map.empty()) {
254 // Search for the key in the map key_map from hash of ngrams to index of ngram.
255 uint32_t idx_hash = (common_ngram_map_hash(key_tokens, 0, n) % map.key_map.size());
256 uint32_t idx_key = map.key_map[idx_hash];
257 if (idx_key != 0 && idx_key < cur_len - n - m - 1) {
258 // Check if the key matches the key at idx_key (because of possible collisions).
259 bool match = true;
260 for (size_t k = 0; k < n; ++k) {
261 if (inp[idx_key + k] != key_tokens[k]) {
262 match = false;
263 break;
264 }
265 }
266 LOG_DBG("%s: key hash %x -> idx_key %d: match %d\n", __func__, idx_hash, idx_key, match ? 1 : 0);
267 if (match) {
268 match_pos = idx_key;
269 }
270 }
271 }
272 if (match_pos == 0 && map.size_last_begin > (size_t) (n + m + 1)) {

Callers 1

draftMethod · 0.85

Calls 7

common_ngram_map_hashFunction · 0.85
maxFunction · 0.85
minFunction · 0.85
common_tokens_to_strFunction · 0.85
sizeMethod · 0.45
push_backMethod · 0.45
emptyMethod · 0.45

Tested by 1

draftMethod · 0.68