| 416 | } |
| 417 | |
| 418 | void server_tokens::keep_first(size_t n) { |
| 419 | GGML_ASSERT(n <= tokens.size()); |
| 420 | if (has_mtmd) { |
| 421 | if (n == tokens.size()) { |
| 422 | return; // nothing to do |
| 423 | } |
| 424 | // we throw an error if we try to remove a token in the middle of an image |
| 425 | // for ex. with input of 5 text tokens and 2 images: |
| 426 | // [0] [1] [2] [3] [4] [img0] [img0] [img0] [img1] [img1] |
| 427 | // n 1 2 3 4 5 6 7 8 9 10 |
| 428 | // allowed to resize ^ ^ |
| 429 | // disallowed to resize ^ ^ ^ |
| 430 | if (n > 0) { |
| 431 | // make sure we never remove tokens in the middle of an image |
| 432 | // note that the case where we keep a full image at the end is allowed: |
| 433 | // tokens[n - 1] == LLAMA_TOKEN_NULL && tokens[n] != LLAMA_TOKEN_NULL |
| 434 | if (tokens[n - 1] == LLAMA_TOKEN_NULL && tokens[n] == LLAMA_TOKEN_NULL) { |
| 435 | find_chunk(n - 1); // will throw an error if the token is not begin-of-chunk |
| 436 | } |
| 437 | } |
| 438 | // remove all image chunks that are not used anymore |
| 439 | for (auto it = map_idx_to_media.begin(); it != map_idx_to_media.end(); ) { |
| 440 | size_t idx = it->first; |
| 441 | if (idx >= n) { |
| 442 | it = map_idx_to_media.erase(it); |
| 443 | } else { |
| 444 | ++it; |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | tokens.resize(n); |
| 449 | } |
| 450 | |
| 451 | std::string server_tokens::detokenize(const llama_context * ctx, bool special) const { |
| 452 | llama_tokens text_tokens; |
no test coverage detected