Skip to content

Commit 17c10ac

Browse files
committed
ggml : force no_alloc == false when creating opt tensors (close #1699)
This is needed to make operators like ggml_view() be able to store their parameters in the ggml context's memory and not get discarded when no_alloc is true
1 parent e9b66ee commit 17c10ac

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

ggml.c

+9
Original file line numberDiff line numberDiff line change
@@ -3721,6 +3721,7 @@ struct ggml_context {
37213721
void * mem_buffer;
37223722
bool mem_buffer_owned;
37233723
bool no_alloc;
3724+
bool no_alloc_save; // this is used to save the no_alloc state when using scratch buffers
37243725

37253726
int n_objects;
37263727

@@ -4055,6 +4056,7 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
40554056
/*.mem_buffer =*/ params.mem_buffer ? params.mem_buffer : GGML_ALIGNED_MALLOC(mem_size),
40564057
/*.mem_buffer_owned =*/ params.mem_buffer ? false : true,
40574058
/*.no_alloc =*/ params.no_alloc,
4059+
/*.no_alloc_save =*/ params.no_alloc,
40584060
/*.n_objects =*/ 0,
40594061
/*.objects_begin =*/ NULL,
40604062
/*.objects_end =*/ NULL,
@@ -4132,11 +4134,18 @@ size_t ggml_get_mem_size(struct ggml_context * ctx) {
41324134
// operators when using scratch buffers
41334135
// TODO: implement a better way
41344136
void ggml_scratch_save(struct ggml_context * ctx) {
4137+
// this is needed to allow opt tensors to store their data
4138+
// TODO: again, need to find a better way
4139+
ctx->no_alloc_save = ctx->no_alloc;
4140+
ctx->no_alloc = false;
4141+
41354142
ctx->scratch_save = ctx->scratch;
41364143
ctx->scratch.data = NULL;
41374144
}
41384145

41394146
void ggml_scratch_load(struct ggml_context * ctx) {
4147+
ctx->no_alloc = ctx->no_alloc_save;
4148+
41404149
ctx->scratch = ctx->scratch_save;
41414150
}
41424151

0 commit comments

Comments
 (0)