Chapter 21: Modern LLMs: RoPE, Flash-GQA, Q8_0/Q4_0, GGUF & LoRA
"Autoregressive token decoding is 95% memory bandwidth; quantization trades spare ALU flops for 4x to 7x DRAM bandwidth relief."
21.1 From GPT-2 to the Modern LLaMA Architecture
Next-token decoding loads billions of weight bytes to perform a single vector-matrix product (GEMV) per token. RCRE v2.0 implements the modern open-weights transformer stack in include/rcre_llm.h and src/transformer/llm.c:
- RMSNorm: eliminates mean-centering subtraction: $\(\text{RMSNorm}(\mathbf{x})_i = w_i \cdot \frac{x_i}{\sqrt{\frac{1}{D}\sum_{j=0}^{D-1} x_j^2 + \epsilon}}\)$
- Rotary Position Embeddings (RoPE): rotates complex pairs \((x_{2k}, x_{2k+1})\) inside query and key vectors by \(\theta_k = \text{base}^{-2k/d}\) at token position \(p\), encoding relative distance purely inside the inner product \(\langle \mathbf{q}_p, \mathbf{k}_q \rangle\).
- SwiGLU FFN: \(\text{SwiGLU}(\mathbf{x}) = (\text{SiLU}(\mathbf{x}\mathbf{W}_{\text{gate}}) \odot (\mathbf{x}\mathbf{W}_{\text{up}}))\mathbf{W}_{\text{down}}\) where \(\text{SiLU}(z) = \frac{z}{1 + e^{-z}}\).
- Grouped-Query Attention (GQA): shares \(H_{\text{kv}} \ll H_q\) key/value heads across query groups, shrinking KV cache DRAM footprint proportionally.
21.2 Tiled Online-Softmax Flash-Attention
Instead of allocating an \(\mathcal{O}(T)\) attention-score scratch vector before normalizing, rcre_llm_flash_attention_gqa() tracks a single running maximum \(m\) and denominator \(l\) across sequence steps \(t = 0 \dots T-1\):
A final division \(\mathbf{o} \leftarrow \mathbf{o} / l\) matches exact two-pass softmax with zero heap allocations.
21.3 Block Quantization (Q8_0, Q4_0) & Zero-Copy GGUF v2/v3
Per-block quantization compresses weights in 32-element groups sharing one IEEE FP16 scale factor \(d\):
RCRE_BlockQ8_0(34bytes /32weights):1FP16 scale +32int8quants (\(3.76\times\) smaller than FP32).RCRE_BlockQ4_0(18bytes /32weights):1FP16 scale +16packed nibble bytes (\(7.1\times\) smaller than FP32).
On ARM NEON (rcre_vec_dot_q8_0()), blocks widen int8x8_t -> int16x8_t -> int32x4_t -> float32x4_t inside registers without materializing dequantized weight matrices in RAM. src/transformer/gguf.c binds these quantized tensors directly via rcre_io_mmap_open() using aligned GGUF v2/v3 metadata, paired with hash-indexed RCRE_ByteBPE, nucleus/min-\(p\) RCRE_Sampler, and rank-\(r\) RCRE_LoRAAdapter fine-tuning (\(\mathbf{W}_0 + \frac{\alpha}{r}\mathbf{B}\mathbf{A}\)).
21.4 Chapter Project: Q8_0 Block Quantizer, RoPE & Online Softmax
Companion code in examples/ch21/main.c (make ch21) implements symmetric Q8_0 block quantization/dot-product, 2D RoPE rotation, and single-pass online-softmax attention verified against exact double-pass reference values under ASan and UBSan.