From 28aa818cd0776c00c6a5aae943323a41b9e1ac11 Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Wed, 11 Sep 2024 11:31:51 +0300 Subject: [PATCH] ggml : remove ggml_cplan + rework ggml_cgraph ggml-ci --- examples/baby-llama/baby-llama.cpp | 28 ++- examples/benchmark/benchmark-matmult.cpp | 23 +-- examples/llava/llava.cpp | 4 +- ggml/include/ggml.h | 82 +++++--- ggml/src/ggml-backend.c | 37 ++-- ggml/src/ggml-impl.h | 11 + ggml/src/ggml.c | 250 +++++++++++++---------- tests/test-grad0.cpp | 20 +- tests/test-opt.cpp | 10 +- tests/test-rope.cpp | 18 +- 10 files changed, 272 insertions(+), 211 deletions(-) diff --git a/examples/baby-llama/baby-llama.cpp b/examples/baby-llama/baby-llama.cpp index 3ce91070b4ed72..d786dc94d3332d 100644 --- a/examples/baby-llama/baby-llama.cpp +++ b/examples/baby-llama/baby-llama.cpp @@ -17,17 +17,6 @@ constexpr float rms_norm_eps = LLAMA_DEFAULT_RMS_EPS; constexpr float rms_norm_eps = 5e-6f; #endif -static void ggml_graph_compute_helper(std::vector & buf, ggml_cgraph * graph, int n_threads) { - struct ggml_cplan plan = ggml_graph_plan(graph, n_threads, nullptr); - - if (plan.work_size > 0) { - buf.resize(plan.work_size); - plan.work_data = buf.data(); - } - - ggml_graph_compute(graph, &plan); -} - static struct ggml_tensor * randomize_tensor( struct ggml_tensor * tensor, int ndims, const int64_t ne[], float fmin, float fmax ) { @@ -1514,8 +1503,6 @@ int main(int argc, char ** argv) { int n_tokens = model.hparams.n_ctx; int n_vocab = model.hparams.n_vocab; - std::vector work_buffer; - for (int ex=0; ex & buf, ggml_cgraph * graph, int n_threads) { - struct ggml_cplan plan = ggml_graph_plan(graph, n_threads, nullptr); - - if (plan.work_size > 0) { - buf.resize(plan.work_size); - plan.work_data = buf.data(); - } - - ggml_graph_compute(graph, &plan); -} - static float tensor_sum_elements(const ggml_tensor * tensor) { double sum = 0; if (tensor->type == GGML_TYPE_F32) { @@ -179,9 +168,8 @@ int main(int argc, char ** argv) { TENSOR_DUMP(m11); TENSOR_DUMP(m2); - std::vector work_buffer; - - ggml_graph_compute_helper(work_buffer, gf, benchmark_params.n_threads); + ggml_graph_prepare(gf, benchmark_params.n_threads, nullptr); + ggml_graph_work_init(gf, nullptr); TENSOR_DUMP(ggml_graph_node(gf, 0)); @@ -234,7 +222,7 @@ int main(int argc, char ** argv) { long long int start = ggml_time_us(); //printf("Running ggml_graph_compute\n"); - ggml_graph_compute_helper(work_buffer, gf31, benchmark_params.n_threads); + ggml_graph_compute(gf31); long long int stop = ggml_time_us(); long long int usec = stop-start; @@ -267,8 +255,11 @@ int main(int argc, char ** argv) { } // Running a different graph computation to make sure we override the CPU cache lines - ggml_graph_compute_helper(work_buffer, gf32, benchmark_params.n_threads); + ggml_graph_compute(gf32); } + + ggml_graph_work_free(gf); + printf("\n"); printf("Average%78.2f\n",gflops_sum/((double)benchmark_params.n_iterations)); printf("=====================================================================================\n"); diff --git a/examples/llava/llava.cpp b/examples/llava/llava.cpp index e162586ed88d23..46413c90156a8a 100644 --- a/examples/llava/llava.cpp +++ b/examples/llava/llava.cpp @@ -183,7 +183,9 @@ static bool clip_llava_handle_patches(clip_ctx * ctx_clip, std::vector struct ggml_tensor *flatten = ggml_view_2d(model.ctx, permuted_cont, clip_n_mmproj_embd(ctx_clip), num_patches_height * num_patches_width * num_patches_per_side * num_patches_per_side, size_ele * clip_n_mmproj_embd(ctx_clip), 0); // ggml_tensor_printf(flatten,"flatten",__LINE__,false,false); ggml_build_forward_expand(gf, flatten); - ggml_graph_compute_with_ctx(model.ctx, gf, 1); + ggml_graph_prepare(gf, 1, nullptr); + ggml_graph_work_init(gf, model.ctx); + ggml_graph_compute(gf); struct ggml_tensor* result = ggml_graph_node(gf, -1); memcpy(image_embd_out, image_embd_v[0], clip_embd_nbytes(ctx_clip)); // main image as global context diff --git a/ggml/include/ggml.h b/ggml/include/ggml.h index 86ad6fb6224d51..d620a7dd851517 100644 --- a/ggml/include/ggml.h +++ b/ggml/include/ggml.h @@ -644,20 +644,6 @@ extern "C" { typedef struct ggml_threadpool * ggml_threadpool_t; - // the compute plan that needs to be prepared for ggml_graph_compute() - // since https://github.com/ggerganov/ggml/issues/287 - struct ggml_cplan { - size_t work_size; // size of work buffer, calculated by `ggml_graph_plan()` - uint8_t * work_data; // work buffer, to be allocated by caller before calling to `ggml_graph_compute()` - - int n_threads; - struct ggml_threadpool * threadpool; - - // abort ggml_graph_compute when true - ggml_abort_callback abort_callback; - void * abort_callback_data; - }; - // scratch buffer struct ggml_scratch { size_t offs; @@ -2068,23 +2054,57 @@ extern "C" { GGML_API struct ggml_threadpool_params ggml_threadpool_params_default(int n_threads); GGML_API void ggml_threadpool_params_init (struct ggml_threadpool_params * p, int n_threads); GGML_API bool ggml_threadpool_params_match (const struct ggml_threadpool_params * p0, const struct ggml_threadpool_params * p1); - GGML_API struct ggml_threadpool * ggml_threadpool_new (struct ggml_threadpool_params * params); - GGML_API void ggml_threadpool_free (struct ggml_threadpool * threadpool); - GGML_API int ggml_threadpool_get_n_threads(struct ggml_threadpool * threadpool); - GGML_API void ggml_threadpool_pause (struct ggml_threadpool * threadpool); - GGML_API void ggml_threadpool_resume (struct ggml_threadpool * threadpool); - - // ggml_graph_plan() has to be called before ggml_graph_compute() - // when plan.work_size > 0, caller must allocate memory for plan.work_data - GGML_API struct ggml_cplan ggml_graph_plan( - const struct ggml_cgraph * cgraph, - int n_threads, /* = GGML_DEFAULT_N_THREADS */ - struct ggml_threadpool * threadpool /* = NULL */ ); - GGML_API enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan); - - // same as ggml_graph_compute() but the work data is allocated as a part of the context - // note: the drawback of this API is that you must have ensured that the context has enough memory for the work data - GGML_API enum ggml_status ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads); + GGML_API struct ggml_threadpool * ggml_threadpool_new (struct ggml_threadpool_params * params); + GGML_API void ggml_threadpool_free (struct ggml_threadpool * threadpool); + GGML_API int ggml_threadpool_get_n_threads (struct ggml_threadpool * threadpool); + GGML_API void ggml_threadpool_pause (struct ggml_threadpool * threadpool); + GGML_API void ggml_threadpool_resume (struct ggml_threadpool * threadpool); + + // loops through the graph and determines: + // + // - work size needed for CPU computation + // - number of threads to start + // + GGML_API enum ggml_status ggml_graph_prepare( + struct ggml_cgraph * cgraph, + int n_threads, /* = GGML_DEFAULT_N_THREADS */ + struct ggml_threadpool * threadpool /* = NULL */ ); + + // get the estimated work size for the graph from ggml_graph_prepare() + GGML_API size_t ggml_graph_work_size(const struct ggml_cgraph * cgraph); + + // if ctx is NULL, the work buffer will be dynamically allocated. in this case, call ggml_graph_work_free() to free the buffer + // otherwise, the work buffer will be allocated in the context. no need to free it + GGML_API enum ggml_status ggml_graph_work_init(struct ggml_cgraph * cgraph, struct ggml_context * ctx); + GGML_API void ggml_graph_work_free(struct ggml_cgraph * cgraph); + + GGML_API void ggml_graph_set_abort_callback(struct ggml_cgraph * cgraph, ggml_abort_callback abort_callback, void * abort_data); + + // note: call ggml_graph_prepare() and ggml_graph_work_init() first + // + // sample usages: + // + // - no dynamic allocations: + // + // ... prepare ggml_context ctx ... + // + // ggml_graph_prepare (cgraph, n_threads, threadpool); + // ggml_graph_work_init(cgraph, ctx); + // + // ggml_graph_compute (cgraph); // can call many times + // + // // no need to call ggml_graph_work_free() because it is allocated in ctx + // + // - dynamic allocations: + // + // ggml_graph_prepare (cgraph, n_threads, threadpool); + // ggml_graph_work_init(cgraph, NULL); // will allocate memory + // + // ggml_graph_compute (cgraph); // can call many times + // + // ggml_graph_work_free(cgraph); + // + GGML_API enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph); GGML_API struct ggml_tensor * ggml_graph_get_tensor(struct ggml_cgraph * cgraph, const char * name); diff --git a/ggml/src/ggml-backend.c b/ggml/src/ggml-backend.c index b5d9301a787629..10739e0c7b7720 100644 --- a/ggml/src/ggml-backend.c +++ b/ggml/src/ggml-backend.c @@ -751,8 +751,10 @@ GGML_CALL static ggml_backend_buffer_type_t ggml_backend_cpu_get_default_buffer_ GGML_UNUSED(backend); } +// TODO: this struct should no longer be needed +// instead, the new ggml_graph_work_init() + ggml_graph_work_free() API should be enough to replace this +// for now, keeping the implementation as it is, to avoid making a mistake struct ggml_backend_plan_cpu { - struct ggml_cplan cplan; struct ggml_cgraph cgraph; }; @@ -761,19 +763,19 @@ GGML_CALL static ggml_backend_graph_plan_t ggml_backend_cpu_graph_plan_create(gg struct ggml_backend_plan_cpu * cpu_plan = malloc(sizeof(struct ggml_backend_plan_cpu)); - cpu_plan->cplan = ggml_graph_plan(cgraph, cpu_ctx->n_threads, cpu_ctx->threadpool); cpu_plan->cgraph = *cgraph; // FIXME: deep copy + ggml_graph_prepare(&cpu_plan->cgraph, cpu_ctx->n_threads, cpu_ctx->threadpool); - if (cpu_plan->cplan.work_size > 0) { - cpu_plan->cplan.work_data = malloc(cpu_plan->cplan.work_size); - if (cpu_plan->cplan.work_data == NULL) { + if (cpu_plan->cgraph.work_size > 0) { + cpu_plan->cgraph.work_data = malloc(cpu_plan->cgraph.work_size); + if (cpu_plan->cgraph.work_data == NULL) { free(cpu_plan); return NULL; } } - cpu_plan->cplan.abort_callback = cpu_ctx->abort_callback; - cpu_plan->cplan.abort_callback_data = cpu_ctx->abort_callback_data; + cpu_plan->cgraph.abort_callback = cpu_ctx->abort_callback; + cpu_plan->cgraph.abort_callback_data = cpu_ctx->abort_callback_data; return cpu_plan; } @@ -781,7 +783,7 @@ GGML_CALL static ggml_backend_graph_plan_t ggml_backend_cpu_graph_plan_create(gg GGML_CALL static void ggml_backend_cpu_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) { struct ggml_backend_plan_cpu * cpu_plan = (struct ggml_backend_plan_cpu *)plan; - free(cpu_plan->cplan.work_data); + free(cpu_plan->cgraph.work_data); free(cpu_plan); GGML_UNUSED(backend); @@ -790,7 +792,7 @@ GGML_CALL static void ggml_backend_cpu_graph_plan_free(ggml_backend_t backend, g GGML_CALL static enum ggml_status ggml_backend_cpu_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) { struct ggml_backend_plan_cpu * cpu_plan = (struct ggml_backend_plan_cpu *)plan; - return ggml_graph_compute(&cpu_plan->cgraph, &cpu_plan->cplan); + return ggml_graph_compute(&cpu_plan->cgraph); GGML_UNUSED(backend); } @@ -798,23 +800,24 @@ GGML_CALL static enum ggml_status ggml_backend_cpu_graph_plan_compute(ggml_backe GGML_CALL static enum ggml_status ggml_backend_cpu_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) { struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context; - struct ggml_cplan cplan = ggml_graph_plan(cgraph, cpu_ctx->n_threads, cpu_ctx->threadpool); + ggml_graph_prepare(cgraph, cpu_ctx->n_threads, cpu_ctx->threadpool); - if (cpu_ctx->work_size < cplan.work_size) { + if (cpu_ctx->work_size < cgraph->work_size) { free(cpu_ctx->work_data); - cpu_ctx->work_data = malloc(cplan.work_size); + cpu_ctx->work_data = malloc(cgraph->work_size); if (cpu_ctx->work_data == NULL) { cpu_ctx->work_size = 0; return GGML_STATUS_ALLOC_FAILED; } - cpu_ctx->work_size = cplan.work_size; + cpu_ctx->work_size = cgraph->work_size; } - cplan.work_data = cpu_ctx->work_data; + cgraph->work_data = cpu_ctx->work_data; + cgraph->work_own = false; // always freed by ggml_backend_cpu_graph_plan_free - cplan.abort_callback = cpu_ctx->abort_callback; - cplan.abort_callback_data = cpu_ctx->abort_callback_data; + cgraph->abort_callback = cpu_ctx->abort_callback; + cgraph->abort_callback_data = cpu_ctx->abort_callback_data; - return ggml_graph_compute(cgraph, &cplan); + return ggml_graph_compute(cgraph); } GGML_CALL static bool ggml_backend_cpu_supports_op(ggml_backend_t backend, const struct ggml_tensor * op) { diff --git a/ggml/src/ggml-impl.h b/ggml/src/ggml-impl.h index cb7f7728bd98ab..6539e8cb4ca127 100644 --- a/ggml/src/ggml-impl.h +++ b/ggml/src/ggml-impl.h @@ -773,6 +773,17 @@ struct ggml_cgraph { struct ggml_hash_set visited_hash_set; enum ggml_cgraph_eval_order order; + + bool work_own; + size_t work_size; // size of work buffer, calculated by `ggml_graph_plan()` + uint8_t * work_data; // work buffer, to be allocated by caller before calling to `ggml_graph_compute()` + + int n_threads; + struct ggml_threadpool * threadpool; + + // abort ggml_graph_compute when true + ggml_abort_callback abort_callback; + void * abort_callback_data; }; struct ggml_cgraph ggml_graph_view(struct ggml_cgraph * cgraph, int i0, int i1); diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c index d9b70aa6bbf3ce..6bb98b48022af3 100644 --- a/ggml/src/ggml.c +++ b/ggml/src/ggml.c @@ -1121,21 +1121,21 @@ ggml_type_traits_t ggml_internal_get_type_traits(enum ggml_type type) { #define GGML_F32x4_ADD vaddq_f32 #define GGML_F32x4_MUL vmulq_f32 #define GGML_F32x4_REDUCE_ONE(x) vaddvq_f32(x) -#define GGML_F32x4_REDUCE(res, x) \ -{ \ - int offset = GGML_F32_ARR >> 1; \ - for (int i = 0; i < offset; ++i) { \ - x[i] = vaddq_f32(x[i], x[offset+i]); \ - } \ - offset >>= 1; \ - for (int i = 0; i < offset; ++i) { \ - x[i] = vaddq_f32(x[i], x[offset+i]); \ - } \ - offset >>= 1; \ - for (int i = 0; i < offset; ++i) { \ - x[i] = vaddq_f32(x[i], x[offset+i]); \ - } \ - res = GGML_F32x4_REDUCE_ONE(x[0]); \ +#define GGML_F32x4_REDUCE(res, x) \ +{ \ + int offset = GGML_F32_ARR >> 1; \ + for (int i = 0; i < offset; ++i) { \ + (x)[i] = vaddq_f32((x)[i], (x)[offset+i]); \ + } \ + offset >>= 1; \ + for (int i = 0; i < offset; ++i) { \ + (x)[i] = vaddq_f32((x)[i], (x)[offset+i]); \ + } \ + offset >>= 1; \ + for (int i = 0; i < offset; ++i) { \ + (x)[i] = vaddq_f32((x)[i], (x)[offset+i]); \ + } \ + (res) = GGML_F32x4_REDUCE_ONE((x)[0]); \ } #define GGML_F32_VEC GGML_F32x4 @@ -1162,30 +1162,30 @@ ggml_type_traits_t ggml_internal_get_type_traits(enum ggml_type type) { #define GGML_F16x8_FMA(a, b, c) vfmaq_f16(a, b, c) #define GGML_F16x8_ADD vaddq_f16 #define GGML_F16x8_MUL vmulq_f16 - #define GGML_F16x8_REDUCE(res, x) \ - do { \ - int offset = GGML_F16_ARR >> 1; \ - for (int i = 0; i < offset; ++i) { \ - x[i] = vaddq_f16(x[i], x[offset+i]); \ - } \ - offset >>= 1; \ - for (int i = 0; i < offset; ++i) { \ - x[i] = vaddq_f16(x[i], x[offset+i]); \ - } \ - offset >>= 1; \ - for (int i = 0; i < offset; ++i) { \ - x[i] = vaddq_f16(x[i], x[offset+i]); \ - } \ - const float32x4_t t0 = vcvt_f32_f16(vget_low_f16 (x[0])); \ - const float32x4_t t1 = vcvt_f32_f16(vget_high_f16(x[0])); \ - res = (ggml_float) vaddvq_f32(vaddq_f32(t0, t1)); \ + #define GGML_F16x8_REDUCE(res, x) \ + do { \ + int offset = GGML_F16_ARR >> 1; \ + for (int i = 0; i < offset; ++i) { \ + (x)[i] = vaddq_f16((x)[i], (x)[offset+i]); \ + } \ + offset >>= 1; \ + for (int i = 0; i < offset; ++i) { \ + (x)[i] = vaddq_f16((x)[i], (x)[offset+i]); \ + } \ + offset >>= 1; \ + for (int i = 0; i < offset; ++i) { \ + (x)[i] = vaddq_f16((x)[i], (x)[offset+i]); \ + } \ + const float32x4_t t0 = vcvt_f32_f16(vget_low_f16 ((x)[0])); \ + const float32x4_t t1 = vcvt_f32_f16(vget_high_f16((x)[0])); \ + (res) = (ggml_float) vaddvq_f32(vaddq_f32(t0, t1)); \ } while (0) #define GGML_F16_VEC GGML_F16x8 #define GGML_F16_VEC_ZERO GGML_F16x8_ZERO #define GGML_F16_VEC_SET1 GGML_F16x8_SET1 #define GGML_F16_VEC_LOAD(p, i) GGML_F16x8_LOAD(p) - #define GGML_F16_VEC_STORE(p, r, i) GGML_F16x8_STORE((ggml_fp16_internal_t *)(p), r[i]) + #define GGML_F16_VEC_STORE(p, r, i) GGML_F16x8_STORE((ggml_fp16_internal_t *)(p), (r)[i]) #define GGML_F16_VEC_FMA GGML_F16x8_FMA #define GGML_F16_VEC_ADD GGML_F16x8_ADD #define GGML_F16_VEC_MUL GGML_F16x8_MUL @@ -2001,7 +2001,6 @@ struct ggml_threadpool { ggml_cond_t cond; // cond.var for waiting for new work struct ggml_cgraph * cgraph; - struct ggml_cplan * cplan; // synchronization primitives atomic_int n_graph; // incremented when there is work to be done (i.e each graph) @@ -19089,14 +19088,21 @@ struct ggml_cgraph * ggml_new_graph_custom(struct ggml_context * ctx, size_t siz assert(obj_size == (size_t)((char *)p - (char *)cgraph)); *cgraph = (struct ggml_cgraph) { - /*.size =*/ size, - /*.n_nodes =*/ 0, - /*.n_leafs =*/ 0, - /*.nodes =*/ nodes_ptr, - /*.grads =*/ grads_ptr, - /*.leafs =*/ leafs_ptr, - /*.hash_table =*/ { hash_size, hash_used, hash_keys_ptr }, - /*.order =*/ GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT, + /*.size =*/ size, + /*.n_nodes =*/ 0, + /*.n_leafs =*/ 0, + /*.nodes =*/ nodes_ptr, + /*.grads =*/ grads_ptr, + /*.leafs =*/ leafs_ptr, + /*.visited_hash_set =*/ { hash_size, hash_used, hash_keys_ptr }, + /*.order =*/ GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT, + /*.work_own =*/ false, + /*.work_size =*/ 0, + /*.work_data =*/ NULL, + /*.n_threads =*/ GGML_DEFAULT_N_THREADS, + /*.threadpool =*/ NULL, + /*.abort_callback =*/ NULL, + /*.abort_callback_data =*/ NULL, }; ggml_hash_set_reset(&cgraph->visited_hash_set); @@ -19110,14 +19116,21 @@ struct ggml_cgraph * ggml_new_graph(struct ggml_context * ctx) { struct ggml_cgraph ggml_graph_view(struct ggml_cgraph * cgraph0, int i0, int i1) { struct ggml_cgraph cgraph = { - /*.size =*/ 0, - /*.n_nodes =*/ i1 - i0, - /*.n_leafs =*/ 0, - /*.nodes =*/ cgraph0->nodes + i0, - /*.grads =*/ cgraph0->grads ? cgraph0->grads + i0 : NULL, - /*.leafs =*/ NULL, - /*.hash_table =*/ { 0, NULL, NULL }, - /*.order =*/ cgraph0->order, + /*.size =*/ 0, + /*.n_nodes =*/ i1 - i0, + /*.n_leafs =*/ 0, + /*.nodes =*/ cgraph0->nodes + i0, + /*.grads =*/ cgraph0->grads ? cgraph0->grads + i0 : NULL, + /*.leafs =*/ NULL, + /*.hash_table =*/ { 0, NULL, NULL }, + /*.order =*/ cgraph0->order, + /*.work_own =*/ false, + /*.work_size =*/ 0, + /*.work_data =*/ NULL, + /*.n_threads =*/ GGML_DEFAULT_N_THREADS, + /*.threadpool =*/ NULL, + /*.abort_callback =*/ NULL, + /*.abort_callback_data =*/ NULL, }; return cgraph; @@ -19753,11 +19766,10 @@ void ggml_threadpool_resume(struct ggml_threadpool * threadpool) { #endif } -struct ggml_cplan ggml_graph_plan( - const struct ggml_cgraph * cgraph, - int n_threads, - struct ggml_threadpool * threadpool) { - +enum ggml_status ggml_graph_prepare( + struct ggml_cgraph * cgraph, + int n_threads, + struct ggml_threadpool * threadpool) { if (threadpool == NULL) { GGML_PRINT_DEBUG("Threadpool is not specified. Will create a disposable threadpool : n_threads %d\n", n_threads); } @@ -19767,9 +19779,6 @@ struct ggml_cplan ggml_graph_plan( size_t work_size = 0; - struct ggml_cplan cplan; - memset(&cplan, 0, sizeof(struct ggml_cplan)); - int max_tasks = 1; // thread scheduling for the different operations + work buffer size estimation @@ -19921,28 +19930,68 @@ struct ggml_cplan ggml_graph_plan( work_size += CACHE_LINE_SIZE*(n_threads); } - cplan.threadpool = threadpool; - cplan.n_threads = MIN(max_tasks, n_threads); - cplan.work_size = work_size; - cplan.work_data = NULL; + cgraph->threadpool = threadpool; + cgraph->n_threads = MIN(max_tasks, n_threads); + cgraph->work_size = work_size; + + ggml_graph_work_free(cgraph); + + return GGML_STATUS_SUCCESS; +} + +size_t ggml_graph_work_size(const struct ggml_cgraph * cgraph) { + return cgraph->work_size; +} + +enum ggml_status ggml_graph_work_init(struct ggml_cgraph * cgraph, struct ggml_context * ctx) { + GGML_ASSERT(cgraph->n_threads > 0 && "call ggml_graph_prepare first"); + + ggml_graph_work_free(cgraph); + + if (cgraph->work_size > 0) { + if (ctx == NULL) { + cgraph->work_data = GGML_ALIGNED_MALLOC(cgraph->work_size); + if (cgraph->work_data == NULL) { + return GGML_STATUS_ALLOC_FAILED; + } + + cgraph->work_own = true; + } else { + struct ggml_object * obj = ggml_new_object(ctx, GGML_OBJECT_TYPE_WORK_BUFFER, cgraph->work_size); + + cgraph->work_data = (uint8_t *)ctx->mem_buffer + obj->offs; + cgraph->work_own = false; + } + } + + return GGML_STATUS_SUCCESS; +} - return cplan; +void ggml_graph_work_free(struct ggml_cgraph * cgraph) { + if (cgraph->work_data && cgraph->work_own) { + GGML_ALIGNED_FREE(cgraph->work_data); + cgraph->work_data = NULL; + } +} + +void ggml_graph_set_abort_callback(struct ggml_cgraph * cgraph, ggml_abort_callback abort_callback, void * abort_data) { + cgraph->abort_callback = abort_callback; + cgraph->abort_callback_data = abort_data; } static thread_ret_t ggml_graph_compute_thread(void * data) { struct ggml_compute_state * state = (struct ggml_compute_state *) data; const struct ggml_cgraph * cgraph = state->threadpool->cgraph; - const struct ggml_cplan * cplan = state->threadpool->cplan; set_numa_thread_affinity(state->ith); struct ggml_compute_params params = { - /*.ith =*/ state->ith, - /*.nth =*/ state->threadpool->n_threads_cur, - /*.wsize =*/ cplan->work_size, - /*.wdata =*/ cplan->work_data, - /*.threadpool=*/ state->threadpool, + /*.ith =*/ state->ith, + /*.nth =*/ state->threadpool->n_threads_cur, + /*.wsize =*/ cgraph->work_size, + /*.wdata =*/ cgraph->work_data, + /*.threadpool =*/ state->threadpool, }; for (int node_n = 0; node_n < cgraph->n_nodes; node_n++) { @@ -19950,7 +19999,7 @@ static thread_ret_t ggml_graph_compute_thread(void * data) { ggml_compute_forward(¶ms, node); - if (state->ith == 0 && cplan->abort_callback && cplan->abort_callback(cplan->abort_callback_data)) { + if (state->ith == 0 && cgraph->abort_callback && cgraph->abort_callback(cgraph->abort_callback_data)) { state->threadpool->ec = GGML_STATUS_ABORTED; } @@ -20104,14 +20153,12 @@ bool ggml_threadpool_params_match(const struct ggml_threadpool_params * p0, cons static struct ggml_threadpool * ggml_threadpool_new_impl( struct ggml_threadpool_params * tpp, - struct ggml_cgraph * cgraph, - struct ggml_cplan * cplan) { + struct ggml_cgraph * cgraph) { struct ggml_threadpool * threadpool = GGML_ALIGNED_MALLOC(sizeof(struct ggml_threadpool)); { threadpool->cgraph = cgraph; - threadpool->cplan = cplan; threadpool->n_graph = 0; threadpool->n_barrier = 0; threadpool->n_barrier_passed = 0; @@ -20169,16 +20216,15 @@ static struct ggml_threadpool * ggml_threadpool_new_impl( } struct ggml_threadpool * ggml_threadpool_new(struct ggml_threadpool_params * tpp) { - return ggml_threadpool_new_impl(tpp, NULL, NULL); + return ggml_threadpool_new_impl(tpp, NULL); } -enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan) { - GGML_ASSERT(cplan); - GGML_ASSERT(cplan->n_threads > 0); - GGML_ASSERT(cplan->work_size == 0 || cplan->work_data != NULL); +enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph) { + GGML_ASSERT((cgraph->n_threads > 0 ) && "call ggml_graph_prepare first"); + GGML_ASSERT((cgraph->work_size == 0 || cgraph->work_data != NULL) && "call ggml_graph_work_init first"); - int n_threads = cplan->n_threads; - struct ggml_threadpool * threadpool = cplan->threadpool; + int n_threads = cgraph->n_threads; + struct ggml_threadpool * threadpool = cgraph->threadpool; bool disposable_threadpool = false; @@ -20187,19 +20233,18 @@ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cpl disposable_threadpool = true; struct ggml_threadpool_params ttp = ggml_threadpool_params_default(n_threads); - threadpool = ggml_threadpool_new_impl(&ttp, cgraph, cplan); + threadpool = ggml_threadpool_new_impl(&ttp, cgraph); } else { // Reset some of the parameters that need resetting // No worker threads should be accessing the parameters below at this stage - threadpool->cgraph = cgraph; - threadpool->cplan = cplan; - threadpool->n_threads_cur = n_threads; - threadpool->current_chunk = 0; - threadpool->ec = GGML_STATUS_SUCCESS; + threadpool->cgraph = cgraph; + threadpool->n_threads_cur = n_threads; + threadpool->current_chunk = 0; + threadpool->ec = GGML_STATUS_SUCCESS; } if (n_threads > threadpool->n_threads_max) { - GGML_PRINT("WARNING: cplan is requesting more threads than the threadpool contains. Expect a bad time!\n"); + GGML_PRINT("WARNING: cgraph is requesting more threads than the threadpool contains. Expect a bad time!\n"); } #ifdef GGML_USE_OPENMP @@ -20238,16 +20283,6 @@ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cpl return ret; } -enum ggml_status ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads) { - struct ggml_cplan cplan = ggml_graph_plan(cgraph, n_threads, NULL); - - struct ggml_object * obj = ggml_new_object(ctx, GGML_OBJECT_TYPE_WORK_BUFFER, cplan.work_size); - - cplan.work_data = (uint8_t *)ctx->mem_buffer + obj->offs; - - return ggml_graph_compute(cgraph, &cplan); -} - struct ggml_tensor * ggml_graph_get_tensor(struct ggml_cgraph * cgraph, const char * name) { for (int i = 0; i < cgraph->n_leafs; i++) { struct ggml_tensor * leaf = cgraph->leafs[i]; @@ -21055,9 +21090,8 @@ static enum ggml_opt_result ggml_opt_adam( float * pf = params.past > 0 ? opt->adam.pf->data : NULL; // past function values - struct ggml_cplan cplan = ggml_graph_plan(gb, params.n_threads, NULL); - struct ggml_object * obj = ggml_new_object(ctx, GGML_OBJECT_TYPE_WORK_BUFFER, cplan.work_size); - cplan.work_data = (uint8_t *)ctx->mem_buffer + obj->offs; + ggml_graph_prepare (gb, params.n_threads, NULL); + ggml_graph_work_init(gb, ctx); bool cancel = false; @@ -21073,7 +21107,7 @@ static enum ggml_opt_result ggml_opt_adam( } // ggml_graph_reset (gf); ggml_set_f32 (f->grad, 1.0f); - ggml_graph_compute(gb, &cplan); + ggml_graph_compute(gb); ggml_opt_acc_grad(np, ps, g, accum_norm); fx += ggml_get_f32_1d(f, 0); } @@ -21164,7 +21198,7 @@ static enum ggml_opt_result ggml_opt_adam( } // ggml_graph_reset (gf); ggml_set_f32 (f->grad, 1.0f); - ggml_graph_compute(gb, &cplan); + ggml_graph_compute(gb); ggml_opt_acc_grad(np, ps, g, accum_norm); fx += ggml_get_f32_1d(f, 0); } @@ -21249,7 +21283,6 @@ static enum ggml_opt_result linesearch_backtracking( const float * xp, struct ggml_tensor * f, struct ggml_cgraph * gb, - struct ggml_cplan * cplan, const int np, struct ggml_tensor * ps[], bool * cancel, @@ -21306,7 +21339,7 @@ static enum ggml_opt_result linesearch_backtracking( } // ggml_graph_reset (gf); ggml_set_f32 (f->grad, 1.0f); - ggml_graph_compute(gb, cplan); + ggml_graph_compute(gb); ggml_opt_acc_grad(np, ps, g, accum_norm); *fx += ggml_get_f32_1d(f, 0); } @@ -21402,9 +21435,8 @@ static enum ggml_opt_result ggml_opt_lbfgs( opt->iter = iter; } - struct ggml_cplan cplan = ggml_graph_plan(gb, params.n_threads, NULL); - struct ggml_object * obj = ggml_new_object(ctx, GGML_OBJECT_TYPE_WORK_BUFFER, cplan.work_size); - cplan.work_data = (uint8_t *)ctx->mem_buffer + obj->offs; + ggml_graph_prepare (gb, params.n_threads, NULL); + ggml_graph_work_init(gb, ctx); float * x = opt->lbfgs.x->data; // current parameters float * xp = opt->lbfgs.xp->data; // previous parameters @@ -21449,7 +21481,7 @@ static enum ggml_opt_result ggml_opt_lbfgs( } // ggml_graph_reset (gf); ggml_set_f32 (f->grad, 1.0f); - ggml_graph_compute(gb, &cplan); + ggml_graph_compute(gb); ggml_opt_acc_grad(np, ps, g, accum_norm); fx += ggml_get_f32_1d(f, 0); } @@ -21515,7 +21547,7 @@ static enum ggml_opt_result ggml_opt_lbfgs( // to determine if the optimization should be cancelled // this is a simple change, but not doing this atm, since I don't have a nice // way to test and don't want to break something with so many changes lined up - ls = linesearch_backtracking(¶ms, nx, x, &fx, g, d, step, xp, f, gb, &cplan, np, ps, &cancel, callback, callback_data); + ls = linesearch_backtracking(¶ms, nx, x, &fx, g, d, step, xp, f, gb, np, ps, &cancel, callback, callback_data); if (cancel) { return GGML_OPT_RESULT_CANCEL; } diff --git a/tests/test-grad0.cpp b/tests/test-grad0.cpp index 1834c11d894b4c..f47cc5299ff2b3 100644 --- a/tests/test-grad0.cpp +++ b/tests/test-grad0.cpp @@ -242,12 +242,16 @@ static bool check_gradient( ggml_graph_cpy(gf, gb); ggml_build_backward_expand(ctx0, gf, gb, false); - ggml_graph_compute_with_ctx(ctx0, gf, n_threads); + ggml_graph_prepare(gf, n_threads, nullptr); + ggml_graph_work_init(gf, ctx0); + ggml_graph_compute(gf); ggml_graph_reset (gf); ggml_set_f32 (f->grad, 1.0f); - ggml_graph_compute_with_ctx(ctx0, gb, n_threads); + ggml_graph_prepare(gb, n_threads, nullptr); + ggml_graph_work_init(gb, ctx0); + ggml_graph_compute(gb); // ggml_graph_dump_dot(gf, NULL, "test-grad0-forward.dot"); // ggml_graph_dump_dot(gb, gf, "test-grad0-backward.dot"); @@ -262,13 +266,17 @@ static bool check_gradient( const float xp = x0 + eps; ggml_set_f32_1d(x[i], k, xp); - ggml_graph_compute_with_ctx(ctx0, gf, n_threads); + ggml_graph_prepare(gf, n_threads, nullptr); + ggml_graph_work_init(gf, ctx0); + ggml_graph_compute(gf); const double f0 = ggml_get_f32_1d(f, 0); ggml_set_f32_1d(x[i], k, xm); - ggml_graph_compute_with_ctx(ctx0, gf, n_threads); + ggml_graph_prepare(gf, n_threads, nullptr); + ggml_graph_work_init(gf, ctx0); + ggml_graph_compute(gf); const double f1 = ggml_get_f32_1d(f, 0); const double g0 = (f0 - f1)/(2.0*(double) eps); @@ -301,7 +309,9 @@ static bool check_gradient( ggml_graph_reset (gf); ggml_set_f32 (f->grad, 1.0f); - ggml_graph_compute_with_ctx(ctx0, gb, n_threads); + ggml_graph_prepare(gb, n_threads, nullptr); + ggml_graph_work_init(gb, ctx0); + ggml_graph_compute(gb); const double g1 = ggml_get_f32_1d(x[i]->grad, k); diff --git a/tests/test-opt.cpp b/tests/test-opt.cpp index 546ca230ba4170..8bc10da7f837b4 100644 --- a/tests/test-opt.cpp +++ b/tests/test-opt.cpp @@ -113,7 +113,10 @@ int main(void) { ggml_build_forward_expand(ge, e); ggml_graph_reset(ge); - ggml_graph_compute_with_ctx(ctx, ge, /*n_threads*/ 1); + ggml_graph_prepare(ge, 1, nullptr); + ggml_graph_work_init(ge, nullptr); + ggml_graph_compute(ge); + ggml_graph_work_free(ge); const float fe = ggml_get_f32_1d(e, 0); printf("%s: e = %.4f\n", __func__, fe); @@ -124,7 +127,10 @@ int main(void) { ggml_graph_reset(ge); - ggml_graph_compute_with_ctx(ctx, ge, /*n_threads*/ 1); + ggml_graph_prepare(ge, 1, nullptr); + ggml_graph_work_init(ge, nullptr); + ggml_graph_compute(ge); + ggml_graph_work_free(ge); const float fe_opt = ggml_get_f32_1d(e, 0); printf("%s: original e = %.4f\n", __func__, fe); diff --git a/tests/test-rope.cpp b/tests/test-rope.cpp index 246bb227d1e199..6abcdf25cd1e74 100644 --- a/tests/test-rope.cpp +++ b/tests/test-rope.cpp @@ -112,17 +112,6 @@ static struct ggml_tensor * get_random_tensor_f32( return result; } -static void ggml_graph_compute_helper(std::vector & buf, ggml_cgraph * graph, int n_threads) { - struct ggml_cplan plan = ggml_graph_plan(graph, n_threads, nullptr); - - if (plan.work_size > 0) { - buf.resize(plan.work_size); - plan.work_data = buf.data(); - } - - ggml_graph_compute(graph, &plan); -} - int main(int /*argc*/, const char ** /*argv*/) { struct ggml_init_params params = { /* .mem_size = */ 128*1024*1024, @@ -130,8 +119,6 @@ int main(int /*argc*/, const char ** /*argv*/) { /* .no_alloc = */ false, }; - std::vector work_buffer; - struct ggml_context * ctx0 = ggml_init(params); struct ggml_tensor * x; @@ -175,7 +162,10 @@ int main(int /*argc*/, const char ** /*argv*/) { ggml_build_forward_expand(gf, r1); ggml_build_forward_expand(gf, r2); - ggml_graph_compute_helper(work_buffer, gf, 4); + ggml_graph_prepare(gf, 4, nullptr); + ggml_graph_work_init(gf, nullptr); + ggml_graph_compute(gf); + ggml_graph_work_free(gf); // check that r1 and r2 are the same {