Precompute the frequency tensor for complex exponentials (cis) with given dimensions. This function calculates a frequency tensor with complex exponentials using the given dimension 'dim' and the end index 'end'. The 'theta' parameter scales the frequencies. The returned tensor contain
(
dim: int,
pos: Union[np.ndarray, int],
theta: float = 10000.0,
use_real=False,
linear_factor=1.0,
ntk_factor=1.0,
repeat_interleave_real=True,
freqs_dtype=torch.float32, # torch.float32, torch.float64 (flux)
)
| 575 | |
| 576 | |
| 577 | def get_1d_rotary_pos_embed( |
| 578 | dim: int, |
| 579 | pos: Union[np.ndarray, int], |
| 580 | theta: float = 10000.0, |
| 581 | use_real=False, |
| 582 | linear_factor=1.0, |
| 583 | ntk_factor=1.0, |
| 584 | repeat_interleave_real=True, |
| 585 | freqs_dtype=torch.float32, # torch.float32, torch.float64 (flux) |
| 586 | ): |
| 587 | """ |
| 588 | Precompute the frequency tensor for complex exponentials (cis) with given dimensions. |
| 589 | |
| 590 | This function calculates a frequency tensor with complex exponentials using the given dimension 'dim' and the end |
| 591 | index 'end'. The 'theta' parameter scales the frequencies. The returned tensor contains complex values in complex64 |
| 592 | data type. |
| 593 | |
| 594 | Args: |
| 595 | dim (`int`): Dimension of the frequency tensor. |
| 596 | pos (`np.ndarray` or `int`): Position indices for the frequency tensor. [S] or scalar |
| 597 | theta (`float`, *optional*, defaults to 10000.0): |
| 598 | Scaling factor for frequency computation. Defaults to 10000.0. |
| 599 | use_real (`bool`, *optional*): |
| 600 | If True, return real part and imaginary part separately. Otherwise, return complex numbers. |
| 601 | linear_factor (`float`, *optional*, defaults to 1.0): |
| 602 | Scaling factor for the context extrapolation. Defaults to 1.0. |
| 603 | ntk_factor (`float`, *optional*, defaults to 1.0): |
| 604 | Scaling factor for the NTK-Aware RoPE. Defaults to 1.0. |
| 605 | repeat_interleave_real (`bool`, *optional*, defaults to `True`): |
| 606 | If `True` and `use_real`, real part and imaginary part are each interleaved with themselves to reach `dim`. |
| 607 | Otherwise, they are concateanted with themselves. |
| 608 | freqs_dtype (`torch.float32` or `torch.float64`, *optional*, defaults to `torch.float32`): |
| 609 | the dtype of the frequency tensor. |
| 610 | Returns: |
| 611 | `torch.Tensor`: Precomputed frequency tensor with complex exponentials. [S, D/2] |
| 612 | """ |
| 613 | assert dim % 2 == 0 |
| 614 | |
| 615 | if isinstance(pos, int): |
| 616 | pos = torch.arange(pos) |
| 617 | if isinstance(pos, np.ndarray): |
| 618 | pos = torch.from_numpy(pos) # type: ignore # [S] |
| 619 | |
| 620 | theta = theta * ntk_factor |
| 621 | freqs = ( |
| 622 | 1.0 |
| 623 | / (theta ** (torch.arange(0, dim, 2, dtype=freqs_dtype, device=pos.device)[: (dim // 2)] / dim)) |
| 624 | / linear_factor |
| 625 | ) # [D/2] |
| 626 | freqs = torch.outer(pos, freqs) # type: ignore # [S, D/2] |
| 627 | if use_real and repeat_interleave_real: |
| 628 | # flux, hunyuan-dit, cogvideox |
| 629 | freqs_cos = freqs.cos().repeat_interleave(2, dim=1).float() # [S, D] |
| 630 | freqs_sin = freqs.sin().repeat_interleave(2, dim=1).float() # [S, D] |
| 631 | return freqs_cos, freqs_sin |
| 632 | elif use_real: |
| 633 | # stable audio |
| 634 | freqs_cos = torch.cat([freqs.cos(), freqs.cos()], dim=-1).float() # [S, D] |
no outgoing calls
no test coverage detected