Skip to content

Prompt attention masks are misordered when generating multiple images per prompt #15462

Backlog

Description

@mertkayacs

Describe the bug

I found that some pipelines pair embeddings with the wrong attention mask when I batch prompts with different masks and request multiple images per prompt. For two prompts and two images each, the embeddings are ordered [p0, p0, p1, p1], while the masks are ordered [m0, m1, m0, m1]. In Sana and SanaSprint, the denoiser can drop valid tokens or attend to padding for the middle two samples. In AuraFlow, the mismatch appears in the masks returned by encode_prompt. The pipeline's handling of precomputed padding is tracked separately in #13624.

I found the same ordering bug in Sana ControlNet, SanaSprint Img2Img, Sana PAG, Kandinsky 3, Kandinsky 3 Img2Img, Lumina2, PRX and PRX Pixel. The PRX instance is also covered by #13608. I have a patch that repeats each mask row alongside its embedding.

Reproduction

import torch
from diffusers import SanaPipeline

pipe = SanaPipeline.__new__(SanaPipeline)
pipe.text_encoder = None
embeds = torch.tensor([[[1.0], [2.0], [3.0]], [[4.0], [5.0], [6.0]]])
mask = torch.tensor([[True, True, False], [True, False, False]])

expanded, expanded_mask, *_ = pipe.encode_prompt(
    None,
    device=torch.device("cpu"),
    do_classifier_free_guidance=False,
    num_images_per_prompt=2,
    prompt_embeds=embeds,
    prompt_attention_mask=mask,
)

print(expanded[:, 0, 0].tolist())
print(expanded_mask.tolist())

The second and third mask rows are swapped. I expect the first mask row twice, followed by the second mask row twice.

Context

I found this while reading prompt batch expansion in encode_prompt, then reproduced it with precomputed embeddings on CPU.

System Info

Diffusers 0.44.0.dev0 at 08d5de30ed6ccde08f27ef2884e9c4c971cc3bbb, Python 3.12.3, PyTorch 2.9.1+cpu, Transformers 5.24.1, Linux x86_64.

Activity

  1. mertkayacs commented on Dec 4, 2026

    @mertkayacs
    ContributorAuthor

    I opened #15492 with the fix for the affected pipelines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions