Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,11 @@ def qeff_apply_interleaved_mrope(freqs, mrope_section):
returns:
x_t: (bs, seq_len, head_dim // 2)
"""
freqs_t = freqs[0] # just overwrite the first dimension T
half_shape = freqs.shape[-1] // 2
freqs_t = freqs[0].clone()
for dim, offset in enumerate((1, 2), start=1): # H, W
length = mrope_section[dim] * 3
idx = slice(offset, length, 3)
freqs_t[..., idx] = freqs[dim, ..., idx]
offset += half_shape
length += half_shape
idx = slice(offset, length, 3)
freqs_t[..., idx] = freqs[dim, ..., idx]
return freqs_t


Expand Down Expand Up @@ -100,8 +95,12 @@ def qeff_apply_rotary_pos_emb(q, k, cos, sin, position_ids, mrope_section, unsqu
Returns:
`tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
"""
cos = cos[position_ids]
sin = sin[position_ids]
# Safe gather: map padded -1 IDs to 0 for gather, then zero them out after interleave.
invalid_pos_mask = position_ids < 0
safe_position_ids = torch.where(invalid_pos_mask, torch.zeros_like(position_ids), position_ids)
flat_pos = safe_position_ids.reshape(-1)
cos = cos.index_select(0, flat_pos).reshape(*safe_position_ids.shape, cos.shape[-1])
sin = sin.index_select(0, flat_pos).reshape(*safe_position_ids.shape, sin.shape[-1])
cos = qeff_apply_interleaved_mrope(cos, mrope_section)
sin = qeff_apply_interleaved_mrope(sin, mrope_section)
cos = cos.unsqueeze(unsqueeze_dim)
Expand Down
Loading