Mikubill / sd-webui-controlnet

WebUI extension for ControlNet
GNU General Public License v3.0
16.96k stars 1.95k forks source link

[Bug]: Controlnet interfere hires step after it has been turned off #2840

Open coder168 opened 5 months ago

coder168 commented 5 months ago

Is there an existing issue for this?

What happened?

This is a very wired bug. It seems controlnet can interfere hires step somehow even after controlnet is disabled. It is tricky, it seems controlnet cause certain impacts which has not been completed restored after disabled. Please investigate and let us know what you found, I am quite curious.

Steps to reproduce the problem

  1. Go to txt2img;
  2. put a simple text, like '1 pretty girl' into positive input, set seed as an fixed value like "123";
  3. enable "Hires. fix", adjust usual settings as desired, like upscaler as "4x-UltraSharp", hires steps as '12' and denoising strength as '0.4';
  4. click "generate' button, and get desired images. 1st run, the results (without controlnet) are normal;
  5. enable controlnet, select reference (or ipadapter), setup image reference;
  6. click "generate" button, and get desired images. 2nd run, the results (with controlnet) are normal;
  7. simply disable controlnet, and then click "generate" button, and images broken and turned to black! lowres stage works fine, but broken after starting hires stage. 3rd run, the results (without controlnet) are broken!
  8. run without problem by disable hires stage. Remain broken even make change of positive text and another settings like upscaler, the results remain broken;
  9. "reload UI" can not resolve problem, it has to restart whole sd-webui and it works again

What should have happened?

3rd run should not broken. Since it is the exactly same input and settings like 1st run. If 1st run works fine, 3rd run should get exactly same results. The only difference is 3rd run after 2nd run which controlnet enable.

Commit where the problem happens

webui: version: v1.9.3 controlnet: v1.1.446

What browsers do you use to access the UI ?

Google Chrome

Command Line Arguments

--medvram --opt-split-attention --xformers --disable-nan-check

List of enabled extensions

Console logs

Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)]
Version: v1.9.3
Commit hash: 1c0a0c4c26f78c32095ebc7f8af82f5c04fca8c0
Launching Web UI with arguments: --medvram --opt-split-attention --xformers --disable-nan-check --ckpt-dir D:\app2\sd-models\Stable-diffusion --lora-dir D:\app2\sd-models\Lora --embeddings-dir D:\app2\sd-models\embeddings --controlnet-dir D:\app2\sd-models\controlnet
2024-05-04 22:56:00.588405: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-05-04 22:56:01.835632: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
ControlNet preprocessor location: D:\app2\stable-diffusion-webui\extensions\sd-webui-controlnet\annotator\downloads
2024-05-04 22:56:10,215 - ControlNet - INFO - ControlNet v1.1.446
2024-05-04 22:56:10,502 - ControlNet - INFO - ControlNet v1.1.446
Loading weights [879db523c3] from D:\app2\sd-models\Stable-diffusion\dreamshaper_8.safetensors
2024-05-04 22:56:11,265 - ControlNet - INFO - ControlNet UI callback registered.
Creating model from config: D:\app2\stable-diffusion-webui\configs\v1-inference.yaml
Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
Startup time: 25.0s (prepare environment: 4.4s, import torch: 6.4s, import gradio: 1.8s, setup paths: 4.9s, initialize shared: 0.4s, other imports: 1.0s, list SD models: 0.4s, load scripts: 4.1s, create ui: 1.2s, gradio launch: 0.5s).
Loading VAE weights specified in settings: D:\app2\sd-models\Stable-diffusion\sd-v1-5-pruned-emaonly.vae.pt
Applying attention optimization: xformers... done.
Model loaded in 5.1s (load weights from disk: 0.9s, create model: 1.8s, apply weights to model: 1.1s, apply half(): 0.1s, load VAE: 0.6s, calculate empty prompt: 0.5s).
100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:04<00:00,  4.28it/s]
tiled upscale: 100%|███████████████████████████████████████████████████████████████████| 15/15 [00:03<00:00,  4.76it/s]
100%|██████████████████████████████████████████████████████████████████████████████████| 12/12 [00:09<00:00,  1.30it/s]
Total progress: 100%|██████████████████████████████████████████████████████████████████| 32/32 [00:24<00:00,  1.28it/s]
2024-05-04 22:58:30,209 - ControlNet - INFO - unit_separate = False, style_align = False 32/32 [00:24<00:00,  1.27it/s]
2024-05-04 22:58:30,493 - ControlNet - INFO - Using preprocessor: reference_only
2024-05-04 22:58:30,494 - ControlNet - INFO - preprocessor resolution = 512
2024-05-04 22:58:30,819 - ControlNet - INFO - ControlNet Hooked - Time = 0.6211252212524414
  0%|                                                                                           | 0/20 [00:00<?, ?it/s]2024-05-04 22:58:32,974 - ControlNet - INFO - ControlNet used torch.float16 VAE to encode torch.Size([1, 4, 96, 64])./s]
100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:10<00:00,  1.96it/s]
tiled upscale: 100%|███████████████████████████████████████████████████████████████████| 15/15 [00:03<00:00,  4.84it/s]
  0%|                                                                                           | 0/12 [00:00<?, ?it/s]2024-05-04 22:58:48,672 - ControlNet - INFO - ControlNet used torch.float16 VAE to encode torch.Size([1, 4, 192, 128]).
100%|██████████████████████████████████████████████████████████████████████████████████| 12/12 [00:23<00:00,  1.95s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████| 32/32 [00:44<00:00,  1.38s/it]
100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:03<00:00,  5.42it/s]
tiled upscale: 100%|███████████████████████████████████████████████████████████████████| 15/15 [00:03<00:00,  4.84it/s]
  0%|                                                                                           | 0/12 [00:00<?, ?it/s]D:\app2\stable-diffusion-webui\modules\sd_samplers_common.py:68: RuntimeWarning: invalid value encountered in cast
  x_sample = x_sample.astype(np.uint8)
100%|██████████████████████████████████████████████████████████████████████████████████| 12/12 [00:09<00:00,  1.32it/s]
D:\app2\stable-diffusion-webui\modules\processing.py:1026: RuntimeWarning: invalid value encountered in cast  1.26it/s]
  x_sample = x_sample.astype(np.uint8)
Total progress: 100%|██████████████████████████████████████████████████████████████████| 32/32 [00:22<00:00,  1.40it/s]
100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:04<00:00,  4.95it/s]
Total progress: 100%|██████████████████████████████████████████████████████████████████| 20/20 [00:04<00:00,  4.85it/s]
100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:03<00:00,  5.04it/s]
tiled upscale: 100%|███████████████████████████████████████████████████████████████████| 15/15 [00:02<00:00,  6.76it/s]
  0%|                                                                                           | 0/12 [00:00<?, ?it/s]D:\app2\stable-diffusion-webui\modules\sd_samplers_common.py:68: RuntimeWarning: invalid value encountered in cast
  x_sample = x_sample.astype(np.uint8)
100%|██████████████████████████████████████████████████████████████████████████████████| 12/12 [00:09<00:00,  1.28it/s]
D:\app2\stable-diffusion-webui\modules\processing.py:1026: RuntimeWarning: invalid value encountered in cast  1.18it/s]
  x_sample = x_sample.astype(np.uint8)
Total progress: 100%|██████████████████████████████████████████████████████████████████| 32/32 [00:22<00:00,  1.40it/s]
100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:03<00:00,  5.02it/s]
tiled upscale: 100%|███████████████████████████████████████████████████████████████████| 15/15 [00:03<00:00,  4.80it/s]
100%|██████████████████████████████████████████████████████████████████████████████████| 12/12 [00:09<00:00,  1.32it/s]
Total progress: 100%|██████████████████████████████████████████████████████████████████| 32/32 [00:22<00:00,  1.42it/s]
Restarting UI...100%|██████████████████████████████████████████████████████████████████| 32/32 [00:22<00:00,  1.25it/s]
Closing server running on port: 7860
2024-05-04 23:03:13,794 - ControlNet - INFO - ControlNet v1.1.446
2024-05-04 23:03:14,265 - ControlNet - INFO - ControlNet UI callback registered.
Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
Startup time: 2.4s (load scripts: 0.9s, create ui: 0.8s, gradio launch: 0.5s).
100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:04<00:00,  4.99it/s]
tiled upscale: 100%|███████████████████████████████████████████████████████████████████| 15/15 [00:03<00:00,  4.82it/s]
  0%|                                                                                           | 0/12 [00:00<?, ?it/s]D:\app2\stable-diffusion-webui\modules\sd_samplers_common.py:68: RuntimeWarning: invalid value encountered in cast
  x_sample = x_sample.astype(np.uint8)
100%|██████████████████████████████████████████████████████████████████████████████████| 12/12 [00:09<00:00,  1.32it/s]
D:\app2\stable-diffusion-webui\modules\processing.py:1026: RuntimeWarning: invalid value encountered in cast  1.28it/s]
  x_sample = x_sample.astype(np.uint8)
Total progress: 100%|██████████████████████████████████████████████████████████████████| 32/32 [00:22<00:00,  1.42it/s]
Total progress: 100%|██████████████████████████████████████████████████████████████████| 32/32 [00:22<00:00,  1.28it/s]

Additional information

  1. There are certain warnings appear in log when hires step is broken after controlnet is disabled :
    D:\app2\stable-diffusion-webui\modules\processing.py:1026: RuntimeWarning: invalid value encountered in cast  1.28it/s]
    x_sample = x_sample.astype(np.uint8)
  2. after hires stage is broken, re-enable controlnet, hires stage works as desired again.
coder168 commented 5 months ago

I did little more tracing, what I found as follows:

To create a public link, set share=True in launch(). Startup time: 24.6s (prepare environment: 4.5s, import torch: 10.6s, import gradio: 1.2s, setup paths: 1.1s, initialize shared: 0.4s, other imports: 0.9s, list SD models: 0.4s, load scripts: 3.9s, create ui: 1.2s, gradio launch: 0.4s). Applying attention optimization: xformers... done. Model loaded in 4.3s (load weights from disk: 0.9s, create model: 1.8s, apply weights to model: 1.0s, calculate empty prompt: 0.5s). 100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:05<00:00, 3.75it/s] tiled upscale: 100%|███████████████████████████████████████████████████████████████████| 15/15 [00:03<00:00, 4.82it/s] 100%|██████████████████████████████████████████████████████████████████████████████████| 10/10 [00:08<00:00, 1.23it/s] Total progress: 100%|██████████████████████████████████████████████████████████████████| 30/30 [00:23<00:00, 1.25it/s] 2024-05-05 23:23:14,109 - ControlNet - INFO - unit_separate = False, style_align = False 30/30 [00:23<00:00, 1.25it/s] 2024-05-05 23:23:14,378 - ControlNet - INFO - Using preprocessor: reference_only 2024-05-05 23:23:14,379 - ControlNet - INFO - preprocessor resolution = 512 2024-05-05 23:23:14,780 - ControlNet - INFO - ControlNet Hooked - Time = 0.6819639205932617 0%| | 0/20 [00:00<?, ?it/s]2024-05-05 23:23:16,929 - ControlNet - INFO - ControlNet used torch.float16 VAE to encode torch.Size([1, 4, 96, 64])./s] 100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:12<00:00, 1.57it/s] tiled upscale: 100%|███████████████████████████████████████████████████████████████████| 15/15 [00:03<00:00, 4.88it/s] 0%| | 0/10 [00:00<?, ?it/s]2024-05-05 23:23:35,053 - ControlNet - INFO - ControlNet used torch.float16 VAE to encode torch.Size([1, 4, 192, 128]). 100%|██████████████████████████████████████████████████████████████████████████████████| 10/10 [00:20<00:00, 2.08s/it] Total progress: 100%|██████████████████████████████████████████████████████████████████| 30/30 [00:43<00:00, 1.46s/it] 100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:04<00:00, 4.01it/s] tiled upscale: 100%|███████████████████████████████████████████████████████████████████| 15/15 [00:03<00:00, 4.87it/s] 0%| | 0/10 [00:01<?, ?it/s] Error completing request Arguments: ('task(kbsgfg8stzpcs7p)', <gradio.routes.Request object at 0x000001FAF2186710>, 'masterpiece, best quality, photography, 8K, HDR,\n1woman, beautiful, white background,', 'BadDream, UnrealisticDream, By bad artist -neg, badhandv4, ng_deepnegative_v1_75t,\nworst quality, low quality, normal quality,', [], 1, 1, 7, 768, 512, True, 0.4, 2, '4x-UltraSharp', 10, 0, 0, 'Use same checkpoint', 'DPM++ 2M', 'Use same scheduler', '', '', [], 0, 20, 'Euler a', 'Automatic', False, '', 0.8, 123456, False, -1, 0, 0, 0, False, 0.0, 4, 0.0, 512, 512, True, 'None', 'None', 0, False, 1, 0, False, 1, UiControlNetUnit(enabled=False, module='reference_only', model='None', weight=1, image={'image': array([[[218, 221, 222], [219, 220, 224], [218, 221, 224], ..., [208, 212, 215], [207, 211, 216], [208, 209, 214]],


[[218, 220, 223], [219, 221, 224], [218, 220, 224], ..., [209, 211, 215], [208, 210, 216], *** [207, 210, 216]],


[[217, 220, 223], [218, 221, 224], [217, 220, 223], ..., [209, 212, 217], [209, 211, 215], *** [209, 210, 214]],


*** ...,


[[198, 200, 204], [196, 199, 203], [196, 200, 203], ..., [197, 203, 208], [196, 202, 207], *** [197, 202, 208]],


[[197, 200, 203], [196, 200, 202], [196, 200, 203], ..., [195, 202, 206], [196, 201, 206], *** [196, 201, 207]],


[[197, 199, 203], [195, 199, 202], [197, 199, 204], ..., [195, 201, 206], [195, 202, 206], [196, 200, 204]]], dtype=uint8), 'mask': array([[[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], *** [0, 0, 0]],


[[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], *** [0, 0, 0]],


[[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], *** [0, 0, 0]],


*** ...,


[[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], *** [0, 0, 0]],


[[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], *** [0, 0, 0]],


[[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]]], dtype=uint8)}, resize_mode='Crop and Resize', low_vram=True, processor_res=512, threshold_a=0.5, threshold_b=0.5, guidance_start=0, guidance_end=1, pixel_perfect=True, control_mode='Balanced', inpaint_crop_input_image=False, hr_option='Both', save_detected_map=True, advanced_weighting=None, effective_region_mask=None, pulid_mode='Fidelity', ipadapter_input=None), UiControlNetUnit(enabled=False, module='none', model='None', weight=1, image=None, resize_mode='Crop and Resize', low_vram=False, processor_res=-1, threshold_a=-1, threshold_b=-1, guidance_start=0, guidance_end=1, pixel_perfect=False, control_mode='Balanced', inpaint_crop_input_image=False, hr_option='Both', save_detected_map=True, advanced_weighting=None, effective_region_mask=None, pulid_mode='Fidelity', ipadapter_input=None), UiControlNetUnit(enabled=False, module='none', model='None', weight=1, image=None, resize_mode='Crop and Resize', low_vram=False, processor_res=-1, threshold_a=-1, threshold_b=-1, guidance_start=0, guidance_end=1, pixel_perfect=False, control_mode='Balanced', inpaint_crop_input_image=False, hr_option='Both', save_detected_map=True, advanced_weighting=None, effective_region_mask=None, pulid_mode='Fidelity', ipadapter_input=None), False, False, 'positive', 'comma', 0, False, False, 'start', '', 1, '', [], 0, '', [], 0, '', [], True, False, False, False, False, False, False, 0, False, None, None, False, None, None, False, None, None, False, 50) {} Traceback (most recent call last): File "D:\app2\stable-diffusion-webui\modules\call_queue.py", line 57, in f res = list(func(args, kwargs)) File "D:\app2\stable-diffusion-webui\modules\call_queue.py", line 36, in f res = func(*args, kwargs) File "D:\app2\stable-diffusion-webui\modules\txt2img.py", line 109, in txt2img processed = processing.process_images(p) File "D:\app2\stable-diffusion-webui\modules\processing.py", line 845, in process_images res = process_images_inner(p) File "D:\app2\stable-diffusion-webui\extensions\sd-webui-controlnet\scripts\batch_hijack.py", line 59, in processing_process_images_hijack return getattr(processing, '__controlnet_original_process_images_inner')(p, *args, kwargs) File "D:\app2\stable-diffusion-webui\modules\processing.py", line 981, in process_images_inner samples_ddim = p.sample(conditioning=p.c, unconditional_conditioning=p.uc, seeds=p.seeds, subseeds=p.subseeds, subseed_strength=p.subseed_strength, prompts=p.prompts) File "D:\app2\stable-diffusion-webui\modules\processing.py", line 1344, in sample return self.sample_hr_pass(samples, decoded_samples, seeds, subseeds, subseed_strength, prompts) File "D:\app2\stable-diffusion-webui\modules\processing.py", line 1429, in sample_hr_pass samples = self.sampler.sample_img2img(self, samples, noise, self.hr_c, self.hr_uc, steps=self.hr_second_pass_steps or self.steps, image_conditioning=image_conditioning) File "D:\app2\stable-diffusion-webui\modules\sd_samplers_kdiffusion.py", line 172, in sample_img2img samples = self.launch_sampling(t_enc + 1, lambda: self.func(self.model_wrap_cfg, xi, extra_args=self.sampler_extra_args, disable=False, callback=self.callback_state, extra_params_kwargs)) File "D:\app2\stable-diffusion-webui\modules\sd_samplers_common.py", line 272, in launch_sampling return func() File "D:\app2\stable-diffusion-webui\modules\sd_samplers_kdiffusion.py", line 172, in samples = self.launch_sampling(t_enc + 1, lambda: self.func(self.model_wrap_cfg, xi, extra_args=self.sampler_extra_args, disable=False, callback=self.callback_state, *extra_params_kwargs)) File "D:\app2\stable-diffusion-webui\venv\lib\site-packages\torch\utils_contextlib.py", line 115, in decorate_context return func(args, kwargs) File "D:\app2\stable-diffusion-webui\repositories\k-diffusion\k_diffusion\sampling.py", line 594, in sample_dpmpp_2m denoised = model(x, sigmas[i] * s_in, extra_args) File "D:\app2\stable-diffusion-webui\venv\lib\site-packages\torch\nn\modules\module.py", line 1518, in _wrapped_call_impl return self._call_impl(*args, *kwargs) File "D:\app2\stable-diffusion-webui\venv\lib\site-packages\torch\nn\modules\module.py", line 1527, in _call_impl return forward_call(args, kwargs) File "D:\app2\stable-diffusion-webui\modules\sd_samplers_cfg_denoiser.py", line 269, in forward devices.test_for_nans(x_out, "unet") File "D:\app2\stable-diffusion-webui\modules\devices.py", line 255, in test_for_nans raise NansException(message) modules.devices.NansException: A tensor with all NaNs was produced in Unet. This could be either because there's not enough precision to represent the picture, or because your video card does not support half type. Try setting the "Upcast cross attention layer to float32" option in Settings > Stable Diffusion or using the --no-half commandline argument to fix this. Use --disable-nan-check commandline argument to disable this check.




- by sending a new request(in a new browser tab) with different settings & prompt using hires step, sometime it works again. When it work again, it works for any prompts including the previous one which did get errors. It seems somehow the new request cleanup certain internal mess-up left by the task with controlnet.
huchenlei commented 5 months ago

Issue confirmed reproduable. The cause is actuall --medvram argument.

If you add --controlnet-loglevel DEBUG, you will observe following code raises exception: https://github.com/Mikubill/sd-webui-controlnet/blob/80bf7481c2491609e969030dd9cab9dc3b894367/scripts/hook.py#L700C1-L707C36

Sample log

024-05-06 16:13:03,251 - ControlNet - INFO - ControlNet used torch.float16 VAE to encode torch.Size([1, 4, 128, 128]).
2024-05-06 16:13:03,631 - ControlNet - DEBUG - register_forward_pre_hook
2024-05-06 16:13:03,631 - ControlNet - DEBUG - DiffusionWrapper.forward() missing 2 required positional arguments: 'x' and 't'
  5%|████████                                                                                                                                                         | 1/20 [00:02<00:47,  2.51s/it]2024-05-06 16:13:04,643 - ControlNet - DEBUG - register_forward_pre_hook█████████████████████████▌                                                                    | 21/40 [00:10<00:18,  1.04it/s] 
2024-05-06 16:13:04,643 - ControlNet - DEBUG - DiffusionWrapper.forward() missing 2 required positional arguments: 'x' and 't'
 10%|████████████████                                                                                                                                                 | 2/20 [00:03<00:28,  1.60s/it]2024-05-06 16:13:05,613 - ControlNet - DEBUG - register_forward_pre_hook█████████████████████████████▏                                                                | 22/40 [00:11<00:17,  1.04it/s] 
2024-05-06 16:13:05,613 - ControlNet - DEBUG - DiffusionWrapper.forward() missing 2 required positional arguments: 'x' and 't'
 15%|████████████████████████▏                                                                                        
coder168 commented 5 months ago

Thanks for @huchenlei update. This issue is "medvram" related.

I remove "medvram" from startup script and re-run the same tests as mentioned. The problem is gone and webui & controlnet works fine as expected without the issue mentioned.

It seems when handling "medvram", controlnet hooking mechanism make certain unexpected effects. I am curious why such effect still works even after controlnet has been disabled. In theory, each request should be handled independently, webui should be able to handle multiple requests with different settings from different users without compromise.