Skip to content

Commit cb871fa

Browse files
committed
gguf : do not support passing existing ggml_context to gguf_init
1 parent 860c9c6 commit cb871fa

File tree

2 files changed

+18
-38
lines changed

2 files changed

+18
-38
lines changed

ggml.c

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18388,6 +18388,8 @@ static bool gguf_fread_str(void * dst, FILE * file, size_t * offset) {
1838818388
}
1838918389

1839018390
struct gguf_context * gguf_init(const char * fname, struct gguf_init_params params) {
18391+
GGML_ASSERT(!params.load || params.malloc || params.ctx != NULL);
18392+
1839118393
FILE * file = fopen(fname, "rb");
1839218394
if (!file) {
1839318395
return NULL;
@@ -18518,8 +18520,7 @@ struct gguf_context * gguf_init(const char * fname, struct gguf_init_params para
1851818520

1851918521
const size_t size_cur = (ne*ggml_type_size(info->type))/ggml_blck_size(info->type);
1852018522

18521-
// TODO: pad size_cur to alignment
18522-
ctx->size_data += size_cur;
18523+
ctx->size_data += GGML_PAD(size_cur, ctx->alignment);
1852318524
}
1852418525

1852518526
// TODO: simplify
@@ -18528,28 +18529,18 @@ struct gguf_context * gguf_init(const char * fname, struct gguf_init_params para
1852818529
ctx->data = GGML_ALIGNED_MALLOC(ctx->size_data);
1852918530
fseek(file, ctx->offset, SEEK_SET);
1853018531
ok = ok && gguf_fread_el(ctx->data, ctx->size_data, file, &offset);
18531-
} else if (params.ctx != NULL) {
18532-
bool ctx_new = false;
18533-
bool ctx_no_alloc = false;
18534-
18535-
if (*params.ctx == NULL) {
18536-
const size_t mem_size =
18537-
ctx->header.n_tensors*ggml_tensor_overhead() + 1 +
18538-
ctx->size_data;
18539-
18540-
struct ggml_init_params pdata = {
18541-
.mem_size = mem_size,
18542-
.mem_buffer = NULL,
18543-
.no_alloc = false,
18544-
};
18532+
} else {
18533+
const size_t mem_size =
18534+
ctx->header.n_tensors*ggml_tensor_overhead() + 1 +
18535+
ctx->size_data;
1854518536

18546-
*params.ctx = ggml_init(pdata);
18537+
struct ggml_init_params pdata = {
18538+
.mem_size = mem_size,
18539+
.mem_buffer = NULL,
18540+
.no_alloc = false,
18541+
};
1854718542

18548-
ctx_new = true;
18549-
} else {
18550-
ctx_no_alloc = ggml_get_no_alloc(*params.ctx);
18551-
ggml_set_no_alloc(*params.ctx, false);
18552-
}
18543+
*params.ctx = ggml_init(pdata);
1855318544

1855418545
struct ggml_context * ctx_data = *params.ctx;
1855518546

@@ -18561,11 +18552,7 @@ struct gguf_context * gguf_init(const char * fname, struct gguf_init_params para
1856118552
if (!ok) {
1856218553
fprintf(stderr, "%s: failed to read tensor data\n", __func__);
1856318554
fclose(file);
18564-
if (ctx_new) {
18565-
ggml_free(ctx_data);
18566-
} else {
18567-
ggml_set_no_alloc(ctx_data, ctx_no_alloc);
18568-
}
18555+
ggml_free(ctx_data);
1856918556
gguf_free(ctx);
1857018557
return NULL;
1857118558
}
@@ -18597,18 +18584,12 @@ struct gguf_context * gguf_init(const char * fname, struct gguf_init_params para
1859718584
if (!ok) {
1859818585
fprintf(stderr, "%s: failed to create tensors\n", __func__);
1859918586
fclose(file);
18600-
if (ctx_new) {
18601-
ggml_free(ctx_data);
18602-
} else {
18603-
ggml_set_no_alloc(ctx_data, ctx_no_alloc);
18604-
}
18587+
ggml_free(ctx_data);
1860518588
gguf_free(ctx);
1860618589
return NULL;
1860718590
}
1860818591

18609-
ggml_set_no_alloc(ctx_data, ctx_no_alloc);
18610-
} else {
18611-
GGML_ASSERT("gguf: invalid params - load requires malloc or ctx");
18592+
ggml_set_no_alloc(ctx_data, false);
1861218593
}
1861318594
}
1861418595

ggml.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,9 +1636,8 @@ extern "C" {
16361636

16371637
struct gguf_init_params {
16381638
bool load; // load the tensor data
1639-
bool malloc; // if false, use the provided ggml_context to allocate the tensor data
1640-
// it no ggml_context is provided, it will be created
1641-
// if true, use malloc to allocate the tensor data
1639+
bool malloc; // if false, create a ggml_context and allocate the tensor data in it
1640+
// if true, use malloc to allocate the tensor data instead
16421641

16431642
struct ggml_context ** ctx;
16441643
};

0 commit comments

Comments
 (0)