Skip to content

Commit 7d2e391

Browse files
committed
ggml_graph_compute: deprecate using ggml_context, try resolve issue #287
1 parent d7d2e6a commit 7d2e391

File tree

3 files changed

+86
-31
lines changed

3 files changed

+86
-31
lines changed

examples/train-text-from-scratch/train-text-from-scratch.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,11 +1426,9 @@ struct ggml_tensor * forward_batch_wo_cache_flash_attn_train(
14261426

14271427
gf->n_nodes = 0;
14281428
gf->n_leafs = 0;
1429-
gf->work_size = 0;
14301429
gf->perf_runs = 0;
14311430
gf->perf_cycles = 0;
14321431
gf->perf_time_us = 0;
1433-
gf->work = NULL;
14341432

14351433
const auto & hparams = model->hparams;
14361434
//const int n_ctx = hparams.n_ctx;

ggml.c

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16605,8 +16605,6 @@ struct ggml_cgraph ggml_build_forward(struct ggml_tensor * tensor) {
1660516605
/*.n_nodes =*/ 0,
1660616606
/*.n_leafs =*/ 0,
1660716607
/*.n_threads =*/ GGML_DEFAULT_N_THREADS,
16608-
/*.work_size =*/ 0,
16609-
/*.work =*/ NULL,
1661016608
/*.nodes =*/ { NULL },
1661116609
/*.grads =*/ { NULL },
1661216610
/*.leafs =*/ { NULL },
@@ -16778,6 +16776,7 @@ void clear_numa_thread_affinity(void) {}
1677816776

1677916777
struct ggml_compute_state_shared {
1678016778
struct ggml_cgraph * cgraph;
16779+
struct ggml_cgraph_context * cgraph_ctx;
1678116780

1678216781
int64_t perf_node_start_cycles;
1678316782
int64_t perf_node_start_time_us;
@@ -16807,6 +16806,7 @@ static void ggml_graph_compute_perf_stats_node(struct ggml_tensor * node, const
1680716806
static thread_ret_t ggml_graph_compute_thread(void * data) {
1680816807
struct ggml_compute_state * state = (struct ggml_compute_state *) data;
1680916808
struct ggml_cgraph * cgraph = state->shared->cgraph;
16809+
struct ggml_cgraph_context * ctx = state->shared->cgraph_ctx;
1681016810

1681116811
const int n_threads = state->shared->n_threads;
1681216812
set_numa_thread_affinity(state->ith, n_threads);
@@ -16821,8 +16821,8 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
1682116821
/*.type =*/ GGML_TASK_FINALIZE,
1682216822
/*.ith =*/ 0,
1682316823
/*.nth =*/ 0,
16824-
/*.wsize =*/ cgraph->work ? ggml_nbytes(cgraph->work) : 0,
16825-
/*.wdata =*/ cgraph->work ? cgraph->work->data : NULL,
16824+
/*.wsize =*/ ctx->work_size,
16825+
/*.wdata =*/ ctx->work_data,
1682616826
};
1682716827

1682816828
if (node_n != -1) {
@@ -16889,8 +16889,8 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
1688916889
/*.type =*/ GGML_TASK_COMPUTE,
1689016890
/*.ith =*/ state->ith,
1689116891
/*.nth =*/ node->n_tasks,
16892-
/*.wsize =*/ cgraph->work ? ggml_nbytes(cgraph->work) : 0,
16893-
/*.wdata =*/ cgraph->work ? cgraph->work->data : NULL,
16892+
/*.wsize =*/ ctx->work_size,
16893+
/*.wdata =*/ ctx->work_data,
1689416894
};
1689516895

1689616896
if (state->ith < node->n_tasks) {
@@ -16901,23 +16901,20 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
1690116901
return 0;
1690216902
}
1690316903

16904-
void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph) {
16905-
const int n_threads = cgraph->n_threads;
16904+
// Prepare for graph computing.
16905+
// Will set: node->n_tasks, ctx->{work_size, planned}
16906+
void ggml_graph_compute_plan(struct ggml_cgraph_context * ctx, struct ggml_cgraph * cgraph) {
16907+
GGML_ASSERT(ctx);
16908+
// This function is actually reentrant, but duplicate calls is unnecessary.
16909+
GGML_ASSERT(ctx->work_size == 0);
16910+
GGML_ASSERT(ctx->work_data == NULL);
16911+
GGML_ASSERT(!ctx->planned);
1690616912

16907-
struct ggml_compute_state_shared state_shared = {
16908-
/*.cgraph =*/ cgraph,
16909-
/*.perf_node_start_cycles =*/ 0,
16910-
/*.perf_node_start_time_us =*/ 0,
16911-
/*.n_threads =*/ n_threads,
16912-
/*.n_active =*/ n_threads,
16913-
/*.node_n =*/ -1,
16914-
};
16915-
struct ggml_compute_state * workers = alloca(sizeof(struct ggml_compute_state)*n_threads);
16913+
int n_threads = cgraph->n_threads;
16914+
size_t work_size = 0;
1691616915

1691716916
// initialize tasks + work buffer
1691816917
{
16919-
size_t work_size = 0;
16920-
1692116918
// thread scheduling for the different operations
1692216919
for (int i = 0; i < cgraph->n_nodes; i++) {
1692316920
struct ggml_tensor * node = cgraph->nodes[i];
@@ -17247,19 +17244,53 @@ void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph)
1724717244
} break;
1724817245
}
1724917246
}
17247+
}
1725017248

17251-
if (cgraph->work != NULL && work_size > cgraph->work_size) {
17252-
GGML_ASSERT(false); // TODO: better handling
17253-
}
17249+
if (work_size > 0) {
17250+
work_size += CACHE_LINE_SIZE*(n_threads - 1);
17251+
}
17252+
17253+
ctx->work_size = work_size;
17254+
ctx->work_data = NULL;
17255+
ctx->planned = true;
17256+
}
1725417257

17255-
if (work_size > 0 && cgraph->work == NULL) {
17256-
cgraph->work_size = work_size + CACHE_LINE_SIZE*(n_threads - 1);
17258+
void ggml_graph_compute_v2(struct ggml_cgraph_context * ctx, struct ggml_cgraph * cgraph) {
17259+
if (ctx == NULL) {
17260+
ctx = alloca(sizeof(struct ggml_cgraph_context));
17261+
GGML_ASSERT(ctx);
17262+
ctx->work_size = 0;
17263+
ctx->work_data = NULL;
17264+
ctx->planned = false;
17265+
} else {
17266+
// The work_size and work_data MAY have default values even if has been planned.
17267+
if (ctx->work_size > 0) {
17268+
GGML_ASSERT(ctx->work_data);
17269+
}
17270+
}
1725717271

17258-
GGML_PRINT_DEBUG("%s: allocating work buffer for graph (%zu bytes)\n", __func__, cgraph->work_size);
17259-
cgraph->work = ggml_new_tensor_1d(ctx, GGML_TYPE_I8, cgraph->work_size);
17272+
if (!ctx->planned) {
17273+
ggml_graph_compute_plan(ctx, cgraph);
17274+
if (ctx->work_size > 0) {
17275+
ctx->work_data = malloc(ctx->work_size * sizeof(GGML_TYPE_I8));
17276+
GGML_ASSERT(ctx->work_data);
17277+
GGML_PRINT_DEBUG("%s: allocating work buffer for graph (%zu bytes)\n", __func__, work_size);
1726017278
}
1726117279
}
1726217280

17281+
const int n_threads = cgraph->n_threads;
17282+
17283+
struct ggml_compute_state_shared state_shared = {
17284+
/*.cgraph =*/ cgraph,
17285+
/*.cgraph_ctx =*/ ctx,
17286+
/*.perf_node_start_cycles =*/ 0,
17287+
/*.perf_node_start_time_us =*/ 0,
17288+
/*.n_threads =*/ n_threads,
17289+
/*.n_active =*/ n_threads,
17290+
/*.node_n =*/ -1,
17291+
};
17292+
struct ggml_compute_state * workers = alloca(sizeof(struct ggml_compute_state)*n_threads);
17293+
1726317294
// create thread pool
1726417295
if (n_threads > 1) {
1726517296
for (int j = 1; j < n_threads; ++j) {
@@ -17311,6 +17342,12 @@ void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph)
1731117342
}
1731217343
}
1731317344

17345+
// Deprecated, keep it only for backward compatibility.
17346+
void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph) {
17347+
UNUSED(ctx);
17348+
ggml_graph_compute_v2(NULL, cgraph);
17349+
}
17350+
1731417351
void ggml_graph_reset(struct ggml_cgraph * cgraph) {
1731517352
for (int i = 0; i < cgraph->n_nodes; i++) {
1731617353
struct ggml_tensor * grad = cgraph->grads[i];

ggml.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,15 +409,23 @@ extern "C" {
409409

410410
static const size_t GGML_TENSOR_SIZE = sizeof(struct ggml_tensor);
411411

412+
// graph compute context
413+
struct ggml_cgraph_context {
414+
// After call to `ggml_graph_compute_plan()`, `planned` is set as true,
415+
// `work_size` will be updated as non-zero when buffer is required. When
416+
// need buffer, caller MUST allocate memory for `work_data`.
417+
// See https://github.com/ggerganov/ggml/issues/287
418+
size_t work_size;
419+
void * work_data;
420+
bool planned; // true means ready to compute graph nodes.
421+
};
422+
412423
// computation graph
413424
struct ggml_cgraph {
414425
int n_nodes;
415426
int n_leafs;
416427
int n_threads;
417428

418-
size_t work_size;
419-
struct ggml_tensor * work;
420-
421429
struct ggml_tensor * nodes[GGML_MAX_NODES];
422430
struct ggml_tensor * grads[GGML_MAX_NODES];
423431
struct ggml_tensor * leafs[GGML_MAX_NODES];
@@ -1273,6 +1281,18 @@ extern "C" {
12731281
GGML_API struct ggml_cgraph ggml_build_forward (struct ggml_tensor * tensor);
12741282
GGML_API struct ggml_cgraph ggml_build_backward(struct ggml_context * ctx, struct ggml_cgraph * gf, bool keep);
12751283

1284+
// Since https://github.com/ggerganov/ggml/issues/287
1285+
GGML_API void ggml_graph_compute_plan(struct ggml_cgraph_context * ctx, struct ggml_cgraph * cgraph);
1286+
// Since https://github.com/ggerganov/ggml/issues/287
1287+
// When `ctx` is NULL, `ggml_graph_compute_v2()` calculates work_size and allocates memory for `work_data`.
1288+
// Another use case: allocate buffer explicitly:
1289+
// - call `ggml_graph_compute_plan()`;
1290+
// - allocate memory for `ctx->work_data`;
1291+
// - finally call `ggml_graph_compute_v2()`.
1292+
// NOTE: don't manually set `ctx->planned`.
1293+
GGML_API void ggml_graph_compute_v2(struct ggml_cgraph_context * ctx, struct ggml_cgraph * cgraph);
1294+
// Deprecated, `ctx` is not required. Use `ggml_graph_compute_v2` instead.
1295+
// See https://github.com/ggerganov/ggml/issues/287
12761296
GGML_API void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph);
12771297
GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph);
12781298

0 commit comments

Comments
 (0)