Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
Error if more than 75 tokens used
**Describe the bug**
When typing the prompt (txt2img) the number of tokens increases from 75 to 150, but then it errors when I try to generate images. It will run if the number of tokens are below 75.

> Traceback (most recent call last):
>   File "F:\stable-diffusion-webui\modules\ui.py", line 182, in f
>     res = list(func(*args, **kwargs))
>   File "F:\stable-diffusion-webui\webui.py", line 69, in f
>     res = func(*args, **kwargs)
>   File "F:\stable-diffusion-webui\modules\txt2img.py", line 43, in txt2img
>     processed = process_images(p)
>   File "F:\stable-diffusion-webui\modules\processing.py", line 394, in process_images
>     c = prompt_parser.get_multicond_learned_conditioning(shared.sd_model, prompts, p.steps)
>   File "F:\stable-diffusion-webui\modules\prompt_parser.py", line 203, in get_multicond_learned_conditioning
>     learned_conditioning = get_learned_conditioning(model, prompt_flat_list, steps)
>   File "F:\stable-diffusion-webui\modules\prompt_parser.py", line 138, in get_learned_conditioning
>     conds = model.get_learned_conditioning(texts)
>   File "F:\stable-diffusion-webui\repositories\stable-diffusion\ldm\models\diffusion\ddpm.py", line 558, in get_learned_conditioning
>     c = self.cond_stage_model(c)
>   File "F:\stable-diffusion-webui\venv\lib\site-packages\torch\nn\modules\module.py", line 1130, in _call_impl
>     return forward_call(*input, **kwargs)
>   File "F:\stable-diffusion-webui\modules\sd_hijack.py", line 315, in forward
>     z1 = self.process_tokens([x[:75] for x in remade_batch_tokens], [x[:75] for x in batch_multipliers])
>   File "F:\stable-diffusion-webui\modules\sd_hijack.py", line 330, in process_tokens
>     tokens = torch.asarray(remade_batch_tokens).to(device)
> ValueError: expected sequence of length 2 at dim 1 (got 77)

**To Reproduce**
Steps to reproduce the behavior:
See above

**Expected behavior**
No errors and image is created.

**Desktop (please complete the following information):**
 - OS: Windows 11
 - Browser: Chrome
 - Commit revision: a05c824384f21dfd729e521e8d6cef8da3250bfc 




Discussion:
Can you send the prompt + all the parameters if possible?
This works, with 75/75 tokens:

> solo, a close-up portrait of a petite blonde anamorphic [woman:black vines:9], ((naked)), eerie glow, biomutant, dystopian, ((gnarly roots growing out of her skin)), (fingers and toes growing into the ground), stern face, green_eyes, sorceress, glowing, in a laboratory covered in black oil, cyberpunk, by greg rutkowski and wlop
> Steps: 25, Sampler: Euler a, CFG scale: 11, Seed: 4099293485, Size: 576x576, Model hash: 7460a6fa

But if I change one word in the prompt (green_eyes to bloodshot_eyes), nothing else, the token count goes to 82/150 and the error appears.

> solo, a close-up portrait of a petite blonde anamorphic [woman:black vines:9],  ((naked)), eerie glow, biomutant, dystopian, ((gnarly roots growing out of her skin)), (fingers and toes growing into the ground), stern face, bloodshot_eyes, sorceress, glowing, in a laboratory covered in black oil, cyberpunk, by greg rutkowski and wlop
I just tried some random prompts that were larger than 75/75 (115/150 and 272/300 in these example) and it works, even if I include the prompt above as part of it?!
Ah, I found it.
If I use the same, failing prompt and increase it a little, it works. It seems to be something about the specific 82/150 size. Add the word "art" to the end, and it's 83/150...works. 

Resolving patch:
diff --git a/modules/sd_hijack.py b/modules/sd_hijack.py
--- a/modules/sd_hijack.py
+++ b/modules/sd_hijack.py
@@ -321,7 +321,17 @@ def forward(self, text):
                         fixes.append(fix[1])
                 self.hijack.fixes.append(fixes)
             
-            z1 = self.process_tokens([x[:75] for x in remade_batch_tokens], [x[:75] for x in batch_multipliers])
+            tokens = []
+            multipliers = []
+            for i in range(len(remade_batch_tokens)):
+                if len(remade_batch_tokens[i]) > 0:
+                    tokens.append(remade_batch_tokens[i][:75])
+                    multipliers.append(batch_multipliers[i][:75])
+                else:
+                    tokens.append([self.wrapped.tokenizer.eos_token_id] * 75)
+                    multipliers.append([1.0] * 75)
+
+            z1 = self.process_tokens(tokens, multipliers)
             z = z1 if z is None else torch.cat((z, z1), axis=-2)
             
             remade_batch_tokens = rem_tokens


Test patch: