36 lines
1.3 KiB
Markdown
36 lines
1.3 KiB
Markdown
# 🎯 AIRA Attention Mechanisms (`ai/attention`)
|
|
|
|
[](https://github.com/adameehanairagroup/Airalang)
|
|
[](https://github.com/adameehanairagroup/Airalang)
|
|
[](https://github.com/adameehanairagroup)
|
|
|
|
The `ai/attention` package provides native implementations of modern self-attention and cross-attention architectures.
|
|
|
|
---
|
|
|
|
## 🧮 Mathematical Formulation
|
|
|
|
### Scaled Dot-Product Attention
|
|
$$ ext{Attention}(Q, K, V) = ext{softmax}\left(rac{Q K^T}{\sqrt{d_k}}
|
|
ight) V$$
|
|
|
|
In pure `.aira`:
|
|
|
|
```aira
|
|
import "ai/attention";
|
|
|
|
// Instantiate MultiHeadAttention with 8 heads across 512 dimensions
|
|
let mha = new MultiHeadAttention(512, 8);
|
|
|
|
// Forward pass: Query, Key, Value, and Causal Attention Mask
|
|
let context_vectors = mha.forward(Q, K, V, causal_mask);
|
|
```
|
|
|
|
### Rotary Positional Embeddings (RoPE)
|
|
Enables transformers to dynamically encode relative token positions without hard positional tables:
|
|
|
|
```aira
|
|
let rope = new RotaryEmbedding(dim_per_head=64, max_seq_len=4096);
|
|
let q_rotated = rope.apply(Q, position_ids);
|
|
let k_rotated = rope.apply(K, position_ids);
|
|
```
|