Skip to content

Commit 6c55fe1

Browse files
committed
Code cleanup
1 parent a91c122 commit 6c55fe1

File tree

2 files changed

+15
-25
lines changed

2 files changed

+15
-25
lines changed

llama.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ struct llama_vocab {
248248

249249
llama_trie special_token_trie;
250250
std::unordered_map<token, id> special_token_to_id;
251-
std::vector<id> special_tokens;
252251
size_t max_special_token_length;
253252
};
254253

@@ -539,14 +538,13 @@ struct llama_file_loader {
539538

540539
for (uint32_t i = 0; i < hparams.n_vocab_sp; i++) {
541540
uint32_t token_id = file.read_u32();
542-
const auto & token = vocab.id_to_token[token_id].tok;
541+
const auto & word = vocab.id_to_token[token_id].tok;
543542

544-
vocab.special_token_trie.add(token);
545-
vocab.special_tokens.push_back(token_id);
546-
vocab.special_token_to_id[token] = token_id;
543+
vocab.special_token_trie.add(word);
544+
vocab.special_token_to_id[word] = token_id;
547545

548-
if (vocab.max_special_token_length < token.size()) {
549-
vocab.max_special_token_length = token.size();
546+
if (vocab.max_special_token_length < word.size()) {
547+
vocab.max_special_token_length = word.size();
550548
}
551549
}
552550
}
@@ -641,9 +639,8 @@ struct llama_file_saver {
641639
file.write_raw(token_score.tok.data(), token_score.tok.size());
642640
file.write_raw(&token_score.score, sizeof(token_score.score));
643641
}
644-
uint32_t n_vocab_sp = any_file_loader->hparams.n_vocab_sp;
645-
for (uint32_t i = 0; i < n_vocab; i++) {
646-
file.write_u32(any_file_loader->vocab.special_tokens[i]);
642+
for (const auto & pair : any_file_loader->vocab.special_token_to_id) {
643+
file.write_u32(pair.second);
647644
}
648645
}
649646
void write_tensor(llama_load_tensor & tensor, enum ggml_type new_type, const void * new_data, size_t new_size) {
@@ -1964,24 +1961,23 @@ static std::vector<llama_vocab::id> llama_tokenize(const llama_vocab & vocab, co
19641961
return output;
19651962
}
19661963

1967-
auto offsets = vocab.special_token_trie.split(text);
1964+
std::vector<int> offsets = vocab.special_token_trie.split(text);
19681965
int start = 0;
19691966
for (int end : offsets) {
19701967
if (start >= end) {
19711968
continue;
19721969
}
19731970

1974-
size_t part_length = end - start;
1975-
//printf("\"%.*s\"\n", (int) part_length, text.c_str() + start);
1976-
1977-
if (vocab.max_special_token_length < part_length) {
1978-
tokenizer.tokenize(text.c_str() + start, part_length, output);
1979-
} else {
1980-
auto token_it = vocab.special_token_to_id.find(std::string(text.c_str() + start, part_length));
1971+
const char *part = text.c_str() + start;
1972+
size_t part_len = end - start;
1973+
if (vocab.max_special_token_length < part_len) {
1974+
tokenizer.tokenize(part, part_len, output);
1975+
} else {
1976+
auto token_it = vocab.special_token_to_id.find(std::string(part, part_len));
19811977
if (token_it != vocab.special_token_to_id.end()) {
19821978
output.push_back(token_it->second);
19831979
} else {
1984-
tokenizer.tokenize(text.c_str() + start, part_length, output);
1980+
tokenizer.tokenize(part, part_len, output);
19851981
}
19861982
}
19871983
start = end;
@@ -3515,10 +3511,6 @@ llama_token llama_token_nl() {
35153511
return 13;
35163512
}
35173513

3518-
bool llama_is_special_token(const struct llama_context *ctx, llama_token token) {
3519-
return std::find(ctx->vocab.special_tokens.begin(), ctx->vocab.special_tokens.end(), token) != ctx->vocab.special_tokens.end();
3520-
}
3521-
35223514

35233515
void llama_print_timings(struct llama_context * ctx) {
35243516
const int64_t t_end_us = ggml_time_us();

llama.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,6 @@ extern "C" {
248248
LLAMA_API llama_token llama_token_eos(); // end-of-sentence
249249
LLAMA_API llama_token llama_token_nl(); // next-line
250250

251-
LLAMA_API bool llama_is_special_token(const struct llama_context * ctx, llama_token token);
252-
253251
// Sampling functions
254252

255253
/// @details Repetition penalty described in CTRL academic paper https://arxiv.org/abs/1909.05858, with negative logit fix.

0 commit comments

Comments
 (0)