MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / apply_rotary_emb

Function apply_rotary_emb

diffusers/src/diffusers/models/embeddings.py:643–689  ·  view source on GitHub ↗

Apply rotary embeddings to input tensors using the given frequency tensor. This function applies rotary embeddings to the given query or key 'x' tensors using the provided frequency tensor 'freqs_cis'. The input tensors are reshaped as complex numbers, and the frequency tensor is reshap

(
    x: torch.Tensor,
    freqs_cis: Union[torch.Tensor, Tuple[torch.Tensor]],
    use_real: bool = True,
    use_real_unbind_dim: int = -1,
)

Source from the content-addressed store, hash-verified

641
642
643def apply_rotary_emb(
644 x: torch.Tensor,
645 freqs_cis: Union[torch.Tensor, Tuple[torch.Tensor]],
646 use_real: bool = True,
647 use_real_unbind_dim: int = -1,
648) -> Tuple[torch.Tensor, torch.Tensor]:
649 """
650 Apply rotary embeddings to input tensors using the given frequency tensor. This function applies rotary embeddings
651 to the given query or key 'x' tensors using the provided frequency tensor 'freqs_cis'. The input tensors are
652 reshaped as complex numbers, and the frequency tensor is reshaped for broadcasting compatibility. The resulting
653 tensors contain rotary embeddings and are returned as real tensors.
654
655 Args:
656 x (`torch.Tensor`):
657 Query or key tensor to apply rotary embeddings. [B, H, S, D] xk (torch.Tensor): Key tensor to apply
658 freqs_cis (`Tuple[torch.Tensor]`): Precomputed frequency tensor for complex exponentials. ([S, D], [S, D],)
659
660 Returns:
661 Tuple[torch.Tensor, torch.Tensor]: Tuple of modified query tensor and key tensor with rotary embeddings.
662 """
663 if use_real:
664 cos, sin = freqs_cis # [S, D]
665 cos = cos[None, None]
666 sin = sin[None, None]
667 cos, sin = cos.to(x.device), sin.to(x.device)
668
669 if use_real_unbind_dim == -1:
670 # Used for flux, cogvideox, hunyuan-dit
671 x_real, x_imag = x.reshape(*x.shape[:-1], -1, 2).unbind(-1) # [B, S, H, D//2]
672 x_rotated = torch.stack([-x_imag, x_real], dim=-1).flatten(3)
673 elif use_real_unbind_dim == -2:
674 # Used for Stable Audio
675 x_real, x_imag = x.reshape(*x.shape[:-1], 2, -1).unbind(-2) # [B, S, H, D//2]
676 x_rotated = torch.cat([-x_imag, x_real], dim=-1)
677 else:
678 raise ValueError(f"`use_real_unbind_dim={use_real_unbind_dim}` but should be -1 or -2.")
679
680 out = (x.float() * cos + x_rotated.float() * sin).to(x.dtype)
681
682 return out
683 else:
684 # used for lumina
685 x_rotated = torch.view_as_complex(x.float().reshape(*x.shape[:-1], -1, 2))
686 freqs_cis = freqs_cis.unsqueeze(2)
687 x_out = torch.view_as_real(x_rotated * freqs_cis).flatten(3)
688
689 return x_out.type_as(x)
690
691
692class FluxPosEmbed(nn.Module):

Callers 11

__call__Method · 0.85
__call__Method · 0.85
__call__Method · 0.85
__call__Method · 0.85
__call__Method · 0.85
__call__Method · 0.85
__call__Method · 0.85
__call__Method · 0.85
__call__Method · 0.85
__call__Method · 0.85

Calls 1

toMethod · 0.45

Tested by

no test coverage detected