Skip to content

Commit

Permalink
Fix n^2 loop in tokenization (ggerganov#254)
Browse files Browse the repository at this point in the history
This causes long prompts to parse very slowly.
  • Loading branch information
glinscott authored Mar 18, 2023
1 parent b2de7f1 commit a81d0c2
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ std::vector<gpt_vocab::id> llama_tokenize(const gpt_vocab & vocab, const std::st
// Forward pass
for (int i = 0; i < len; i++) {
int max_len = std::min(len - i, MAX_TOKEN_LEN);
for (int sub_len = 1; sub_len <= len - i; sub_len++) {
for (int sub_len = 1; sub_len <= max_len; sub_len++) {
auto sub = text.substr(i, sub_len);
auto token = vocab.token_to_id.find(sub);
if (token != vocab.token_to_id.end()) {
Expand Down

0 comments on commit a81d0c2

Please sign in to comment.