read and create ggml_context containing the tensors and their data
| 452 | |
| 453 | // read and create ggml_context containing the tensors and their data |
| 454 | struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) { |
| 455 | |
| 456 | struct ggml_context * meta = NULL; |
| 457 | |
| 458 | struct gguf_init_params params = { |
| 459 | /*.no_alloc = */ true, |
| 460 | /*.ctx = */ &meta, |
| 461 | }; |
| 462 | |
| 463 | struct gguf_context * ctx = gguf_init_from_file(fname, params); |
| 464 | if (!ctx) { |
| 465 | throw std::runtime_error(format("%s: failed to load CLIP model from %s. Does this file exist?\n", __func__, fname)); |
| 466 | } |
| 467 | |
| 468 | if (verbosity >= 1) { |
| 469 | const int n_tensors = gguf_get_n_tensors(ctx); |
| 470 | const int n_kv = gguf_get_n_kv(ctx); |
| 471 | const int ftype = get_u32(ctx, KEY_FTYPE); |
| 472 | const std::string ftype_str = get_ftype(ftype); |
| 473 | const int idx_desc = get_key_idx(ctx, KEY_DESCRIPTION); |
| 474 | const std::string description = gguf_get_val_str(ctx, idx_desc); |
| 475 | const int idx_name = gguf_find_key(ctx, KEY_NAME); |
| 476 | if (idx_name != -1) { // make name optional temporarily as some of the uploaded models missing it due to a bug |
| 477 | const std::string name = gguf_get_val_str(ctx, idx_name); |
| 478 | printf("%s: model name: %s\n", __func__, name.c_str()); |
| 479 | } |
| 480 | printf("%s: description: %s\n", __func__, description.c_str()); |
| 481 | printf("%s: GGUF version: %d\n", __func__, gguf_get_version(ctx)); |
| 482 | printf("%s: alignment: %zu\n", __func__, gguf_get_alignment(ctx)); |
| 483 | printf("%s: n_tensors: %d\n", __func__, n_tensors); |
| 484 | printf("%s: n_kv: %d\n", __func__, n_kv); |
| 485 | printf("%s: ftype: %s\n", __func__, ftype_str.c_str()); |
| 486 | printf("\n"); |
| 487 | } |
| 488 | |
| 489 | // kv |
| 490 | if (verbosity >= 3) { |
| 491 | const int n_kv = gguf_get_n_kv(ctx); |
| 492 | |
| 493 | for (int i = 0; i < n_kv; ++i) { |
| 494 | const char * key = gguf_get_key(ctx, i); |
| 495 | |
| 496 | printf("%s: kv[%d]: key = %s\n", __func__, i, key); |
| 497 | } |
| 498 | printf("\n"); |
| 499 | } |
| 500 | |
| 501 | // data |
| 502 | size_t ctx_size = 0; |
| 503 | { |
| 504 | const int n_tensors = gguf_get_n_tensors(ctx); |
| 505 | |
| 506 | for (int i = 0; i < n_tensors; ++i) { |
| 507 | const char * name = gguf_get_tensor_name(ctx, i); |
| 508 | const size_t offset = gguf_get_tensor_offset(ctx, i); |
| 509 | |
| 510 | struct ggml_tensor * cur = ggml_get_tensor(meta, name); |
| 511 | ctx_size += sizeof(struct ggml_tensor) + GGML_OBJECT_SIZE; |
no test coverage detected