remove duplicated ctx/model functions

ggml-ci
This commit is contained in:
slaren 2023-09-28 19:35:57 +02:00
parent 65b83f37bd
commit 5659391b6a
18 changed files with 60 additions and 101 deletions

View file

@ -849,10 +849,10 @@ std::vector<llama_token> llama_tokenize(
// upper limit for the number of tokens // upper limit for the number of tokens
int n_tokens = text.length() + add_bos; int n_tokens = text.length() + add_bos;
std::vector<llama_token> result(n_tokens); std::vector<llama_token> result(n_tokens);
n_tokens = llama_tokenize_with_model(model, text.data(), text.length(), result.data(), result.size(), add_bos); n_tokens = llama_tokenize(model, text.data(), text.length(), result.data(), result.size(), add_bos);
if (n_tokens < 0) { if (n_tokens < 0) {
result.resize(-n_tokens); result.resize(-n_tokens);
int check = llama_tokenize_with_model(model, text.data(), text.length(), result.data(), result.size(), add_bos); int check = llama_tokenize(model, text.data(), text.length(), result.data(), result.size(), add_bos);
GGML_ASSERT(check == -n_tokens); GGML_ASSERT(check == -n_tokens);
} else { } else {
result.resize(n_tokens); result.resize(n_tokens);
@ -862,10 +862,10 @@ std::vector<llama_token> llama_tokenize(
std::string llama_token_to_piece(const struct llama_context * ctx, llama_token token) { std::string llama_token_to_piece(const struct llama_context * ctx, llama_token token) {
std::vector<char> result(8, 0); std::vector<char> result(8, 0);
const int n_tokens = llama_token_to_piece(ctx, token, result.data(), result.size()); const int n_tokens = llama_token_to_piece(llama_get_model(ctx), token, result.data(), result.size());
if (n_tokens < 0) { if (n_tokens < 0) {
result.resize(-n_tokens); result.resize(-n_tokens);
int check = llama_token_to_piece(ctx, token, result.data(), result.size()); int check = llama_token_to_piece(llama_get_model(ctx), token, result.data(), result.size());
GGML_ASSERT(check == -n_tokens); GGML_ASSERT(check == -n_tokens);
} else { } else {
result.resize(n_tokens); result.resize(n_tokens);
@ -920,7 +920,7 @@ llama_token llama_sample_token(
std::vector<llama_token_data> & candidates, std::vector<llama_token_data> & candidates,
int idx) { int idx) {
const int n_ctx = llama_n_ctx(ctx); const int n_ctx = llama_n_ctx(ctx);
const int n_vocab = llama_n_vocab(ctx); const int n_vocab = llama_n_vocab(llama_get_model(ctx));
const float temp = params.temp; const float temp = params.temp;
const int32_t top_k = params.top_k <= 0 ? n_vocab : params.top_k; const int32_t top_k = params.top_k <= 0 ? n_vocab : params.top_k;
@ -1206,7 +1206,7 @@ void dump_non_result_info_yaml(FILE * stream, const gpt_params & params, const l
#endif // NDEBUG #endif // NDEBUG
fprintf(stream, "model_desc: %s\n", model_desc); fprintf(stream, "model_desc: %s\n", model_desc);
fprintf(stream, "n_vocab: %d # output size of the final layer, 32001 for some models\n", llama_n_vocab(lctx)); fprintf(stream, "n_vocab: %d # output size of the final layer, 32001 for some models\n", llama_n_vocab(llama_get_model(lctx)));
#ifdef __OPTIMIZE__ #ifdef __OPTIMIZE__
fprintf(stream, "optimize: true\n"); fprintf(stream, "optimize: true\n");

View file

@ -155,7 +155,7 @@ int main(int argc, char ** argv) {
continue; continue;
} }
auto n_vocab = llama_n_vocab(ctx); auto n_vocab = llama_n_vocab(model);
auto * logits = llama_get_logits_ith(ctx, i_batch[i]); auto * logits = llama_get_logits_ith(ctx, i_batch[i]);
std::vector<llama_token_data> candidates; std::vector<llama_token_data> candidates;

View file

@ -70,7 +70,7 @@ bool eval_float(void * model, float * input, int N){
MyModel * mymodel = (MyModel*)model; MyModel * mymodel = (MyModel*)model;
llama_context * ctx = mymodel->ctx; llama_context * ctx = mymodel->ctx;
gpt_params params = mymodel->params; gpt_params params = mymodel->params;
int n_emb = llama_n_embd(ctx); int n_emb = llama_n_embd(llama_get_model(ctx));
int n_past = mymodel->n_past; int n_past = mymodel->n_past;
int n_batch = N; // params.n_batch; int n_batch = N; // params.n_batch;
@ -132,7 +132,7 @@ llama_token sampling_id(struct MyModel* mymodel) {
// out of user input, sample next token // out of user input, sample next token
const float temp = params.temp; const float temp = params.temp;
const int32_t top_k = params.top_k <= 0 ? llama_n_vocab(ctx) : params.top_k; const int32_t top_k = params.top_k <= 0 ? llama_n_vocab(llama_get_model(ctx)) : params.top_k;
const float top_p = params.top_p; const float top_p = params.top_p;
const float tfs_z = params.tfs_z; const float tfs_z = params.tfs_z;
const float typical_p = params.typical_p; const float typical_p = params.typical_p;
@ -148,7 +148,7 @@ llama_token sampling_id(struct MyModel* mymodel) {
llama_token id = 0; llama_token id = 0;
{ {
auto logits = llama_get_logits(ctx); auto logits = llama_get_logits(ctx);
auto n_vocab = llama_n_vocab(ctx); auto n_vocab = llama_n_vocab(llama_get_model(ctx));
// Apply params.logit_bias map // Apply params.logit_bias map
for (auto it = params.logit_bias.begin(); it != params.logit_bias.end(); it++) { for (auto it = params.logit_bias.begin(); it != params.logit_bias.end(); it++) {

View file

@ -8,7 +8,7 @@ int main(int argc, char** argv) {
auto mymodel = create_mymodel(argc, argv); auto mymodel = create_mymodel(argc, argv);
int N = 10; int N = 10;
int max_tgt_len = 500; int max_tgt_len = 500;
int n_embd = llama_n_embd(mymodel->ctx); int n_embd = llama_n_embd(llama_get_model(mymodel->ctx));
// add random float embd to test evaluation // add random float embd to test evaluation
float * data = new float[N*n_embd]; float * data = new float[N*n_embd];

View file

@ -42,7 +42,7 @@ int main(int argc, char ** argv) {
return 1; return 1;
} }
const int n_ctx_train = llama_n_ctx_train(ctx); const int n_ctx_train = llama_n_ctx_train(model);
const int n_ctx = llama_n_ctx(ctx); const int n_ctx = llama_n_ctx(ctx);
if (n_ctx > n_ctx_train) { if (n_ctx > n_ctx_train) {
@ -87,8 +87,8 @@ int main(int argc, char ** argv) {
embd_inp.erase(embd_inp.begin(), embd_inp.begin() + n_tokens); embd_inp.erase(embd_inp.begin(), embd_inp.begin() + n_tokens);
} }
const int n_embd = llama_n_embd(ctx); const int n_embd = llama_n_embd(model);
const auto embeddings = llama_get_embeddings(ctx); const auto * embeddings = llama_get_embeddings(ctx);
for (int i = 0; i < n_embd; i++) { for (int i = 0; i < n_embd; i++) {
printf("%f ", embeddings[i]); printf("%f ", embeddings[i]);

View file

@ -189,7 +189,7 @@ int main(int argc, char ** argv) {
return 1; return 1;
} }
const int n_ctx_train = llama_n_ctx_train(ctx); const int n_ctx_train = llama_n_ctx_train(model);
const int n_ctx = llama_n_ctx(ctx); const int n_ctx = llama_n_ctx(ctx);
LOG("n_ctx: %d\n", n_ctx); LOG("n_ctx: %d\n", n_ctx);
@ -230,7 +230,7 @@ int main(int argc, char ** argv) {
} }
} }
const bool add_bos = llama_vocab_type(ctx) == LLAMA_VOCAB_TYPE_SPM; const bool add_bos = llama_vocab_type(model) == LLAMA_VOCAB_TYPE_SPM;
LOG("add_bos: %d\n", add_bos); LOG("add_bos: %d\n", add_bos);
std::vector<llama_token> embd_inp; std::vector<llama_token> embd_inp;
@ -467,7 +467,7 @@ int main(int argc, char ** argv) {
std::vector<llama_token> embd; std::vector<llama_token> embd;
std::vector<llama_token> embd_guidance; std::vector<llama_token> embd_guidance;
const int n_vocab = llama_n_vocab(ctx); const int n_vocab = llama_n_vocab(model);
std::vector<llama_token_data> candidates; std::vector<llama_token_data> candidates;
candidates.reserve(n_vocab); candidates.reserve(n_vocab);

View file

@ -108,7 +108,7 @@ int main(int argc, char ** argv) {
fflush(stderr); fflush(stderr);
const int n_ctx = llama_n_ctx(ctx); const int n_ctx = llama_n_ctx(ctx);
const int n_vocab = llama_n_vocab(ctx); const int n_vocab = llama_n_vocab(model);
std::vector<client> clients(n_clients); std::vector<client> clients(n_clients);
for (size_t i = 0; i < clients.size(); ++i) { for (size_t i = 0; i < clients.size(); ++i) {

View file

@ -150,7 +150,7 @@ static results_perplexity perplexity_v2(llama_context * ctx, const gpt_params &
// Output: `perplexity: 13.5106 [114/114]` // Output: `perplexity: 13.5106 [114/114]`
// BOS tokens will be added for each chunk before eval // BOS tokens will be added for each chunk before eval
const bool is_spm = llama_vocab_type(ctx) == LLAMA_VOCAB_TYPE_SPM; const bool is_spm = llama_vocab_type(llama_get_model(ctx)) == LLAMA_VOCAB_TYPE_SPM;
const bool add_bos = is_spm; const bool add_bos = is_spm;
fprintf(stderr, "%s: tokenizing the input ..\n", __func__); fprintf(stderr, "%s: tokenizing the input ..\n", __func__);
@ -190,7 +190,7 @@ static results_perplexity perplexity_v2(llama_context * ctx, const gpt_params &
const int n_chunk_max = (tokens.size() - calc_chunk + params.ppl_stride - 1) / params.ppl_stride; const int n_chunk_max = (tokens.size() - calc_chunk + params.ppl_stride - 1) / params.ppl_stride;
const int n_chunk = params.n_chunks < 0 ? n_chunk_max : std::min(params.n_chunks, n_chunk_max); const int n_chunk = params.n_chunks < 0 ? n_chunk_max : std::min(params.n_chunks, n_chunk_max);
const int n_vocab = llama_n_vocab(ctx); const int n_vocab = llama_n_vocab(llama_get_model(ctx));
const int n_batch = params.n_batch; const int n_batch = params.n_batch;
int count = 0; int count = 0;
@ -289,7 +289,7 @@ static results_perplexity perplexity(llama_context * ctx, const gpt_params & par
// Output: `perplexity: 13.5106 [114/114]` // Output: `perplexity: 13.5106 [114/114]`
// BOS tokens will be added for each chunk before eval // BOS tokens will be added for each chunk before eval
const bool is_spm = llama_vocab_type(ctx) == LLAMA_VOCAB_TYPE_SPM; const bool is_spm = llama_vocab_type(llama_get_model(ctx)) == LLAMA_VOCAB_TYPE_SPM;
const bool add_bos = is_spm; const bool add_bos = is_spm;
const int n_ctx = llama_n_ctx(ctx); const int n_ctx = llama_n_ctx(ctx);
@ -317,7 +317,7 @@ static results_perplexity perplexity(llama_context * ctx, const gpt_params & par
const int n_chunk_max = tokens.size() / n_ctx; const int n_chunk_max = tokens.size() / n_ctx;
const int n_chunk = params.n_chunks < 0 ? n_chunk_max : std::min(params.n_chunks, n_chunk_max); const int n_chunk = params.n_chunks < 0 ? n_chunk_max : std::min(params.n_chunks, n_chunk_max);
const int n_vocab = llama_n_vocab(ctx); const int n_vocab = llama_n_vocab(llama_get_model(ctx));
const int n_batch = params.n_batch; const int n_batch = params.n_batch;
int count = 0; int count = 0;
@ -478,7 +478,7 @@ static void hellaswag_score(llama_context * ctx, const gpt_params & params) {
size_t hs_task_count = prompt_lines.size()/6; size_t hs_task_count = prompt_lines.size()/6;
fprintf(stderr, "%s : loaded %zu tasks from prompt.\n", __func__, hs_task_count); fprintf(stderr, "%s : loaded %zu tasks from prompt.\n", __func__, hs_task_count);
const bool is_spm = llama_vocab_type(ctx) == LLAMA_VOCAB_TYPE_SPM; const bool is_spm = llama_vocab_type(llama_get_model(ctx)) == LLAMA_VOCAB_TYPE_SPM;
fprintf(stderr, "================================= is_spm = %d\n", is_spm); fprintf(stderr, "================================= is_spm = %d\n", is_spm);
// This is needed as usual for LLaMA models // This is needed as usual for LLaMA models
@ -533,7 +533,7 @@ static void hellaswag_score(llama_context * ctx, const gpt_params & params) {
printf("\ntask\tacc_norm\n"); printf("\ntask\tacc_norm\n");
double acc = 0.0f; double acc = 0.0f;
const int n_vocab = llama_n_vocab(ctx); const int n_vocab = llama_n_vocab(llama_get_model(ctx));
const int n_ctx = llama_n_ctx(ctx); const int n_ctx = llama_n_ctx(ctx);
std::vector<std::vector<int>> ending_tokens(4); std::vector<std::vector<int>> ending_tokens(4);
@ -720,7 +720,7 @@ int main(int argc, char ** argv) {
return 1; return 1;
} }
const int n_ctx_train = llama_n_ctx_train(ctx); const int n_ctx_train = llama_n_ctx_train(model);
if (params.n_ctx > n_ctx_train) { if (params.n_ctx > n_ctx_train) {
fprintf(stderr, "%s: warning: model was trained on only %d context tokens (%d specified)\n", fprintf(stderr, "%s: warning: model was trained on only %d context tokens (%d specified)\n",
__func__, n_ctx_train, params.n_ctx); __func__, n_ctx_train, params.n_ctx);

View file

@ -73,7 +73,7 @@ int main(int argc, char ** argv) {
for (auto i = 0; i < params.n_predict; i++) { for (auto i = 0; i < params.n_predict; i++) {
auto * logits = llama_get_logits(ctx); auto * logits = llama_get_logits(ctx);
auto n_vocab = llama_n_vocab(ctx); auto n_vocab = llama_n_vocab(model);
std::vector<llama_token_data> candidates; std::vector<llama_token_data> candidates;
candidates.reserve(n_vocab); candidates.reserve(n_vocab);
for (llama_token token_id = 0; token_id < n_vocab; token_id++) { for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
@ -133,7 +133,7 @@ int main(int argc, char ** argv) {
// second run // second run
for (auto i = 0; i < params.n_predict; i++) { for (auto i = 0; i < params.n_predict; i++) {
auto * logits = llama_get_logits(ctx2); auto * logits = llama_get_logits(ctx2);
auto n_vocab = llama_n_vocab(ctx2); auto n_vocab = llama_n_vocab(model);
std::vector<llama_token_data> candidates; std::vector<llama_token_data> candidates;
candidates.reserve(n_vocab); candidates.reserve(n_vocab);
for (llama_token token_id = 0; token_id < n_vocab; token_id++) { for (llama_token token_id = 0; token_id < n_vocab; token_id++) {

View file

@ -470,7 +470,7 @@ struct llama_server_context
// out of user input, sample next token // out of user input, sample next token
const float temp = params.temp; const float temp = params.temp;
const int32_t top_k = params.top_k <= 0 ? llama_n_vocab(ctx) : params.top_k; const int32_t top_k = params.top_k <= 0 ? llama_n_vocab(model) : params.top_k;
const float top_p = params.top_p; const float top_p = params.top_p;
const float tfs_z = params.tfs_z; const float tfs_z = params.tfs_z;
const float typical_p = params.typical_p; const float typical_p = params.typical_p;
@ -486,7 +486,7 @@ struct llama_server_context
{ {
auto *logits = llama_get_logits(ctx); auto *logits = llama_get_logits(ctx);
auto n_vocab = llama_n_vocab(ctx); auto n_vocab = llama_n_vocab(model);
// Apply params.logit_bias map // Apply params.logit_bias map
for (const auto &it : params.logit_bias) for (const auto &it : params.logit_bias)
@ -690,7 +690,7 @@ struct llama_server_context
std::vector<float> getEmbedding() std::vector<float> getEmbedding()
{ {
static const int n_embd = llama_n_embd(ctx); static const int n_embd = llama_n_embd(model);
if (!params.embedding) if (!params.embedding)
{ {
LOG_WARNING("embedding disabled", { LOG_WARNING("embedding disabled", {
@ -1166,7 +1166,7 @@ static void parse_options_completion(const json &body, llama_server_context &lla
const auto &logit_bias = body.find("logit_bias"); const auto &logit_bias = body.find("logit_bias");
if (logit_bias != body.end() && logit_bias->is_array()) if (logit_bias != body.end() && logit_bias->is_array())
{ {
const int n_vocab = llama_n_vocab(llama.ctx); const int n_vocab = llama_n_vocab(llama.model);
for (const auto &el : *logit_bias) for (const auto &el : *logit_bias)
{ {
if (el.is_array() && el.size() == 2 && el[0].is_number_integer()) if (el.is_array() && el.size() == 2 && el[0].is_number_integer())

View file

@ -122,7 +122,7 @@ int main(int argc, char ** argv) {
while (n_cur <= n_len) { while (n_cur <= n_len) {
// sample the next token // sample the next token
{ {
auto n_vocab = llama_n_vocab(ctx); auto n_vocab = llama_n_vocab(model);
auto * logits = llama_get_logits_ith(ctx, batch.n_tokens - 1); auto * logits = llama_get_logits_ith(ctx, batch.n_tokens - 1);
std::vector<llama_token_data> candidates; std::vector<llama_token_data> candidates;

View file

@ -78,8 +78,8 @@ int main(int argc, char ** argv) {
// the 2 models should have the same vocab // the 2 models should have the same vocab
const int n_ctx = llama_n_ctx(ctx_tgt); const int n_ctx = llama_n_ctx(ctx_tgt);
const int n_vocab = llama_n_vocab(ctx_tgt); const int n_vocab = llama_n_vocab(model_tgt);
//GGML_ASSERT(n_vocab == llama_n_vocab(ctx_dft)); //GGML_ASSERT(n_vocab == llama_n_vocab(model_dft));
// how many tokens to draft each time // how many tokens to draft each time
int n_draft = params.n_draft; int n_draft = params.n_draft;

View file

@ -975,10 +975,10 @@ int tokenize_file(struct llama_context * lctx, const char * filename, std::vecto
buf[size] = '\0'; buf[size] = '\0';
int n_tokens = llama_tokenize(lctx, buf.data(), buf.size(), out.data(), out.size(), false); int n_tokens = llama_tokenize(llama_get_model(lctx), buf.data(), buf.size(), out.data(), out.size(), false);
if (n_tokens < 0) { if (n_tokens < 0) {
out.resize(-n_tokens); out.resize(-n_tokens);
n_tokens = llama_tokenize(lctx, buf.data(), buf.size(), out.data(), out.size(), false); n_tokens = llama_tokenize(llama_get_model(lctx), buf.data(), buf.size(), out.data(), out.size(), false);
} }
GGML_ASSERT(n_tokens >= 0); GGML_ASSERT(n_tokens >= 0);
out.resize(n_tokens); out.resize(n_tokens);
@ -2027,7 +2027,7 @@ int main(int argc, char ** argv) {
printf("%s: number of training tokens: %d\n", __func__, (int) train_tokens.size()); printf("%s: number of training tokens: %d\n", __func__, (int) train_tokens.size());
struct my_llama_model model; struct my_llama_model model;
model.hparams.n_vocab = llama_n_vocab(lctx); model.hparams.n_vocab = llama_n_vocab(lmodel);
model.hparams.n_ctx = params.n_ctx; model.hparams.n_ctx = params.n_ctx;
model.hparams.n_embd = params.n_embd; model.hparams.n_embd = params.n_embd;
model.hparams.n_head = params.n_head; model.hparams.n_head = params.n_head;

View file

@ -887,10 +887,10 @@ static void llama_nop(struct ggml_tensor * tensor) { // don't offload by default
static std::string llama_token_to_str(const struct llama_context * ctx, llama_token token) { static std::string llama_token_to_str(const struct llama_context * ctx, llama_token token) {
std::vector<char> result(8, 0); std::vector<char> result(8, 0);
const int n_tokens = llama_token_to_piece(ctx, token, result.data(), result.size()); const int n_tokens = llama_token_to_piece(llama_get_model(ctx), token, result.data(), result.size());
if (n_tokens < 0) { if (n_tokens < 0) {
result.resize(-n_tokens); result.resize(-n_tokens);
int check = llama_token_to_piece(ctx, token, result.data(), result.size()); int check = llama_token_to_piece(llama_get_model(ctx), token, result.data(), result.size());
GGML_ASSERT(check == -n_tokens); GGML_ASSERT(check == -n_tokens);
} else { } else {
result.resize(n_tokens); result.resize(n_tokens);
@ -5386,7 +5386,7 @@ void llama_sample_classifier_free_guidance(
GGML_ASSERT(ctx); GGML_ASSERT(ctx);
auto n_vocab = llama_n_vocab(ctx); auto n_vocab = llama_n_vocab(llama_get_model(ctx));
GGML_ASSERT(n_vocab == (int)candidates->size); GGML_ASSERT(n_vocab == (int)candidates->size);
GGML_ASSERT(!candidates->sorted); GGML_ASSERT(!candidates->sorted);
@ -5415,7 +5415,7 @@ void llama_sample_classifier_free_guidance(
llama_token llama_sample_token_mirostat(struct llama_context * ctx, llama_token_data_array * candidates, float tau, float eta, int m, float * mu) { llama_token llama_sample_token_mirostat(struct llama_context * ctx, llama_token_data_array * candidates, float tau, float eta, int m, float * mu) {
GGML_ASSERT(ctx); GGML_ASSERT(ctx);
auto N = float(llama_n_vocab(ctx)); auto N = float(llama_n_vocab(llama_get_model(ctx)));
int64_t t_start_sample_us; int64_t t_start_sample_us;
t_start_sample_us = ggml_time_us(); t_start_sample_us = ggml_time_us();
@ -5602,7 +5602,7 @@ struct llama_logit_info {
}; };
llama_logit_info(llama_context * ctx) llama_logit_info(llama_context * ctx)
: logits(llama_get_logits(ctx)) : logits(llama_get_logits(ctx))
, n_vocab(llama_n_vocab(ctx)) , n_vocab(llama_n_vocab(llama_get_model(ctx)))
, max_l(*std::max_element(logits, logits + n_vocab)) , max_l(*std::max_element(logits, logits + n_vocab))
, normalizer(1.0f / std::accumulate(logits, logits + n_vocab, 0.0f, sum_exp{max_l})) , normalizer(1.0f / std::accumulate(logits, logits + n_vocab, 0.0f, sum_exp{max_l}))
{ } { }
@ -6835,35 +6835,23 @@ const llama_model * llama_get_model(const struct llama_context * ctx) {
return &ctx->model; return &ctx->model;
} }
int llama_n_vocab(const struct llama_context * ctx) {
return llama_model_n_vocab(&ctx->model);
}
int llama_n_ctx(const struct llama_context * ctx) { int llama_n_ctx(const struct llama_context * ctx) {
return ctx->cparams.n_ctx; return ctx->cparams.n_ctx;
} }
int llama_n_ctx_train(const struct llama_context * ctx) { enum llama_vocab_type llama_vocab_type(const struct llama_model * model) {
return llama_model_n_ctx_train(&ctx->model); return model->vocab.type;
} }
int llama_n_embd(const struct llama_context * ctx) { int llama_n_vocab(const struct llama_model * model) {
return llama_model_n_embd(&ctx->model);
}
enum llama_vocab_type llama_vocab_type(const struct llama_context * ctx) {
return ctx->model.vocab.type;
}
int llama_model_n_vocab(const struct llama_model * model) {
return model->vocab.id_to_token.size(); return model->vocab.id_to_token.size();
} }
int llama_model_n_ctx_train(const struct llama_model * model) { int llama_n_ctx_train(const struct llama_model * model) {
return model->hparams.n_ctx_train; return model->hparams.n_ctx_train;
} }
int llama_model_n_embd(const struct llama_model * model) { int llama_n_embd(const struct llama_model * model) {
return model->hparams.n_embd; return model->hparams.n_embd;
} }
@ -7464,16 +7452,6 @@ llama_token llama_token_nl(const struct llama_context * ctx) {
} }
int llama_tokenize( int llama_tokenize(
struct llama_context * ctx,
const char * text,
int text_len,
llama_token * tokens,
int n_max_tokens,
bool add_bos) {
return llama_tokenize_with_model(&ctx->model, text, text_len, tokens, n_max_tokens, add_bos);
}
int llama_tokenize_with_model(
const struct llama_model * model, const struct llama_model * model,
const char * text, const char * text,
int text_len, int text_len,
@ -7494,13 +7472,9 @@ int llama_tokenize_with_model(
return res.size(); return res.size();
} }
int llama_token_to_piece(const struct llama_context * ctx, llama_token token, char * buf, int length) {
return llama_token_to_piece_with_model(&ctx->model, token, buf, length);
}
// does not write null-terminator to buf // does not write null-terminator to buf
int llama_token_to_piece_with_model(const struct llama_model * model, llama_token token, char * buf, int length) { int llama_token_to_piece(const struct llama_model * model, llama_token token, char * buf, int length) {
if (0 <= token && token < llama_model_n_vocab(model)) { if (0 <= token && token < llama_n_vocab(model)) {
if (llama_is_normal_token(model->vocab, token)) { if (llama_is_normal_token(model->vocab, token)) {
std::string result = model->vocab.id_to_token[token].text; std::string result = model->vocab.id_to_token[token].text;
if (llama_vocab_get_type(model->vocab) == LLAMA_VOCAB_TYPE_SPM) { if (llama_vocab_get_type(model->vocab) == LLAMA_VOCAB_TYPE_SPM) {

29
llama.h
View file

@ -274,16 +274,13 @@ extern "C" {
LLAMA_API const struct llama_model * llama_get_model(const struct llama_context * ctx); LLAMA_API const struct llama_model * llama_get_model(const struct llama_context * ctx);
LLAMA_API int llama_n_vocab (const struct llama_context * ctx);
LLAMA_API int llama_n_ctx (const struct llama_context * ctx); LLAMA_API int llama_n_ctx (const struct llama_context * ctx);
LLAMA_API int llama_n_ctx_train(const struct llama_context * ctx);
LLAMA_API int llama_n_embd (const struct llama_context * ctx);
LLAMA_API enum llama_vocab_type llama_vocab_type(const struct llama_context * ctx); LLAMA_API enum llama_vocab_type llama_vocab_type(const struct llama_model * model);
LLAMA_API int llama_model_n_vocab (const struct llama_model * model); LLAMA_API int llama_n_vocab (const struct llama_model * model);
LLAMA_API int llama_model_n_ctx_train(const struct llama_model * model); LLAMA_API int llama_n_ctx_train(const struct llama_model * model);
LLAMA_API int llama_model_n_embd (const struct llama_model * model); LLAMA_API int llama_n_embd (const struct llama_model * model);
// Get a string describing the model type // Get a string describing the model type
LLAMA_API int llama_model_desc(const struct llama_model * model, char * buf, size_t buf_size); LLAMA_API int llama_model_desc(const struct llama_model * model, char * buf, size_t buf_size);
@ -454,7 +451,9 @@ extern "C" {
struct llama_context * ctx, struct llama_context * ctx,
struct llama_batch batch); struct llama_batch batch);
// Set the number of threads // Set the number of threads used for decoding
// n_threads is the number of threads used for generation (single token)
// n_threads_batch is the number of threads used for prompt and batch processing (multiple tokens)
LLAMA_API void llama_set_n_threads(struct llama_context * ctx, uint32_t n_threads, uint32_t n_threads_batch); LLAMA_API void llama_set_n_threads(struct llama_context * ctx, uint32_t n_threads, uint32_t n_threads_batch);
// Token logits obtained from the last call to llama_eval() // Token logits obtained from the last call to llama_eval()
@ -496,14 +495,6 @@ extern "C" {
// Returns the number of tokens on success, no more than n_max_tokens // Returns the number of tokens on success, no more than n_max_tokens
// Returns a negative number on failure - the number of tokens that would have been returned // Returns a negative number on failure - the number of tokens that would have been returned
LLAMA_API int llama_tokenize( LLAMA_API int llama_tokenize(
struct llama_context * ctx,
const char * text,
int text_len,
llama_token * tokens,
int n_max_tokens,
bool add_bos);
LLAMA_API int llama_tokenize_with_model(
const struct llama_model * model, const struct llama_model * model,
const char * text, const char * text,
int text_len, int text_len,
@ -516,12 +507,6 @@ extern "C" {
// Does not write null terminator to the buffer. // Does not write null terminator to the buffer.
// User code is responsible to remove the leading whitespace of the first non-BOS token when decoding multiple tokens. // User code is responsible to remove the leading whitespace of the first non-BOS token when decoding multiple tokens.
LLAMA_API int llama_token_to_piece( LLAMA_API int llama_token_to_piece(
const struct llama_context * ctx,
llama_token token,
char * buf,
int length);
LLAMA_API int llama_token_to_piece_with_model(
const struct llama_model * model, const struct llama_model * model,
llama_token token, llama_token token,
char * buf, char * buf,

View file

@ -84,7 +84,7 @@ int main(int argc, char **argv) {
} }
} }
if (llama_vocab_type(ctx) != LLAMA_VOCAB_TYPE_BPE) { if (llama_vocab_type(model) != LLAMA_VOCAB_TYPE_BPE) {
fprintf(stderr, "%s : error: vocab type is not SPM\n", __func__); fprintf(stderr, "%s : error: vocab type is not SPM\n", __func__);
llama_free_model(model); llama_free_model(model);
llama_free(ctx); llama_free(ctx);

View file

@ -86,7 +86,7 @@ int main(int argc, char **argv) {
} }
} }
if (llama_vocab_type(ctx) != LLAMA_VOCAB_TYPE_SPM) { if (llama_vocab_type(model) != LLAMA_VOCAB_TYPE_SPM) {
fprintf(stderr, "%s : error: vocab type is not SPM\n", __func__); fprintf(stderr, "%s : error: vocab type is not SPM\n", __func__);
llama_free_model(model); llama_free_model(model);
llama_free(ctx); llama_free(ctx);

View file

@ -74,7 +74,7 @@ int main(int argc, char **argv) {
} }
} }
GGML_ASSERT(llama_vocab_type(ctx) == LLAMA_VOCAB_TYPE_SPM); GGML_ASSERT(llama_vocab_type(model) == LLAMA_VOCAB_TYPE_SPM);
#ifdef _WIN32 #ifdef _WIN32
// We need this for unicode console support // We need this for unicode console support
@ -82,7 +82,7 @@ int main(int argc, char **argv) {
atexit([]() { console::cleanup(); }); atexit([]() { console::cleanup(); });
#endif #endif
const int n_vocab = llama_n_vocab(ctx); const int n_vocab = llama_n_vocab(model);
for (int i = 0; i < n_vocab; ++i) { for (int i = 0; i < n_vocab; ++i) {
std::string str = llama_detokenize_spm(ctx, std::vector<int>(1, i)); std::string str = llama_detokenize_spm(ctx, std::vector<int>(1, i));