Qwen Image: txt_seq_lens is redundant and not used · Issue #12344 · huggingface/diffusers · GitHub
Skip to content

Qwen Image: txt_seq_lens is redundant and not used #12344

Closed

Description

@dxqb

Describe the bug

Here

the parameter txt_seq_lens is calculated.

It is set to the length of the text attention mask for each batch sample - but both the attention mask and txt_seq_lens are passed to the transformer:
https://github.com/huggingface/diffusers/blob/efb7a299af46d739dec6a57a5d2814165fba24b5/src/diffusers/pipelines/qwenimage/pipeline_qwenimage.py#L695C38-L695C39

This is redundant in principle, but not only that: both of them are not used
We have already learned that the attention mask passed to the transformer is ignored: #12294
The only point in code where txt_seq_lens is used is here:

Therefore only the maximum value in the 1D array is used, which is the longest mask. The longest mask when using the entire Qwen Image pipeline is by definition the shape of the text embeddings, because they are encoded using the longest padding strategy here:

txt, max_length=self.tokenizer_max_length + drop_idx, padding=True, truncation=True, return_tensors="pt"

[True is equivalent to longest]

Using this padding strategy, you always have at least 1 batch sample in which the attention mask is completely True, therefore max(txt_seq_lens) is the token length.

If you do not use the entire pipeline but only the transformer, you could pass txt_seq_lens where max(txt_seq_lens) is not equal to the shape of the embeddings. However, then there is a shape mismatch here:

x_out = torch.view_as_real(x_rotated * freqs_cis).flatten(3)
~~~~~~~~~~^~~~~~~~~~~
RuntimeError: The size of tensor a (512) must match the size of tensor b (6) at non-singleton dimension 1
max(txt_seq_lens) in this example was 6, while the embeddings shape was [..., 512, ...]

Conclusion:
txt_seq_len is redundant, basically unused, and can only be set to 1 valid value

What could be done about this depends on how this was intended. txt_seq_lens is in principle a comfortable and nice way to set an attention mask, if the attention mask is always a sequence of [True, True, True, ... False, False False]. But then we don't need to pass an attention mask, and txt_seq_lens has to accept all valid values, not only the shape of the embeddings.

If the attention mask will be used (see #12294) then there is no need for txt_seq_lens.

Reproduction

Let me know if this needs reproduction, and which part.

Logs

System Info

diffusers HEAD

Who can help?

@yiyixuxu @DN6
CC @JingyeChen-Canva

Activity

  1. added
    bugSomething isn't working
    on Sep 17, 2025
  2. harshaljanjani commented on Nov 11, 2025

    @harshaljanjani

    Hello @sayakpaul!

    Would it be alright if I try to tackle this issue as part of the Diffusers MVP program? It’d be my first contribution to Diffusers, though I’ve raised PRs to Transformers before. From the issue description, it looks like txt_seq_lens might be redundant and a bit confusing -- I wanted to confirm if it’s safe to remove it completely, or if there’s a specific reason to keep it around.
    Secondly, I wanted to ask where can I find info on the hardware requirements for testing the QwenImage pipeline; and in regard to testing, should I run tests with the full model, or are there lightweight test fixtures already available in the repo?

    Thanks a lot for your time and guidance!

  3. sayakpaul commented on Nov 11, 2025

    @sayakpaul
    Member

    Thanks for your interest here!

    From the issue description, it looks like txt_seq_lens might be redundant and a bit confusing

    Please use your understanding to determine if it's truly redundant first.

    Cc @naykun who contributed the pipeline in the first place.

  4. dxqb commented on Nov 11, 2025

    @dxqb
    ContributorAuthor

    Without input from Alibaba we can only speculate, but I think I know the intention of passing the sequence lengths now:

    In their original training code they must have used large batch sizes, that are probably bucketed by image sequence length, but they cannot be bucketed by text sequence lengths because each caption has a different length. Attention masks can be used for that, and are used for that in diffusers right now in some pipelines, but this means you cannot use efficient flash attention: Flash attention neither supports attention masks, nor different sequence lengths in the same batch - but "varlen" flash attention does support different sequence lengths in the same batch.

    To use varlen flash attention, you need to know the sequence lengths on CPU: Converting an attention mask (which lives on GPU) back to CPU sequence lengths, as is currently done by the diffusers attention dispatch here,

    , is very inefficient: It requires calling item() which causes CUDA synchronization events (and is incompatible with torch.compile).

    Therefore I speculate that they have not used attention masks in their training code, but they achieved the same goal with passing sequence lengths to varlen attention. This would also match that attention masks are ignored in their diffusers code, see #12294)

    If you want to make a small project out of this @harshaljanjani , my suggestion would be to

    • rework the attention dispatch mechanism in diffusers, which is suboptimal for various reasons. Among them that sequence lengths cannot be passed to it, and attention masks are ignored for some backends (see dispatch_attention_fn silently ignores attn_mask for certain backends #12605)
    • ideally then change the Qwen Image pipeline, and potentially other pipelines, to pass sequence lengths to the attention mechanism whenever possible (because more efficient), and have the attention dispatch convert sequence lengths to attention masks for backends that don't support varlen, but do support attention masks

    However, some input from Alibaba @naykun would be really appreciated here, to reduce the amount of speculation.

    Note that I am in no way associated with diffusers. This is just my personal opinion.

    Also see here for a more detailled explanation why "varlen" attention isn't possible efficiently with the current attention dispatch: Nerogar/OneTrainer#1110

  5. dxqb commented on Nov 12, 2025

    @dxqb
    ContributorAuthor

    My speculation in the previous comment has been confirmed here: #12294 (comment)

  6. sayakpaul commented on Nov 12, 2025

    @sayakpaul
    Member

    I am also guessing a couple issues you have opened are related? 👀

  7. dxqb commented on Nov 12, 2025

    @dxqb
    ContributorAuthor

    I am also guessing a couple issues you have opened are related? 👀

    Now they are. I didn't know at the time. My proposed solution above adresses both this issue and #12294

  8. github-actions commented on Jan 9, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

    Please note that issues that do not follow the contributing guidelines are likely to be ignored.

  9. removed
    staleIssues that haven't received updates
    on Jan 9, 2026
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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions