leejet / stable-diffusion.cpp

Stable Diffusion and Flux in pure C/C++
MIT License
3.27k stars 273 forks source link

Calling txt2img twice in a row on the same sd_ctx causes a crash #366

Closed stduhpf closed 3 weeks ago

stduhpf commented 3 weeks ago

I was just trying to mess around to find a way to generate multiple images in a row without having to reload all models everytime, but this doesn't seem to be working. The second image (batch) crashes the app consistently (when calling text_model->compute() during the get_learned_condition step).

        {
            sd_image_t* results;
            results = txt2img(sd_ctx,
                                params.prompt1.c_str(),
                                params.negative_prompt.c_str(),
                                params.clip_skip,
                                params.cfg_scale,
                                params.guidance,
                                params.width,
                                params.height,
                                params.sample_method,
                                params.sample_steps,
                                params.seed,
                                params.batch_count,
                                NULL,
                                params.control_strength,
                                params.style_ratio,
                                params.normalize_input,
                                params.input_id_images_path.c_str());

            if (results == NULL) {
                printf("generate failed\n");
                free_sd_ctx(sd_ctx);
                return 1;
            }

            size_t last            = params.output_path.find_last_of(".");
            std::string dummy_name = (last != std::string::npos ? params.output_path.substr(0, last) : params.output_path) + "_p1";
            for (int i = 0; i < params.batch_count; i++) {
                if (results[i].data == NULL) {
                    continue;
                }
                std::string final_image_path = i > 0 ? dummy_name + "_" + std::to_string(i + 1) + ".png" : dummy_name + ".png";
                stbi_write_png(final_image_path.c_str(), results[i].width, results[i].height, results[i].channel,
                            results[i].data, 0, get_image_params(params, params.seed + i).c_str());
                printf("save result image to '%s'\n", final_image_path.c_str());
                free(results[i].data);
                results[i].data = NULL;
            }
            free(results);
        }
        {
            sd_image_t* results;
            results = txt2img(sd_ctx,
                                params.prompt2.c_str(),
                                params.negative_prompt.c_str(),
                                params.clip_skip,
                                params.cfg_scale,
                                params.guidance,
                                params.width,
                                params.height,
                                params.sample_method,
                                params.sample_steps,
                                params.seed,
                                params.batch_count,
                                NULL,
                                params.control_strength,
                                params.style_ratio,
                                params.normalize_input,
                                params.input_id_images_path.c_str());

            if (results == NULL) {
                printf("generate failed\n");
                free_sd_ctx(sd_ctx);
                return 1;
            }

            size_t last            = params.output_path.find_last_of(".");
            std::string dummy_name = (last != std::string::npos ? params.output_path.substr(0, last) : params.output_path) + "_p2";
            for (int i = 0; i < params.batch_count; i++) {
                if (results[i].data == NULL) {
                    continue;
                }
                std::string final_image_path = i > 0 ? dummy_name + "_" + std::to_string(i + 1) + ".png" : dummy_name + ".png";
                stbi_write_png(final_image_path.c_str(), results[i].width, results[i].height, results[i].channel,
                            results[i].data, 0, get_image_params(params, params.seed + i).c_str());
                printf("save result image to '%s'\n", final_image_path.c_str());
                free(results[i].data);
                results[i].data = NULL;
            }
            free(results);
        }
[INFO ] stable-diffusion.cpp:184  - loading model from '..\ComfyUI\models\checkpoints\dreamshaper_8LCM.q8_0.gguf'
[INFO ] model.cpp:786  - load ..\ComfyUI\models\checkpoints\dreamshaper_8LCM.q8_0.gguf using gguf format
WARNING: Behavior may be unexpected when allocating 0 bytes for ggml_calloc!
[INFO ] stable-diffusion.cpp:224  - Version: SD 1.x
[INFO ] stable-diffusion.cpp:255  - Weight type:                 q8_0
[INFO ] stable-diffusion.cpp:256  - Conditioner weight type:     q8_0
[INFO ] stable-diffusion.cpp:257  - Diffsuion model weight type: q8_0
[INFO ] stable-diffusion.cpp:258  - VAE weight type:             q8_0
[INFO ] model.cpp:1671 - unknown tensor 'cond_stage_model.logit_scale | f16 | 1 [1, 1, 1, 1, 1]' in model file
[INFO ] model.cpp:1671 - unknown tensor 'cond_stage_model.text_projection | q8_0 | 2 [768, 768, 1, 1, 1]' in model file
[INFO ] stable-diffusion.cpp:486  - total params memory size = 1618.48MB (VRAM 0.00MB, RAM 1618.48MB): clip 125.20MB(RAM), unet 1398.81MB(RAM), vae 94.47MB(RAM), controlnet 0.00MB(VRAM), pmid 0.00MB(RAM)
[INFO ] stable-diffusion.cpp:490  - loading model from '..\ComfyUI\models\checkpoints\dreamshaper_8LCM.q8_0.gguf' completed, taking 0.96s
[INFO ] stable-diffusion.cpp:517  - running in eps-prediction mode
[INFO ] stable-diffusion.cpp:635  - Attempting to apply 0 LoRAs
[INFO ] stable-diffusion.cpp:1112 - apply_loras completed, taking 0.00s
[INFO ] stable-diffusion.cpp:1236 - get_learned_condition completed, taking 36 ms
[INFO ] stable-diffusion.cpp:1259 - sampling using LCM method
[INFO ] stable-diffusion.cpp:1263 - generating image: 1/1 - seed 69
  |==================================================| 4/4 - 3.09s/it
[INFO ] stable-diffusion.cpp:1295 - sampling completed, taking 13.06s
[INFO ] stable-diffusion.cpp:1303 - generating 1 latent images completed, taking 13.15s
[INFO ] stable-diffusion.cpp:1306 - decoding 1 latents
[INFO ] stable-diffusion.cpp:1316 - latent 1 decoded, taking 9.25s
[INFO ] stable-diffusion.cpp:1320 - decode_first_stage completed, taking 9.25s
[INFO ] stable-diffusion.cpp:1429 - txt2img completed in 22.45s
save result image to 'test.png'
[INFO ] stable-diffusion.cpp:635  - Attempting to apply 0 LoRAs
[INFO ] stable-diffusion.cpp:1112 - apply_loras completed, taking 0.00s
<Crash>
SkutteOleg commented 3 weeks ago

Set free_params_immediately to false when calling new_sd_ctx

stduhpf commented 3 weeks ago

Set free_params_immediately to false when calling new_sd_ctx

This works, thanks! But why?