@@ -779,31 +779,41 @@ class StableDiffusionGGML {
779779 float skip_layer_start = 0.01 ,
780780 float skip_layer_end = 0.2 ,
781781 std::function<void (int , ggml_tensor*, SDVersion)> step_callback = nullptr) {
782+ struct ggml_init_params params;
783+ size_t data_size = ggml_row_size (init_latent->type , init_latent->ne [0 ]);
784+ for (int i = 1 ; i < 4 ; i++) {
785+ data_size *= init_latent->ne [i];
786+ }
787+ data_size += 1024 ;
788+ params.mem_size = data_size * 5 ;
789+ params.mem_buffer = NULL ;
790+ params.no_alloc = false ;
791+ ggml_context* tmp_ctx = ggml_init (params);
792+
782793 size_t steps = sigmas.size () - 1 ;
783794 // noise = load_tensor_from_file(work_ctx, "./rand0.bin");
784795 // print_ggml_tensor(noise);
785796 struct ggml_tensor * x = ggml_dup_tensor (work_ctx, init_latent);
786797 copy_ggml_tensor (x, init_latent);
787798 x = denoiser->noise_scaling (sigmas[0 ], noise, x);
788799
789- struct ggml_tensor * noised_input = ggml_dup_tensor (work_ctx , noise);
800+ struct ggml_tensor * noised_input = ggml_dup_tensor (tmp_ctx , noise);
790801
791802 bool has_unconditioned = cfg_scale != 1.0 && uncond.c_crossattn != NULL ;
792803 bool has_skiplayer = slg_scale != 0.0 && skip_layers.size () > 0 ;
793804
794-
795805 // denoise wrapper
796- struct ggml_tensor * out_cond = ggml_dup_tensor (work_ctx , x);
806+ struct ggml_tensor * out_cond = ggml_dup_tensor (tmp_ctx , x);
797807 struct ggml_tensor * out_uncond = NULL ;
798- struct ggml_tensor * out_skip = NULL ;
808+ struct ggml_tensor * out_skip = NULL ;
799809
800810 if (has_unconditioned) {
801- out_uncond = ggml_dup_tensor (work_ctx , x);
811+ out_uncond = ggml_dup_tensor (tmp_ctx , x);
802812 }
803813 if (has_skiplayer) {
804- out_skip = ggml_dup_tensor (work_ctx , x);
814+ out_skip = ggml_dup_tensor (tmp_ctx , x);
805815 }
806- struct ggml_tensor * denoised = ggml_dup_tensor (work_ctx , x);
816+ struct ggml_tensor * denoised = ggml_dup_tensor (tmp_ctx , x);
807817
808818 auto denoise = [&](ggml_tensor* input, float sigma, int step) -> ggml_tensor* {
809819 if (step == 1 ) {
@@ -948,6 +958,7 @@ class StableDiffusionGGML {
948958 control_net->free_compute_buffer ();
949959 }
950960 diffusion_model->free_compute_buffer ();
961+ ggml_free (tmp_ctx);
951962 return x;
952963 }
953964
@@ -1373,6 +1384,7 @@ sd_image_t* generate_image(sd_ctx_t* sd_ctx,
13731384 skip_layer_start,
13741385 skip_layer_end,
13751386 step_callback);
1387+
13761388 // struct ggml_tensor* x_0 = load_tensor_from_file(ctx, "samples_ddim.bin");
13771389 // print_ggml_tensor(x_0);
13781390 int64_t sampling_end = ggml_time_ms ();
0 commit comments