#define GGML_MAX_DIMS 4
#define GGML_MAX_PARAMS 2048
-#define GGML_MAX_CONTEXTS 64
#define GGML_MAX_SRC 10
#define GGML_MAX_N_THREADS 512
#define GGML_MAX_OP_PARAMS 64
};
// scratch buffer
+ // TODO: deprecate and remove
struct ggml_scratch {
size_t offs;
size_t size;
// main
- GGML_API struct ggml_context * ggml_init(struct ggml_init_params params);
- GGML_API void ggml_free(struct ggml_context * ctx);
+ GGML_API struct ggml_context * ggml_init (struct ggml_init_params params);
+ GGML_API void ggml_reset(struct ggml_context * ctx);
+ GGML_API void ggml_free (struct ggml_context * ctx);
GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
}
#define GGML_DEBUG 0
+
#define GGML_GELU_FP16
#define GGML_GELU_QUICK_FP16
struct ggml_context {
size_t mem_size;
- void* mem_buffer;
+ void * mem_buffer;
bool mem_buffer_owned;
bool no_alloc;
bool no_alloc_save; // this is used to save the no_alloc state when using scratch buffers
//
struct ggml_state {
- struct ggml_context_container contexts[GGML_MAX_CONTEXTS];
struct ggml_numa_nodes numa;
};
const uint64_t t_start = ggml_time_us(); UNUSED(t_start);
g_state = (struct ggml_state) {
- /*.contexts =*/ { { 0 } },
/*.numa =*/ {
.n_nodes = 0,
.total_cpus = 0,
is_first_call = false;
}
- // find non-used context in g_state
- struct ggml_context * ctx = NULL;
-
- for (int i = 0; i < GGML_MAX_CONTEXTS; i++) {
- if (!g_state.contexts[i].used) {
- g_state.contexts[i].used = true;
- ctx = &g_state.contexts[i].context;
-
- GGML_PRINT_DEBUG("%s: found unused context %d\n", __func__, i);
- break;
- }
- }
-
- if (ctx == NULL) {
- GGML_PRINT_DEBUG("%s: no unused context found\n", __func__);
-
- ggml_critical_section_end();
+ ggml_critical_section_end();
- return NULL;
- }
+ struct ggml_context * ctx = GGML_MALLOC(sizeof(struct ggml_context));
// allow to call ggml_init with 0 size
if (params.mem_size == 0) {
GGML_PRINT_DEBUG("%s: context initialized\n", __func__);
- ggml_critical_section_end();
-
return ctx;
}
-void ggml_free(struct ggml_context * ctx) {
+void ggml_reset(struct ggml_context * ctx) {
if (ctx == NULL) {
return;
}
- // make this function thread safe
- ggml_critical_section_start();
-
- bool found = false;
-
- for (int i = 0; i < GGML_MAX_CONTEXTS; i++) {
- if (&g_state.contexts[i].context == ctx) {
- g_state.contexts[i].used = false;
-
- GGML_PRINT_DEBUG("%s: context %d has been freed. memory used = %zu\n",
- __func__, i, ggml_used_mem(ctx));
-
- if (ctx->mem_buffer_owned) {
- ggml_aligned_free(ctx->mem_buffer, ctx->mem_size);
- }
+ ctx->n_objects = 0;
+ ctx->objects_begin = NULL;
+ ctx->objects_end = NULL;
+ ctx->scratch = (struct ggml_scratch) { 0, 0, NULL, };
+ ctx->scratch_save = (struct ggml_scratch) { 0, 0, NULL, };
+}
- found = true;
- break;
- }
+void ggml_free(struct ggml_context * ctx) {
+ if (ctx == NULL) {
+ return;
}
- if (!found) {
- GGML_PRINT_DEBUG("%s: context not found\n", __func__);
+ if (ctx->mem_buffer_owned) {
+ ggml_aligned_free(ctx->mem_buffer, ctx->mem_size);
}
- ggml_critical_section_end();
+ GGML_FREE(ctx);
}
size_t ggml_used_mem(const struct ggml_context * ctx) {