ALiBi (Attention with Linear Biases) is a positional encoding mechanism designed for transformer-based models, particularly for tasks that require long-range dependencies, such as language modeling and text generation. ALiBi was introduced in the paper "ALiBi: Attention with Linear Biases Enables Large Language Models" by Press et al. (2021).
Key Idea:
The core idea behind ALiBi is to modify the attention mechanism in transformers to incorporate linear biases that depend on the relative positions of the tokens. This allows the model to capture long-range dependencies more effectively, without relying on explicit positional encodings.
How ALiBi Works:
- Relative Positional Encoding: ALiBi uses a relative positional encoding scheme, where each token is represented by its relative position with respect to other tokens. This is different from traditional positional encodings, which use absolute positions.
- Linear Biases: ALiBi introduces linear biases that depend on the relative positions of the tokens. These biases are added to the attention weights, allowing the model to capture long-range dependencies.
- Attention Mechanism: The attention mechanism in ALiBi is modified to incorporate the linear biases. The attention weights are computed as follows:
Attention(Q, K, V) = softmax(Q * K^T / sqrt(d) + B * R) * V
where Q, K, and V are the query, key, and value matrices, respectively, d is the dimensionality of the embeddings, B is the linear bias matrix, and R is the relative positional encoding matrix.
Linear Bias Matrix (B):
The linear bias matrix B is a learnable parameter that depends on the relative positions of the tokens. It is computed as follows:
B = W * R
where W is a learnable weight matrix, and R is the relative positional encoding matrix.
Relative Positional Encoding Matrix (R):
The relative positional encoding matrix R is computed as follows:
R = [r_1, r_2, ..., r_n]
where r_i is the relative positional encoding of the i-th token, and n is the sequence length.
Advantages:
ALiBi has several advantages over traditional positional encoding mechanisms:
- Long-range dependencies: ALiBi can capture long-range dependencies more effectively, without relying on explicit positional encodings.