Struct burn_core::nn::RotaryEncoding
source · pub struct RotaryEncoding<B: Backend> {
pub freq_complex: Tensor<B, 3>,
pub max_sequence_length: usize,
pub theta: f32,
}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<B, 3>Frequency Tensor of shape (max_sequence_length, d_model, 2) with real and imaginary components
max_sequence_length: usizeMaximum sequence length of input
theta: f32Scaling factor for frequency computation.
Implementations§
source§impl<B: Backend> RotaryEncoding<B>
impl<B: Backend> RotaryEncoding<B>
sourcepub fn forward<const D: usize>(&self, x: Tensor<B, D>) -> Tensor<B, D>
pub fn forward<const D: usize>(&self, x: Tensor<B, D>) -> Tensor<B, 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 tensor does not have at least 2 dimensions for sequence length and hidden dimension.
sourcepub fn apply<const D: usize>(
&self,
x: Tensor<B, D>,
start: usize,
) -> Tensor<B, D>
pub fn apply<const D: usize>( &self, x: Tensor<B, D>, start: usize, ) -> Tensor<B, 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 tensor does not have at least 2 dimensions for sequence length and hidden dimension.
Trait Implementations§
source§impl<B> AutodiffModule<B> for RotaryEncoding<B>
impl<B> AutodiffModule<B> for RotaryEncoding<B>
§type InnerModule = RotaryEncoding<<B as AutodiffBackend>::InnerBackend>
type InnerModule = RotaryEncoding<<B as AutodiffBackend>::InnerBackend>
source§fn valid(&self) -> Self::InnerModule
fn valid(&self) -> Self::InnerModule
source§impl<B: Backend> Clone for RotaryEncoding<B>
impl<B: Backend> Clone for RotaryEncoding<B>
source§impl<B: Backend> Display for RotaryEncoding<B>
impl<B: Backend> Display for RotaryEncoding<B>
source§impl<B: Backend> Module<B> for RotaryEncoding<B>
impl<B: Backend> Module<B> for RotaryEncoding<B>
§type Record = RotaryEncodingRecord<B>
type Record = RotaryEncodingRecord<B>
source§fn load_record(self, record: Self::Record) -> Self
fn load_record(self, record: Self::Record) -> Self
source§fn into_record(self) -> Self::Record
fn into_record(self) -> Self::Record
source§fn num_params(&self) -> usize
fn num_params(&self) -> usize
source§fn visit<Visitor: ModuleVisitor<B>>(&self, visitor: &mut Visitor)
fn visit<Visitor: ModuleVisitor<B>>(&self, visitor: &mut Visitor)
source§fn map<Mapper: ModuleMapper<B>>(self, mapper: &mut Mapper) -> Self
fn map<Mapper: ModuleMapper<B>>(self, mapper: &mut Mapper) -> Self
source§fn collect_devices(&self, devices: Devices<B>) -> Devices<B>
fn collect_devices(&self, devices: Devices<B>) -> Devices<B>
source§fn to_device(self, device: &B::Device) -> Self
fn to_device(self, device: &B::Device) -> Self
source§fn fork(self, device: &B::Device) -> Self
fn fork(self, device: &B::Device) -> Self
source§fn devices(&self) -> Devices<B>
fn devices(&self) -> Devices<B>
source§fn save_file<FR, PB>(
self,
file_path: PB,
recorder: &FR,
) -> Result<(), RecorderError>
fn save_file<FR, PB>( self, file_path: PB, recorder: &FR, ) -> Result<(), RecorderError>
source§fn load_file<FR, PB>(
self,
file_path: PB,
recorder: &FR,
device: &B::Device,
) -> Result<Self, RecorderError>
fn load_file<FR, PB>( self, file_path: PB, recorder: &FR, device: &B::Device, ) -> Result<Self, RecorderError>
source§fn quantize_weights<C: Calibration>(self, quantizer: &mut Quantizer<C>) -> Self
fn quantize_weights<C: Calibration>(self, quantizer: &mut Quantizer<C>) -> Self
source§impl<B: Backend> ModuleDisplay for RotaryEncoding<B>
impl<B: Backend> ModuleDisplay for RotaryEncoding<B>
source§fn custom_settings(&self) -> Option<DisplaySettings>
fn custom_settings(&self) -> Option<DisplaySettings>
Auto Trait Implementations§
impl<B> Freeze for RotaryEncoding<B>where
<B as Backend>::FloatTensorPrimitive<3>: Freeze,
<B as Backend>::QuantizedTensorPrimitive<3>: Freeze,
impl<B> RefUnwindSafe for RotaryEncoding<B>where
<B as Backend>::FloatTensorPrimitive<3>: RefUnwindSafe,
<B as Backend>::QuantizedTensorPrimitive<3>: RefUnwindSafe,
impl<B> Send for RotaryEncoding<B>
impl<B> Sync for RotaryEncoding<B>where
<B as Backend>::FloatTensorPrimitive<3>: Sync,
<B as Backend>::QuantizedTensorPrimitive<3>: Sync,
impl<B> Unpin for RotaryEncoding<B>where
<B as Backend>::FloatTensorPrimitive<3>: Unpin,
<B as Backend>::QuantizedTensorPrimitive<3>: Unpin,
impl<B> UnwindSafe for RotaryEncoding<B>where
<B as Backend>::FloatTensorPrimitive<3>: UnwindSafe,
<B as Backend>::QuantizedTensorPrimitive<3>: UnwindSafe,
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)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>
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