Attention
Attention is a mechanism that computes weighted sums of input tokens, allowing the model to focus on the most relevant parts of the sequence when generating each output.
Attention is the core building block of transformer models, introduced in the 2017 paper 'Attention Is All You Need'. It enables each token to 'look at' all other tokens in the sequence, assigning relevance scores (weights) that determine how much each token influences the representation of the current one. The most common form is scaled dot-product attention: each token is projected into query (Q), key (K), and value (V) vectors; the dot product between a query and all keys gives attention scores, which are normalized via softmax and used to blend the values. This mechanism gives transformers their global receptive field-every output token can directly attend to any input token, unlike RNNs which process sequentially. Multi-head attention runs several attention heads in parallel, each learning different relationships (e.g., syntax, semantics, coreference). Attention's O(n^2) complexity in sequence length n is both its strength and weakness: it enables long-range dependencies but makes long-context inference expensive. Variants like causal attention (masked to prevent future token leakage) are used in autoregressive LLMs. FlashAttention reimplements the algorithm to reduce memory reads/writes, achieving up to 10x speedup for long sequences. Sparse attention patterns (e.g., sliding window, global+local) sacrifice some expressivity for efficiency. Attention is often visualized as heatmaps showing token-to-token relevance, useful for interpretability. In decoder-only models like GPT, each token's attention is computed over all preceding tokens in the context window.
Attention(Q, K, V) = softmax(Q * K^T / sqrt(d_k)) * VAttention does not understand meaning-it is a statistical weighting mechanism that learns to correlate tokens based on training data, not symbolic reasoning.
Related terms
Concepts you'll usually encounter alongside this one.
FlashAttention is an I/O-aware algorithm that computes exact attention without materializing the full attention matrix, reducing memory usage from O(n^2) to O(n) and improving speed 2-10x on GPUs.
The KV cache stores intermediate key and value tensors from previous tokens during autoregressive generation, enabling subquadratic computation by avoiding redundant re-encoding of the full context.
A neural network architecture using self-attention mechanisms to process sequential data in parallel, forming the foundation of modern LLMs.
Used in benchmarks
Workloads on MyAI Bench that involve Attention.
Mentioned in tutorials
Hands-on guides that put this concept to work.
Stand up a fully functional local LLM stack on Ubuntu 22.04, NVIDIA drivers, CUDA, Ollama, and your first model, in under 30 minutes.
Run local LLMs on Windows 11 the right way, WSL2, the WSL CUDA toolkit, and Ollama with full GPU acceleration. Native installer covered too.
Apple Silicon is shockingly good at LLM inference. Get Ollama running with unified memory acceleration on any M-series Mac in 15 minutes.
DeepSeek's reasoning model rivals GPT-4 on math and code. Run the distilled 7B/14B/32B variants on consumer hardware in 30 minutes.
See how this term affects your memory budget.
Real numbers, real chips, every workload.
Picks for builders, by use case.
Stay Ahead of the AI Curve
Get weekly AI hardware news, benchmark updates, and deals in your inbox. Founding-subscriber list, be one of the first.