Skip to content

Commit c6a2c9e

Browse files
authored
gguf : use ggml log system (#13571)
* gguf : use ggml log system * llama : remove unnecessary new lines in exception messages
1 parent 07ad2b6 commit c6a2c9e

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

ggml/src/gguf.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,10 @@ bool gguf_read_emplace_helper(const struct gguf_reader & gr, std::vector<struct
299299
return false;
300300
}
301301
} catch (std::length_error &) {
302-
fprintf(stderr, "%s: encountered length_error while reading value for key '%s'\n", __func__, key.c_str());
302+
GGML_LOG_ERROR("%s: encountered length_error while reading value for key '%s'\n", __func__, key.c_str());
303303
return false;
304304
} catch (std::bad_alloc &) {
305-
fprintf(stderr, "%s: encountered bad_alloc error while reading value for key '%s'\n", __func__, key.c_str());
305+
GGML_LOG_ERROR("%s: encountered bad_alloc error while reading value for key '%s'\n", __func__, key.c_str());
306306
return false;
307307
}
308308
kv.emplace_back(key, value);
@@ -328,14 +328,14 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
328328
ok = ok && gr.read(magic, 4);
329329

330330
if (!ok) {
331-
fprintf(stderr, "%s: failed to read magic\n", __func__);
331+
GGML_LOG_ERROR("%s: failed to read magic\n", __func__);
332332
gguf_free(ctx);
333333
return nullptr;
334334
}
335335

336336
for (uint32_t i = 0; i < magic.size(); i++) {
337337
if (magic[i] != GGUF_MAGIC[i]) {
338-
fprintf(stderr, "%s: invalid magic characters: '%c%c%c%c', expected 'GGUF'\n", __func__, magic[0], magic[1], magic[2], magic[3]);
338+
GGML_LOG_ERROR("%s: invalid magic characters: '%c%c%c%c', expected 'GGUF'\n", __func__, magic[0], magic[1], magic[2], magic[3]);
339339
gguf_free(ctx);
340340
return nullptr;
341341
}
@@ -348,11 +348,11 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
348348

349349
if (ok && gr.read(ctx->version)) {
350350
if (ctx->version == 1) {
351-
fprintf(stderr, "%s: GGUFv1 is no longer supported, please use a more up-to-date version\n", __func__);
351+
GGML_LOG_ERROR("%s: GGUFv1 is no longer supported, please use a more up-to-date version\n", __func__);
352352
ok = false;
353353
}
354354
if (ctx->version > GGUF_VERSION) {
355-
fprintf(stderr, "%s: this GGUF file is version %" PRIu32 " but this software only supports up to version %d\n",
355+
GGML_LOG_ERROR("%s: this GGUF file is version %" PRIu32 " but this software only supports up to version %d\n",
356356
__func__, ctx->version, GGUF_VERSION);
357357
ok = false;
358358
}
@@ -363,7 +363,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
363363
if (ok && gr.read(n_tensors)) {
364364
static_assert(sizeof(size_t) <= 8 && sizeof(gguf_tensor_info) >= 2, "int64_t insufficient for indexing");
365365
if (n_tensors < 0 || n_tensors > int64_t(SIZE_MAX/sizeof(gguf_tensor_info))) {
366-
fprintf(stderr, "%s: number of tensors is %" PRIi64 " but must be in [0, %zu]\n",
366+
GGML_LOG_ERROR("%s: number of tensors is %" PRIi64 " but must be in [0, %zu]\n",
367367
__func__, n_tensors, SIZE_MAX/sizeof(gguf_tensor_info));
368368
ok = false;
369369
}
@@ -374,7 +374,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
374374
if (ok && gr.read(n_kv)) {
375375
static_assert(sizeof(size_t) <= 8 && sizeof(gguf_tensor_info) >= 2, "int64_t insufficient for indexing");
376376
if (n_kv < 0 || n_kv > int64_t(SIZE_MAX/sizeof(gguf_kv))) {
377-
fprintf(stderr, "%s: number of key value pairs is %" PRIi64 " but must be in [0, %zu]\n",
377+
GGML_LOG_ERROR("%s: number of key value pairs is %" PRIi64 " but must be in [0, %zu]\n",
378378
__func__, n_kv, SIZE_MAX/sizeof(gguf_kv));
379379
ok = false;
380380
}
@@ -383,7 +383,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
383383
}
384384

385385
if (!ok) {
386-
fprintf(stderr, "%s: failed to read header\n", __func__);
386+
GGML_LOG_ERROR("%s: failed to read header\n", __func__);
387387
gguf_free(ctx);
388388
return nullptr;
389389
}
@@ -399,15 +399,15 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
399399
try {
400400
ok = ok && gr.read(key);
401401
} catch (std::length_error &) {
402-
fprintf(stderr, "%s: encountered length_error while reading key %" PRIi64 "\n", __func__, i);
402+
GGML_LOG_ERROR("%s: encountered length_error while reading key %" PRIi64 "\n", __func__, i);
403403
ok = false;
404404
} catch (std::bad_alloc &) {
405-
fprintf(stderr, "%s: encountered bad_alloc error while reading key %" PRIi64 "\n", __func__, i);
405+
GGML_LOG_ERROR("%s: encountered bad_alloc error while reading key %" PRIi64 "\n", __func__, i);
406406
ok = false;
407407
}
408408
for (size_t j = 0; ok && j < ctx->kv.size(); ++j) {
409409
if (key == ctx->kv[j].key) {
410-
fprintf(stderr, "%s: duplicate key '%s' for tensors %zu and %" PRIi64 " \n", __func__, key.c_str(), j, i);
410+
GGML_LOG_ERROR("%s: duplicate key '%s' for tensors %zu and %" PRIi64 " \n", __func__, key.c_str(), j, i);
411411
ok = false;
412412
}
413413
}
@@ -441,14 +441,14 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
441441
case GGUF_TYPE_ARRAY:
442442
default:
443443
{
444-
fprintf(stderr, "%s: key '%s' has invalid GGUF type %d\n", __func__, key.c_str(), type);
444+
GGML_LOG_ERROR("%s: key '%s' has invalid GGUF type %d\n", __func__, key.c_str(), type);
445445
ok = false;
446446
} break;
447447
}
448448
}
449449

450450
if (!ok) {
451-
fprintf(stderr, "%s: failed to read key-value pairs\n", __func__);
451+
GGML_LOG_ERROR("%s: failed to read key-value pairs\n", __func__);
452452
gguf_free(ctx);
453453
return nullptr;
454454
}
@@ -458,7 +458,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
458458
ctx->alignment = alignment_idx == -1 ? GGUF_DEFAULT_ALIGNMENT : gguf_get_val_u32(ctx, alignment_idx);
459459

460460
if (ctx->alignment == 0 || (ctx->alignment & (ctx->alignment - 1)) != 0) {
461-
fprintf(stderr, "%s: alignment %zu is not a power of 2\n", __func__, ctx->alignment);
461+
GGML_LOG_ERROR("%s: alignment %zu is not a power of 2\n", __func__, ctx->alignment);
462462
gguf_free(ctx);
463463
return nullptr;
464464
}
@@ -474,14 +474,14 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
474474
try {
475475
ok = ok && gr.read(name);
476476
} catch (std::length_error &) {
477-
fprintf(stderr, "%s: encountered length_error while reading tensor name %" PRIi64 "\n", __func__, i);
477+
GGML_LOG_ERROR("%s: encountered length_error while reading tensor name %" PRIi64 "\n", __func__, i);
478478
ok = false;
479479
} catch (std::bad_alloc &) {
480-
fprintf(stderr, "%s: encountered bad_alloc error while reading tensor name %" PRIi64 "\n", __func__, i);
480+
GGML_LOG_ERROR("%s: encountered bad_alloc error while reading tensor name %" PRIi64 "\n", __func__, i);
481481
ok = false;
482482
}
483483
if (name.length() >= GGML_MAX_NAME) {
484-
fprintf(stderr, "%s: tensor name %" PRIi64 " is too long: %zu >= %d\n", __func__, i, name.length(), GGML_MAX_NAME);
484+
GGML_LOG_ERROR("%s: tensor name %" PRIi64 " is too long: %zu >= %d\n", __func__, i, name.length(), GGML_MAX_NAME);
485485
ok = false;
486486
break;
487487
}
@@ -490,7 +490,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
490490
// make sure there are no duplicate tensor names
491491
for (int64_t j = 0; ok && j < i; ++j) {
492492
if (strcmp(info.t.name, ctx->info[j].t.name) == 0) {
493-
fprintf(stderr, "%s: duplicate tensor name '%s' for tensors %" PRIi64 " and %" PRIi64 "\n", __func__, info.t.name, j, i);
493+
GGML_LOG_ERROR("%s: duplicate tensor name '%s' for tensors %" PRIi64 " and %" PRIi64 "\n", __func__, info.t.name, j, i);
494494
ok = false;
495495
break;
496496
}
@@ -505,7 +505,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
505505
uint32_t n_dims = -1;
506506
ok = ok && gr.read(n_dims);
507507
if (n_dims > GGML_MAX_DIMS) {
508-
fprintf(stderr, "%s: tensor '%s' has invalid number of dimensions: %" PRIu32 " > %" PRIu32 "\n",
508+
GGML_LOG_ERROR("%s: tensor '%s' has invalid number of dimensions: %" PRIu32 " > %" PRIu32 "\n",
509509
__func__, info.t.name, n_dims, GGML_MAX_DIMS);
510510
ok = false;
511511
break;
@@ -518,7 +518,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
518518

519519
// check that all ne are non-negative
520520
if (info.t.ne[j] < 0) {
521-
fprintf(stderr, "%s: tensor '%s' dimension %" PRIu32 " has invalid number of elements: %" PRIi64 " < 0\n",
521+
GGML_LOG_ERROR("%s: tensor '%s' dimension %" PRIu32 " has invalid number of elements: %" PRIi64 " < 0\n",
522522
__func__, info.t.name, j, info.t.ne[j]);
523523
ok = false;
524524
break;
@@ -530,7 +530,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
530530
(INT64_MAX/info.t.ne[2] <= info.t.ne[0]*info.t.ne[1]) ||
531531
(INT64_MAX/info.t.ne[3] <= info.t.ne[0]*info.t.ne[1]*info.t.ne[2]))) {
532532

533-
fprintf(stderr, "%s: total number of elements in tensor '%s' with shape "
533+
GGML_LOG_ERROR("%s: total number of elements in tensor '%s' with shape "
534534
"(%" PRIi64 ", %" PRIi64 ", %" PRIi64 ", %" PRIi64 ") is >= %" PRIi64 "\n",
535535
__func__, info.t.name, info.t.ne[0], info.t.ne[1], info.t.ne[2], info.t.ne[3], INT64_MAX);
536536
ok = false;
@@ -547,7 +547,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
547547

548548
// check that tensor type is within defined range
549549
if (info.t.type < 0 || info.t.type >= GGML_TYPE_COUNT) {
550-
fprintf(stderr, "%s: tensor '%s' has invalid ggml type %d (%s)\n",
550+
GGML_LOG_ERROR("%s: tensor '%s' has invalid ggml type %d (%s)\n",
551551
__func__, info.t.name, info.t.type, ggml_type_name(info.t.type));
552552
ok = false;
553553
break;
@@ -557,7 +557,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
557557

558558
// check that row size is divisible by block size
559559
if (blck_size == 0 || info.t.ne[0] % blck_size != 0) {
560-
fprintf(stderr, "%s: tensor '%s' of type %d (%s) has %" PRId64 " elements per row, "
560+
GGML_LOG_ERROR("%s: tensor '%s' of type %d (%s) has %" PRId64 " elements per row, "
561561
"not a multiple of block size (%" PRId64 ")\n",
562562
__func__, info.t.name, (int) info.t.type, ggml_type_name(info.t.type), info.t.ne[0], blck_size);
563563
ok = false;
@@ -582,15 +582,15 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
582582
}
583583

584584
if (!ok) {
585-
fprintf(stderr, "%s: failed to read tensor info\n", __func__);
585+
GGML_LOG_ERROR("%s: failed to read tensor info\n", __func__);
586586
gguf_free(ctx);
587587
return nullptr;
588588
}
589589
GGML_ASSERT(int64_t(ctx->info.size()) == n_tensors);
590590

591591
// we require the data section to be aligned, so take into account any padding
592592
if (fseek(file, GGML_PAD(ftell(file), ctx->alignment), SEEK_SET) != 0) {
593-
fprintf(stderr, "%s: failed to seek to beginning of data section\n", __func__);
593+
GGML_LOG_ERROR("%s: failed to seek to beginning of data section\n", __func__);
594594
gguf_free(ctx);
595595
return nullptr;
596596
}
@@ -604,9 +604,9 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
604604
for (size_t i = 0; i < ctx->info.size(); ++i) {
605605
const gguf_tensor_info & ti = ctx->info[i];
606606
if (ti.offset != ctx->size) {
607-
fprintf(stderr, "%s: tensor '%s' has offset %" PRIu64 ", expected %zu\n",
607+
GGML_LOG_ERROR("%s: tensor '%s' has offset %" PRIu64 ", expected %zu\n",
608608
__func__, ti.t.name, ti.offset, ctx->size);
609-
fprintf(stderr, "%s: failed to read tensor data\n", __func__);
609+
GGML_LOG_ERROR("%s: failed to read tensor data\n", __func__);
610610
gguf_free(ctx);
611611
return nullptr;
612612
}
@@ -634,7 +634,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
634634

635635
*params.ctx = ggml_init(pdata);
636636
if (*params.ctx == nullptr) {
637-
fprintf(stderr, "%s: failed to initialize ggml context for storing tensors\n", __func__);
637+
GGML_LOG_ERROR("%s: failed to initialize ggml context for storing tensors\n", __func__);
638638
gguf_free(ctx);
639639
return nullptr;
640640
}
@@ -656,7 +656,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
656656
ok = ok && gr.read(data->data, ctx->size);
657657

658658
if (!ok) {
659-
fprintf(stderr, "%s: failed to read tensor data binary blob\n", __func__);
659+
GGML_LOG_ERROR("%s: failed to read tensor data binary blob\n", __func__);
660660
ggml_free(ctx_data);
661661
*params.ctx = nullptr;
662662
gguf_free(ctx);
@@ -689,7 +689,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
689689
}
690690

691691
if (!ok) {
692-
fprintf(stderr, "%s: failed to create tensors\n", __func__);
692+
GGML_LOG_ERROR("%s: failed to create tensors\n", __func__);
693693
ggml_free(ctx_data);
694694
*params.ctx = nullptr;
695695
gguf_free(ctx);
@@ -706,7 +706,7 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
706706
FILE * file = ggml_fopen(fname, "rb");
707707

708708
if (!file) {
709-
fprintf(stderr, "%s: failed to open GGUF file '%s'\n", __func__, fname);
709+
GGML_LOG_ERROR("%s: failed to open GGUF file '%s'\n", __func__, fname);
710710
return nullptr;
711711
}
712712

@@ -1305,7 +1305,7 @@ bool gguf_write_to_file(const struct gguf_context * ctx, const char * fname, boo
13051305
FILE * file = ggml_fopen(fname, "wb");
13061306

13071307
if (!file) {
1308-
fprintf(stderr, "%s: failed to open file '%s' for writing GGUF data\n", __func__, fname);
1308+
GGML_LOG_ERROR("%s: failed to open file '%s' for writing GGUF data\n", __func__, fname);
13091309
return false;
13101310
}
13111311

src/llama-model-loader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ llama_model_loader::llama_model_loader(
469469

470470
meta.reset(gguf_init_from_file(fname.c_str(), params));
471471
if (!meta) {
472-
throw std::runtime_error(format("%s: failed to load model from %s\n", __func__, fname.c_str()));
472+
throw std::runtime_error(format("%s: failed to load model from %s", __func__, fname.c_str()));
473473
}
474474

475475
get_key(llm_kv(LLM_KV_GENERAL_ARCHITECTURE), arch_name, false);
@@ -528,7 +528,7 @@ llama_model_loader::llama_model_loader(
528528
};
529529
gguf_context_ptr ctx_gguf { gguf_init_from_file(fname_split, split_params) };
530530
if (!ctx_gguf) {
531-
throw std::runtime_error(format("%s: failed to load GGUF split from %s\n", __func__, fname_split));
531+
throw std::runtime_error(format("%s: failed to load GGUF split from %s", __func__, fname_split));
532532
}
533533

534534
// check idx

0 commit comments

Comments
 (0)