Ported from Koboldcpp, original PR: https://github.com/LostRuins/koboldcpp/pull/982 (Original author: pi6am)
| 1834 | |
| 1835 | // Ported from Koboldcpp, original PR: https://github.com/LostRuins/koboldcpp/pull/982 (Original author: pi6am) |
| 1836 | static void get_overlapping_token_sequences(const llama_vocab & vocab, const std::string& str, std::unordered_multimap<llama_token, std::vector<llama_token>>& token_sequences, int max_tail_len = -1) { |
| 1837 | for (llama_token token_id = 0; token_id < (llama_token) vocab.n_tokens(); token_id++) { |
| 1838 | std::string word = vocab.detokenize({token_id}, true); |
| 1839 | if (word.find(str) != std::string::npos) { |
| 1840 | token_sequences.emplace(token_id, std::vector<llama_token>()); |
| 1841 | } else { |
| 1842 | size_t word_len = word.size(); |
| 1843 | size_t str_len = str.size(); |
| 1844 | size_t pos = -1; |
| 1845 | while ((pos = word.find(str[0], pos + 1)) != std::string::npos) { |
| 1846 | bool match = true; |
| 1847 | size_t i; |
| 1848 | for (i = 1; i < str_len && i + pos < word_len; ++i) { |
| 1849 | if (word[pos + i] != str[i]) { |
| 1850 | match = false; |
| 1851 | break; |
| 1852 | } |
| 1853 | } |
| 1854 | if (match) { |
| 1855 | std::vector<llama_token> tokenization = vocab.tokenize(str.substr(i), false, false); |
| 1856 | if (max_tail_len >= 0 && tokenization.size() > (size_t)max_tail_len) { |
| 1857 | tokenization.resize(max_tail_len); |
| 1858 | } |
| 1859 | |
| 1860 | // Ensure we don't already have a duplicate matching tokenization |
| 1861 | auto its = token_sequences.equal_range(token_id); |
| 1862 | bool found = false; |
| 1863 | for (auto it = its.first; it != its.second; ++it) { |
| 1864 | if (tokenization == it->second) { |
| 1865 | found = true; |
| 1866 | break; |
| 1867 | } |
| 1868 | } |
| 1869 | if (!found) { |
| 1870 | token_sequences.emplace(token_id, tokenization); |
| 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | } |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | static const char * llama_sampler_dry_name(const struct llama_sampler * /*smpl*/) { |
| 1879 | return "dry"; |
no test coverage detected