pub struct RotaryPositionEmbedding {
pub head_dim: usize,
pub base: f64,
pub max_seq_len: usize,
/* private fields */
}Expand description
Rotary Position Embedding (RoPE) as introduced in Su et al. 2021.
Pre-computes cosine and sine caches up to max_seq_len positions and applies
the rotation in-place to the last dimension of the input tensor.
Fields§
§head_dim: usizeDimension of each attention head.
base: f64Base for the geometric frequency sequence (default 10000.0).
max_seq_len: usizeMaximum sequence length for which the cache was pre-computed.
Implementations§
Source§impl RotaryPositionEmbedding
impl RotaryPositionEmbedding
Sourcepub fn new(
head_dim: usize,
max_seq_len: usize,
base: f64,
) -> Result<Self, PositionError>
pub fn new( head_dim: usize, max_seq_len: usize, base: f64, ) -> Result<Self, PositionError>
Create a new RoPE module, pre-computing the cos/sin cache.
Returns an error if head_dim is not even.
Sourcepub fn apply(
&self,
x: &ArrayD<f64>,
seq_offset: usize,
) -> Result<ArrayD<f64>, PositionError>
pub fn apply( &self, x: &ArrayD<f64>, seq_offset: usize, ) -> Result<ArrayD<f64>, PositionError>
Apply RoPE to the input tensor starting at seq_offset.
x is expected to have shape [seq_len, ..., head_dim] where the last
axis is the head dimension (or any shape where the last axis == head_dim).
Sourcepub fn rotate_half(x: &ArrayD<f64>) -> ArrayD<f64>
pub fn rotate_half(x: &ArrayD<f64>) -> ArrayD<f64>
Compute rotate_half: negate the first half and concatenate with the second half.
Given x shaped [..., head_dim], returns [-x[..., head_dim/2:], x[..., :head_dim/2]].
Sourcepub fn frequencies_at(&self, pos: usize) -> ArrayView1<'_, f64>
pub fn frequencies_at(&self, pos: usize) -> ArrayView1<'_, f64>
Return the pre-computed frequencies (cos values) at a specific position.
Trait Implementations§
Source§impl Clone for RotaryPositionEmbedding
impl Clone for RotaryPositionEmbedding
Source§fn clone(&self) -> RotaryPositionEmbedding
fn clone(&self) -> RotaryPositionEmbedding
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for RotaryPositionEmbedding
impl RefUnwindSafe for RotaryPositionEmbedding
impl Send for RotaryPositionEmbedding
impl Sync for RotaryPositionEmbedding
impl Unpin for RotaryPositionEmbedding
impl UnsafeUnpin for RotaryPositionEmbedding
impl UnwindSafe for RotaryPositionEmbedding
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more