We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `scripts/img2imgalt.py`
Content:
```
1 from collections import namedtuple
2 
3 import numpy as np
4 from tqdm import trange
5 
6 import modules.scripts as scripts
7 import gradio as gr
8 
9 from modules import processing, shared, sd_samplers, prompt_parser
10 from modules.processing import Processed
11 from modules.shared import opts, cmd_opts, state
12 
13 import torch
14 import k_diffusion as K
15 
16 from PIL import Image
17 from torch import autocast
18 from einops import rearrange, repeat
19 
20 
21 def find_noise_for_image(p, cond, uncond, cfg_scale, steps):
22     x = p.init_latent
23 
24     s_in = x.new_ones([x.shape[0]])
25     dnw = K.external.CompVisDenoiser(shared.sd_model)
26     sigmas = dnw.get_sigmas(steps).flip(0)
27 
28     shared.state.sampling_steps = steps
29 
30     for i in trange(1, len(sigmas)):
31         shared.state.sampling_step += 1
32 
33         x_in = torch.cat([x] * 2)
34         sigma_in = torch.cat([sigmas[i] * s_in] * 2)
35         cond_in = torch.cat([uncond, cond])
36 
37         c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)]
38         t = dnw.sigma_to_t(sigma_in)
39 
40         eps = shared.sd_model.apply_model(x_in * c_in, t, cond=cond_in)
41         denoised_uncond, denoised_cond = (x_in + eps * c_out).chunk(2)
42 
43         denoised = denoised_uncond + (denoised_cond - denoised_uncond) * cfg_scale
44 
45         d = (x - denoised) / sigmas[i]
46         dt = sigmas[i] - sigmas[i - 1]
47 
48         x = x + d * dt
49 
50         sd_samplers.store_latent(x)
51 
52         # This shouldn't be necessary, but solved some VRAM issues
53         del x_in, sigma_in, cond_in, c_out, c_in, t,
54         del eps, denoised_uncond, denoised_cond, denoised, d, dt
55 
56     shared.state.nextjob()
57 
58     return x / x.std()
59 
60 
61 Cached = namedtuple("Cached", ["noise", "cfg_scale", "steps", "latent", "original_prompt", "original_negative_prompt", "sigma_adjustment"])
62 
63 
64 # Based on changes suggested by briansemrau in https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/736
65 def find_noise_for_image_sigma_adjustment(p, cond, uncond, cfg_scale, steps):
66     x = p.init_latent
67 
68     s_in = x.new_ones([x.shape[0]])
69     dnw = K.external.CompVisDenoiser(shared.sd_model)
70     sigmas = dnw.get_sigmas(steps).flip(0)
71 
72     shared.state.sampling_steps = steps
73 
74     for i in trange(1, len(sigmas)):
75         shared.state.sampling_step += 1
76 
77         x_in = torch.cat([x] * 2)
78         sigma_in = torch.cat([sigmas[i - 1] * s_in] * 2)
79         cond_in = torch.cat([uncond, cond])
80 
81         c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)]
82 
83         if i == 1:
84             t = dnw.sigma_to_t(torch.cat([sigmas[i] * s_in] * 2))
85         else:
86             t = dnw.sigma_to_t(sigma_in)
87 
88         eps = shared.sd_model.apply_model(x_in * c_in, t, cond=cond_in)
89         denoised_uncond, denoised_cond = (x_in + eps * c_out).chunk(2)
90 
91         denoised = denoised_uncond + (denoised_cond - denoised_uncond) * cfg_scale
92 
93         if i == 1:
94             d = (x - denoised) / (2 * sigmas[i])
95         else:
96             d = (x - denoised) / sigmas[i - 1]
97 
98         dt = sigmas[i] - sigmas[i - 1]
99         x = x + d * dt
100 
101         sd_samplers.store_latent(x)
102 
103         # This shouldn't be necessary, but solved some VRAM issues
104         del x_in, sigma_in, cond_in, c_out, c_in, t,
105         del eps, denoised_uncond, denoised_cond, denoised, d, dt
106 
107     shared.state.nextjob()
108 
109     return x / sigmas[-1]
110 
111 
112 class Script(scripts.Script):
113     def __init__(self):
114         self.cache = None
115 
116     def title(self):
117         return "img2img alternative test"
118 
119     def show(self, is_img2img):
120         return is_img2img
121 
122     def ui(self, is_img2img):
123         info = gr.Markdown('''
124         * `CFG Scale` should be 2 or lower.
125         ''')
126 
127         override_sampler = gr.Checkbox(label="Override `Sampling method` to Euler?(this method is built for it)", value=True)
128 
129         override_prompt = gr.Checkbox(label="Override `prompt` to the same value as `original prompt`?(and `negative prompt`)", value=True)
130         original_prompt = gr.Textbox(label="Original prompt", lines=1)
131         original_negative_prompt = gr.Textbox(label="Original negative prompt", lines=1)
132 
133         override_steps = gr.Checkbox(label="Override `Sampling Steps` to the same value as `Decode steps`?", value=True)
134         st = gr.Slider(label="Decode steps", minimum=1, maximum=150, step=1, value=50)
135 
136         override_strength = gr.Checkbox(label="Override `Denoising strength` to 1?", value=True)
137 
138         cfg = gr.Slider(label="Decode CFG scale", minimum=0.0, maximum=15.0, step=0.1, value=1.0)
139         randomness = gr.Slider(label="Randomness", minimum=0.0, maximum=1.0, step=0.01, value=0.0)
140         sigma_adjustment = gr.Checkbox(label="Sigma adjustment for finding noise for image", value=False)
141 
142         return [
143             info, 
144             override_sampler,
145             override_prompt, original_prompt, original_negative_prompt, 
146             override_steps, st,
147             override_strength,
148             cfg, randomness, sigma_adjustment,
149         ]
150 
151     def run(self, p, _, override_sampler, override_prompt, original_prompt, original_negative_prompt, override_steps, st, override_strength, cfg, randomness, sigma_adjustment):
152         # Override
153         if override_sampler:
154             p.sampler_index = [sampler.name for sampler in sd_samplers.samplers].index("Euler")
155         if override_prompt:
156             p.prompt = original_prompt
157             p.negative_prompt = original_negative_prompt
158         if override_steps:
159             p.steps = st
160         if override_strength:
161             p.denoising_strength = 1.0
162 
163 
164         def sample_extra(conditioning, unconditional_conditioning, seeds, subseeds, subseed_strength):
165             lat = (p.init_latent.cpu().numpy() * 10).astype(int)
166 
167             same_params = self.cache is not None and self.cache.cfg_scale == cfg and self.cache.steps == st \
168                                 and self.cache.original_prompt == original_prompt \
169                                 and self.cache.original_negative_prompt == original_negative_prompt \
170                                 and self.cache.sigma_adjustment == sigma_adjustment
171             same_everything = same_params and self.cache.latent.shape == lat.shape and np.abs(self.cache.latent-lat).sum() < 100
172 
173             if same_everything:
174                 rec_noise = self.cache.noise
175             else:
176                 shared.state.job_count += 1
177                 cond = p.sd_model.get_learned_conditioning(p.batch_size * [original_prompt])
178                 uncond = p.sd_model.get_learned_conditioning(p.batch_size * [original_negative_prompt])
179                 if sigma_adjustment:
180                     rec_noise = find_noise_for_image_sigma_adjustment(p, cond, uncond, cfg, st)
181                 else:
182                     rec_noise = find_noise_for_image(p, cond, uncond, cfg, st)
183                 self.cache = Cached(rec_noise, cfg, st, lat, original_prompt, original_negative_prompt, sigma_adjustment)
184 
185             rand_noise = processing.create_random_tensors(p.init_latent.shape[1:], seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, seed_resize_from_h=p.seed_resize_from_h, seed_resize_from_w=p.seed_resize_from_w, p=p)
186             
187             combined_noise = ((1 - randomness) * rec_noise + randomness * rand_noise) / ((randomness**2 + (1-randomness)**2) ** 0.5)
188             
189             sampler = sd_samplers.create_sampler_with_index(sd_samplers.samplers, p.sampler_index, p.sd_model)
190 
191             sigmas = sampler.model_wrap.get_sigmas(p.steps)
192             
193             noise_dt = combined_noise - (p.init_latent / sigmas[0])
194             
195             p.seed = p.seed + 1
196             
197             return sampler.sample_img2img(p, p.init_latent, noise_dt, conditioning, unconditional_conditioning)
198 
199         p.sample = sample_extra
200 
201         p.extra_generation_params["Decode prompt"] = original_prompt
202         p.extra_generation_params["Decode negative prompt"] = original_negative_prompt
203         p.extra_generation_params["Decode CFG scale"] = cfg
204         p.extra_generation_params["Decode steps"] = st
205         p.extra_generation_params["Randomness"] = randomness
206         p.extra_generation_params["Sigma Adjustment"] = sigma_adjustment
207 
208         processed = processing.process_images(p)
209 
210         return processed
211 
212
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/scripts/img2imgalt.py b/scripts/img2imgalt.py
--- a/scripts/img2imgalt.py
+++ b/scripts/img2imgalt.py
@@ -34,6 +34,9 @@
         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 @@
         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 @@
             
             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

{"golden_diff": "diff --git a/scripts/img2imgalt.py b/scripts/img2imgalt.py\n--- a/scripts/img2imgalt.py\n+++ b/scripts/img2imgalt.py\n@@ -34,6 +34,9 @@\n         sigma_in = torch.cat([sigmas[i] * s_in] * 2)\r\n         cond_in = torch.cat([uncond, cond])\r\n \r\n+        image_conditioning = torch.cat([p.image_conditioning] * 2)\r\n+        cond_in = {\"c_concat\": [image_conditioning], \"c_crossattn\": [cond_in]}\r\n+\r\n         c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)]\r\n         t = dnw.sigma_to_t(sigma_in)\r\n \r\n@@ -78,6 +81,9 @@\n         sigma_in = torch.cat([sigmas[i - 1] * s_in] * 2)\r\n         cond_in = torch.cat([uncond, cond])\r\n \r\n+        image_conditioning = torch.cat([p.image_conditioning] * 2)\r\n+        cond_in = {\"c_concat\": [image_conditioning], \"c_crossattn\": [cond_in]}\r\n+\r\n         c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)]\r\n \r\n         if i == 1:\r\n@@ -194,7 +200,7 @@\n             \r\n             p.seed = p.seed + 1\r\n             \r\n-            return sampler.sample_img2img(p, p.init_latent, noise_dt, conditioning, unconditional_conditioning)\r\n+            return sampler.sample_img2img(p, p.init_latent, noise_dt, conditioning, unconditional_conditioning, image_conditioning=p.image_conditioning)\r\n \r\n         p.sample = sample_extra\n", "issue": "[Bug]: TypeError: 'NoneType' object is not subscriptable when using img2img alternative test\n### Is there an existing issue for this?\n\n- [X] I have searched the existing issues and checked the recent builds/commits\n\n### What happened?\n\nWebui fails to generate an image if using the img2img alternative test since the runway inpainting support update.\r\nUnrelated to this, it does not support the unlimited tokens either.\n\n### Steps to reproduce the problem\n\n1. Go to imgimg\r\n2. Choose img2img alternative test\r\n3. Upload image and write prompt, parameters, settings, etc.\r\n4. Generate.\r\n\n\n### What should have happened?\n\nAn image should generate.\n\n### Commit where the problem happens\n\nbf30673f5132c8f28357b31224c54331e788d3e7\n\n### What platforms do you use to access UI ?\n\nWindows\n\n### What browsers do you use to access the UI ?\n\nGoogle Chrome\n\n### Command Line Arguments\n\n```Shell\n--deepdanbooru --xformers --gradio-img2img-tool color-sketch\n```\n\n\n### Additional information, context and logs\n\nTraceback (most recent call last):\r\n  File \"G:\\stable-webui\\modules\\ui.py\", line 212, in f\r\n    res = list(func(*args, **kwargs))\r\n  File \"G:\\stable-webui\\webui.py\", line 63, in f\r\n    res = func(*args, **kwargs)\r\n  File \"G:\\stable-webui\\modules\\img2img.py\", line 124, in img2img\r\n    processed = modules.scripts.scripts_img2img.run(p, *args)\r\n  File \"G:\\stable-webui\\modules\\scripts.py\", line 173, in run\r\n    processed = script.run(p, *script_args)\r\n  File \"G:\\stable-webui\\scripts\\img2imgalt.py\", line 208, in run\r\n    processed = processing.process_images(p)\r\n  File \"G:\\stable-webui\\modules\\processing.py\", line 411, in process_images\r\n    samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength)\r\n  File \"G:\\stable-webui\\scripts\\img2imgalt.py\", line 197, in sample_extra\r\n    return sampler.sample_img2img(p, p.init_latent, noise_dt, conditioning, unconditional_conditioning)\r\n  File \"G:\\stable-webui\\modules\\sd_samplers.py\", line 423, in sample_img2img\r\n    samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, xi, extra_args={\r\n  File \"G:\\stable-webui\\modules\\sd_samplers.py\", line 356, in launch_sampling\r\n    return func()\r\n  File \"G:\\stable-webui\\modules\\sd_samplers.py\", line 423, in <lambda>\r\n    samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, xi, extra_args={\r\n  File \"G:\\stable-webui\\venv\\lib\\site-packages\\torch\\autograd\\grad_mode.py\", line 27, in decorate_context\r\n    return func(*args, **kwargs)\r\n  File \"G:\\stable-webui\\repositories\\k-diffusion\\k_diffusion\\sampling.py\", line 64, in sample_euler\r\n    denoised = model(x, sigma_hat * s_in, **extra_args)\r\n  File \"G:\\stable-webui\\venv\\lib\\site-packages\\torch\\nn\\modules\\module.py\", line 1130, in _call_impl\r\n    return forward_call(*input, **kwargs)\r\n  File \"G:\\stable-webui\\modules\\sd_samplers.py\", line 269, in forward\r\n    image_cond_in = torch.cat([torch.stack([image_cond[i] for _ in range(n)]) for i, n in enumerate(repeats)] + [image_cond])\r\n  File \"G:\\stable-webui\\modules\\sd_samplers.py\", line 269, in <listcomp>\r\n    image_cond_in = torch.cat([torch.stack([image_cond[i] for _ in range(n)]) for i, n in enumerate(repeats)] + [image_cond])\r\n  File \"G:\\stable-webui\\modules\\sd_samplers.py\", line 269, in <listcomp>\r\n    image_cond_in = torch.cat([torch.stack([image_cond[i] for _ in range(n)]) for i, n in enumerate(repeats)] + [image_cond])\r\nTypeError: 'NoneType' object is not subscriptable\n", "before_files": [{"content": "from collections import namedtuple\r\n\r\nimport numpy as np\r\nfrom tqdm import trange\r\n\r\nimport modules.scripts as scripts\r\nimport gradio as gr\r\n\r\nfrom modules import processing, shared, sd_samplers, prompt_parser\r\nfrom modules.processing import Processed\r\nfrom modules.shared import opts, cmd_opts, state\r\n\r\nimport torch\r\nimport k_diffusion as K\r\n\r\nfrom PIL import Image\r\nfrom torch import autocast\r\nfrom einops import rearrange, repeat\r\n\r\n\r\ndef find_noise_for_image(p, cond, uncond, cfg_scale, steps):\r\n    x = p.init_latent\r\n\r\n    s_in = x.new_ones([x.shape[0]])\r\n    dnw = K.external.CompVisDenoiser(shared.sd_model)\r\n    sigmas = dnw.get_sigmas(steps).flip(0)\r\n\r\n    shared.state.sampling_steps = steps\r\n\r\n    for i in trange(1, len(sigmas)):\r\n        shared.state.sampling_step += 1\r\n\r\n        x_in = torch.cat([x] * 2)\r\n        sigma_in = torch.cat([sigmas[i] * s_in] * 2)\r\n        cond_in = torch.cat([uncond, cond])\r\n\r\n        c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)]\r\n        t = dnw.sigma_to_t(sigma_in)\r\n\r\n        eps = shared.sd_model.apply_model(x_in * c_in, t, cond=cond_in)\r\n        denoised_uncond, denoised_cond = (x_in + eps * c_out).chunk(2)\r\n\r\n        denoised = denoised_uncond + (denoised_cond - denoised_uncond) * cfg_scale\r\n\r\n        d = (x - denoised) / sigmas[i]\r\n        dt = sigmas[i] - sigmas[i - 1]\r\n\r\n        x = x + d * dt\r\n\r\n        sd_samplers.store_latent(x)\r\n\r\n        # This shouldn't be necessary, but solved some VRAM issues\r\n        del x_in, sigma_in, cond_in, c_out, c_in, t,\r\n        del eps, denoised_uncond, denoised_cond, denoised, d, dt\r\n\r\n    shared.state.nextjob()\r\n\r\n    return x / x.std()\r\n\r\n\r\nCached = namedtuple(\"Cached\", [\"noise\", \"cfg_scale\", \"steps\", \"latent\", \"original_prompt\", \"original_negative_prompt\", \"sigma_adjustment\"])\r\n\r\n\r\n# Based on changes suggested by briansemrau in https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/736\r\ndef find_noise_for_image_sigma_adjustment(p, cond, uncond, cfg_scale, steps):\r\n    x = p.init_latent\r\n\r\n    s_in = x.new_ones([x.shape[0]])\r\n    dnw = K.external.CompVisDenoiser(shared.sd_model)\r\n    sigmas = dnw.get_sigmas(steps).flip(0)\r\n\r\n    shared.state.sampling_steps = steps\r\n\r\n    for i in trange(1, len(sigmas)):\r\n        shared.state.sampling_step += 1\r\n\r\n        x_in = torch.cat([x] * 2)\r\n        sigma_in = torch.cat([sigmas[i - 1] * s_in] * 2)\r\n        cond_in = torch.cat([uncond, cond])\r\n\r\n        c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)]\r\n\r\n        if i == 1:\r\n            t = dnw.sigma_to_t(torch.cat([sigmas[i] * s_in] * 2))\r\n        else:\r\n            t = dnw.sigma_to_t(sigma_in)\r\n\r\n        eps = shared.sd_model.apply_model(x_in * c_in, t, cond=cond_in)\r\n        denoised_uncond, denoised_cond = (x_in + eps * c_out).chunk(2)\r\n\r\n        denoised = denoised_uncond + (denoised_cond - denoised_uncond) * cfg_scale\r\n\r\n        if i == 1:\r\n            d = (x - denoised) / (2 * sigmas[i])\r\n        else:\r\n            d = (x - denoised) / sigmas[i - 1]\r\n\r\n        dt = sigmas[i] - sigmas[i - 1]\r\n        x = x + d * dt\r\n\r\n        sd_samplers.store_latent(x)\r\n\r\n        # This shouldn't be necessary, but solved some VRAM issues\r\n        del x_in, sigma_in, cond_in, c_out, c_in, t,\r\n        del eps, denoised_uncond, denoised_cond, denoised, d, dt\r\n\r\n    shared.state.nextjob()\r\n\r\n    return x / sigmas[-1]\r\n\r\n\r\nclass Script(scripts.Script):\r\n    def __init__(self):\r\n        self.cache = None\r\n\r\n    def title(self):\r\n        return \"img2img alternative test\"\r\n\r\n    def show(self, is_img2img):\r\n        return is_img2img\r\n\r\n    def ui(self, is_img2img):\r\n        info = gr.Markdown('''\r\n        * `CFG Scale` should be 2 or lower.\r\n        ''')\r\n\r\n        override_sampler = gr.Checkbox(label=\"Override `Sampling method` to Euler?(this method is built for it)\", value=True)\r\n\r\n        override_prompt = gr.Checkbox(label=\"Override `prompt` to the same value as `original prompt`?(and `negative prompt`)\", value=True)\r\n        original_prompt = gr.Textbox(label=\"Original prompt\", lines=1)\r\n        original_negative_prompt = gr.Textbox(label=\"Original negative prompt\", lines=1)\r\n\r\n        override_steps = gr.Checkbox(label=\"Override `Sampling Steps` to the same value as `Decode steps`?\", value=True)\r\n        st = gr.Slider(label=\"Decode steps\", minimum=1, maximum=150, step=1, value=50)\r\n\r\n        override_strength = gr.Checkbox(label=\"Override `Denoising strength` to 1?\", value=True)\r\n\r\n        cfg = gr.Slider(label=\"Decode CFG scale\", minimum=0.0, maximum=15.0, step=0.1, value=1.0)\r\n        randomness = gr.Slider(label=\"Randomness\", minimum=0.0, maximum=1.0, step=0.01, value=0.0)\r\n        sigma_adjustment = gr.Checkbox(label=\"Sigma adjustment for finding noise for image\", value=False)\r\n\r\n        return [\r\n            info, \r\n            override_sampler,\r\n            override_prompt, original_prompt, original_negative_prompt, \r\n            override_steps, st,\r\n            override_strength,\r\n            cfg, randomness, sigma_adjustment,\r\n        ]\r\n\r\n    def run(self, p, _, override_sampler, override_prompt, original_prompt, original_negative_prompt, override_steps, st, override_strength, cfg, randomness, sigma_adjustment):\r\n        # Override\r\n        if override_sampler:\r\n            p.sampler_index = [sampler.name for sampler in sd_samplers.samplers].index(\"Euler\")\r\n        if override_prompt:\r\n            p.prompt = original_prompt\r\n            p.negative_prompt = original_negative_prompt\r\n        if override_steps:\r\n            p.steps = st\r\n        if override_strength:\r\n            p.denoising_strength = 1.0\r\n\r\n\r\n        def sample_extra(conditioning, unconditional_conditioning, seeds, subseeds, subseed_strength):\r\n            lat = (p.init_latent.cpu().numpy() * 10).astype(int)\r\n\r\n            same_params = self.cache is not None and self.cache.cfg_scale == cfg and self.cache.steps == st \\\r\n                                and self.cache.original_prompt == original_prompt \\\r\n                                and self.cache.original_negative_prompt == original_negative_prompt \\\r\n                                and self.cache.sigma_adjustment == sigma_adjustment\r\n            same_everything = same_params and self.cache.latent.shape == lat.shape and np.abs(self.cache.latent-lat).sum() < 100\r\n\r\n            if same_everything:\r\n                rec_noise = self.cache.noise\r\n            else:\r\n                shared.state.job_count += 1\r\n                cond = p.sd_model.get_learned_conditioning(p.batch_size * [original_prompt])\r\n                uncond = p.sd_model.get_learned_conditioning(p.batch_size * [original_negative_prompt])\r\n                if sigma_adjustment:\r\n                    rec_noise = find_noise_for_image_sigma_adjustment(p, cond, uncond, cfg, st)\r\n                else:\r\n                    rec_noise = find_noise_for_image(p, cond, uncond, cfg, st)\r\n                self.cache = Cached(rec_noise, cfg, st, lat, original_prompt, original_negative_prompt, sigma_adjustment)\r\n\r\n            rand_noise = processing.create_random_tensors(p.init_latent.shape[1:], seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, seed_resize_from_h=p.seed_resize_from_h, seed_resize_from_w=p.seed_resize_from_w, p=p)\r\n            \r\n            combined_noise = ((1 - randomness) * rec_noise + randomness * rand_noise) / ((randomness**2 + (1-randomness)**2) ** 0.5)\r\n            \r\n            sampler = sd_samplers.create_sampler_with_index(sd_samplers.samplers, p.sampler_index, p.sd_model)\r\n\r\n            sigmas = sampler.model_wrap.get_sigmas(p.steps)\r\n            \r\n            noise_dt = combined_noise - (p.init_latent / sigmas[0])\r\n            \r\n            p.seed = p.seed + 1\r\n            \r\n            return sampler.sample_img2img(p, p.init_latent, noise_dt, conditioning, unconditional_conditioning)\r\n\r\n        p.sample = sample_extra\r\n\r\n        p.extra_generation_params[\"Decode prompt\"] = original_prompt\r\n        p.extra_generation_params[\"Decode negative prompt\"] = original_negative_prompt\r\n        p.extra_generation_params[\"Decode CFG scale\"] = cfg\r\n        p.extra_generation_params[\"Decode steps\"] = st\r\n        p.extra_generation_params[\"Randomness\"] = randomness\r\n        p.extra_generation_params[\"Sigma Adjustment\"] = sigma_adjustment\r\n\r\n        processed = processing.process_images(p)\r\n\r\n        return processed\r\n\r\n", "path": "scripts/img2imgalt.py"}], "after_files": [{"content": "from collections import namedtuple\r\n\r\nimport numpy as np\r\nfrom tqdm import trange\r\n\r\nimport modules.scripts as scripts\r\nimport gradio as gr\r\n\r\nfrom modules import processing, shared, sd_samplers, prompt_parser\r\nfrom modules.processing import Processed\r\nfrom modules.shared import opts, cmd_opts, state\r\n\r\nimport torch\r\nimport k_diffusion as K\r\n\r\nfrom PIL import Image\r\nfrom torch import autocast\r\nfrom einops import rearrange, repeat\r\n\r\n\r\ndef find_noise_for_image(p, cond, uncond, cfg_scale, steps):\r\n    x = p.init_latent\r\n\r\n    s_in = x.new_ones([x.shape[0]])\r\n    dnw = K.external.CompVisDenoiser(shared.sd_model)\r\n    sigmas = dnw.get_sigmas(steps).flip(0)\r\n\r\n    shared.state.sampling_steps = steps\r\n\r\n    for i in trange(1, len(sigmas)):\r\n        shared.state.sampling_step += 1\r\n\r\n        x_in = torch.cat([x] * 2)\r\n        sigma_in = torch.cat([sigmas[i] * s_in] * 2)\r\n        cond_in = torch.cat([uncond, cond])\r\n\r\n        image_conditioning = torch.cat([p.image_conditioning] * 2)\r\n        cond_in = {\"c_concat\": [image_conditioning], \"c_crossattn\": [cond_in]}\r\n\r\n        c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)]\r\n        t = dnw.sigma_to_t(sigma_in)\r\n\r\n        eps = shared.sd_model.apply_model(x_in * c_in, t, cond=cond_in)\r\n        denoised_uncond, denoised_cond = (x_in + eps * c_out).chunk(2)\r\n\r\n        denoised = denoised_uncond + (denoised_cond - denoised_uncond) * cfg_scale\r\n\r\n        d = (x - denoised) / sigmas[i]\r\n        dt = sigmas[i] - sigmas[i - 1]\r\n\r\n        x = x + d * dt\r\n\r\n        sd_samplers.store_latent(x)\r\n\r\n        # This shouldn't be necessary, but solved some VRAM issues\r\n        del x_in, sigma_in, cond_in, c_out, c_in, t,\r\n        del eps, denoised_uncond, denoised_cond, denoised, d, dt\r\n\r\n    shared.state.nextjob()\r\n\r\n    return x / x.std()\r\n\r\n\r\nCached = namedtuple(\"Cached\", [\"noise\", \"cfg_scale\", \"steps\", \"latent\", \"original_prompt\", \"original_negative_prompt\", \"sigma_adjustment\"])\r\n\r\n\r\n# Based on changes suggested by briansemrau in https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/736\r\ndef find_noise_for_image_sigma_adjustment(p, cond, uncond, cfg_scale, steps):\r\n    x = p.init_latent\r\n\r\n    s_in = x.new_ones([x.shape[0]])\r\n    dnw = K.external.CompVisDenoiser(shared.sd_model)\r\n    sigmas = dnw.get_sigmas(steps).flip(0)\r\n\r\n    shared.state.sampling_steps = steps\r\n\r\n    for i in trange(1, len(sigmas)):\r\n        shared.state.sampling_step += 1\r\n\r\n        x_in = torch.cat([x] * 2)\r\n        sigma_in = torch.cat([sigmas[i - 1] * s_in] * 2)\r\n        cond_in = torch.cat([uncond, cond])\r\n\r\n        image_conditioning = torch.cat([p.image_conditioning] * 2)\r\n        cond_in = {\"c_concat\": [image_conditioning], \"c_crossattn\": [cond_in]}\r\n\r\n        c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)]\r\n\r\n        if i == 1:\r\n            t = dnw.sigma_to_t(torch.cat([sigmas[i] * s_in] * 2))\r\n        else:\r\n            t = dnw.sigma_to_t(sigma_in)\r\n\r\n        eps = shared.sd_model.apply_model(x_in * c_in, t, cond=cond_in)\r\n        denoised_uncond, denoised_cond = (x_in + eps * c_out).chunk(2)\r\n\r\n        denoised = denoised_uncond + (denoised_cond - denoised_uncond) * cfg_scale\r\n\r\n        if i == 1:\r\n            d = (x - denoised) / (2 * sigmas[i])\r\n        else:\r\n            d = (x - denoised) / sigmas[i - 1]\r\n\r\n        dt = sigmas[i] - sigmas[i - 1]\r\n        x = x + d * dt\r\n\r\n        sd_samplers.store_latent(x)\r\n\r\n        # This shouldn't be necessary, but solved some VRAM issues\r\n        del x_in, sigma_in, cond_in, c_out, c_in, t,\r\n        del eps, denoised_uncond, denoised_cond, denoised, d, dt\r\n\r\n    shared.state.nextjob()\r\n\r\n    return x / sigmas[-1]\r\n\r\n\r\nclass Script(scripts.Script):\r\n    def __init__(self):\r\n        self.cache = None\r\n\r\n    def title(self):\r\n        return \"img2img alternative test\"\r\n\r\n    def show(self, is_img2img):\r\n        return is_img2img\r\n\r\n    def ui(self, is_img2img):\r\n        info = gr.Markdown('''\r\n        * `CFG Scale` should be 2 or lower.\r\n        ''')\r\n\r\n        override_sampler = gr.Checkbox(label=\"Override `Sampling method` to Euler?(this method is built for it)\", value=True)\r\n\r\n        override_prompt = gr.Checkbox(label=\"Override `prompt` to the same value as `original prompt`?(and `negative prompt`)\", value=True)\r\n        original_prompt = gr.Textbox(label=\"Original prompt\", lines=1)\r\n        original_negative_prompt = gr.Textbox(label=\"Original negative prompt\", lines=1)\r\n\r\n        override_steps = gr.Checkbox(label=\"Override `Sampling Steps` to the same value as `Decode steps`?\", value=True)\r\n        st = gr.Slider(label=\"Decode steps\", minimum=1, maximum=150, step=1, value=50)\r\n\r\n        override_strength = gr.Checkbox(label=\"Override `Denoising strength` to 1?\", value=True)\r\n\r\n        cfg = gr.Slider(label=\"Decode CFG scale\", minimum=0.0, maximum=15.0, step=0.1, value=1.0)\r\n        randomness = gr.Slider(label=\"Randomness\", minimum=0.0, maximum=1.0, step=0.01, value=0.0)\r\n        sigma_adjustment = gr.Checkbox(label=\"Sigma adjustment for finding noise for image\", value=False)\r\n\r\n        return [\r\n            info, \r\n            override_sampler,\r\n            override_prompt, original_prompt, original_negative_prompt, \r\n            override_steps, st,\r\n            override_strength,\r\n            cfg, randomness, sigma_adjustment,\r\n        ]\r\n\r\n    def run(self, p, _, override_sampler, override_prompt, original_prompt, original_negative_prompt, override_steps, st, override_strength, cfg, randomness, sigma_adjustment):\r\n        # Override\r\n        if override_sampler:\r\n            p.sampler_index = [sampler.name for sampler in sd_samplers.samplers].index(\"Euler\")\r\n        if override_prompt:\r\n            p.prompt = original_prompt\r\n            p.negative_prompt = original_negative_prompt\r\n        if override_steps:\r\n            p.steps = st\r\n        if override_strength:\r\n            p.denoising_strength = 1.0\r\n\r\n\r\n        def sample_extra(conditioning, unconditional_conditioning, seeds, subseeds, subseed_strength):\r\n            lat = (p.init_latent.cpu().numpy() * 10).astype(int)\r\n\r\n            same_params = self.cache is not None and self.cache.cfg_scale == cfg and self.cache.steps == st \\\r\n                                and self.cache.original_prompt == original_prompt \\\r\n                                and self.cache.original_negative_prompt == original_negative_prompt \\\r\n                                and self.cache.sigma_adjustment == sigma_adjustment\r\n            same_everything = same_params and self.cache.latent.shape == lat.shape and np.abs(self.cache.latent-lat).sum() < 100\r\n\r\n            if same_everything:\r\n                rec_noise = self.cache.noise\r\n            else:\r\n                shared.state.job_count += 1\r\n                cond = p.sd_model.get_learned_conditioning(p.batch_size * [original_prompt])\r\n                uncond = p.sd_model.get_learned_conditioning(p.batch_size * [original_negative_prompt])\r\n                if sigma_adjustment:\r\n                    rec_noise = find_noise_for_image_sigma_adjustment(p, cond, uncond, cfg, st)\r\n                else:\r\n                    rec_noise = find_noise_for_image(p, cond, uncond, cfg, st)\r\n                self.cache = Cached(rec_noise, cfg, st, lat, original_prompt, original_negative_prompt, sigma_adjustment)\r\n\r\n            rand_noise = processing.create_random_tensors(p.init_latent.shape[1:], seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, seed_resize_from_h=p.seed_resize_from_h, seed_resize_from_w=p.seed_resize_from_w, p=p)\r\n            \r\n            combined_noise = ((1 - randomness) * rec_noise + randomness * rand_noise) / ((randomness**2 + (1-randomness)**2) ** 0.5)\r\n            \r\n            sampler = sd_samplers.create_sampler_with_index(sd_samplers.samplers, p.sampler_index, p.sd_model)\r\n\r\n            sigmas = sampler.model_wrap.get_sigmas(p.steps)\r\n            \r\n            noise_dt = combined_noise - (p.init_latent / sigmas[0])\r\n            \r\n            p.seed = p.seed + 1\r\n            \r\n            return sampler.sample_img2img(p, p.init_latent, noise_dt, conditioning, unconditional_conditioning, image_conditioning=p.image_conditioning)\r\n\r\n        p.sample = sample_extra\r\n\r\n        p.extra_generation_params[\"Decode prompt\"] = original_prompt\r\n        p.extra_generation_params[\"Decode negative prompt\"] = original_negative_prompt\r\n        p.extra_generation_params[\"Decode CFG scale\"] = cfg\r\n        p.extra_generation_params[\"Decode steps\"] = st\r\n        p.extra_generation_params[\"Randomness\"] = randomness\r\n        p.extra_generation_params[\"Sigma Adjustment\"] = sigma_adjustment\r\n\r\n        processed = processing.process_images(p)\r\n\r\n        return processed\r\n\r\n", "path": "scripts/img2imgalt.py"}]}