Skip to content

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

@FredyRivera-dev

Description

@FredyRivera-dev

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:

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<?, ?it/s]

--------------------------------------------------------------------------------
ERROR: 'NoneType' object has no attribute 'ndim'
--------------------------------------------------------------------------------

ERROR: <class 'AttributeError'>
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleIssues that haven't received updates

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions