Skip to content

Commit 01948dd

Browse files
committed
examples : make n_ctx warning work again
This was broken by commit e36ecdc ("build : on Mac OS enable Metal by default (ggml-org#2901)").
1 parent 178b185 commit 01948dd

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

examples/embedding/embedding.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ int main(int argc, char ** argv) {
1717

1818
params.embedding = true;
1919

20-
if (params.n_ctx > 2048) {
21-
fprintf(stderr, "%s: warning: model might not support context sizes greater than 2048 tokens (%d specified);"
22-
"expect poor results\n", __func__, params.n_ctx);
23-
}
24-
2520
fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
2621

2722
if (params.seed == LLAMA_DEFAULT_SEED) {
@@ -47,6 +42,12 @@ int main(int argc, char ** argv) {
4742
return 1;
4843
}
4944

45+
const int n_ctx_train = llama_n_ctx_train(ctx);
46+
if (params.n_ctx > n_ctx_train) {
47+
fprintf(stderr, "%s: warning: model was trained on only %d context tokens (%d specified)\n",
48+
__func__, n_ctx_train, params.n_ctx);
49+
}
50+
5051
// print system information
5152
{
5253
fprintf(stderr, "\n");

examples/main/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ int main(int argc, char ** argv) {
186186
return 1;
187187
}
188188

189-
if (params.n_ctx > llama_n_ctx(ctx)) {
190-
LOG_TEE("%s: warning: base model only supports context sizes no greater than %d tokens (%d specified)\n", __func__, llama_n_ctx(ctx), params.n_ctx);
189+
const int n_ctx_train = llama_n_ctx_train(ctx);
190+
if (params.n_ctx > n_ctx_train) {
191+
LOG_TEE("%s: warning: model was trained on only %d context tokens (%d specified)\n",
192+
__func__, n_ctx_train, params.n_ctx);
191193
} else if (params.n_ctx < 8) {
192194
LOG_TEE("%s: warning: minimum context size is 8, using minimum size.\n", __func__);
193195
params.n_ctx = 8;

examples/perplexity/perplexity.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,10 @@ int main(int argc, char ** argv) {
693693
return 1;
694694
}
695695

696-
if (params.n_ctx > llama_n_ctx(ctx)) {
697-
fprintf(stderr, "%s: warning: model might not support context sizes greater than %d tokens (%d specified);"
698-
"expect poor results\n", __func__, llama_n_ctx(ctx), params.n_ctx);
696+
const int n_ctx_train = llama_n_ctx_train(ctx);
697+
if (params.n_ctx > n_ctx_train) {
698+
fprintf(stderr, "%s: warning: model was trained on only %d context tokens (%d specified)\n",
699+
__func__, n_ctx_train, params.n_ctx);
699700
}
700701

701702
// print system information

llama.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5649,15 +5649,19 @@ void llama_free(struct llama_context * ctx) {
56495649
}
56505650

56515651
int llama_n_vocab(const struct llama_context * ctx) {
5652-
return ctx->model.vocab.id_to_token.size();
5652+
return llama_model_n_vocab(&ctx->model);
56535653
}
56545654

56555655
int llama_n_ctx(const struct llama_context * ctx) {
5656-
return ctx->model.hparams.n_ctx;
5656+
return llama_model_n_ctx(&ctx->model);
5657+
}
5658+
5659+
int llama_n_ctx_train(const struct llama_context * ctx) {
5660+
return llama_model_n_ctx_train(&ctx->model);
56575661
}
56585662

56595663
int llama_n_embd(const struct llama_context * ctx) {
5660-
return ctx->model.hparams.n_embd;
5664+
return llama_model_n_embd(&ctx->model);
56615665
}
56625666

56635667
enum llama_vocab_type llama_vocab_type(const struct llama_context * ctx) {
@@ -5672,6 +5676,10 @@ int llama_model_n_ctx(const struct llama_model * model) {
56725676
return model->hparams.n_ctx;
56735677
}
56745678

5679+
int llama_model_n_ctx_train(const struct llama_model * model) {
5680+
return model->hparams.n_ctx_train;
5681+
}
5682+
56755683
int llama_model_n_embd(const struct llama_model * model) {
56765684
return model->hparams.n_embd;
56775685
}

llama.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,17 @@ extern "C" {
245245
LLAMA_API bool llama_mmap_supported (void);
246246
LLAMA_API bool llama_mlock_supported(void);
247247

248-
LLAMA_API int llama_n_vocab(const struct llama_context * ctx);
249-
LLAMA_API int llama_n_ctx (const struct llama_context * ctx);
250-
LLAMA_API int llama_n_embd (const struct llama_context * ctx);
248+
LLAMA_API int llama_n_vocab (const struct llama_context * ctx);
249+
LLAMA_API int llama_n_ctx (const struct llama_context * ctx);
250+
LLAMA_API int llama_n_ctx_train(const struct llama_context * ctx);
251+
LLAMA_API int llama_n_embd (const struct llama_context * ctx);
251252

252253
LLAMA_API enum llama_vocab_type llama_vocab_type(const struct llama_context * ctx);
253254

254-
LLAMA_API int llama_model_n_vocab(const struct llama_model * model);
255-
LLAMA_API int llama_model_n_ctx (const struct llama_model * model);
256-
LLAMA_API int llama_model_n_embd (const struct llama_model * model);
255+
LLAMA_API int llama_model_n_vocab (const struct llama_model * model);
256+
LLAMA_API int llama_model_n_ctx (const struct llama_model * model);
257+
LLAMA_API int llama_model_n_ctx_train(const struct llama_model * model);
258+
LLAMA_API int llama_model_n_embd (const struct llama_model * model);
257259

258260
// Get a string describing the model type
259261
LLAMA_API int llama_model_desc(const struct llama_model * model, char * buf, size_t buf_size);

0 commit comments

Comments
 (0)