Skip to content

Commit cec6a3b

Browse files
author
jaime-m-p
committed
Add per token attrib enum
1 parent 750f60c commit cec6a3b

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

llama.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,14 +2147,16 @@ struct llama_control_vector {
21472147
};
21482148

21492149
struct llama_vocab {
2150-
using id = int32_t;
2151-
using token = std::string;
2152-
using ttype = llama_token_type;
2150+
using id = int32_t;
2151+
using token = std::string;
2152+
using ttype = llama_token_type;
2153+
using tattrib = llama_token_attrib;
21532154

21542155
struct token_data {
2155-
token text;
2156-
float score;
2157-
ttype type;
2156+
token text;
2157+
float score;
2158+
ttype type;
2159+
tattrib attribs;
21582160
};
21592161

21602162
enum llama_vocab_type type = LLAMA_VOCAB_TYPE_SPM;
@@ -4865,6 +4867,24 @@ static void llm_load_vocab(
48654867

48664868
LLAMA_LOG_INFO("%s: token to piece cache size = %.4f MB\n", __func__, size_cache / 1024.0 / 1024.0);
48674869
}
4870+
4871+
// Handle per token attributes
4872+
//NOTE: Each model customizes per token attributes.
4873+
//NOTE: Per token attributes are missing from the GGUF file.
4874+
//TODO: Merge llama_token_type and llama_token_attrib.
4875+
{
4876+
// convert token type as an attribute
4877+
for (auto data : vocab.id_to_token) {
4878+
uint32_t attrib = LLAMA_TOKEN_ATTRIB_UNDEFINED;
4879+
attrib |= LLAMA_TOKEN_ATTRIB_UNKNOWN * (data.type == LLAMA_TOKEN_TYPE_UNKNOWN);
4880+
attrib |= LLAMA_TOKEN_ATTRIB_UNUSED * (data.type == LLAMA_TOKEN_TYPE_UNUSED);
4881+
attrib |= LLAMA_TOKEN_ATTRIB_NORMAL * (data.type == LLAMA_TOKEN_TYPE_NORMAL);
4882+
attrib |= LLAMA_TOKEN_ATTRIB_CONTROL * (data.type == LLAMA_TOKEN_TYPE_CONTROL);
4883+
attrib |= LLAMA_TOKEN_ATTRIB_USER_DEFINED * (data.type == LLAMA_TOKEN_TYPE_USER_DEFINED);
4884+
attrib |= LLAMA_TOKEN_ATTRIB_BYTE * (data.type == LLAMA_TOKEN_TYPE_BYTE);
4885+
data.attribs = (llama_token_attrib) attrib;
4886+
}
4887+
}
48684888
}
48694889

48704890
static void llm_load_print_meta(llama_model_loader & ml, llama_model & model) {

llama.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ extern "C" {
107107
LLAMA_TOKEN_TYPE_BYTE = 6,
108108
};
109109

110+
enum llama_token_attrib {
111+
LLAMA_TOKEN_ATTRIB_UNDEFINED = 0,
112+
LLAMA_TOKEN_ATTRIB_UNKNOWN = 1 << 1,
113+
LLAMA_TOKEN_ATTRIB_UNUSED = 1 << 2,
114+
LLAMA_TOKEN_ATTRIB_NORMAL = 1 << 3,
115+
LLAMA_TOKEN_ATTRIB_CONTROL = 1 << 4, // SPECIAL?
116+
LLAMA_TOKEN_ATTRIB_USER_DEFINED = 1 << 5,
117+
LLAMA_TOKEN_ATTRIB_BYTE = 1 << 6,
118+
LLAMA_TOKEN_ATTRIB_NORMALIZED = 1 << 7,
119+
LLAMA_TOKEN_ATTRIB_LSTRIP = 1 << 8,
120+
LLAMA_TOKEN_ATTRIB_RSTRIP = 1 << 9,
121+
LLAMA_TOKEN_ATTRIB_SINGLE_WORD = 1 << 10,
122+
};
123+
110124
// model file types
111125
enum llama_ftype {
112126
LLAMA_FTYPE_ALL_F32 = 0,

0 commit comments

Comments
 (0)