Make SD3Transformer2DModel hidden states contiguous#14186
Open
menglcai wants to merge 2 commits into
Open
Conversation
sayakpaul
reviewed
Jul 14, 2026
Comment on lines
+322
to
+324
| hidden_states = hidden_states.contiguous() | ||
| if encoder_hidden_states is not None: | ||
| encoder_hidden_states = encoder_hidden_states.contiguous() |
Member
There was a problem hiding this comment.
Could also comment on the position in the block of code where contiguous() should be applied?
Author
There was a problem hiding this comment.
You're right. After investigating, the non-contiguous tensor originates from PatchEmbed.forward - flatten(2).transpose(1, 2) (BCHW → BNC) produces a non-contiguous layout.
diffusers/src/diffusers/models/embeddings.py
Line 562 in 4f37263
So the fix is more precisely placed right after self.pos_embed(), not inside the loop.
Updated the patch: moved .contiguous() to immediately after self.pos_embed(hidden_states), dropped the encoder_hidden_states call, and added a comment explaining the root cause.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Ensures hidden states are contiguous after each
JointTransformerBlockcall inSD3Transformer2DModel.On ROCm, non-contiguous tensors produced by
JointTransformerBlockcause performance degradation that accumulates across transformer blocks.Benchmarks (SD3-medium, 512×512, 28 steps, fp16):
Before submitting
.ai/review-rules.md?documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@sayakpaul @yiyixuxu
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.