pub struct RotaryEmbedding { /* private fields */ }Expand description
Precomputed cos/sin tables for every position up to max_position,
so RotaryEmbedding::apply is a table lookup per (position, pair)
rather than a cos/sin call — cheap enough to matter once this runs
once per token per layer per head during decode.
Implementations§
Source§impl RotaryEmbedding
impl RotaryEmbedding
Sourcepub fn new(rope_dim: usize, theta: f32, max_position: usize) -> Self
pub fn new(rope_dim: usize, theta: f32, max_position: usize) -> Self
Precomputes rotation tables for every position in 0..max_position.
rope_dim must be even (each pair needs two dimensions; see
crate::config::QwenConfig::from_metadata, which rejects an odd
rope_dimension_count before a RotaryEmbedding is ever built).
Sourcepub fn apply(&self, x: &Tensor, positions: &[usize]) -> Result<Tensor>
pub fn apply(&self, x: &Tensor, positions: &[usize]) -> Result<Tensor>
Rotates x, shaped [n_heads, seq, head_dim], in place per
position. positions[s] is the absolute sequence position of
x’s s-th row — not necessarily s itself, since a KV-cache
decode step’s single query token sits at cache_len, not at 0
(see crate::kv_cache::KvCache).
Only the leading rope_dim elements of each head are rotated;
head_dim - rope_dim trailing elements (when rope_dim < head_dim)
pass through unchanged, matching llama.cpp’s partial-rotary
support.
§Errors
Error::ShapeMismatch if x is not rank 3, if its last dimension
is smaller than rope_dim, or if positions.len() does not match
x’s sequence dimension.