Diego Devesa commited on
Commit
a2211c9
·
1 Parent(s): c39b646

gguf : use ggml log system (llama/13571)

Browse files

* gguf : use ggml log system

* llama : remove unnecessary new lines in exception messages

Files changed (1) hide show
  1. ggml/src/gguf.cpp +33 -33
ggml/src/gguf.cpp CHANGED
@@ -299,10 +299,10 @@ bool gguf_read_emplace_helper(const struct gguf_reader & gr, std::vector<struct
299
  return false;
300
  }
301
  } catch (std::length_error &) {
302
- fprintf(stderr, "%s: encountered length_error while reading value for key '%s'\n", __func__, key.c_str());
303
  return false;
304
  } catch (std::bad_alloc &) {
305
- fprintf(stderr, "%s: encountered bad_alloc error while reading value for key '%s'\n", __func__, key.c_str());
306
  return false;
307
  }
308
  kv.emplace_back(key, value);
@@ -328,14 +328,14 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
328
  ok = ok && gr.read(magic, 4);
329
 
330
  if (!ok) {
331
- fprintf(stderr, "%s: failed to read magic\n", __func__);
332
  gguf_free(ctx);
333
  return nullptr;
334
  }
335
 
336
  for (uint32_t i = 0; i < magic.size(); i++) {
337
  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]);
339
  gguf_free(ctx);
340
  return nullptr;
341
  }
@@ -348,11 +348,11 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
348
 
349
  if (ok && gr.read(ctx->version)) {
350
  if (ctx->version == 1) {
351
- fprintf(stderr, "%s: GGUFv1 is no longer supported, please use a more up-to-date version\n", __func__);
352
  ok = false;
353
  }
354
  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",
356
  __func__, ctx->version, GGUF_VERSION);
357
  ok = false;
358
  }
@@ -363,7 +363,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
363
  if (ok && gr.read(n_tensors)) {
364
  static_assert(sizeof(size_t) <= 8 && sizeof(gguf_tensor_info) >= 2, "int64_t insufficient for indexing");
365
  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",
367
  __func__, n_tensors, SIZE_MAX/sizeof(gguf_tensor_info));
368
  ok = false;
369
  }
@@ -374,7 +374,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
374
  if (ok && gr.read(n_kv)) {
375
  static_assert(sizeof(size_t) <= 8 && sizeof(gguf_tensor_info) >= 2, "int64_t insufficient for indexing");
376
  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",
378
  __func__, n_kv, SIZE_MAX/sizeof(gguf_kv));
379
  ok = false;
380
  }
@@ -383,7 +383,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
383
  }
384
 
385
  if (!ok) {
386
- fprintf(stderr, "%s: failed to read header\n", __func__);
387
  gguf_free(ctx);
388
  return nullptr;
389
  }
@@ -399,15 +399,15 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
399
  try {
400
  ok = ok && gr.read(key);
401
  } catch (std::length_error &) {
402
- fprintf(stderr, "%s: encountered length_error while reading key %" PRIi64 "\n", __func__, i);
403
  ok = false;
404
  } catch (std::bad_alloc &) {
405
- fprintf(stderr, "%s: encountered bad_alloc error while reading key %" PRIi64 "\n", __func__, i);
406
  ok = false;
407
  }
408
  for (size_t j = 0; ok && j < ctx->kv.size(); ++j) {
409
  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);
411
  ok = false;
412
  }
413
  }
@@ -441,14 +441,14 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
441
  case GGUF_TYPE_ARRAY:
442
  default:
443
  {
444
- fprintf(stderr, "%s: key '%s' has invalid GGUF type %d\n", __func__, key.c_str(), type);
445
  ok = false;
446
  } break;
447
  }
448
  }
449
 
450
  if (!ok) {
451
- fprintf(stderr, "%s: failed to read key-value pairs\n", __func__);
452
  gguf_free(ctx);
453
  return nullptr;
454
  }
@@ -458,7 +458,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
458
  ctx->alignment = alignment_idx == -1 ? GGUF_DEFAULT_ALIGNMENT : gguf_get_val_u32(ctx, alignment_idx);
459
 
460
  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);
462
  gguf_free(ctx);
463
  return nullptr;
464
  }
@@ -474,14 +474,14 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
474
  try {
475
  ok = ok && gr.read(name);
476
  } catch (std::length_error &) {
477
- fprintf(stderr, "%s: encountered length_error while reading tensor name %" PRIi64 "\n", __func__, i);
478
  ok = false;
479
  } catch (std::bad_alloc &) {
480
- fprintf(stderr, "%s: encountered bad_alloc error while reading tensor name %" PRIi64 "\n", __func__, i);
481
  ok = false;
482
  }
483
  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);
485
  ok = false;
486
  break;
487
  }
@@ -490,7 +490,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
490
  // make sure there are no duplicate tensor names
491
  for (int64_t j = 0; ok && j < i; ++j) {
492
  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);
494
  ok = false;
495
  break;
496
  }
@@ -505,7 +505,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
505
  uint32_t n_dims = -1;
506
  ok = ok && gr.read(n_dims);
507
  if (n_dims > GGML_MAX_DIMS) {
508
- fprintf(stderr, "%s: tensor '%s' has invalid number of dimensions: %" PRIu32 " > %" PRIu32 "\n",
509
  __func__, info.t.name, n_dims, GGML_MAX_DIMS);
510
  ok = false;
511
  break;
@@ -518,7 +518,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
518
 
519
  // check that all ne are non-negative
520
  if (info.t.ne[j] < 0) {
521
- fprintf(stderr, "%s: tensor '%s' dimension %" PRIu32 " has invalid number of elements: %" PRIi64 " < 0\n",
522
  __func__, info.t.name, j, info.t.ne[j]);
523
  ok = false;
524
  break;
@@ -530,7 +530,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
530
  (INT64_MAX/info.t.ne[2] <= info.t.ne[0]*info.t.ne[1]) ||
531
  (INT64_MAX/info.t.ne[3] <= info.t.ne[0]*info.t.ne[1]*info.t.ne[2]))) {
532
 
533
- fprintf(stderr, "%s: total number of elements in tensor '%s' with shape "
534
  "(%" PRIi64 ", %" PRIi64 ", %" PRIi64 ", %" PRIi64 ") is >= %" PRIi64 "\n",
535
  __func__, info.t.name, info.t.ne[0], info.t.ne[1], info.t.ne[2], info.t.ne[3], INT64_MAX);
536
  ok = false;
@@ -547,7 +547,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
547
 
548
  // check that tensor type is within defined range
549
  if (info.t.type < 0 || info.t.type >= GGML_TYPE_COUNT) {
550
- fprintf(stderr, "%s: tensor '%s' has invalid ggml type %d (%s)\n",
551
  __func__, info.t.name, info.t.type, ggml_type_name(info.t.type));
552
  ok = false;
553
  break;
@@ -557,7 +557,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
557
 
558
  // check that row size is divisible by block size
559
  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, "
561
  "not a multiple of block size (%" PRId64 ")\n",
562
  __func__, info.t.name, (int) info.t.type, ggml_type_name(info.t.type), info.t.ne[0], blck_size);
563
  ok = false;
@@ -582,7 +582,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
582
  }
583
 
584
  if (!ok) {
585
- fprintf(stderr, "%s: failed to read tensor info\n", __func__);
586
  gguf_free(ctx);
587
  return nullptr;
588
  }
@@ -590,7 +590,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
590
 
591
  // we require the data section to be aligned, so take into account any padding
592
  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__);
594
  gguf_free(ctx);
595
  return nullptr;
596
  }
@@ -604,9 +604,9 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
604
  for (size_t i = 0; i < ctx->info.size(); ++i) {
605
  const gguf_tensor_info & ti = ctx->info[i];
606
  if (ti.offset != ctx->size) {
607
- fprintf(stderr, "%s: tensor '%s' has offset %" PRIu64 ", expected %zu\n",
608
  __func__, ti.t.name, ti.offset, ctx->size);
609
- fprintf(stderr, "%s: failed to read tensor data\n", __func__);
610
  gguf_free(ctx);
611
  return nullptr;
612
  }
@@ -634,7 +634,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
634
 
635
  *params.ctx = ggml_init(pdata);
636
  if (*params.ctx == nullptr) {
637
- fprintf(stderr, "%s: failed to initialize ggml context for storing tensors\n", __func__);
638
  gguf_free(ctx);
639
  return nullptr;
640
  }
@@ -656,7 +656,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
656
  ok = ok && gr.read(data->data, ctx->size);
657
 
658
  if (!ok) {
659
- fprintf(stderr, "%s: failed to read tensor data binary blob\n", __func__);
660
  ggml_free(ctx_data);
661
  *params.ctx = nullptr;
662
  gguf_free(ctx);
@@ -689,7 +689,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
689
  }
690
 
691
  if (!ok) {
692
- fprintf(stderr, "%s: failed to create tensors\n", __func__);
693
  ggml_free(ctx_data);
694
  *params.ctx = nullptr;
695
  gguf_free(ctx);
@@ -706,7 +706,7 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
706
  FILE * file = ggml_fopen(fname, "rb");
707
 
708
  if (!file) {
709
- fprintf(stderr, "%s: failed to open GGUF file '%s'\n", __func__, fname);
710
  return nullptr;
711
  }
712
 
@@ -1305,7 +1305,7 @@ bool gguf_write_to_file(const struct gguf_context * ctx, const char * fname, boo
1305
  FILE * file = ggml_fopen(fname, "wb");
1306
 
1307
  if (!file) {
1308
- fprintf(stderr, "%s: failed to open file '%s' for writing GGUF data\n", __func__, fname);
1309
  return false;
1310
  }
1311
 
 
299
  return false;
300
  }
301
  } catch (std::length_error &) {
302
+ GGML_LOG_ERROR("%s: encountered length_error while reading value for key '%s'\n", __func__, key.c_str());
303
  return false;
304
  } catch (std::bad_alloc &) {
305
+ GGML_LOG_ERROR("%s: encountered bad_alloc error while reading value for key '%s'\n", __func__, key.c_str());
306
  return false;
307
  }
308
  kv.emplace_back(key, value);
 
328
  ok = ok && gr.read(magic, 4);
329
 
330
  if (!ok) {
331
+ GGML_LOG_ERROR("%s: failed to read magic\n", __func__);
332
  gguf_free(ctx);
333
  return nullptr;
334
  }
335
 
336
  for (uint32_t i = 0; i < magic.size(); i++) {
337
  if (magic[i] != GGUF_MAGIC[i]) {
338
+ GGML_LOG_ERROR("%s: invalid magic characters: '%c%c%c%c', expected 'GGUF'\n", __func__, magic[0], magic[1], magic[2], magic[3]);
339
  gguf_free(ctx);
340
  return nullptr;
341
  }
 
348
 
349
  if (ok && gr.read(ctx->version)) {
350
  if (ctx->version == 1) {
351
+ GGML_LOG_ERROR("%s: GGUFv1 is no longer supported, please use a more up-to-date version\n", __func__);
352
  ok = false;
353
  }
354
  if (ctx->version > GGUF_VERSION) {
355
+ GGML_LOG_ERROR("%s: this GGUF file is version %" PRIu32 " but this software only supports up to version %d\n",
356
  __func__, ctx->version, GGUF_VERSION);
357
  ok = false;
358
  }
 
363
  if (ok && gr.read(n_tensors)) {
364
  static_assert(sizeof(size_t) <= 8 && sizeof(gguf_tensor_info) >= 2, "int64_t insufficient for indexing");
365
  if (n_tensors < 0 || n_tensors > int64_t(SIZE_MAX/sizeof(gguf_tensor_info))) {
366
+ GGML_LOG_ERROR("%s: number of tensors is %" PRIi64 " but must be in [0, %zu]\n",
367
  __func__, n_tensors, SIZE_MAX/sizeof(gguf_tensor_info));
368
  ok = false;
369
  }
 
374
  if (ok && gr.read(n_kv)) {
375
  static_assert(sizeof(size_t) <= 8 && sizeof(gguf_tensor_info) >= 2, "int64_t insufficient for indexing");
376
  if (n_kv < 0 || n_kv > int64_t(SIZE_MAX/sizeof(gguf_kv))) {
377
+ GGML_LOG_ERROR("%s: number of key value pairs is %" PRIi64 " but must be in [0, %zu]\n",
378
  __func__, n_kv, SIZE_MAX/sizeof(gguf_kv));
379
  ok = false;
380
  }
 
383
  }
384
 
385
  if (!ok) {
386
+ GGML_LOG_ERROR("%s: failed to read header\n", __func__);
387
  gguf_free(ctx);
388
  return nullptr;
389
  }
 
399
  try {
400
  ok = ok && gr.read(key);
401
  } catch (std::length_error &) {
402
+ GGML_LOG_ERROR("%s: encountered length_error while reading key %" PRIi64 "\n", __func__, i);
403
  ok = false;
404
  } catch (std::bad_alloc &) {
405
+ GGML_LOG_ERROR("%s: encountered bad_alloc error while reading key %" PRIi64 "\n", __func__, i);
406
  ok = false;
407
  }
408
  for (size_t j = 0; ok && j < ctx->kv.size(); ++j) {
409
  if (key == ctx->kv[j].key) {
410
+ GGML_LOG_ERROR("%s: duplicate key '%s' for tensors %zu and %" PRIi64 " \n", __func__, key.c_str(), j, i);
411
  ok = false;
412
  }
413
  }
 
441
  case GGUF_TYPE_ARRAY:
442
  default:
443
  {
444
+ GGML_LOG_ERROR("%s: key '%s' has invalid GGUF type %d\n", __func__, key.c_str(), type);
445
  ok = false;
446
  } break;
447
  }
448
  }
449
 
450
  if (!ok) {
451
+ GGML_LOG_ERROR("%s: failed to read key-value pairs\n", __func__);
452
  gguf_free(ctx);
453
  return nullptr;
454
  }
 
458
  ctx->alignment = alignment_idx == -1 ? GGUF_DEFAULT_ALIGNMENT : gguf_get_val_u32(ctx, alignment_idx);
459
 
460
  if (ctx->alignment == 0 || (ctx->alignment & (ctx->alignment - 1)) != 0) {
461
+ GGML_LOG_ERROR("%s: alignment %zu is not a power of 2\n", __func__, ctx->alignment);
462
  gguf_free(ctx);
463
  return nullptr;
464
  }
 
474
  try {
475
  ok = ok && gr.read(name);
476
  } catch (std::length_error &) {
477
+ GGML_LOG_ERROR("%s: encountered length_error while reading tensor name %" PRIi64 "\n", __func__, i);
478
  ok = false;
479
  } catch (std::bad_alloc &) {
480
+ GGML_LOG_ERROR("%s: encountered bad_alloc error while reading tensor name %" PRIi64 "\n", __func__, i);
481
  ok = false;
482
  }
483
  if (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);
485
  ok = false;
486
  break;
487
  }
 
490
  // make sure there are no duplicate tensor names
491
  for (int64_t j = 0; ok && j < i; ++j) {
492
  if (strcmp(info.t.name, ctx->info[j].t.name) == 0) {
493
+ GGML_LOG_ERROR("%s: duplicate tensor name '%s' for tensors %" PRIi64 " and %" PRIi64 "\n", __func__, info.t.name, j, i);
494
  ok = false;
495
  break;
496
  }
 
505
  uint32_t n_dims = -1;
506
  ok = ok && gr.read(n_dims);
507
  if (n_dims > GGML_MAX_DIMS) {
508
+ GGML_LOG_ERROR("%s: tensor '%s' has invalid number of dimensions: %" PRIu32 " > %" PRIu32 "\n",
509
  __func__, info.t.name, n_dims, GGML_MAX_DIMS);
510
  ok = false;
511
  break;
 
518
 
519
  // check that all ne are non-negative
520
  if (info.t.ne[j] < 0) {
521
+ GGML_LOG_ERROR("%s: tensor '%s' dimension %" PRIu32 " has invalid number of elements: %" PRIi64 " < 0\n",
522
  __func__, info.t.name, j, info.t.ne[j]);
523
  ok = false;
524
  break;
 
530
  (INT64_MAX/info.t.ne[2] <= info.t.ne[0]*info.t.ne[1]) ||
531
  (INT64_MAX/info.t.ne[3] <= info.t.ne[0]*info.t.ne[1]*info.t.ne[2]))) {
532
 
533
+ GGML_LOG_ERROR("%s: total number of elements in tensor '%s' with shape "
534
  "(%" PRIi64 ", %" PRIi64 ", %" PRIi64 ", %" PRIi64 ") is >= %" PRIi64 "\n",
535
  __func__, info.t.name, info.t.ne[0], info.t.ne[1], info.t.ne[2], info.t.ne[3], INT64_MAX);
536
  ok = false;
 
547
 
548
  // check that tensor type is within defined range
549
  if (info.t.type < 0 || info.t.type >= GGML_TYPE_COUNT) {
550
+ GGML_LOG_ERROR("%s: tensor '%s' has invalid ggml type %d (%s)\n",
551
  __func__, info.t.name, info.t.type, ggml_type_name(info.t.type));
552
  ok = false;
553
  break;
 
557
 
558
  // check that row size is divisible by block size
559
  if (blck_size == 0 || info.t.ne[0] % blck_size != 0) {
560
+ GGML_LOG_ERROR("%s: tensor '%s' of type %d (%s) has %" PRId64 " elements per row, "
561
  "not a multiple of block size (%" PRId64 ")\n",
562
  __func__, info.t.name, (int) info.t.type, ggml_type_name(info.t.type), info.t.ne[0], blck_size);
563
  ok = false;
 
582
  }
583
 
584
  if (!ok) {
585
+ GGML_LOG_ERROR("%s: failed to read tensor info\n", __func__);
586
  gguf_free(ctx);
587
  return nullptr;
588
  }
 
590
 
591
  // we require the data section to be aligned, so take into account any padding
592
  if (fseek(file, GGML_PAD(ftell(file), ctx->alignment), SEEK_SET) != 0) {
593
+ GGML_LOG_ERROR("%s: failed to seek to beginning of data section\n", __func__);
594
  gguf_free(ctx);
595
  return nullptr;
596
  }
 
604
  for (size_t i = 0; i < ctx->info.size(); ++i) {
605
  const gguf_tensor_info & ti = ctx->info[i];
606
  if (ti.offset != ctx->size) {
607
+ GGML_LOG_ERROR("%s: tensor '%s' has offset %" PRIu64 ", expected %zu\n",
608
  __func__, ti.t.name, ti.offset, ctx->size);
609
+ GGML_LOG_ERROR("%s: failed to read tensor data\n", __func__);
610
  gguf_free(ctx);
611
  return nullptr;
612
  }
 
634
 
635
  *params.ctx = ggml_init(pdata);
636
  if (*params.ctx == nullptr) {
637
+ GGML_LOG_ERROR("%s: failed to initialize ggml context for storing tensors\n", __func__);
638
  gguf_free(ctx);
639
  return nullptr;
640
  }
 
656
  ok = ok && gr.read(data->data, ctx->size);
657
 
658
  if (!ok) {
659
+ GGML_LOG_ERROR("%s: failed to read tensor data binary blob\n", __func__);
660
  ggml_free(ctx_data);
661
  *params.ctx = nullptr;
662
  gguf_free(ctx);
 
689
  }
690
 
691
  if (!ok) {
692
+ GGML_LOG_ERROR("%s: failed to create tensors\n", __func__);
693
  ggml_free(ctx_data);
694
  *params.ctx = nullptr;
695
  gguf_free(ctx);
 
706
  FILE * file = ggml_fopen(fname, "rb");
707
 
708
  if (!file) {
709
+ GGML_LOG_ERROR("%s: failed to open GGUF file '%s'\n", __func__, fname);
710
  return nullptr;
711
  }
712
 
 
1305
  FILE * file = ggml_fopen(fname, "wb");
1306
 
1307
  if (!file) {
1308
+ GGML_LOG_ERROR("%s: failed to open file '%s' for writing GGUF data\n", __func__, fname);
1309
  return false;
1310
  }
1311