Struct RotaryEncoding
pub struct RotaryEncoding {
pub freq_complex: Tensor<3>,
pub theta: Tensor<1>,
/* private fields */
}Expand description
A module that applies rotary positional encoding to a tensor. Rotary Position Encoding or Embedding (RoPE), is a type of position embedding which encodes absolute positional information with rotation matrix and naturally incorporates explicit relative position dependency in self-attention formulation.
Introduced in the paper: RoFormer: Enhanced Transformer with Rotary Position Embedding
Should be created using RotaryEncodingConfig.
Fields§
§freq_complex: Tensor<3>Complex frequency tensor of shape (max_sequence_length, d_model, 2) with real and imaginary components
theta: Tensor<1>Frequency vector used to compute/apply the complex rotations.
Implementations§
§impl RotaryEncoding
impl RotaryEncoding
pub fn forward<const D: usize>(&self, x: Tensor<D>) -> Tensor<D>
pub fn forward<const D: usize>(&self, x: Tensor<D>) -> Tensor<D>
Applies rotary positional encoding to a tensor of dimensions (…, seq_len, d_model)
§Arguments:
x- Input tensor of shape (…, seq_len, d_model). Accommodate both 3D and 4D tensors for (batch size, seq_len, hidden_dim) or (batch size, num_heads, seq_len, hidden_dim) respectively.
§Returns:
Output tensor with the same shape as input tensor after applying rotary encoding.
§Panics
If the input has fewer than 2 dimensions or its last axis is not d_model.
pub fn apply<const D: usize>(&self, x: Tensor<D>, start: usize) -> Tensor<D>
pub fn apply<const D: usize>(&self, x: Tensor<D>, start: usize) -> Tensor<D>
Applies rotary positional encoding to a tensor of dimensions (…, seq_len, d_model)
§Arguments:
x- Input tensor of shape (…, seq_len, d_model). Accommodate both 3D and 4D tensors for (batch size, seq_len, hidden_dim) or (batch size, num_heads, seq_len, hidden_dim) respectively.start- Sequence start position index.
§Returns:
Output tensor with the same shape as input tensor after applying rotary encoding.
§Panics
If the input has fewer than 2 dimensions or its last axis is not d_model.
pub fn shift(&mut self, start: usize)
pub fn shift(&mut self, start: usize)
Shifts the pre-computed rotary frequency to cover a new range of positions.
This method updates the internal frequency tensor freq_complex to store
the rotary positional encodings for a new window of positions starting at start.
pub fn reset(&mut self)
pub fn reset(&mut self)
Resets the pre-computed rotary frequency window to start at position 0.
Trait Implementations§
§impl Clone for RotaryEncoding
impl Clone for RotaryEncoding
§fn clone(&self) -> RotaryEncoding
fn clone(&self) -> RotaryEncoding
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl Debug for RotaryEncoding
impl Debug for RotaryEncoding
§impl Display for RotaryEncoding
impl Display for RotaryEncoding
§impl Module for RotaryEncoding
impl Module for RotaryEncoding
§fn num_params(&self) -> usize
fn num_params(&self) -> usize
§fn visit<Visitor>(&self, visitor: &mut Visitor)where
Visitor: ModuleVisitor,
fn visit<Visitor>(&self, visitor: &mut Visitor)where
Visitor: ModuleVisitor,
§fn map<Mapper>(self, mapper: &mut Mapper) -> RotaryEncodingwhere
Mapper: ModuleMapper,
fn map<Mapper>(self, mapper: &mut Mapper) -> RotaryEncodingwhere
Mapper: ModuleMapper,
§fn collect_devices(&self, devices: Vec<Device>) -> Vec<Device>
fn collect_devices(&self, devices: Vec<Device>) -> Vec<Device>
§fn to_device(self, device: &Device) -> RotaryEncoding
fn to_device(self, device: &Device) -> RotaryEncoding
§fn fork(self, device: &Device) -> RotaryEncoding
fn fork(self, device: &Device) -> RotaryEncoding
§fn valid(&self) -> RotaryEncoding
fn valid(&self) -> RotaryEncoding
§fn train(self) -> RotaryEncoding
fn train(self) -> RotaryEncoding
§fn materialize(self) -> RotaryEncoding
fn materialize(self) -> RotaryEncoding
§fn devices(&self) -> Vec<Device>
fn devices(&self) -> Vec<Device>
§fn set_require_grad(self, require_grad: bool) -> Self
fn set_require_grad(self, require_grad: bool) -> Self
§fn set_require_grad_group(self, group: ParamGroup, require_grad: bool) -> Self
fn set_require_grad_group(self, group: ParamGroup, require_grad: bool) -> Self
§fn no_grad(self) -> Self
fn no_grad(self) -> Self
§fn freeze_group(self, group: ParamGroup) -> Self
fn freeze_group(self, group: ParamGroup) -> Self
§fn unfreeze_group(self, group: ParamGroup) -> Self
fn unfreeze_group(self, group: ParamGroup) -> Self
§fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
§fn quantize_weights_group(
self,
quantizer: &mut Quantizer,
group: ParamGroup,
) -> Self
fn quantize_weights_group( self, quantizer: &mut Quantizer, group: ParamGroup, ) -> Self
§fn apply_reparameterization<R>(self, reparameterizer: R) -> Selfwhere
Self: Sized,
R: Reparameterizer,
fn apply_reparameterization<R>(self, reparameterizer: R) -> Selfwhere
Self: Sized,
R: Reparameterizer,
Reparameterizer. Read more§fn apply_lora(self, lora: Lora) -> Selfwhere
Self: Sized,
fn apply_lora(self, lora: Lora) -> Selfwhere
Self: Sized,
§fn apply_qlora(self, qlora: QLora) -> Selfwhere
Self: Sized,
fn apply_qlora(self, qlora: QLora) -> Selfwhere
Self: Sized,
§fn into_record(self) -> ModuleRecordwhere
Self: Sized,
fn into_record(self) -> ModuleRecordwhere
Self: Sized,
ModuleRecord. Read more§fn into_record_group(self, group: ParamGroup) -> ModuleRecordwhere
Self: Sized,
fn into_record_group(self, group: ParamGroup) -> ModuleRecordwhere
Self: Sized,
§fn try_load_record(self, record: ModuleRecord) -> Result<Self, RecordError>where
Self: Sized,
fn try_load_record(self, record: ModuleRecord) -> Result<Self, RecordError>where
Self: Sized,
ModuleRecord to this module, returning the loaded
module. Read more§fn load_record(self, record: ModuleRecord) -> Selfwhere
Self: Sized,
fn load_record(self, record: ModuleRecord) -> Selfwhere
Self: Sized,
ModuleRecord to this module, consuming and returning
it. Read more§fn save_file<P>(self, path: P) -> Result<(), RecordError>
fn save_file<P>(self, path: P) -> Result<(), RecordError>
§fn load_file<P>(self, path: P) -> Self
fn load_file<P>(self, path: P) -> Self
§fn try_load_file<P>(self, path: P) -> Result<Self, RecordError>
fn try_load_file<P>(self, path: P) -> Result<Self, RecordError>
§impl ModuleDisplay for RotaryEncoding
impl ModuleDisplay for RotaryEncoding
§fn custom_settings(&self) -> Option<DisplaySettings>
fn custom_settings(&self) -> Option<DisplaySettings>
§fn custom_content(&self, content: Content) -> Option<Content>
fn custom_content(&self, content: Content) -> Option<Content>
§fn format(&self, passed_settings: DisplaySettings) -> String
fn format(&self, passed_settings: DisplaySettings) -> String
§impl ModuleDisplayDefault for RotaryEncoding
impl ModuleDisplayDefault for RotaryEncoding
Auto Trait Implementations§
impl Freeze for RotaryEncoding
impl RefUnwindSafe for RotaryEncoding
impl Send for RotaryEncoding
impl Sync for RotaryEncoding
impl Unpin for RotaryEncoding
impl UnsafeUnpin for RotaryEncoding
impl UnwindSafe for RotaryEncoding
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<K, Q> Comparable<Q> for K
impl<K, Q> Comparable<Q> for K
§impl<K, Q> Equivalent<Q> for K
impl<K, Q> Equivalent<Q> for K
§fn equivalent(&self, key: &Q) -> bool
fn equivalent(&self, key: &Q) -> bool
key and return true if they are equal.impl<T> ErasedDestructor for Twhere
T: 'static,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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>
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§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string()] Read more§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString]. Read more