Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stduhpf opened this issue Aug 25, 2024 · 2 comments
Closed

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

stduhpf opened this issue Aug 25, 2024 · 2 comments

Comments

@stduhpf
Copy link
Contributor

stduhpf commented Aug 25, 2024

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
Copy link
Contributor

Set free_params_immediately to false when calling new_sd_ctx

@stduhpf
Copy link
Contributor Author

stduhpf commented Aug 25, 2024

Set free_params_immediately to false when calling new_sd_ctx

This works, thanks!
But why?

@stduhpf stduhpf closed this as completed Aug 25, 2024
mudler added a commit to mudler/LocalAI that referenced this issue Dec 3, 2024
mudler added a commit to mudler/LocalAI that referenced this issue Dec 3, 2024
* feat(backend): add stablediffusion-ggml

Signed-off-by: Ettore Di Giacinto <[email protected]>

* chore(ci): track stablediffusion-ggml

Signed-off-by: Ettore Di Giacinto <[email protected]>

* fixups

Signed-off-by: Ettore Di Giacinto <[email protected]>

* Use default scheduler and sampler if not specified

Signed-off-by: Ettore Di Giacinto <[email protected]>

* fixups

Signed-off-by: Ettore Di Giacinto <[email protected]>

* Move cfg scale out of diffusers block

Signed-off-by: Ettore Di Giacinto <[email protected]>

* Make it working

Signed-off-by: Ettore Di Giacinto <[email protected]>

* fix: set free_params_immediately to false to call the model in sequence

leejet/stable-diffusion.cpp#366

Signed-off-by: Ettore Di Giacinto <[email protected]>

---------

Signed-off-by: Ettore Di Giacinto <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants