char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

KnightYao[Model] Support for Spark2_5ForCausalLM implementation (#27868)3ad1ba733

char/aliceai-t5-moe
7.2 KiB205 linesraw
1#pragma once
2
3#include "llama.h"
4
5#include <string>
6#include <vector>
7#include <memory>
8
9// pre-tokenization types
10enum llama_vocab_pre_type {
11    LLAMA_VOCAB_PRE_TYPE_DEFAULT           = 0,
12    LLAMA_VOCAB_PRE_TYPE_LLAMA3            = 1,
13    LLAMA_VOCAB_PRE_TYPE_DEEPSEEK_LLM      = 2,
14    LLAMA_VOCAB_PRE_TYPE_DEEPSEEK_CODER    = 3,
15    LLAMA_VOCAB_PRE_TYPE_FALCON            = 4,
16    LLAMA_VOCAB_PRE_TYPE_MPT               = 5,
17    LLAMA_VOCAB_PRE_TYPE_STARCODER         = 6,
18    LLAMA_VOCAB_PRE_TYPE_GPT2              = 7,
19    LLAMA_VOCAB_PRE_TYPE_REFACT            = 8,
20    LLAMA_VOCAB_PRE_TYPE_COMMAND_R         = 9,
21    LLAMA_VOCAB_PRE_TYPE_STABLELM2         = 10,
22    LLAMA_VOCAB_PRE_TYPE_QWEN2             = 11,
23    LLAMA_VOCAB_PRE_TYPE_OLMO              = 12,
24    LLAMA_VOCAB_PRE_TYPE_DBRX              = 13,
25    LLAMA_VOCAB_PRE_TYPE_SMAUG             = 14,
26    LLAMA_VOCAB_PRE_TYPE_PORO              = 15,
27    LLAMA_VOCAB_PRE_TYPE_CHATGLM3          = 16,
28    LLAMA_VOCAB_PRE_TYPE_CHATGLM4          = 17,
29    LLAMA_VOCAB_PRE_TYPE_VIKING            = 18,
30    LLAMA_VOCAB_PRE_TYPE_JAIS              = 19,
31    LLAMA_VOCAB_PRE_TYPE_TEKKEN            = 20,
32    LLAMA_VOCAB_PRE_TYPE_SMOLLM            = 21,
33    LLAMA_VOCAB_PRE_TYPE_CODESHELL         = 22,
34    LLAMA_VOCAB_PRE_TYPE_BLOOM             = 23,
35    LLAMA_VOCAB_PRE_TYPE_GPT3_FINNISH      = 24,
36    LLAMA_VOCAB_PRE_TYPE_EXAONE            = 25,
37    LLAMA_VOCAB_PRE_TYPE_CHAMELEON         = 26,
38    LLAMA_VOCAB_PRE_TYPE_MINERVA           = 27,
39    LLAMA_VOCAB_PRE_TYPE_DEEPSEEK3_LLM     = 28,
40    LLAMA_VOCAB_PRE_TYPE_GPT4O             = 29,
41    LLAMA_VOCAB_PRE_TYPE_SUPERBPE          = 30,
42    LLAMA_VOCAB_PRE_TYPE_TRILLION          = 31,
43    LLAMA_VOCAB_PRE_TYPE_BAILINGMOE        = 32,
44    LLAMA_VOCAB_PRE_TYPE_LLAMA4            = 33,
45    LLAMA_VOCAB_PRE_TYPE_PIXTRAL           = 34,
46    LLAMA_VOCAB_PRE_TYPE_SEED_CODER        = 35,
47    LLAMA_VOCAB_PRE_TYPE_HUNYUAN           = 36,
48    LLAMA_VOCAB_PRE_TYPE_KIMI_K2           = 37,
49    LLAMA_VOCAB_PRE_TYPE_HUNYUAN_DENSE     = 38,
50    LLAMA_VOCAB_PRE_TYPE_GROK_2            = 39,
51    LLAMA_VOCAB_PRE_TYPE_GRANITE_DOCLING   = 40,
52    LLAMA_VOCAB_PRE_TYPE_MINIMAX_M2        = 41,
53    LLAMA_VOCAB_PRE_TYPE_AFMOE             = 42,
54    LLAMA_VOCAB_PRE_TYPE_SOLAR_OPEN        = 43,
55    LLAMA_VOCAB_PRE_TYPE_YOUTU             = 44,
56    LLAMA_VOCAB_PRE_TYPE_EXAONE_MOE        = 45,
57    LLAMA_VOCAB_PRE_TYPE_QWEN35            = 46,
58    LLAMA_VOCAB_PRE_TYPE_TINY_AYA          = 47,
59    LLAMA_VOCAB_PRE_TYPE_JOYAI_LLM         = 48,
60    LLAMA_VOCAB_PRE_TYPE_JAIS2             = 49,
61    LLAMA_VOCAB_PRE_TYPE_GEMMA4            = 50,
62    LLAMA_VOCAB_PRE_TYPE_SARVAM_MOE        = 51,
63    LLAMA_VOCAB_PRE_TYPE_MINICPM5          = 52,
64    LLAMA_VOCAB_PRE_TYPE_WHITESPACE        = 53,
65    LLAMA_VOCAB_PRE_TYPE_GRANITE_EMB_MULTI = 54,
66    LLAMA_VOCAB_PRE_TYPE_MELLUM2           = 55,
67    LLAMA_VOCAB_PRE_TYPE_LAGUNA            = 56,
68    LLAMA_VOCAB_PRE_TYPE_HY_V4             = 57,
69    LLAMA_VOCAB_PRE_TYPE_SPARK2_5          = 58,
70};
71
72struct LLM_KV;
73struct llama_model_loader;
74
75struct llama_vocab {
76    struct token_data {
77        std::string      text;
78        float            score;
79        llama_token_attr attr;
80    };
81
82    struct normalizer_options {
83        bool lowercase     = true;
84        bool strip_accents = true;
85        // TODO: clean_text, handle_chinese_chars
86    };
87
88    llama_vocab();
89    ~llama_vocab();
90
91    void load(llama_model_loader & ml, const LLM_KV & kv);
92
93    std::string get_tokenizer_model() const;
94    std::string get_tokenizer_pre() const;
95
96    enum llama_vocab_type     get_type()     const;
97    enum llama_vocab_pre_type get_pre_type() const;
98
99    uint32_t n_tokens() const;
100    uint32_t n_token_types() const;
101
102    std::string type_name() const;
103
104    bool is_normal      (llama_token id) const;
105    bool is_unknown     (llama_token id) const;
106    bool is_control     (llama_token id) const;
107    bool is_byte        (llama_token id) const;
108    bool is_user_defined(llama_token id) const;
109    bool is_unused      (llama_token id) const;
110    bool is_eog         (llama_token id) const;
111
112    uint8_t     token_to_byte(llama_token id) const;
113    llama_token byte_to_token(uint8_t ch)     const;
114
115    llama_token text_to_token(const std::string & text) const;
116
117    const token_data & get_token_data(llama_token id) const;
118
119    const char *     token_get_text (llama_token id) const;
120    float            token_get_score(llama_token id) const;
121    llama_token_attr token_get_attr (llama_token id) const;
122
123    llama_token token_bos() const;
124    llama_token token_eos() const;
125    llama_token token_eot() const;
126    llama_token token_eom() const;
127    llama_token token_unk() const;
128    llama_token token_sep() const;
129    llama_token token_nl () const;
130    llama_token token_pad() const;
131    llama_token token_mask() const;
132
133    llama_token token_prefix() const;
134    llama_token token_middle() const;
135    llama_token token_suffix() const;
136
137    llama_token token_fim_pre() const;
138    llama_token token_fim_suf() const;
139    llama_token token_fim_mid() const;
140    llama_token token_fim_pad() const;
141    llama_token token_fim_rep() const;
142    llama_token token_fim_sep() const;
143
144    bool get_add_space_prefix          () const;
145    bool get_add_bos                   () const;
146    bool get_add_eos                   () const;
147    bool get_add_sep                   () const;
148    bool get_ignore_merges             () const;
149    bool get_clean_spaces              () const;
150    bool get_remove_extra_whitespaces  () const;
151    bool get_escape_whitespaces        () const;
152    bool get_treat_whitespace_as_suffix() const;
153    const normalizer_options & get_normalizer_opts() const;
154
155    const std::vector<llama_token> & get_suppress_tokens() const;
156
157    int max_token_len() const;
158
159    int find_bpe_rank(const std::string & token_left, const std::string & token_right) const;
160    std::vector<std::string> get_bpe_merges() const;
161
162    std::vector<char> get_precompiled_charsmap() const;
163
164    int32_t tokenize(
165                   const char * text,
166                      int32_t   text_len,
167                  llama_token * tokens,
168                      int32_t   n_tokens_max,
169                         bool   add_special,
170                         bool   parse_special) const;
171
172    std::vector<llama_token> tokenize(
173            const std::string & raw_text,
174                         bool   add_special,
175                         bool   parse_special = false) const;
176
177    // does not write null-terminator to buf
178    int32_t token_to_piece(
179                  llama_token   token,
180                         char * buf,
181                      int32_t   length,
182                      int32_t   lstrip,
183                         bool   special) const;
184
185    // use cached data
186    const std::string & token_to_piece(llama_token token) const;
187
188    int32_t detokenize(
189            const llama_token * tokens,
190                      int32_t   n_tokens,
191                         char * text,
192                      int32_t   text_len_max,
193                         bool   remove_special,
194                         bool   unparse_special) const;
195
196    std::string detokenize(
197            const std::vector<llama_token> & tokens,
198                                      bool   special) const;
199
200    void print_info() const;
201
202private:
203    struct impl;
204    std::unique_ptr<impl> pimpl;
205};