Skip to content

Commit 7620b92

Browse files
committed
use new graph api to avoid stack overflow on msvc
1 parent 3ffffa6 commit 7620b92

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

stable-diffusion.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
#include <vector>
1515

1616
#include "ggml/ggml.h"
17-
#include "stable-diffusion.h"
1817
#include "rng.h"
1918
#include "rng_philox.h"
19+
#include "stable-diffusion.h"
2020

2121
static SDLogLevel log_level = SDLogLevel::INFO;
2222

@@ -3122,8 +3122,8 @@ class StableDiffusionGGML {
31223122
struct ggml_tensor* out = diffusion_model.forward(ctx, x_t, NULL, c, t_emb);
31233123
ctx_size += ggml_used_mem(ctx) + ggml_used_mem_of_data(ctx);
31243124

3125-
struct ggml_cgraph diffusion_graph = ggml_build_forward(out);
3126-
struct ggml_cplan cplan = ggml_graph_plan(&diffusion_graph, n_threads);
3125+
struct ggml_cgraph* diffusion_graph = ggml_build_forward_ctx(ctx, out);
3126+
struct ggml_cplan cplan = ggml_graph_plan(diffusion_graph, n_threads);
31273127

31283128
ctx_size += cplan.work_size;
31293129
LOG_DEBUG("diffusion context need %.2fMB static memory, with work_size needing %.2fMB",
@@ -3155,8 +3155,8 @@ class StableDiffusionGGML {
31553155
struct ggml_tensor* out = diffusion_model.forward(ctx, x_t, NULL, c, t_emb);
31563156
ggml_hold_dynamic_tensor(out);
31573157

3158-
struct ggml_cgraph diffusion_graph = ggml_build_forward(out);
3159-
struct ggml_cplan cplan = ggml_graph_plan(&diffusion_graph, n_threads);
3158+
struct ggml_cgraph* diffusion_graph = ggml_build_forward_ctx(ctx, out);
3159+
struct ggml_cplan cplan = ggml_graph_plan(diffusion_graph, n_threads);
31603160

31613161
ggml_set_dynamic(ctx, false);
31623162
struct ggml_tensor* buf = ggml_new_tensor_1d(ctx, GGML_TYPE_I8, cplan.work_size);
@@ -3165,7 +3165,7 @@ class StableDiffusionGGML {
31653165
cplan.work_data = (uint8_t*)buf->data;
31663166

31673167
int64_t t0 = ggml_time_ms();
3168-
ggml_graph_compute(&diffusion_graph, &cplan);
3168+
ggml_graph_compute(diffusion_graph, &cplan);
31693169

31703170
double result = 0.f;
31713171

@@ -3222,8 +3222,8 @@ class StableDiffusionGGML {
32223222

32233223
struct ggml_tensor* hidden_states = cond_stage_model.text_model.forward(ctx, input_ids);
32243224

3225-
struct ggml_cgraph cond_graph = ggml_build_forward(hidden_states);
3226-
struct ggml_cplan cplan = ggml_graph_plan(&cond_graph, n_threads);
3225+
struct ggml_cgraph* cond_graph = ggml_build_forward_ctx(ctx, hidden_states);
3226+
struct ggml_cplan cplan = ggml_graph_plan(cond_graph, n_threads);
32273227
ctx_size += cplan.work_size;
32283228

32293229
ctx_size += ggml_used_mem(ctx) + ggml_used_mem_of_data(ctx);
@@ -3251,14 +3251,14 @@ class StableDiffusionGGML {
32513251
ggml_set_dynamic(ctx, params.dynamic);
32523252

32533253
struct ggml_tensor* hidden_states = cond_stage_model.text_model.forward(ctx, input_ids);
3254-
struct ggml_cgraph cond_graph = ggml_build_forward(hidden_states);
3254+
struct ggml_cgraph* cond_graph = ggml_build_forward_ctx(ctx, hidden_states);
32553255
LOG_DEBUG("building condition graph completed: %d nodes, %d leafs",
3256-
cond_graph.n_nodes, cond_graph.n_leafs);
3256+
cond_graph->n_nodes, cond_graph->n_leafs);
32573257

32583258
memcpy(input_ids->data, tokens.data(), tokens.size() * ggml_element_size(input_ids));
32593259

32603260
int64_t t0 = ggml_time_ms();
3261-
ggml_graph_compute_with_ctx(ctx, &cond_graph, n_threads);
3261+
ggml_graph_compute_with_ctx(ctx, cond_graph, n_threads);
32623262
int64_t t1 = ggml_time_ms();
32633263
LOG_DEBUG("computing condition graph completed, taking %.2fs", (t1 - t0) * 1.0f / 1000);
32643264

@@ -3360,8 +3360,8 @@ class StableDiffusionGGML {
33603360
struct ggml_tensor* out = diffusion_model.forward(ctx, noised_input, NULL, context, t_emb);
33613361
ctx_size += ggml_used_mem(ctx) + ggml_used_mem_of_data(ctx);
33623362

3363-
struct ggml_cgraph diffusion_graph = ggml_build_forward(out);
3364-
struct ggml_cplan cplan = ggml_graph_plan(&diffusion_graph, n_threads);
3363+
struct ggml_cgraph* diffusion_graph = ggml_build_forward_ctx(ctx, out);
3364+
struct ggml_cplan cplan = ggml_graph_plan(diffusion_graph, n_threads);
33653365

33663366
ctx_size += cplan.work_size;
33673367
LOG_DEBUG("diffusion context need %.2fMB static memory, with work_size needing %.2fMB",
@@ -3393,8 +3393,8 @@ class StableDiffusionGGML {
33933393
struct ggml_tensor* out = diffusion_model.forward(ctx, noised_input, NULL, context, t_emb);
33943394
ggml_hold_dynamic_tensor(out);
33953395

3396-
struct ggml_cgraph diffusion_graph = ggml_build_forward(out);
3397-
struct ggml_cplan cplan = ggml_graph_plan(&diffusion_graph, n_threads);
3396+
struct ggml_cgraph* diffusion_graph = ggml_build_forward_ctx(ctx, out);
3397+
struct ggml_cplan cplan = ggml_graph_plan(diffusion_graph, n_threads);
33983398

33993399
ggml_set_dynamic(ctx, false);
34003400
struct ggml_tensor* buf = ggml_new_tensor_1d(ctx, GGML_TYPE_I8, cplan.work_size);
@@ -3452,12 +3452,12 @@ class StableDiffusionGGML {
34523452
if (cfg_scale != 1.0 && uc != NULL) {
34533453
// uncond
34543454
copy_ggml_tensor(context, uc);
3455-
ggml_graph_compute(&diffusion_graph, &cplan);
3455+
ggml_graph_compute(diffusion_graph, &cplan);
34563456
copy_ggml_tensor(out_uncond, out);
34573457

34583458
// cond
34593459
copy_ggml_tensor(context, c);
3460-
ggml_graph_compute(&diffusion_graph, &cplan);
3460+
ggml_graph_compute(diffusion_graph, &cplan);
34613461

34623462
out_cond = out;
34633463

@@ -3474,7 +3474,7 @@ class StableDiffusionGGML {
34743474
} else {
34753475
// cond
34763476
copy_ggml_tensor(context, c);
3477-
ggml_graph_compute(&diffusion_graph, &cplan);
3477+
ggml_graph_compute(diffusion_graph, &cplan);
34783478
}
34793479

34803480
// v = out, eps = out
@@ -3607,8 +3607,8 @@ class StableDiffusionGGML {
36073607
struct ggml_tensor* moments = first_stage_model.encode(ctx, x);
36083608
ctx_size += ggml_used_mem(ctx) + ggml_used_mem_of_data(ctx);
36093609

3610-
struct ggml_cgraph vae_graph = ggml_build_forward(moments);
3611-
struct ggml_cplan cplan = ggml_graph_plan(&vae_graph, n_threads);
3610+
struct ggml_cgraph* vae_graph = ggml_build_forward_ctx(ctx, moments);
3611+
struct ggml_cplan cplan = ggml_graph_plan(vae_graph, n_threads);
36123612

36133613
ctx_size += cplan.work_size;
36143614
LOG_DEBUG("vae context need %.2fMB static memory, with work_size needing %.2fMB",
@@ -3632,10 +3632,10 @@ class StableDiffusionGGML {
36323632
}
36333633

36343634
struct ggml_tensor* moments = first_stage_model.encode(ctx, x);
3635-
struct ggml_cgraph vae_graph = ggml_build_forward(moments);
3635+
struct ggml_cgraph* vae_graph = ggml_build_forward_ctx(ctx, moments);
36363636

36373637
int64_t t0 = ggml_time_ms();
3638-
ggml_graph_compute_with_ctx(ctx, &vae_graph, n_threads);
3638+
ggml_graph_compute_with_ctx(ctx, vae_graph, n_threads);
36393639
int64_t t1 = ggml_time_ms();
36403640

36413641
#ifdef GGML_PERF
@@ -3736,8 +3736,8 @@ class StableDiffusionGGML {
37363736
struct ggml_tensor* img = first_stage_model.decoder.forward(ctx, z);
37373737
ctx_size += ggml_used_mem(ctx) + ggml_used_mem_of_data(ctx);
37383738

3739-
struct ggml_cgraph vae_graph = ggml_build_forward(img);
3740-
struct ggml_cplan cplan = ggml_graph_plan(&vae_graph, n_threads);
3739+
struct ggml_cgraph* vae_graph = ggml_build_forward_ctx(ctx, img);
3740+
struct ggml_cplan cplan = ggml_graph_plan(vae_graph, n_threads);
37413741

37423742
ctx_size += cplan.work_size;
37433743
LOG_DEBUG("vae context need %.2fMB static memory, with work_size needing %.2fMB",
@@ -3761,10 +3761,10 @@ class StableDiffusionGGML {
37613761
}
37623762

37633763
struct ggml_tensor* img = first_stage_model.decode(ctx, z);
3764-
struct ggml_cgraph vae_graph = ggml_build_forward(img);
3764+
struct ggml_cgraph* vae_graph = ggml_build_forward_ctx(ctx, img);
37653765

37663766
int64_t t0 = ggml_time_ms();
3767-
ggml_graph_compute_with_ctx(ctx, &vae_graph, n_threads);
3767+
ggml_graph_compute_with_ctx(ctx, vae_graph, n_threads);
37683768
int64_t t1 = ggml_time_ms();
37693769

37703770
#ifdef GGML_PERF

0 commit comments

Comments
 (0)