Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
[Bug]: TypeError: 'NoneType' object is not subscriptable when using img2img alternative test
### Is there an existing issue for this?

- [X] I have searched the existing issues and checked the recent builds/commits

### What happened?

Webui fails to generate an image if using the img2img alternative test since the runway inpainting support update.
Unrelated to this, it does not support the unlimited tokens either.

### Steps to reproduce the problem

1. Go to imgimg
2. Choose img2img alternative test
3. Upload image and write prompt, parameters, settings, etc.
4. Generate.


### What should have happened?

An image should generate.

### Commit where the problem happens

bf30673f5132c8f28357b31224c54331e788d3e7

### What platforms do you use to access UI ?

Windows

### What browsers do you use to access the UI ?

Google Chrome

### Command Line Arguments

```Shell
--deepdanbooru --xformers --gradio-img2img-tool color-sketch
```


### Additional information, context and logs

Traceback (most recent call last):
  File "G:\stable-webui\modules\ui.py", line 212, in f
    res = list(func(*args, **kwargs))
  File "G:\stable-webui\webui.py", line 63, in f
    res = func(*args, **kwargs)
  File "G:\stable-webui\modules\img2img.py", line 124, in img2img
    processed = modules.scripts.scripts_img2img.run(p, *args)
  File "G:\stable-webui\modules\scripts.py", line 173, in run
    processed = script.run(p, *script_args)
  File "G:\stable-webui\scripts\img2imgalt.py", line 208, in run
    processed = processing.process_images(p)
  File "G:\stable-webui\modules\processing.py", line 411, in process_images
    samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength)
  File "G:\stable-webui\scripts\img2imgalt.py", line 197, in sample_extra
    return sampler.sample_img2img(p, p.init_latent, noise_dt, conditioning, unconditional_conditioning)
  File "G:\stable-webui\modules\sd_samplers.py", line 423, in sample_img2img
    samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, xi, extra_args={
  File "G:\stable-webui\modules\sd_samplers.py", line 356, in launch_sampling
    return func()
  File "G:\stable-webui\modules\sd_samplers.py", line 423, in <lambda>
    samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, xi, extra_args={
  File "G:\stable-webui\venv\lib\site-packages\torch\autograd\grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "G:\stable-webui\repositories\k-diffusion\k_diffusion\sampling.py", line 64, in sample_euler
    denoised = model(x, sigma_hat * s_in, **extra_args)
  File "G:\stable-webui\venv\lib\site-packages\torch\nn\modules\module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "G:\stable-webui\modules\sd_samplers.py", line 269, in forward
    image_cond_in = torch.cat([torch.stack([image_cond[i] for _ in range(n)]) for i, n in enumerate(repeats)] + [image_cond])
  File "G:\stable-webui\modules\sd_samplers.py", line 269, in <listcomp>
    image_cond_in = torch.cat([torch.stack([image_cond[i] for _ in range(n)]) for i, n in enumerate(repeats)] + [image_cond])
  File "G:\stable-webui\modules\sd_samplers.py", line 269, in <listcomp>
    image_cond_in = torch.cat([torch.stack([image_cond[i] for _ in range(n)]) for i, n in enumerate(repeats)] + [image_cond])
TypeError: 'NoneType' object is not subscriptable


Discussion:
Error is caused the line below not handling image_cond being uninitialised.
https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/f49c08ea566385db339c6628f65c3a121033f67c/modules/sd_samplers.py#L269

Putting an IF check fixes the issue for the img2imgalt script but I'm not sure what this line is meant to do which is a question for @random-thoughtss 
```
        image_cond_in = None
        if image_cond is not None:
            image_cond_in = torch.cat([torch.stack([image_cond[i] for _ in range(n)]) for i, n in enumerate(repeats)] + [image_cond])
```
Setting it to `None` would crash the in-painting model and would break if there is any batch size or large prompt.

This is the diff for the fix. Just need to add image conditioning to the img2imgalt script since it bypasses the regular img2img sampling call.

[https://gist.github.com/random-thoughtss/f3ba564f4b7840136107763bdad26541](https://gist.github.com/random-thoughtss/f3ba564f4b7840136107763bdad26541)

Resolving patch:
diff --git a/scripts/img2imgalt.py b/scripts/img2imgalt.py
--- a/scripts/img2imgalt.py
+++ b/scripts/img2imgalt.py
@@ -34,6 +34,9 @@ def find_noise_for_image(p, cond, uncond, cfg_scale, steps):
         sigma_in = torch.cat([sigmas[i] * s_in] * 2)
         cond_in = torch.cat([uncond, cond])
 
+        image_conditioning = torch.cat([p.image_conditioning] * 2)
+        cond_in = {"c_concat": [image_conditioning], "c_crossattn": [cond_in]}
+
         c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)]
         t = dnw.sigma_to_t(sigma_in)
 
@@ -78,6 +81,9 @@ def find_noise_for_image_sigma_adjustment(p, cond, uncond, cfg_scale, steps):
         sigma_in = torch.cat([sigmas[i - 1] * s_in] * 2)
         cond_in = torch.cat([uncond, cond])
 
+        image_conditioning = torch.cat([p.image_conditioning] * 2)
+        cond_in = {"c_concat": [image_conditioning], "c_crossattn": [cond_in]}
+
         c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)]
 
         if i == 1:
@@ -194,7 +200,7 @@ def sample_extra(conditioning, unconditional_conditioning, seeds, subseeds, subs
             
             p.seed = p.seed + 1
             
-            return sampler.sample_img2img(p, p.init_latent, noise_dt, conditioning, unconditional_conditioning)
+            return sampler.sample_img2img(p, p.init_latent, noise_dt, conditioning, unconditional_conditioning, image_conditioning=p.image_conditioning)
 
         p.sample = sample_extra
 


Test patch: