This library provides a Pytorch implementation of the Rotary Positional Embedding (RoPE), a crucial component for modern transformer architectures like LLaMA, designed to improve the model's ability to handle long sequences. It offers an easy-to-use API to apply rotary embeddings to query and key tensors. The current version is 0.8.9, and it follows a rapid release cadence for bug fixes and minor improvements.
pip install rotary-embedding-torchVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to initialize `RotaryEmbedding` and apply it to example query and key tensors, typically used in self-attention mechanisms. The `dim` parameter must match the last dimension of your input tensors (head_dim).
Upgrade to `rotary-embedding-torch>=0.8.0`.
Upgrade to `rotary-embedding-torch>=0.8.6` to ensure compatibility with `torch.compile`.
Initialize `RotaryEmbedding` with an appropriate `max_seq_len` if you know the maximum sequence length your model will handle to optimize caching and prevent dynamic re-computation overhead.
pip install rotary-embedding-torch
from rotary_embedding_torch import RotaryEmbedding rope = RotaryEmbedding(dim = 128) # Replace 128 with your actual feature dimension
from rotary_embedding_torch import RotaryEmbedding
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
rope = RotaryEmbedding(dim = 128).to(device)
q = torch.randn(1, 8, 32, 128).to(device)
k = torch.randn(1, 8, 32, 128).to(device)
q, k = rope(q, k)from rotary_embedding_torch import RotaryEmbedding import torch rope = RotaryEmbedding(dim = 128) q = torch.randn(1, 8, 32, 128) k = torch.randn(1, 8, 32, 128) # Correct way to apply rotary embeddings q, k = rope(q, k)