Skip to main content

RotaryEmbedding

Struct RotaryEmbedding 

Source
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

Source

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).

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.