pub struct RoPECache { /* private fields */ }Expand description
Precomputed RoPE (Rotary Position Embedding) frequencies
RoPE is used by most modern LLMs for position encoding. Computing cos/sin values for each position is expensive, so we precompute them once for max_seq_len positions.
§Example
ⓘ
let rope = RoPECache::new(128, 4096, 10000.0, &Device::CPU)?;
let (cos, sin) = rope.get(seq_len)?;Implementations§
Source§impl RoPECache
impl RoPECache
Sourcepub fn new(
head_dim: usize,
max_seq_len: usize,
base: f32,
device: &Device,
) -> Result<Self>
pub fn new( head_dim: usize, max_seq_len: usize, base: f32, device: &Device, ) -> Result<Self>
Create a new RoPE cache with precomputed frequencies
§Arguments
head_dim- Dimension of each attention headmax_seq_len- Maximum sequence length to precomputebase- RoPE base frequency (typically 10000.0 for LLaMA)device- Device to store tensors on
Sourcepub fn get(&self, seq_len: usize) -> Result<(Tensor, Tensor)>
pub fn get(&self, seq_len: usize) -> Result<(Tensor, Tensor)>
Get precomputed cos/sin for a specific sequence length
Returns slices of the precomputed tensors for positions [0, seq_len)
Sourcepub fn get_range(&self, start: usize, end: usize) -> Result<(Tensor, Tensor)>
pub fn get_range(&self, start: usize, end: usize) -> Result<(Tensor, Tensor)>
Get precomputed cos/sin for a range of positions
Useful for KV cache scenarios where we only need positions [start, end)
Sourcepub fn max_seq_len(&self) -> usize
pub fn max_seq_len(&self) -> usize
Get the maximum sequence length
Auto Trait Implementations§
impl !RefUnwindSafe for RoPECache
impl !UnwindSafe for RoPECache
impl Freeze for RoPECache
impl Send for RoPECache
impl Sync for RoPECache
impl Unpin for RoPECache
impl UnsafeUnpin for RoPECache
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
Mutably borrows from an owned value. Read more
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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>
Converts
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>
Converts
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