Skip to content

Commit fbd18e1

Browse files
committed
fix: avoid stack overflow on MSVC
1 parent 09cab2a commit fbd18e1

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

stable-diffusion.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3155,6 +3155,8 @@ class StableDiffusionGGML {
31553155
struct ggml_tensor* c = ggml_new_tensor_4d(res_ctx, GGML_TYPE_F32, 1024, 2, 1, 1);
31563156
ggml_set_f32(c, 0.5);
31573157

3158+
struct ggml_cplan cplan;
3159+
31583160
size_t ctx_size = 10 * 1024 * 1024; // 10MB
31593161
// calculate the amount of memory required
31603162
{
@@ -3179,7 +3181,7 @@ class StableDiffusionGGML {
31793181
ctx_size += ggml_used_mem(ctx) + ggml_used_mem_of_data(ctx);
31803182

31813183
struct ggml_cgraph* diffusion_graph = ggml_build_forward_ctx(ctx, out);
3182-
struct ggml_cplan cplan = ggml_graph_plan(diffusion_graph, n_threads);
3184+
cplan = ggml_graph_plan(diffusion_graph, n_threads);
31833185

31843186
ctx_size += cplan.work_size;
31853187
LOG_DEBUG("diffusion context need %.2fMB static memory, with work_size needing %.2fMB",
@@ -3212,7 +3214,7 @@ class StableDiffusionGGML {
32123214
ggml_hold_dynamic_tensor(out);
32133215

32143216
struct ggml_cgraph* diffusion_graph = ggml_build_forward_ctx(ctx, out);
3215-
struct ggml_cplan cplan = ggml_graph_plan(diffusion_graph, n_threads);
3217+
cplan = ggml_graph_plan(diffusion_graph, n_threads);
32163218

32173219
ggml_set_dynamic(ctx, false);
32183220
struct ggml_tensor* buf = ggml_new_tensor_1d(ctx, GGML_TYPE_I8, cplan.work_size);
@@ -3257,6 +3259,7 @@ class StableDiffusionGGML {
32573259
true);
32583260
std::vector<int>& tokens = tokens_and_weights.first;
32593261
std::vector<float>& weights = tokens_and_weights.second;
3262+
struct ggml_cplan cplan;
32603263
size_t ctx_size = 10 * 1024 * 1024; // 10MB
32613264
// calculate the amount of memory required
32623265
{
@@ -3278,8 +3281,8 @@ class StableDiffusionGGML {
32783281

32793282
struct ggml_tensor* hidden_states = cond_stage_model.text_model.forward(ctx, input_ids);
32803283

3281-
struct ggml_cgraph cond_graph = ggml_build_forward(hidden_states);
3282-
struct ggml_cplan cplan = ggml_graph_plan(&cond_graph, n_threads);
3284+
struct ggml_cgraph* cond_graph = ggml_build_forward_ctx(ctx, hidden_states);
3285+
cplan = ggml_graph_plan(cond_graph, n_threads);
32833286
ctx_size += cplan.work_size;
32843287

32853288
ctx_size += ggml_used_mem(ctx) + ggml_used_mem_of_data(ctx);
@@ -3390,6 +3393,7 @@ class StableDiffusionGGML {
33903393
// print_ggml_tensor(x_t);
33913394
struct ggml_tensor* x = ggml_dup_tensor(res_ctx, x_t);
33923395
copy_ggml_tensor(x, x_t);
3396+
struct ggml_cplan cplan;
33933397

33943398
size_t ctx_size = 10 * 1024 * 1024; // 10MB
33953399
// calculate the amount of memory required
@@ -3417,7 +3421,7 @@ class StableDiffusionGGML {
34173421
ctx_size += ggml_used_mem(ctx) + ggml_used_mem_of_data(ctx);
34183422

34193423
struct ggml_cgraph* diffusion_graph = ggml_build_forward_ctx(ctx, out);
3420-
struct ggml_cplan cplan = ggml_graph_plan(diffusion_graph, n_threads);
3424+
cplan = ggml_graph_plan(diffusion_graph, n_threads);
34213425

34223426
ctx_size += cplan.work_size;
34233427
LOG_DEBUG("diffusion context need %.2fMB static memory, with work_size needing %.2fMB",
@@ -3450,7 +3454,7 @@ class StableDiffusionGGML {
34503454
ggml_hold_dynamic_tensor(out);
34513455

34523456
struct ggml_cgraph* diffusion_graph = ggml_build_forward_ctx(ctx, out);
3453-
struct ggml_cplan cplan = ggml_graph_plan(diffusion_graph, n_threads);
3457+
cplan = ggml_graph_plan(diffusion_graph, n_threads);
34543458

34553459
ggml_set_dynamic(ctx, false);
34563460
struct ggml_tensor* buf = ggml_new_tensor_1d(ctx, GGML_TYPE_I8, cplan.work_size);
@@ -3961,6 +3965,7 @@ class StableDiffusionGGML {
39613965
int64_t W = x->ne[0];
39623966
int64_t H = x->ne[1];
39633967
struct ggml_tensor* result = NULL;
3968+
struct ggml_cplan cplan;
39643969

39653970
// calculate the amount of memory required
39663971
size_t ctx_size = 10 * 1024 * 1024; // 10MB
@@ -3981,7 +3986,7 @@ class StableDiffusionGGML {
39813986
ctx_size += ggml_used_mem(ctx) + ggml_used_mem_of_data(ctx);
39823987

39833988
struct ggml_cgraph* vae_graph = ggml_build_forward_ctx(ctx, moments);
3984-
struct ggml_cplan cplan = ggml_graph_plan(vae_graph, n_threads);
3989+
cplan = ggml_graph_plan(vae_graph, n_threads);
39853990

39863991
ctx_size += cplan.work_size;
39873992
LOG_DEBUG("vae context need %.2fMB static memory, with work_size needing %.2fMB",
@@ -4083,6 +4088,7 @@ class StableDiffusionGGML {
40834088
int64_t W = z->ne[0];
40844089
int64_t H = z->ne[1];
40854090
struct ggml_tensor* result_img = NULL;
4091+
struct ggml_cplan cplan;
40864092

40874093
{
40884094
float* vec = (float*)z->data;
@@ -4110,7 +4116,7 @@ class StableDiffusionGGML {
41104116
ctx_size += ggml_used_mem(ctx) + ggml_used_mem_of_data(ctx);
41114117

41124118
struct ggml_cgraph* vae_graph = ggml_build_forward_ctx(ctx, img);
4113-
struct ggml_cplan cplan = ggml_graph_plan(vae_graph, n_threads);
4119+
cplan = ggml_graph_plan(vae_graph, n_threads);
41144120

41154121
ctx_size += cplan.work_size;
41164122
LOG_DEBUG("vae context need %.2fMB static memory, with work_size needing %.2fMB",

0 commit comments

Comments
 (0)