Skip to content

Commit a546ada

Browse files
committed
Revert "Improve llama_tokenize API"
This reverts commit 5a78a04.
1 parent 5a78a04 commit a546ada

File tree

3 files changed

+22
-44
lines changed

3 files changed

+22
-44
lines changed

llama.cpp

+12-28
Original file line numberDiff line numberDiff line change
@@ -1471,40 +1471,24 @@ int llama_eval(
14711471
return 0;
14721472
}
14731473

1474-
struct llama_token_list {
1475-
std::vector<llama_vocab::id> tokens;
1476-
llama_token_list(std::vector<llama_vocab::id> tokens) : tokens(tokens) {}
1477-
};
1478-
1479-
llama_token_list * llama_tokenize(
1480-
struct llama_context * ctx,
1481-
const char * text,
1482-
bool add_bos) {
1483-
return new llama_token_list(llama_tokenize(ctx->vocab, text, add_bos));
1484-
}
1485-
1486-
int llama_token_list_size(struct llama_token_list * token_list) {
1487-
return token_list->tokens.size();
1488-
}
1474+
int llama_tokenize(
1475+
struct llama_context * ctx,
1476+
const char * text,
1477+
llama_token * tokens,
1478+
int n_max_tokens,
1479+
bool add_bos) {
1480+
auto res = llama_tokenize(ctx->vocab, text, add_bos);
14891481

1490-
bool llama_token_list_copy(
1491-
struct llama_token_list * token_list,
1492-
llama_token * tokens,
1493-
int n_tokens) {
1494-
if (n_tokens != token_list->tokens.size()) {
1482+
if (n_max_tokens < (int) res.size()) {
14951483
fprintf(stderr, "%s: too many tokens\n", __func__);
1496-
return false;
1484+
return -((int) res.size());
14971485
}
14981486

1499-
for (size_t i = 0; i < token_list->tokens.size(); i++) {
1500-
tokens[i] = token_list->tokens[i];
1487+
for (size_t i = 0; i < res.size(); i++) {
1488+
tokens[i] = res[i];
15011489
}
15021490

1503-
return true;
1504-
}
1505-
1506-
int llama_token_list_free(struct llama_token_list * token_list) {
1507-
delete token_list;
1491+
return res.size();
15081492
}
15091493

15101494
int llama_n_vocab(struct llama_context * ctx) {

llama.h

+7-13
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ extern "C" {
6060
void *ctx;
6161
};
6262

63-
struct llama_token_list;
64-
6563
LLAMA_API struct llama_context_params llama_context_default_params();
6664

6765
// Various functions for loading a ggml llama model.
@@ -98,17 +96,13 @@ extern "C" {
9896
// The tokens pointer must be large enough to hold the resulting tokens.
9997
// Returns the number of tokens on success, no more than n_max_tokens
10098
// Returns a negative number on failure - the number of tokens that would have been returned
101-
LLAMA_API struct llama_token_list * llama_tokenize(
102-
struct llama_context * ctx,
103-
const char * text,
104-
bool add_bos);
105-
106-
LLAMA_API int llama_token_list_size(struct llama_token_list * token_list);
107-
LLAMA_API bool llama_token_list_copy(
108-
struct llama_token_list * token_list,
109-
llama_token * tokens,
110-
int n_tokens);
111-
LLAMA_API int llama_token_list_free(struct llama_token_list * token_list);
99+
// TODO: not sure if correct
100+
LLAMA_API int llama_tokenize(
101+
struct llama_context * ctx,
102+
const char * text,
103+
llama_token * tokens,
104+
int n_max_tokens,
105+
bool add_bos);
112106

113107
LLAMA_API int llama_n_vocab(struct llama_context * ctx);
114108
LLAMA_API int llama_n_ctx (struct llama_context * ctx);

utils.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ std::string gpt_random_prompt(std::mt19937 & rng) {
146146

147147
// TODO: not great allocating this every time
148148
std::vector<llama_token> llama_tokenize(struct llama_context * ctx, const std::string & text, bool add_bos) {
149-
llama_token_list * tokens = llama_tokenize(ctx, text.c_str(), add_bos);
150-
std::vector<llama_token> res(llama_token_list_size(tokens));
151-
llama_token_list_copy(tokens, res.data(), res.size());
149+
std::vector<llama_token> res(8096);
150+
int n = llama_tokenize(ctx, text.c_str(), res.data(), res.size(), add_bos);
151+
res.resize(n);
152152

153153
return res;
154154
}

0 commit comments

Comments
 (0)