MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / gguf_init_from_file

Function gguf_init_from_file

ggml.c:19465–19759  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19463}
19464
19465struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_params params) {
19466 FILE * file = fopen(fname, "rb");
19467 if (!file) {
19468 return NULL;
19469 }
19470
19471 // offset from start of file
19472 size_t offset = 0;
19473
19474 char magic[4];
19475 enum ggml_sparse_deriv sparse_deriv;
19476
19477 // check the magic before making allocations
19478 {
19479 gguf_fread_el(file, &magic, sizeof(magic), &offset);
19480
19481 if (strncmp(magic, GGUF_MAGIC, sizeof(magic)) == 0) {
19482 sparse_deriv = GGML_DENSE_INFERENCE;
19483 } else if (strncmp(magic, GGUF_POWERINFER_MAGIC, sizeof(magic)) == 0) {
19484 sparse_deriv = GGML_SPARSE_INFERENCE;
19485 } else {
19486 fprintf(stderr, "%s: invalid magic characters %s.\n", __func__, magic);
19487 fclose(file);
19488 return NULL;
19489 }
19490 }
19491
19492 bool ok = true;
19493
19494 struct gguf_context * ctx = GGML_ALIGNED_MALLOC(sizeof(struct gguf_context));
19495 ctx->sparse_deriv = sparse_deriv;
19496
19497 // read the header
19498 {
19499 strncpy(ctx->header.magic, magic, 4);
19500
19501
19502 ctx->kv = NULL;
19503 ctx->infos = NULL;
19504 ctx->data = NULL;
19505
19506 ok = ok && gguf_fread_el(file, &ctx->header.version, sizeof(ctx->header.version), &offset);
19507 ok = ok && gguf_fread_el(file, &ctx->header.n_tensors, sizeof(ctx->header.n_tensors), &offset);
19508 ok = ok && gguf_fread_el(file, &ctx->header.n_kv, sizeof(ctx->header.n_kv), &offset);
19509
19510 if (ctx->header.version == 1) {
19511 fprintf(stderr, "%s: GGUFv1 is no longer supported. please use a more up-to-date version\n", __func__);
19512 fclose(file);
19513 gguf_free(ctx);
19514 return NULL;
19515 }
19516
19517 if (!ok) {
19518 fprintf(stderr, "%s: failed to read header\n", __func__);
19519 fclose(file);
19520 gguf_free(ctx);
19521 return NULL;
19522 }

Callers 10

llama_model_loaderMethod · 0.70
export_loraFunction · 0.50
clip_model_loadFunction · 0.50
save_llama_model_ggufFunction · 0.50
load_checkpoint_fileFunction · 0.50
load_vocabFunction · 0.50
init_modelFunction · 0.50
gguf_ex_read_0Function · 0.50
gguf_ex_read_1Function · 0.50

Calls 15

gguf_fread_elFunction · 0.85
fprintfFunction · 0.85
gguf_fread_strFunction · 0.85
gguf_freeFunction · 0.70
gguf_find_keyFunction · 0.70
gguf_get_val_u32Function · 0.70
ggml_blck_sizeFunction · 0.70
ggml_type_sizeFunction · 0.70
ggml_tensor_overheadFunction · 0.70
ggml_initFunction · 0.70
ggml_new_tensor_1dFunction · 0.70
ggml_freeFunction · 0.70

Tested by

no test coverage detected