Skip to content

Commit ca1fc20

Browse files
committed
Fix issues revealed by CI
1 parent e468e75 commit ca1fc20

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

llama-util.h

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <vector>
1717
#include <map>
1818
#include <unordered_map>
19+
#include <memory>
1920
#include <stdexcept>
2021

2122
#ifdef __has_include
@@ -546,7 +547,7 @@ typedef llama_buffer llama_ctx_buffer;
546547
struct llama_trie_node {
547548
llama_trie_node(): is_terminator(false) {}
548549

549-
std::unordered_map<char, llama_trie_node*> children;
550+
std::unordered_map<char, std::unique_ptr<llama_trie_node>> children;
550551
bool is_terminator;
551552
};
552553

@@ -561,24 +562,24 @@ struct llama_trie {
561562
return;
562563
}
563564

564-
llama_trie_node *ref = root_;
565+
llama_trie_node *ref = root_.get();
565566
for (char c : word) {
566567
if (ref->children.find(c) == ref->children.end()) {
567-
ref->children[c] = new llama_trie_node();
568+
ref->children[c].reset(new llama_trie_node());
568569
}
569-
ref = ref->children[c];
570+
ref = ref->children[c].get();
570571
}
571572
ref->is_terminator = true;
572573
}
573574

574575
// Will look for the words added to the trie within `text`. Output is the boundaries of the words found.
575576
// Note that this trie will match the longest possible word first!
576-
std::vector<int> split(const std::string & text) const {
577-
std::map<int, llama_trie_node*> states;
578-
std::vector<int> offsets{0};
577+
std::vector<size_t> split(const std::string & text) const {
578+
std::map<size_t, llama_trie_node*> states;
579+
std::vector<size_t> offsets{0};
579580

580-
int skip = 0;
581-
for (int current = 0; current < text.size(); current++) {
581+
size_t skip = 0;
582+
for (size_t current = 0; current < text.size(); current++) {
582583
char current_char = text[current];
583584
if (skip > 0 && current < skip) {
584585
// Prevents the lookahead for matching twice
@@ -592,7 +593,7 @@ struct llama_trie {
592593

593594
// In this case, we already have partial matches (But unfinished)
594595
for (auto state = states.begin(); state != states.end(); ) {
595-
int start = state->first;
596+
size_t start = state->first;
596597
llama_trie_node *trie_pointer = state->second;
597598
if (trie_pointer->is_terminator) {
598599
// This is a final match, we need to reset and
@@ -603,11 +604,11 @@ struct llama_trie {
603604
// Here we are also actively looking for other earlier partial
604605
// matches
605606
// "[CLS]", "L", we need to match CLS even if L is special
606-
int end = 0;
607+
size_t end = 0;
607608
for (const auto & look : states) {
608-
int lookstart = look.first;
609+
size_t lookstart = look.first;
609610
llama_trie_node *looktrie_pointer = look.second;
610-
int lookahead_index = 0;
611+
size_t lookahead_index = 0;
611612
if (lookstart > start) {
612613
// This partial match is later, we can stop looking
613614
break;
@@ -633,7 +634,7 @@ struct llama_trie {
633634

634635
auto looktrie_pointer_it = looktrie_pointer->children.find(next_char);
635636
while (looktrie_pointer_it != looktrie_pointer->children.end()) {
636-
looktrie_pointer = looktrie_pointer_it->second;
637+
looktrie_pointer = looktrie_pointer_it->second.get();
637638
lookahead_index++;
638639
if (looktrie_pointer->is_terminator) {
639640
start = lookstart;
@@ -660,7 +661,7 @@ struct llama_trie {
660661
if (trie_pointer_it != trie_pointer->children.end()) {
661662
// The current character being looked at has a match within the trie
662663
// update the pointer (it will be stored back into states later).
663-
trie_pointer = trie_pointer_it->second;
664+
trie_pointer = trie_pointer_it->second.get();
664665
states[start] = trie_pointer;
665666
++state;
666667
} else {
@@ -679,18 +680,18 @@ struct llama_trie {
679680
// start keeping track of this partial match.
680681
auto children_it = root_->children.find(current_char);
681682
if (current >= skip && children_it != root_->children.end()) {
682-
states[current] = children_it->second;
683+
states[current] = children_it->second.get();
683684
}
684685
}
685686

686687
// We have a cut at the end with states.
687688
for (const auto & state : states) {
688-
int start = state.first;
689+
size_t start = state.first;
689690
llama_trie_node *trie_pointer = state.second;
690691
if (trie_pointer->is_terminator) {
691692
// This is a final match, we need to reset and
692693
// store the results in `offsets`.
693-
int end = text.size();
694+
size_t end = text.size();
694695
offsets.push_back(start);
695696
offsets.push_back(end);
696697
break;
@@ -702,7 +703,7 @@ struct llama_trie {
702703
}
703704

704705
private:
705-
llama_trie_node *root_;
706+
std::unique_ptr<llama_trie_node> root_;
706707
};
707708

708709
#endif

llama.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ struct llama_vocab {
281281

282282
llama_trie special_token_trie;
283283
std::unordered_map<token, id> special_token_to_id;
284-
size_t max_special_token_length;
284+
size_t max_special_token_length = 0;
285285
};
286286

287287
struct llama_model {
@@ -578,7 +578,7 @@ struct llama_file_loader {
578578
vocab.special_token_to_id.reserve(hparams.n_vocab_sp);
579579

580580
for (uint32_t i = 0; i < hparams.n_vocab_sp; i++) {
581-
uint32_t token_id = file.read_u32();
581+
llama_vocab::id token_id = file.read_u32();
582582
const auto & word = vocab.id_to_token[token_id].tok;
583583

584584
vocab.special_token_trie.add(word);
@@ -2108,9 +2108,9 @@ static std::vector<llama_vocab::id> llama_tokenize(const llama_vocab & vocab, co
21082108
return output;
21092109
}
21102110

2111-
std::vector<int> offsets = vocab.special_token_trie.split(text);
2112-
int start = 0;
2113-
for (int end : offsets) {
2111+
std::vector<size_t> offsets = vocab.special_token_trie.split(text);
2112+
size_t start = 0;
2113+
for (size_t end : offsets) {
21142114
if (start >= end) {
21152115
continue;
21162116
}

0 commit comments

Comments
 (0)