pub struct SparsePositionEncoding {
pub max_seq_len: usize,
pub dim: usize,
pub n_buckets: usize,
pub bucket_embeddings: Vec<Vec<f32>>,
}Expand description
Sparse relative position bias for attention (Raffel et al. T5-style, sparse variant).
Rather than dense relative position embeddings, only a subset of relative distances receive learned biases (sparsifying the position information).
Fields§
§max_seq_len: usizeMaximum sequence length.
dim: usizeEmbedding dimension.
n_buckets: usizeNumber of distinct relative distance buckets.
bucket_embeddings: Vec<Vec<f32>>Bucket embedding table (n_buckets × dim).
Implementations§
Source§impl SparsePositionEncoding
impl SparsePositionEncoding
Sourcepub fn new(
max_seq_len: usize,
dim: usize,
n_buckets: usize,
rng: &mut impl Rng,
) -> Self
pub fn new( max_seq_len: usize, dim: usize, n_buckets: usize, rng: &mut impl Rng, ) -> Self
Initialize with random bucket embeddings.
Sourcepub fn relative_position_bucket(&self, rel_pos: i32) -> usize
pub fn relative_position_bucket(&self, rel_pos: i32) -> usize
Map relative distance to a bucket index (logarithmic bucketing for large gaps).
Sourcepub fn position_bias_indices(&self, seq_len: usize) -> Vec<usize>
pub fn position_bias_indices(&self, seq_len: usize) -> Vec<usize>
Compute position bias matrix: shape (seq_len × seq_len × dim). Returns a flat vector of (seq_len × seq_len) bucket indices.
Auto Trait Implementations§
impl Freeze for SparsePositionEncoding
impl RefUnwindSafe for SparsePositionEncoding
impl Send for SparsePositionEncoding
impl Sync for SparsePositionEncoding
impl Unpin for SparsePositionEncoding
impl UnsafeUnpin for SparsePositionEncoding
impl UnwindSafe for SparsePositionEncoding
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
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