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§
Source§impl<B> RotaryEncoding<B>where
B: Backend,
impl<B> RotaryEncoding<B>where
B: Backend,
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>
Source§type InnerModule = RotaryEncoding<<B as AutodiffBackend>::InnerBackend>
type InnerModule = RotaryEncoding<<B as AutodiffBackend>::InnerBackend>
Source§fn valid(&self) -> <RotaryEncoding<B> as AutodiffModule<B>>::InnerModule
fn valid(&self) -> <RotaryEncoding<B> as AutodiffModule<B>>::InnerModule
Source§impl<B> Clone for RotaryEncoding<B>where
B: Backend,
impl<B> Clone for RotaryEncoding<B>where
B: Backend,
Source§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