AI Engineering: Transformers, LLM Architecture & Foundations
Target Audience: AI Researchers, Storage Architects, Systems Performance Engineers, and Infrastructure Specialists.
Core Objective: Master modern Generative AI from foundational neural mechanics and mathematical Transformer derivations to empirical scaling laws, checkpointing I/O bottlenecks, and GPUDirect Storage (GDS) architectures.
1. The Paradigm Shift: From Classical ML to Foundation Modelsβ
For decades, machine learning was defined by task-specific, supervised models. Building a sentiment analyzer or an entity extractor required manual feature engineering, custom architectures, and thousands of human-labeled samples:
Classical Machine Learning (Task-Specific, Supervised):
[Task Data] βββΊ [Feature Engineering] βββΊ [Train Specialized Model] βββΊ [Single Narrow Task]
Modern Foundation Models (Task-Agnostic, Self-Supervised):
[Trillions of Unlabeled Tokens] βββΊ [Next-Token Prediction] βββΊ [Base Foundation LLM]
β
βββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββ€
βΌ βΌ βΌ
[Zero-Shot Q&A] [Code Synthesis] [Autonomous Storage Agents]
Modern Large Language Models (LLMs) are foundation models. Trained on web-scale text and code through self-supervised next-token prediction, they internalize grammar, world knowledge, mathematical reasoning, and API semantics.
2. The Transformer Primitive: Mathematical Derivationβ
Introduced by Vaswani et al. in 2017, the Transformer replaced recurrent neural networks (RNNs) and LSTMs by eliminating sequential temporal recurrences. Instead of processing tokens step-by-step ( sequential operations), Transformers process all tokens simultaneously using Self-Attention.
Scaled Dot-Product Attention
Q K
β β
βββββΊ MatMul βββββ
β
βΌ
Scale (βd_k)
β
βΌ
Causal Mask (Optional)
β
βΌ
Softmax
β
βββββΊ MatMul ββββ V
β
βΌ
Output
Mathematical Formulationβ
Let input sequence , where is sequence length and is hidden dimension. The input is projected into three distinct vector spaces via learned projection matrices :
The Scaled Dot-Product Attention is computed as:
- Attention Scores (): Calculates the pairwise semantic correlation between every query token and key token.
- Scaling Factor (): Prevents the dot product from growing excessively large for high dimensions. For large , dot products have variance , pushing the softmax function into regions with extremely small gradients (), causing the vanishing gradient problem.
- Causal Mask (): An upper-triangular matrix where for and for . This ensures that in decoder-only models, token can only attend to preceding tokens , preventing information leakage from the future.
- Softmax Normalization: Computes row-wise probability distributions:
(where subtracting ensures numerical stability against floating-point overflow).
3. Scaling Laws: Compute, Parameters & Dataβ
AI researchers quantify model training efficiency using empirical Scaling Laws (Kaplan et al., 2020; Chinchilla / Hoffmann et al., 2022).
The Compute Equationβ
The total floating-point operations (, in FLOPs) required to pre-train a decoder-only Transformer is approximated by:
where:
- is the number of non-embedding model parameters.
- is the number of training tokens ingested.
- The factor of stems from: FLOPs per parameter per token in the forward pass + FLOPs per parameter per token in the backward pass (gradients with respect to activations and weights).
The Chinchilla Compute-Optimal Frontierβ
Hoffmann et al. demonstrated that for compute-optimal training, model parameters and training tokens should scale in equal proportion:
Practical Implication: A 70-billion parameter model (llama3.1:70b) requires at least:
Llama 3 was trained on 15 Trillion tokens (significantly "over-trained" beyond Chinchilla optimality) to produce smaller, faster models that minimize inference latency and storage footprint.
4. Storage Architecture for AI Training & High-Performance Computingβ
In multi-thousand GPU clusters (e.g., NVIDIA H100/B200 clusters), storage I/O is the primary source of GPU stall time and multi-million-dollar training delays.
+-------------------------------------------------------------------------------+
| Multi-Node GPU AI Training Cluster |
| |
| Node 1 (8x H100 GPUs) Node 2 (8x H100 GPUs) Node K (8x H100 GPUs)|
| +-----------------------+ +-----------------------+ +-------------------+|
| | GPU HBM3e Memory | | GPU HBM3e Memory | | GPU HBM3e Memory |
| | (80GB @ 3.35 TB/s) | | (80GB @ 3.35 TB/s) | | (80GB @ 3.35 TB/s)|
| +-----------+-----------+ +-----------+-----------+ +---------+---------+|
+--------------|---------------------------|-------------------------|----------+
| | |
v v v
+-------------------------------------------------------------------------------+
| 400 Gb/s RoCEv2 / InfiniBand Low-Latency Fabric (Lossless RDMA Network) |
+--------------------------------------+----------------------------------------+
β
βΌ GPUDirect Storage (GDS) Direct DMA
+-------------------------------------------------------------------------------+
| Parallel Distributed File System / NVMe-oF Tier (Lustre / WekaFS / CephFS) |
| - High Sustained Bandwidth (> 2 TB/s aggregate throughput) |
| - Sub-millisecond direct block metadata operations |
+-------------------------------------------------------------------------------+
1. The Checkpointing I/O Bottleneckβ
During distributed training, models must periodically write a complete state snapshot to non-volatile storage to survive hardware node failures.
A training state checkpoint consists of:
- Model Weights: 2 bytes per parameter in 16-bit (BF16/FP16).
- Gradients: 2 bytes per parameter.
- AdamW Optimizer States: 4 bytes for first momentum () + 4 bytes for second momentum () + 4 bytes for FP32 master weights = 12 bytes per parameter.
- Total Checkpoint Footprint: .
For a 70B parameter model:
If thousands of GPUs synchronize and dump 1.12 TB to disk every 30 minutes, a slow parallel storage array that takes 5 minutes to flush causes an immediate 16.6% GPU idle penalty. High-speed NVMe burst buffers and asynchronous checkpointing (e.g., Megatron-LM async flush) are mandatory.
2. GPUDirect Storage (GDS) & NVMe-oFβ
Traditional Linux I/O paths require the CPU to read data from storage into kernel bounce buffers, copy data to user space, and then issue a CUDA memory copy over the PCIe bus to GPU HBM:
NVIDIA GPUDirect Storage (GDS) utilizes PCIe Peer-to-Peer (P2P) Direct Memory Access (DMA):
- Bypasses CPU cores and OS Page Cache completely.
- Reduces end-to-end I/O latency from 50 ms down to sub-1 ms.
- Saturates PCIe Gen5 bandwidth ( bidirectional per slot).
5. Modern Decoder Blocks: State-of-the-Art Optimizationsβ
Input Token IDs
β
βΌ
[Embedding Layer + RoPE Positional Encoding]
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Transformer Decoder Layer (Repeated 32 to 80 Times) β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Input RMSNorm: x_norm = x / RMS(x) β β
β ββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββ β
β βΌ β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Grouped-Query Attention (GQA) with KV-Cache β β
β ββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββ β
β βΌ β
β [Residual Add (+)] β
β β β
β ββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββ β
β β Post-Attention RMSNorm β β
β ββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββ β
β βΌ β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β SwiGLU Feed-Forward Network: (xW1 * swish(xV)) W2 β β
β ββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββ β
β βΌ β
β [Residual Add (+)] β
ββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
βΌ
[Final RMSNorm]
β
βΌ
[Unembedding Head] βββΊ Logits over Vocabulary
6. Quantization & Model Sizing for Local Laptopsβ
Model weights are trained in 16-bit (FP16 or BF16, 2 bytes per weight). To compute the bare-minimum RAM/VRAM required to load a model for inference:
(where the multiplier accounts for activation memory and KV-cache overhead).
| Model Size | FP16 (2 Bytes) | Q8_0 (1 Byte) | Q4_K_M (0.5 Bytes) | Recommended Laptop Hardware |
|---|---|---|---|---|
| 3B (Llama 3.2) | 7.5 GB | 3.8 GB | 2.5 GB | Any M1/M2/M3 MacBook Air (8GB RAM) |
| 8B (Llama 3.1) | 20.0 GB | 10.0 GB | 6.0 GB | MacBook Pro / PC (16GB RAM) |
| 14B (Qwen 2.5) | 35.0 GB | 17.5 GB | 11.0 GB | MacBook Pro / PC (32GB RAM) |
| 32B (Qwen / DeepSeek) | 80.0 GB | 40.0 GB | 24.0 GB | High-End Workstation (64GB RAM) |
| 70B (Llama 3.3) | 175.0 GB | 87.5 GB | 50.0 GB | Mac Studio / Multi-GPU Rig (64GB-128GB RAM) |