Skip to content

Commit eb5fbde

Browse files
committed
add static ggml_graph_compute_sugar()
1 parent 4d77ad9 commit eb5fbde

File tree

1 file changed

+19
-45
lines changed

1 file changed

+19
-45
lines changed

ggml.c

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17272,7 +17272,6 @@ void ggml_graph_compute(struct ggml_graph_compute_plan * plan, struct ggml_cgrap
1727217272
GGML_ASSERT(plan->n_tasks[i] > 0);
1727317273
}
1727417274
}
17275-
1727617275
}
1727717276

1727817277
const int n_threads = plan->n_threads;
@@ -17339,6 +17338,20 @@ void ggml_graph_compute(struct ggml_graph_compute_plan * plan, struct ggml_cgrap
1733917338
}
1734017339
}
1734117340

17341+
static void ggml_graph_compute_sugar(struct ggml_cgraph * cgraph, int n_threads) {
17342+
struct ggml_graph_compute_plan plan = ggml_graph_compute_make_plan(cgraph, n_threads);
17343+
if (plan.work_size > 0) {
17344+
plan.work_data = malloc(plan.work_size);
17345+
GGML_ASSERT(plan.work_data);
17346+
}
17347+
17348+
ggml_graph_compute(&plan, cgraph);
17349+
17350+
if (plan.work_data) {
17351+
free(plan.work_data);
17352+
}
17353+
}
17354+
1734217355
void ggml_graph_reset(struct ggml_cgraph * cgraph) {
1734317356
for (int i = 0; i < cgraph->n_nodes; i++) {
1734417357
struct ggml_tensor * grad = cgraph->grads[i];
@@ -18193,17 +18206,7 @@ static enum ggml_opt_result ggml_opt_adam(
1819318206
ggml_graph_reset (gf);
1819418207
ggml_set_f32 (f->grad, 1.0f);
1819518208

18196-
{
18197-
struct ggml_graph_compute_plan plan = ggml_graph_compute_make_plan(gb, params.n_threads);
18198-
if (plan.work_size > 0) {
18199-
plan.work_data = malloc(plan.work_size);
18200-
GGML_ASSERT(plan.work_data);
18201-
}
18202-
ggml_graph_compute(&plan, gb);
18203-
if (plan.work_data) {
18204-
free(plan.work_data);
18205-
}
18206-
}
18209+
ggml_graph_compute_sugar(gb, params.n_threads);
1820718210

1820818211
opt->adam.fx_prev = ggml_get_f32_1d(f, 0);
1820918212
opt->adam.fx_best = opt->adam.fx_prev;
@@ -18284,17 +18287,7 @@ static enum ggml_opt_result ggml_opt_adam(
1828418287
ggml_graph_reset (gf);
1828518288
ggml_set_f32 (f->grad, 1.0f);
1828618289

18287-
{
18288-
struct ggml_graph_compute_plan plan = ggml_graph_compute_make_plan(gb, params.n_threads);
18289-
if (plan.work_size > 0) {
18290-
plan.work_data = malloc(plan.work_size);
18291-
GGML_ASSERT(plan.work_data);
18292-
}
18293-
ggml_graph_compute(&plan, gb);
18294-
if (plan.work_data) {
18295-
free(plan.work_data);
18296-
}
18297-
}
18290+
ggml_graph_compute_sugar(gb, params.n_threads);
1829818291

1829918292
const float fx = ggml_get_f32_1d(f, 0);
1830018293

@@ -18416,17 +18409,7 @@ static enum ggml_opt_result linesearch_backtracking(
1841618409
ggml_graph_reset (gf);
1841718410
ggml_set_f32 (f->grad, 1.0f);
1841818411

18419-
{
18420-
struct ggml_graph_compute_plan plan = ggml_graph_compute_make_plan(gb, params->n_threads);
18421-
if (plan.work_size > 0) {
18422-
plan.work_data = malloc(plan.work_size);
18423-
GGML_ASSERT(plan.work_data);
18424-
}
18425-
ggml_graph_compute(&plan, gb);
18426-
if (plan.work_data) {
18427-
free(plan.work_data);
18428-
}
18429-
}
18412+
ggml_graph_compute_sugar(gb, params->n_threads);
1843018413

1843118414
ggml_opt_get_grad(np, ps, g);
1843218415

@@ -18545,17 +18528,8 @@ static enum ggml_opt_result ggml_opt_lbfgs(
1854518528

1854618529
ggml_graph_reset (gf);
1854718530
ggml_set_f32 (f->grad, 1.0f);
18548-
{
18549-
struct ggml_graph_compute_plan plan = ggml_graph_compute_make_plan(gb, params.n_threads);
18550-
if (plan.work_size > 0) {
18551-
plan.work_data = malloc(plan.work_size);
18552-
GGML_ASSERT(plan.work_data);
18553-
}
18554-
ggml_graph_compute(&plan, gb);
18555-
if (plan.work_data) {
18556-
free(plan.work_data);
18557-
}
18558-
}
18531+
18532+
ggml_graph_compute_sugar(gb, params.n_threads);
1855918533

1856018534
ggml_opt_get_grad(np, ps, g);
1856118535

0 commit comments

Comments
 (0)