AUTOMATIC1111 / stable-diffusion-webui

Stable Diffusion web UI
GNU Affero General Public License v3.0
140.82k stars 26.64k forks source link

[Bug]: Runtime error when using upscaling in an API request #8486

Open SagsMug opened 1 year ago

SagsMug commented 1 year ago

Is there an existing issue for this?

What happened?

When i try to use the /sdapi/v1/txt2img endpoint with "enable_hr": True SDW casts an exception at the end of generation

Steps to reproduce the problem

Run:

payload = {
    "prompt": keywords,
    "negative_prompt": "lowres",
    "sampler_index": "DPM++ SDE Karras",
    "steps": 50,
    "cfg_scale": 7,
    "enable_hr": True,
    "hr_upscaler": "Latent",
    "hr_scale": 2,
    "hr_second_pass_steps": 30,
    "batch_size": 1,
    "restore_faces": True
}

response = requests.post(url=f"http://127.0.0.1:7860/sdapi/v1/txt2img", json=payload)

What should have happened?

It should return the generated images in a json message

Commit where the problem happens

Im running the latest commit 0cc0ee1

What platforms do you use to access the UI ?

Linux

What browsers do you use to access the UI ?

Mozilla Firefox

Command Line Arguments

python webui.py --xformers --listen --api

List of extensions

deforum-for-automatic1111-webui novelai-2-local-prompt sd-civitai-browser stable-diffusion-webui-inspiration LDSR Lora ScuNET SwinIR prompt-bracket-checker

Console logs

Traceback (most recent call last):
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/anyio/streams/memory.py", line 94, in receive
    return self.receive_nowait()
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/anyio/streams/memory.py", line 89, in receive_nowait
    raise WouldBlock
anyio.WouldBlock

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/middleware/base.py", line 77, in call_next
    message = await recv_stream.receive()
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/anyio/streams/memory.py", line 114, in receive
    raise EndOfStream
anyio.EndOfStream

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 407, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/fastapi/applications.py", line 270, in __call__
    await super().__call__(scope, receive, send)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/applications.py", line 124, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/middleware/base.py", line 106, in __call__
    response = await self.dispatch_func(request, call_next)
  File "/mnt/c/stable-diffusion-webui/modules/api/api.py", line 96, in log_and_time
    res: Response = await call_next(req)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/middleware/base.py", line 80, in call_next
    raise app_exc
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/middleware/base.py", line 69, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 24, in __call__
    await responder(scope, receive, send)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 44, in __call__
    await self.app(scope, receive, self.send_with_gzip)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/routing.py", line 706, in __call__
    await route.handle(scope, receive, send)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/fastapi/routing.py", line 237, in app
    raw_response = await run_endpoint_function(
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/fastapi/routing.py", line 165, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/home/anon/anaconda3/envs/sdw/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "/mnt/c/stable-diffusion-webui/modules/api/api.py", line 203, in text2imgapi
    processed = process_images(p)
  File "/mnt/c/stable-diffusion-webui/modules/processing.py", line 486, in process_images
    res = process_images_inner(p)
  File "/mnt/c/stable-diffusion-webui/modules/processing.py", line 632, in process_images_inner
    samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts)
  File "/mnt/c/stable-diffusion-webui/modules/processing.py", line 902, in sample
    samples = self.sampler.sample_img2img(self, samples, noise, conditioning, unconditional_conditioning, steps=self.hr_second_pass_steps or self.steps, image_conditioning=image_conditioning)
  File "/mnt/c/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 310, in sample_img2img
    noise_sampler = self.create_noise_sampler(x, sigmas, p)
  File "/mnt/c/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 282, in create_noise_sampler
    sigma_min, sigma_max = sigmas[sigmas > 0].min(), sigmas.max()
RuntimeError: min(): Expected reduction dim to be specified for input.numel() == 0. Specify the reduction dim with the 'dim' argument.

Additional information

I run this through WSL2 and have attempted to update libraries with pip install -r requirements --upgrade which didnt fix the problem

killamonis commented 1 year ago

@SagsMug any luck with this? Getting the same error rn

SagsMug commented 1 year ago

@SagsMug any luck with this? Getting the same error rn

I believe this was a sampler issue, so you could try switching to a different one I just used it without upscaling

Neph0s commented 1 year ago

Same issue

allanwakes commented 1 year ago

we run into the same error here, when we are using 'DPM++ SDE Karras' as sampler in an api call (txt2img).

markogeissler commented 1 year ago

try to add "hr_sampler_name": "DPM++ SDE Karras" in the api request body. fixed it for me

allanwakes commented 1 year ago

@markogeissler Thanks for your guide. I just tried it, no luck...but with more error details compared to previous version (still the same error, my sd release now, is 1.3.2) What release are you using? can you share your api request body? or will it help if I reinstall the sampler?

markogeissler commented 1 year ago

"hr_upscaler": "Latent", "hr_resize_x": 1024, "hr_resize_y": 1024, "hr_sampler_name": "DPM++ SDE Karras", "denoising_strength": 0.7

these are the options i have set for hr

allanwakes commented 1 year ago

On a new installation (version 1.3.2, python 3.10.11, ubuntu), I tried it again with below request payload when posting them to sdapi/v1/txt2img:

payload = {
  "enable_hr": True,
  "hr_scale": 2,
  "hr_upscaler": "R-ESRGAN 4x+",
  "hr_second_pass_steps": 0.35,
  "hr_resize_x": 1536,
  "hr_resize_y": 1536,
  "hr_sampler_name": "DPM++ SDE Karras",
  "prompt": "1girl, stars",
  "styles": [
    ""
  ],
  "batch_size": 1,
  "n_iter": 1,
  "steps": 30,
  "cfg_scale": 12,
  "width": 768,
  "height": 768,
  "override_settings": {},
  "override_settings_restore_afterwards": True,
  "send_images": True,
  "save_images": True,
}

response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
print(response.json())

The error is still here:

/home/aihikes/zoo/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py:315 in create_noise_sampler

  314 from k_diffusion.sampling import BrownianTreeNoiseSampler
❱ 315 sigma_min, sigma_max = sigmas[sigmas > 0].min(),sigmas.max()
  316 current_iter_seeds = p.all_seeds[p.iteration * p.batch_size:(p.iteration + 1) * 

BrownianTreeNoiseSampler = <class 'k_diffusion.sampling.BrownianTreeNoiseSampler'>
                       p = <modules.processing.StableDiffusionProcessingTxt2Img object at 0x7f3f42e10ee0>
                    self = <modules.sd_samplers_kdiffusion.KDiffusionSampler object at 0x7f3f385d5e70>
                  sigmas = tensor([0.], device='cuda:0')

RuntimeError: min(): Expected reduction dim to be specified for input.numel() == 0. Specify the reduction dim with the 'dim' argument.  

update: I found that, in class KDiffusionSampler, its get_sigmas(self, p, steps) method returns sigmas as: tensor([0.], device='cuda:0'), and this empty tensor would cause sigmas[sigmas > 0].min() to throw runtime error. Can I assume this might be a bug about k-diffusion?

Solution: NEED to add denoising_strength in your request body. A request without it will make an empty tensor, causing runtime error. Thank you! @markogeissler

RonGeorge commented 1 year ago

Just adding to the knowledge. I did everything, then realized I had denoising set to 0. Changed that to .1 and it worked.

PippoApps commented 8 months ago

I have the same error. I tried all possible permutations. As soon as I set enable_hr : true an error is triggered. My payload is:

            const result = await sdApi.txt2img({
                prompt: prompt,
                // "negative_prompt": "realistic, easynegative, badhandv4, disfigured, deformed, extra limbs, close up, b&w, blurry",
                width:96,
                height:96,
                override_settings: {
                    sd_model_checkpoint: models[0],
                    // enable_hr: true,
                },
                "denoising_strength": 0.5,
                hr_sampler_name: "DPM++ SDE Karras",
                enable_hr: true,
                hr_upscaler: "ESRGAN_4x",
                hr_scale: 2,
                // hr_resize_x: 300,
                // hr_resize_y:300,
                hr_second_pass_steps: 10,
                sampler_index: "DPM++ SDE Karras",
                restore_faces: false,                
            }); 

if enable_hr:false everything works but no upscaling. Any suggestion?