Skip to content

Commit 47f6585

Browse files
committed
fix positional encoding from becoming > 4096
1 parent 23150e4 commit 47f6585

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

src/diffusers/models/transformers/transformer_qwenimage.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def forward(
235235
video_fhw: Union[Tuple[int, int, int], List[Tuple[int, int, int]]],
236236
txt_seq_lens: Optional[List[int]] = None,
237237
device: torch.device = None,
238-
max_txt_seq_len: Optional[Union[int, torch.Tensor]] = None,
238+
max_txt_seq_len: Optional[int] = None,
239239
) -> Tuple[torch.Tensor, torch.Tensor]:
240240
"""
241241
Args:
@@ -245,9 +245,9 @@ def forward(
245245
Deprecated parameter. Use `max_txt_seq_len` instead. If provided, the maximum value will be used.
246246
device: (`torch.device`, *optional*):
247247
The device on which to perform the RoPE computation.
248-
max_txt_seq_len (`int` or `torch.Tensor`, *optional*):
248+
max_txt_seq_len (`int`, *optional*):
249249
The maximum text sequence length for RoPE computation. This should match the encoder hidden states
250-
sequence length. Can be either an int or a scalar tensor (for torch.compile compatibility).
250+
sequence length.
251251
"""
252252
# Handle deprecated txt_seq_lens parameter
253253
if txt_seq_lens is not None:
@@ -296,9 +296,14 @@ def forward(
296296
else:
297297
max_vid_index = max(height, width, max_vid_index)
298298

299-
max_txt_seq_len_int = int(max_txt_seq_len)
300-
# Create device-specific copy for text freqs without modifying self.pos_freqs
301-
txt_freqs = self.pos_freqs.to(device)[max_vid_index : max_vid_index + max_txt_seq_len_int, ...]
299+
pos_freqs = self.pos_freqs.to(device)
300+
301+
# Clamp text sequence length to avoid buffer overflow
302+
buffer_size = pos_freqs.shape[0]
303+
available_space = buffer_size - max_vid_index
304+
safe_txt_seq_len = min(max_txt_seq_len, available_space)
305+
306+
txt_freqs = pos_freqs[max_vid_index : max_vid_index + safe_txt_seq_len]
302307
vid_freqs = torch.cat(vid_freqs, dim=0)
303308

304309
return vid_freqs, txt_freqs
@@ -367,17 +372,17 @@ def rope_params(self, index, dim, theta=10000):
367372
def forward(
368373
self,
369374
video_fhw: Union[Tuple[int, int, int], List[Tuple[int, int, int]]],
370-
max_txt_seq_len: Union[int, torch.Tensor],
375+
max_txt_seq_len: int,
371376
device: torch.device = None,
372377
) -> Tuple[torch.Tensor, torch.Tensor]:
373378
"""
374379
Args:
375380
video_fhw (`Tuple[int, int, int]` or `List[Tuple[int, int, int]]`):
376381
A list of 3 integers [frame, height, width] representing the shape of the video, or a list of layer
377382
structures.
378-
max_txt_seq_len (`int` or `torch.Tensor`):
383+
max_txt_seq_len (`int`):
379384
The maximum text sequence length for RoPE computation. This should match the encoder hidden states
380-
sequence length. Can be either an int or a scalar tensor (for torch.compile compatibility).
385+
sequence length.
381386
device: (`torch.device`, *optional*):
382387
The device on which to perform the RoPE computation.
383388
"""
@@ -417,9 +422,15 @@ def forward(
417422
max_vid_index = max(height, width, max_vid_index)
418423

419424
max_vid_index = max(max_vid_index, layer_num)
420-
max_txt_seq_len_int = int(max_txt_seq_len)
421-
# Create device-specific copy for text freqs without modifying self.pos_freqs
422-
txt_freqs = self.pos_freqs.to(device)[max_vid_index : max_vid_index + max_txt_seq_len_int, ...]
425+
426+
pos_freqs = self.pos_freqs.to(device)
427+
428+
# Clamp text sequence length to avoid buffer overflow
429+
buffer_size = pos_freqs.shape[0]
430+
available_space = buffer_size - max_vid_index
431+
safe_txt_seq_len = min(max_txt_seq_len, available_space)
432+
433+
txt_freqs = pos_freqs[max_vid_index : max_vid_index + safe_txt_seq_len]
423434
vid_freqs = torch.cat(vid_freqs, dim=0)
424435

425436
return vid_freqs, txt_freqs
@@ -920,7 +931,7 @@ def forward(
920931
encoder_hidden_states = self.txt_in(encoder_hidden_states)
921932

922933
# Use the encoder_hidden_states sequence length for RoPE computation and normalize mask
923-
text_seq_len, _, encoder_hidden_states_mask = compute_text_seq_len_from_mask(
934+
text_seq_len, per_sample_len, encoder_hidden_states_mask = compute_text_seq_len_from_mask(
924935
encoder_hidden_states, encoder_hidden_states_mask
925936
)
926937

@@ -933,6 +944,8 @@ def forward(
933944
else self.time_text_embed(timestep, guidance, hidden_states, additional_t_cond)
934945
)
935946

947+
# Pass the static text_seq_len to RoPE (encoder_hidden_states.shape[1])
948+
# The RoPE class will clamp it to avoid buffer overflow
936949
image_rotary_emb = self.pos_embed(img_shapes, max_txt_seq_len=text_seq_len, device=hidden_states.device)
937950

938951
block_attention_kwargs = attention_kwargs.copy() if attention_kwargs is not None else {}

0 commit comments

Comments
 (0)