Skip to main content

AlibiPositionEncoding

Struct AlibiPositionEncoding 

Source
pub struct AlibiPositionEncoding {
    pub config: PositionEncodingConfig,
}
Expand description

ALiBi (Attention with Linear Biases)

Used in models like BLOOM. Instead of adding position embeddings to inputs, ALiBi adds a bias to attention scores that linearly penalizes distance. This allows extrapolation to longer sequences than seen during training.

Reference: “Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation” https://arxiv.org/abs/2108.12409

Fields§

§config: PositionEncodingConfig

Configuration

Implementations§

Source§

impl AlibiPositionEncoding

Source

pub fn new(config: PositionEncodingConfig) -> Result<Self>

Create a new ALiBi position encoding

Source

pub fn build_bias_graph(&self, graph: &mut EinsumGraph) -> Result<Vec<usize>>

Build einsum graph for ALiBi bias

ALiBi adds linear biases to attention scores based on query-key distance: bias(i, j) = -m * |i - j| where m is a head-specific slope

Input tensors:

  • 0: attention_scores [batch, n_heads, seq_len, seq_len]
  • 1: alibi_slopes [n_heads] (precomputed slopes, one per head)
  • 2: distance_matrix [seq_len, seq_len] (|i - j| for all positions)

Output tensors:

  • output: [batch, n_heads, seq_len, seq_len] (scores with ALiBi bias)
Source

pub fn n_heads(&self) -> usize

Get the number of attention heads

Source

pub fn compute_slopes(&self) -> Vec<f64>

Compute ALiBi slopes for each attention head

Slopes are computed as: m_i = 2^(-8i/n) for i in 1..n_heads This gives different rates of distance penalty per head

Trait Implementations§

Source§

impl Clone for AlibiPositionEncoding

Source§

fn clone(&self) -> AlibiPositionEncoding

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AlibiPositionEncoding

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.