@@ -306,6 +306,7 @@ void ggml_abort(const char * file, int line, const char * fmt, ...) {
306
306
}
307
307
308
308
#define GGML_DEBUG 0
309
+
309
310
#define GGML_GELU_FP16
310
311
#define GGML_GELU_QUICK_FP16
311
312
@@ -2014,7 +2015,7 @@ static const size_t GGML_OBJECT_SIZE = sizeof(struct ggml_object);
2014
2015
2015
2016
struct ggml_context {
2016
2017
size_t mem_size;
2017
- void* mem_buffer;
2018
+ void * mem_buffer;
2018
2019
bool mem_buffer_owned;
2019
2020
bool no_alloc;
2020
2021
bool no_alloc_save; // this is used to save the no_alloc state when using scratch buffers
@@ -3263,7 +3264,6 @@ struct ggml_numa_nodes {
3263
3264
//
3264
3265
3265
3266
struct ggml_state {
3266
- struct ggml_context_container contexts[GGML_MAX_CONTEXTS];
3267
3267
struct ggml_numa_nodes numa;
3268
3268
};
3269
3269
@@ -3845,7 +3845,6 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
3845
3845
const uint64_t t_start = ggml_time_us(); UNUSED(t_start);
3846
3846
3847
3847
g_state = (struct ggml_state) {
3848
- /*.contexts =*/ { { 0 } },
3849
3848
/*.numa =*/ {
3850
3849
.n_nodes = 0,
3851
3850
.total_cpus = 0,
@@ -3864,26 +3863,9 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
3864
3863
is_first_call = false;
3865
3864
}
3866
3865
3867
- // find non-used context in g_state
3868
- struct ggml_context * ctx = NULL;
3869
-
3870
- for (int i = 0; i < GGML_MAX_CONTEXTS; i++) {
3871
- if (!g_state.contexts[i].used) {
3872
- g_state.contexts[i].used = true;
3873
- ctx = &g_state.contexts[i].context;
3874
-
3875
- GGML_PRINT_DEBUG("%s: found unused context %d\n", __func__, i);
3876
- break;
3877
- }
3878
- }
3879
-
3880
- if (ctx == NULL) {
3881
- GGML_PRINT_DEBUG("%s: no unused context found\n", __func__);
3882
-
3883
- ggml_critical_section_end();
3866
+ ggml_critical_section_end();
3884
3867
3885
- return NULL;
3886
- }
3868
+ struct ggml_context * ctx = GGML_MALLOC(sizeof(struct ggml_context));
3887
3869
3888
3870
// allow to call ggml_init with 0 size
3889
3871
if (params.mem_size == 0) {
@@ -3911,42 +3893,31 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
3911
3893
3912
3894
GGML_PRINT_DEBUG("%s: context initialized\n", __func__);
3913
3895
3914
- ggml_critical_section_end();
3915
-
3916
3896
return ctx;
3917
3897
}
3918
3898
3919
- void ggml_free (struct ggml_context * ctx) {
3899
+ void ggml_reset (struct ggml_context * ctx) {
3920
3900
if (ctx == NULL) {
3921
3901
return;
3922
3902
}
3923
3903
3924
- // make this function thread safe
3925
- ggml_critical_section_start();
3926
-
3927
- bool found = false;
3928
-
3929
- for (int i = 0; i < GGML_MAX_CONTEXTS; i++) {
3930
- if (&g_state.contexts[i].context == ctx) {
3931
- g_state.contexts[i].used = false;
3932
-
3933
- GGML_PRINT_DEBUG("%s: context %d has been freed. memory used = %zu\n",
3934
- __func__, i, ggml_used_mem(ctx));
3935
-
3936
- if (ctx->mem_buffer_owned) {
3937
- ggml_aligned_free(ctx->mem_buffer, ctx->mem_size);
3938
- }
3904
+ ctx->n_objects = 0;
3905
+ ctx->objects_begin = NULL;
3906
+ ctx->objects_end = NULL;
3907
+ ctx->scratch = (struct ggml_scratch) { 0, 0, NULL, };
3908
+ ctx->scratch_save = (struct ggml_scratch) { 0, 0, NULL, };
3909
+ }
3939
3910
3940
- found = true;
3941
- break;
3942
- }
3911
+ void ggml_free(struct ggml_context * ctx) {
3912
+ if (ctx == NULL) {
3913
+ return;
3943
3914
}
3944
3915
3945
- if (!found ) {
3946
- GGML_PRINT_DEBUG("%s: context not found\n", __func__ );
3916
+ if (ctx->mem_buffer_owned ) {
3917
+ ggml_aligned_free(ctx->mem_buffer, ctx->mem_size );
3947
3918
}
3948
3919
3949
- ggml_critical_section_end( );
3920
+ GGML_FREE(ctx );
3950
3921
}
3951
3922
3952
3923
size_t ggml_used_mem(const struct ggml_context * ctx) {
0 commit comments