Volume IV

Transformers & Attention

The complete theory of attention mechanisms, transformer architectures, optimization techniques, and KV-cache systems.

16 chapters published
Part 1

Attention Mechanisms

1.

Scaled Dot-Product Attention

The foundational attention mechanism: derivation of query-key-value formulation, the scaling factor, softmax temperature, attention as soft dictionary lookup, and computational complexity analysis.

2.

Types of Attention Mechanisms

A comprehensive taxonomy of attention: multi-head, multi-query, grouped-query, linear attention, sparse attention, sliding window, flash attention, and cross-attention — with complexity analysis and use cases for each.

3.

Multi-Head Attention: Full Mechanics

Complete derivation of multi-head attention: why multiple heads, the projection matrices, concatenation, parameter count, and how heads specialize — positional, syntactic, and semantic attention patterns.

4.

Sliding Window Attention

Fixed-window local attention: derivation, effective receptive field across layers, dilated variants, global+local hybrid patterns (Longformer), and memory-complexity analysis for long-context models.

5.

Flash Attention

IO-aware exact attention: the tiling algorithm, online softmax trick, memory hierarchy exploitation, FlashAttention-2 improvements, and FlashAttention-3 with FP8 — achieving 2-4x speedup without approximation.

6.

Linear Attention & Kernel Methods

Breaking the quadratic barrier: kernel decomposition of softmax, feature maps (elu, random Fourier), causal linear attention, connection to RNNs, RetNet, RWKV, and the expressiveness-efficiency tradeoff.

7.

Sparse Attention Patterns

Sub-quadratic attention through sparsity: strided patterns, block-sparse, BigBird (random+window+global), Routing Transformer, hash-based attention (Reformer), and learned sparsity — complexity proofs for each.

8.

Cross-Attention & Conditioning

Attention between two sequences: encoder-decoder cross-attention, text-to-image conditioning (Stable Diffusion), retrieval-augmented generation, adapter cross-attention, and the mathematical properties of cross vs self attention.

Part 3

Transformer Optimization