huggingface / huggingface/diffusers

StableDiffusion3Pipeline crashes with AttributeError: 'NoneType' object has no attribute 'ndim' when using num_images_per_prompt with batched prompts

Offen
#12,895 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
stale
Vorherrschende Sprache
Python
Sterne
34.5k
Forks
7.3k
Ø Merge
3 T. 3 Std.
Gemergte PRs (30 T.)
91

Beschreibung

### Describe the bug

When using **StableDiffusion3Pipeline** with **batched prompts** (`prompt` as a list) and passing the parameter `num_images_per_prompt` (even when set to `1`), the pipeline crashes with:

```
AttributeError: 'NoneType' object has no attribute 'ndim'
```

This error does **not** occur:

* when `num_images_per_prompt` is omitted
* when using other pipelines (SDXL, SD1.x, FLUX, etc.)
* when running single-prompt (non-batch) inference

This appears to be specific to **Stable Diffusion 3** and its internal prompt/embedding handling.

### Environment

* **diffusers**: 0.36.0
* **torch**: 2.8
* **python**: 3.12
* **device**: CUDA
* **OS**: Linux
* **Pipeline**: `StableDiffusion3Pipeline`
* **Not using**: `prompt_embeds`, custom embeddings, or LoRAs

### Steps to reproduce

Minimal reproduction pattern:

```python
from diffusers import StableDiffusion3Pipeline
import torch

pipe = StableDiffusion3Pipeline.from_pretrained(
"stabilityai/stable-diffusion-3-medium",
torch_dtype=torch.float16
).to("cuda")

prompts = [
"A photo of a cat",
"A photo of a dog",
]

images = pipe(
prompt=prompts, # batched prompts
height=1024,
width=1024,
num_inference_steps=30,
num_images_per_prompt=1, # <- causes crash
).images
```

Removing `num_images_per_prompt` makes the same code run successfully.

### Full traceback

```
0%| | 0/30 [00:00
ERROR: Traceback (most recent call last):
File "/usr/local/lib/python3.12/site-packages/aquilesimage/runtime/batch_inf.py", line 294, in batch_infer
return self.pipeline.generate_batch(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aquilesimage/runtime/requestscopedpipeline.py", line 789, in generate_batch
result = local_pipe(
^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/torch/utils/_contextlib.py", line 120, in decorate_context
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py", line 1064, in __call__
noise_pred = self.transformer(
^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1773, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1784, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/diffusers/models/transformers/transformer_sd3.py", line 327, in forward
encoder_hidden_states, hidden_states = block(
^^^^^^
File "/usr/local/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1773, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1784, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/diffusers/models/attention.py", line 715, in forward
attn_output2 = self.attn2(hidden_states=norm_hidden_states2, **joint_attention_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1773, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1784, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/diffusers/models/attention_processor.py", line 605, in forward
return self.processor(
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/diffusers/models/attention_processor.py", line 1849, in __call__
context_input_ndim = encoder_hidden_states.ndim
^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'ndim'

ERROR: {'height': 1024, 'width': 1024, 'num_inference_steps': 30, 'device': 'cuda', 'num_images_per_prompt': 1}
```

### Expected behavior

* `num_images_per_prompt` should work with batched prompts, or
* the pipeline should raise a clear validation error if this combination is unsupported

At minimum, the pipeline should not pass `encoder_hidden_states=None` into the attention processor.

### Additional notes

* This issue only reproduces with **StableDiffusion3Pipeline**
* Other pipelines handle the same parameters correctly
* As a workaround, expanding prompts manually and **not passing `num_images_per_prompt`** avoids the crash

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Start in diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py around __call__ and the transformer call at line 1064, then trace the inputs through models/transformers/transformer_sd3.py and attention_processor.py. Reproduce with batched prompts and num_images_per_prompt=1; done means the call no longer passes encoder_hidden_states=None, or reports a clear validation error for the unsupported combination.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python, pytorch
Bereich
machine-learning
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.