@@ -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
19861987struct 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
32363237struct 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
39273898size_t ggml_used_mem(const struct ggml_context * ctx) {
0 commit comments