Skip to content

Commit 987f314

Browse files
committed
ggml : allocate contexts on the heap (v2)
1 parent 3689d49 commit 987f314

File tree

3 files changed

+23
-51
lines changed

3 files changed

+23
-51
lines changed

ggml/include/ggml.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@
217217

218218
#define GGML_MAX_DIMS 4
219219
#define GGML_MAX_PARAMS 2048
220-
#define GGML_MAX_CONTEXTS 64
221220
#define GGML_MAX_SRC 10
222221
#define GGML_MAX_N_THREADS 512
223222
#define GGML_MAX_OP_PARAMS 64
@@ -657,6 +656,7 @@ extern "C" {
657656
};
658657

659658
// scratch buffer
659+
// TODO: deprecate and remove
660660
struct ggml_scratch {
661661
size_t offs;
662662
size_t size;
@@ -760,8 +760,9 @@ extern "C" {
760760

761761
// main
762762

763-
GGML_API struct ggml_context * ggml_init(struct ggml_init_params params);
764-
GGML_API void ggml_free(struct ggml_context * ctx);
763+
GGML_API struct ggml_context * ggml_init (struct ggml_init_params params);
764+
GGML_API void ggml_reset(struct ggml_context * ctx);
765+
GGML_API void ggml_free (struct ggml_context * ctx);
765766

766767
GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
767768

ggml/src/ggml-metal.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3129,7 +3129,7 @@ static enum ggml_status ggml_metal_graph_compute(
31293129

31303130
// default buffer
31313131
static id<MTLDevice> g_backend_device = nil;
3132-
static int g_backend_device_ref_count = 0;
3132+
static int g_backend_device_ref_count = 0; // TODO: make thread-safe
31333133

31343134
static id<MTLDevice> ggml_backend_metal_get_device(void) {
31353135
if (g_backend_device == nil) {

ggml/src/ggml.c

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ void ggml_abort(const char * file, int line, const char * fmt, ...) {
308308
}
309309

310310
#define GGML_DEBUG 0
311+
311312
#define GGML_GELU_FP16
312313
#define GGML_GELU_QUICK_FP16
313314

@@ -1985,7 +1986,7 @@ static const size_t GGML_OBJECT_SIZE = sizeof(struct ggml_object);
19851986

19861987
struct ggml_context {
19871988
size_t mem_size;
1988-
void* mem_buffer;
1989+
void * mem_buffer;
19891990
bool mem_buffer_owned;
19901991
bool no_alloc;
19911992
bool no_alloc_save; // this is used to save the no_alloc state when using scratch buffers
@@ -3234,7 +3235,6 @@ struct ggml_numa_nodes {
32343235
//
32353236

32363237
struct ggml_state {
3237-
struct ggml_context_container contexts[GGML_MAX_CONTEXTS];
32383238
struct ggml_numa_nodes numa;
32393239
};
32403240

@@ -3816,17 +3816,12 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
38163816
const uint64_t t_start = ggml_time_us(); UNUSED(t_start);
38173817

38183818
g_state = (struct ggml_state) {
3819-
/*.contexts =*/ { { 0 } },
38203819
/*.numa =*/ {
38213820
.n_nodes = 0,
38223821
.total_cpus = 0,
38233822
},
38243823
};
38253824

3826-
for (int i = 0; i < GGML_MAX_CONTEXTS; ++i) {
3827-
g_state.contexts[i].used = false;
3828-
}
3829-
38303825
const uint64_t t_end = ggml_time_us(); UNUSED(t_end);
38313826

38323827
GGML_PRINT_DEBUG("%s: g_state initialized in %f ms\n", __func__, (t_end - t_start)/1000.0f);
@@ -3839,24 +3834,11 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
38393834
is_first_call = false;
38403835
}
38413836

3842-
// find non-used context in g_state
3843-
struct ggml_context * ctx = NULL;
3844-
3845-
for (int i = 0; i < GGML_MAX_CONTEXTS; i++) {
3846-
if (!g_state.contexts[i].used) {
3847-
g_state.contexts[i].used = true;
3848-
ctx = &g_state.contexts[i].context;
3849-
3850-
GGML_PRINT_DEBUG("%s: found unused context %d\n", __func__, i);
3851-
break;
3852-
}
3853-
}
3837+
ggml_critical_section_end();
38543838

3839+
struct ggml_context * ctx = GGML_ALIGNED_MALLOC(sizeof(struct ggml_context));
38553840
if (ctx == NULL) {
3856-
GGML_LOG_ERROR("%s: ran out of contexts (max = %d)\n", __func__, GGML_MAX_CONTEXTS);
3857-
3858-
ggml_critical_section_end();
3859-
3841+
GGML_LOG_ERROR("%s: failed to allocate ggml_context\n", __func__);
38603842
return NULL;
38613843
}
38623844

@@ -3886,42 +3868,31 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
38863868

38873869
GGML_PRINT_DEBUG("%s: context initialized\n", __func__);
38883870

3889-
ggml_critical_section_end();
3890-
38913871
return ctx;
38923872
}
38933873

3894-
void ggml_free(struct ggml_context * ctx) {
3874+
void ggml_reset(struct ggml_context * ctx) {
38953875
if (ctx == NULL) {
38963876
return;
38973877
}
38983878

3899-
// make this function thread safe
3900-
ggml_critical_section_start();
3901-
3902-
bool found = false;
3903-
3904-
for (int i = 0; i < GGML_MAX_CONTEXTS; i++) {
3905-
if (&g_state.contexts[i].context == ctx) {
3906-
g_state.contexts[i].used = false;
3907-
3908-
GGML_PRINT_DEBUG("%s: context %d has been freed. memory used = %zu\n",
3909-
__func__, i, ggml_used_mem(ctx));
3910-
3911-
if (ctx->mem_buffer_owned) {
3912-
GGML_ALIGNED_FREE(ctx->mem_buffer);
3913-
}
3879+
ctx->n_objects = 0;
3880+
ctx->objects_begin = NULL;
3881+
ctx->objects_end = NULL;
3882+
ctx->scratch = (struct ggml_scratch) { 0, 0, NULL, };
3883+
ctx->scratch_save = (struct ggml_scratch) { 0, 0, NULL, };
3884+
}
39143885

3915-
found = true;
3916-
break;
3917-
}
3886+
void ggml_free(struct ggml_context * ctx) {
3887+
if (ctx == NULL) {
3888+
return;
39183889
}
39193890

3920-
if (!found) {
3921-
GGML_PRINT_DEBUG("%s: context not found\n", __func__);
3891+
if (ctx->mem_buffer_owned) {
3892+
GGML_ALIGNED_FREE(ctx->mem_buffer);
39223893
}
39233894

3924-
ggml_critical_section_end();
3895+
GGML_ALIGNED_FREE(ctx);
39253896
}
39263897

39273898
size_t ggml_used_mem(const struct ggml_context * ctx) {

0 commit comments

Comments
 (0)