Skip to content

Commit ecf90b1

Browse files
authored
gguf : make token scores and types optional (#3347)
1 parent 2619109 commit ecf90b1

File tree

3 files changed

+8
-22
lines changed

3 files changed

+8
-22
lines changed

convert-falcon-hf-to-gguf.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ def parse_args() -> argparse.Namespace:
133133
print("gguf: get tokenizer metadata")
134134

135135
tokens: list[bytearray] = []
136-
scores: list[float] = []
137-
toktypes: list[int] = []
138136

139137
tokenizer_json_file = dir_model / 'tokenizer.json'
140138
if not tokenizer_json_file.is_file():
@@ -177,12 +175,8 @@ def parse_args() -> argparse.Namespace:
177175
text = bytearray(pad_token)
178176

179177
tokens.append(text)
180-
scores.append(0.0) # dymmy
181-
toktypes.append(gguf.TokenType.NORMAL) # dummy
182178

183179
gguf_writer.add_token_list(tokens)
184-
gguf_writer.add_token_scores(scores)
185-
gguf_writer.add_token_types(toktypes)
186180

187181
special_vocab = gguf.SpecialVocab(dir_model, load_merges = True)
188182
special_vocab.add_to_gguf(gguf_writer)

convert-starcoder-hf-to-gguf.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ def parse_args() -> argparse.Namespace:
117117
print("gguf: get tokenizer metadata")
118118

119119
tokens: list[bytearray] = []
120-
scores: list[float] = []
121-
toktypes: list[int] = []
122120

123121
tokenizer_json_file = dir_model / 'tokenizer.json'
124122
if not tokenizer_json_file.is_file():
@@ -161,12 +159,8 @@ def parse_args() -> argparse.Namespace:
161159
text = bytearray(pad_token)
162160

163161
tokens.append(text)
164-
scores.append(0.0) # dymmy
165-
toktypes.append(gguf.TokenType.NORMAL) # dummy
166162

167163
gguf_writer.add_token_list(tokens)
168-
gguf_writer.add_token_scores(scores)
169-
gguf_writer.add_token_types(toktypes)
170164

171165
special_vocab = gguf.SpecialVocab(dir_model, load_merges = True)
172166
special_vocab.add_to_gguf(gguf_writer)

llama.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,20 +1931,18 @@ static void llm_load_vocab(
19311931
throw std::runtime_error("cannot find tokenizer vocab in model file\n");
19321932
}
19331933

1934+
const float * scores = nullptr;
19341935
const int score_idx = gguf_find_key(ctx, kv(LLM_KV_TOKENIZER_SCORES).c_str());
1935-
if (score_idx == -1) {
1936-
throw std::runtime_error("cannot find tokenizer scores in model file\n");
1936+
if (score_idx != -1) {
1937+
scores = (const float * ) gguf_get_arr_data(ctx, score_idx);
19371938
}
19381939

1939-
const float * scores = (const float * ) gguf_get_arr_data(ctx, score_idx);
1940-
1940+
const int * toktypes = nullptr;
19411941
const int toktype_idx = gguf_find_key(ctx, kv(LLM_KV_TOKENIZER_TOKEN_TYPE).c_str());
1942-
if (toktype_idx == -1) {
1943-
throw std::runtime_error("cannot find token type list in GGUF file\n");
1942+
if (toktype_idx != -1) {
1943+
toktypes = (const int * ) gguf_get_arr_data(ctx, toktype_idx);
19441944
}
19451945

1946-
const int * toktypes = (const int * ) gguf_get_arr_data(ctx, toktype_idx);
1947-
19481946
// determine vocab type
19491947
{
19501948
std::string tokenizer_name;
@@ -2012,8 +2010,8 @@ static void llm_load_vocab(
20122010

20132011
auto & token_data = vocab.id_to_token[i];
20142012
token_data.text = std::move(word);
2015-
token_data.score = scores[i];
2016-
token_data.type = (llama_token_type) toktypes[i];
2013+
token_data.score = scores ? scores[i] : 0.0f;
2014+
token_data.type = toktypes ? (llama_token_type) toktypes[i] : LLAMA_TOKEN_TYPE_NORMAL;
20172015
}
20182016

20192017
// determine the newline token: LLaMA "<0x0A>" == 10 == '\n', Falcon 193 == '\n'

0 commit comments

Comments
 (0)