Struct RotaryEncoding
pub struct RotaryEncoding<B>where
B: Backend,{
pub freq_complex: Tensor<B, 3>,
pub theta: Tensor<B, 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<B, 3>
Complex frequency tensor of shape (max_sequence_length, d_model, 2) with real and imaginary components
theta: Tensor<B, 1>
Frequency vector used to compute/apply the complex rotations.
Implementations§
§
impl<B> RotaryEncoding<B>where
B: Backend,
impl<B> RotaryEncoding<B>where
B: Backend,
pub 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.
pub 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§
§
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>
§
fn valid(&self) -> <RotaryEncoding<B> as
AutodiffModule<B>>::InnerModule
fn valid(&self) -> <RotaryEncoding<B> as AutodiffModule<B>>::InnerModule
§
impl<B> Clone for RotaryEncoding<B>where
B: Backend,
impl<B> Clone for RotaryEncoding<B>where
B: Backend,
§
fn clone(&self) -> RotaryEncoding<B>
fn clone(&self) -> RotaryEncoding<B>
1.0.0 · Source§
fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read
more
§
impl<B> Debug for RotaryEncoding<B>
impl<B> Debug for RotaryEncoding<B>
§
impl<B> Display for RotaryEncoding<B>where
B: Backend,
impl<B> Display for RotaryEncoding<B>where
B: Backend,
§
impl<B> Module<B> for RotaryEncoding<B>where
B: Backend,
impl<B> Module<B> for RotaryEncoding<B>where
B: Backend,
§
type Record = RotaryEncodingRecord<B>
type Record = RotaryEncodingRecord<B>
§
fn load_record(
self,
record: <RotaryEncoding<B> as
Module<B>>::Record,
) -> RotaryEncoding<B>
fn load_record( self, record: <RotaryEncoding<B> as Module<B>>::Record, ) -> RotaryEncoding<B>
§
fn into_record(self) -> <RotaryEncoding<B> as
Module<B>>::Record
fn into_record(self) -> <RotaryEncoding<B> as Module<B>>::Record
§
fn num_params(&self) -> usize
fn num_params(&self) -> usize
§
fn visit<Visitor>(&self, visitor: &mut
Visitor)where
Visitor: ModuleVisitor<B>,
fn visit<Visitor>(&self, visitor: &mut
Visitor)where
Visitor: ModuleVisitor<B>,
§
fn map<Mapper>(self, mapper: &mut
Mapper) -> RotaryEncoding<B>where
Mapper: ModuleMapper<B>,
fn map<Mapper>(self, mapper: &mut
Mapper) -> RotaryEncoding<B>where
Mapper: ModuleMapper<B>,
§
fn collect_devices(
&self,
devices: Vec<<B as Backend>::Device>,
) -> Vec<<B as Backend>::Device>
fn collect_devices( &self, devices: Vec<<B as Backend>::Device>, ) -> Vec<<B as Backend>::Device>
§
fn to_device(self, device: &<B as Backend>::Device) -> RotaryEncoding<B>
fn to_device(self, device: &<B as Backend>::Device) -> RotaryEncoding<B>
§
fn fork(self, device: &<B as Backend>::Device) -> RotaryEncoding<B>
fn fork(self, device: &<B as Backend>::Device) -> RotaryEncoding<B>
§
fn devices(&self) -> Vec<<B as Backend>::Device>
fn devices(&self) -> Vec<<B as Backend>::Device>
§
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>
§
fn load_file<FR, PB>(
self,
file_path: PB,
recorder: &FR,
device: &<B as Backend>::Device,
) -> Result<Self, RecorderError>
fn load_file<FR, PB>( self, file_path: PB, recorder: &FR, device: &<B as Backend>::Device, ) -> Result<Self, RecorderError>
§
fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
§
impl<B> ModuleDisplay for RotaryEncoding<B>where
B: Backend,
impl<B> ModuleDisplay for RotaryEncoding<B>where
B: Backend,
§
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<B> ModuleDisplayDefault for RotaryEncoding<B>where
B: Backend,
impl<B> ModuleDisplayDefault for RotaryEncoding<B>where
B: Backend,
Auto Trait Implementations§
impl<B> Freeze for RotaryEncoding<B>
where
<B as Backend>::FloatTensorPrimitive:
Freeze,
<B as Backend>::QuantizedTensorPrimitive:
Freeze,
impl<B> RefUnwindSafe for RotaryEncoding<B>
where
<B as Backend>::FloatTensorPrimitive:
RefUnwindSafe,
<B as Backend>::QuantizedTensorPrimitive:
RefUnwindSafe,
impl<B> Send for RotaryEncoding<B>
impl<B> Sync for RotaryEncoding<B>
impl<B> Unpin for RotaryEncoding<B>
impl<B> UnwindSafe for RotaryEncoding<B>
where
<B as Backend>::FloatTensorPrimitive:
UnwindSafe,
<B as Backend>::QuantizedTensorPrimitive:
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,
§
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
more
Source§
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> 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
§
impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
§
fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string
,
but without panic on OOM.