0.04B Model Architecture Design, Mathematical Modeling, and Intuition

A comprehensive design blueprint for a modern 0.04B (35.93M parameter) autoregressive language model: covering conditional probability modeling, mixing console knob analogies, Chinchilla scaling law saturation, tensor shape flow, parameter breakdowns, AdamW memory mechanics, and FLOPs compute estimation.

Contents19 sections

“When constructing a skyscraper, the first step is never laying bricks on a dirt lot, but drafting an exact architectural blueprint—calculating foundation load tolerances, steel grades, and construction budgets. In large language models (LLMs), that blueprint is the Model Specification & Mathematical Architecture.”

For newcomers entering the field of large language models, dense jargon (RoPE, GQA, SwiGLU, PPL, AMP) and multi-billion-parameter figures often construct an intimidating cognitive barrier.

As the foundational bedrock of this curriculum, this guide dissects a 0.04B (35.93M parameter) modern language model from the inside out. We provide not only relatable real-world analogies, but also complete mathematical derivations and memory ledgers so you can build uncompromising engineering confidence before writing a single line of PyTorch code.

0.1 Foundational Intuition: Understanding LLMs from First Principles

0.1.1 What Is a Large Language Model? From Dictionaries to Autoregression

To silicon hardware, the entirety of human literature is merely a sequence of discrete numerical tokens:

x=(x1,x2,x3,,xT)\mathbf{x} = (x_1, x_2, x_3, \dots, x_T)

Modern Decoder-Only Transformer architectures focus mathematically on a single objective: Conditional Probability Modeling:

P(x)=t=1TP(xtx1,x2,,xt1)P(\mathbf{x}) = \prod_{t=1}^T P(x_t \mid x_1, x_2, \dots, x_{t-1})

In plain language: “Given all preceding words in the sequence, predict what word is most likely to come next.”

  • If the preceding context is ["Ahead", "of", "my", "bed"], the model calculates the probability of the next word being "bright" at 95%, "moonlight" at 2%, and so on.
  • This step-by-step sequential continuation is called Autoregression.

0.1.2 Understanding the Physical Nature of "Parameters"

Beginners frequently wonder: "What are model parameters in physical reality, and where do they reside?"

Diagram
  • Physical Reality: In source code and GPU VRAM, parameters are ordinary floating-point tensor matrices (such as torch.float32 or torch.float16).
  • Intuitive Metaphor: Picture sitting before a gigantic master mixing console equipped with 36 million tunable knobs (0.04B = 3.6×1073.6 \times 10^7 parameters).
  • Before Training (Random Initialization): Every knob is twisted arbitrarily according to a normal distribution. Play an input note, and the monitors emit deafening white noise (unintelligible token gibberish).
  • During Training (Backpropagation & Gradient Updates): We feed the model tens of thousands of master classical poems (460k entries). Calculus chain rules compute prediction errors, gently nudging all 36 million knobs toward harmonious intervals.
  • After Training (Convergence): When all 36 million knobs reach delicate equilibrium, the model naturally produces rhythmic, structured, and evocative stanzas in response to any prompt.

0.1.3 Why 0.04B (36M)? The Golden Sweet Spot for Domain Corpora

LLM parameter scales range across immense orders of magnitude:

  • Toy Miniatures (< 10M): Too few parameters restrict expressive capacity, forcing rote memorization of fixed phrases that collapse under slight prompt variations.
  • Hyperscale Industry Models (7B / 70B / 405B): Vast capabilities, but a single forward pass demands tens to hundreds of gigabytes of VRAM with multi-million-dollar training budgets—impractical for local end-to-end learning.
  • Why Is 0.04B (36M) the Ideal Sweet Spot for Classical Poetry?
  • Chinchilla Scaling Laws and Optimal Saturation:
  • Pioneering empirical research (the Chinchilla Law) establishes that optimal learning requires a balanced ratio between parameter volume and training tokens.
  • Our purified classical poetry corpus comprises 32.76 million tokens (31,126,208 train + 1,638,222 validation).
  • Scaling naively to 0.1B (82M parameters) produces severe data starvation, inducing shortcut memorization and catastrophic overfitting.
  • In contrast, 0.04B (35.93M parameters) and 32.76M tokens establish a near 1 : 1 optimal saturation ratio, allowing the network to absorb 460k poems deeply without memorization traps.
  • Rapid Iteration and High Throughput:
  • On a single consumer GPU or Intel Arc accelerator, 0.04B delivers sustained throughput of ~6,500 to 7,500 tok/s, doubling 0.1B iteration speeds.
  • Running a full training epoch takes ~70 minutes, while a 500-step preview completes in just ~40 minutes.
  • Accessible VRAM Demands:
  • Peak training memory for forward, backward, and AdamW states sits comfortably at ~1.8GB VRAM, enabling full execution on thin laptops, integrated GPUs, and standard desktop cards.

0.2 Modern LLM Architecture Blueprint: Operator Topology and Tensor Flows

Contemporary open-weight architectures (LLaMA-3, Qwen-2.5, DeepSeek) have thoroughly superseded original Transformer Post-LN and absolute positional embeddings with modern operator topologies:

0.2.1 End-to-End Data Flow and Operator Graph

Diagram

0.2.2 Tensor Shape and Computation Stage Flow

Stage / OperatorOperator NameInput Tensor ShapeOutput Tensor ShapeCore Computation & Physical Significance
1. TokenizationByte-Level BPERaw text string[B, T] (integer IDs)4,096 classical poetry vocabulary, 256 byte atoms, zero OOV
2. Vector EmbeddingToken Embedding[B, T][B, T, 512]Maps discrete token IDs into 512-dimensional continuous latent space
3. Attention Pre-NormPre-RMSNorm 1[B, T, 512][B, T, 512]Normalizes input variance to stabilize multi-layer gradient flow
4. Grouped AttentionGQA (8Q : 2KV) + RoPE[B, T, 512][B, T, 512]8 Query heads share 2 Key/Value heads; Rotary embeddings encode positions
5. Residual Addition 1Residual Add 1Dual [B, T, 512][B, T, 512]X=X+Attention(X)X = X + \text{Attention}(X), preserving identity highway
6. FFN Pre-NormPre-RMSNorm 2[B, T, 512][B, T, 512]Normalizes representation energy prior to non-linear expansion
7. Gated FFNSwiGLU[B, T, 512][B, T, 512]Expands to 1,408 dims with dual-channel gating Swish(Wgatex)Wupx\text{Swish}(W_{gate}x) \cdot W_{up}x
8. Residual Addition 2Residual Add 2Dual [B, T, 512][B, T, 512]X=X+SwiGLU(X)X = X + \text{SwiGLU}(X), fusing non-linear memory representations
... Layer Stack12x Transformer[B, T, 512][B, T, 512]Repeats attention and gating transformation across 12 stacked layers
9. Final NormalizationFinal RMSNorm[B, T, 512][B, T, 512]Enforces numerical stability before vocabulary score projection
10. Logits ProjectionLM Head (Weight-Tied)[B, T, 512][B, T, 4096]Reuses input embedding matrix to project hidden states into vocabulary logits
11. Decoding SamplerSoftmax / Constrained[B, T, 4096]Single Token IDSamples next token via temperature, Top-P, and metric rhythm constraints

0.3 Hyperparameter Derivations: The Engineering Rationale

Many tutorials present hyperparameter tables without justification. A rigorous ML architect must understand: what physical constraints and mathematical trade-offs govern each value?

0.3.1 Core Hyperparameter Specification Table

Architecture ParameterSymbol0.04B ValueIndustry BaselineEngineering Rationale & Trade-off
Vocabulary SizeVV4,096LLaMA-3 uses 128kSmall models must avoid oversized vocabularies to reserve parameter budget for hidden layers
Hidden Dimensiondmodeld_{model}512Lightweight standardBalances subspace expressiveness while dividing cleanly into head dimensions (8×64=5128 \times 64 = 512)
Layer DepthLL12Classic benchmark depthEnsures sufficient non-linear depth and hierarchical abstraction levels
Query Headsnqn_q8Per-head dk=64d_k=648 distinct multi-dimensional attention query subspaces
KV Headsnkvn_{kv}2GQA ratio 4Slashes KV-Cache memory footprint and memory bandwidth by 75%
Head Dimensiondkd_k64512/8=64512 / 8 = 64Aligns perfectly with GPU Tensor Core matrix multiplication block sizes
FFN Dimensiondffnd_{ffn}1,40883dmodel\approx \frac{8}{3} d_{model}Follows modern SwiGLU 2/3 parameter allocation scaling heuristics
Context WindowTT1,024Extended to 8k~128kChinese poems span < 100 characters; 1,024 tokens easily accommodates multi-stanza sets
Weight TyingWeight TyingTrueSmall model standardInput embeddings and output projection heads share parameters, saving 2.10M weights

0.3.2 Deep Dive: Rationales for Downscaling Vocabulary from 8192 to 4096

In frontier commercial models, LLaMA-3 uses 128k tokens and Qwen-2.5 uses 152k tokens. Why do we firmly mandate a 4,096 vocabulary for our 0.04B model?

Let us calculate the parameter overhead:

Embedding Parameters=vocab_size×dmodel\text{Embedding Parameters} = \text{vocab\_size} \times d_{model}
  • With an 8192 Vocabulary:
Paramsemb=8,192×5124.19 M\text{Params}_{emb} = 8,192 \times 512 \approx \mathbf{4.19\text{ M}}

In a 38M model, a single embedding matrix consumes 11.0% of the entire parameter budget! Over one-tenth of model capacity is squandered on memorizing token lookup tables instead of deep relational reasoning.

  • With an Optimized 4096 Vocabulary:
Paramsemb=4,096×5122.10 M\text{Params}_{emb} = 4,096 \times 512 \approx \mathbf{2.10\text{ M}}

Embedding parameters drop to just 5.5%! The saved 2.1M parameter budget directly feeds the 12 Transformer hidden layers (enriching attention and SwiGLU capacity).

  • Empirical Character Coverage in Chinese Poetry:
  • Deduplicating the entire 460k poem corpus yields only 10,840 unique Chinese characters;
  • The top 4,000 frequent characters account for 98.81% of all token occurrences;
  • The remaining 1.2% rare characters are cleanly covered by Byte-BPE's 256 foundational byte atoms with zero OOV risk;
  • Next-token prediction narrows from 1-of-8192 to 1-of-4096, cutting entropy by 1 bit and halving Softmax memory overhead for sharper convergence!

0.4 Microscopic Accounting: Distribution of the 35.93M Parameters

Let us inspect every individual component matrix inside the 0.04B architecture with caliper precision:

Module LevelComponent Matrix / OperatorTensor ShapeParameter FormulaExact Parameter Count
1. Token Embeddingtoken_embeddings[V=4096, D=512]4,096×5124,096 \times 5122,097,152 (2.10 M)
2. Single Transformer BlockAttention RMSNorm Scale[D=512]512512512
Query Projection WqW_q[D=512, n_q*d_k=512]512×512512 \times 512262,144
Key Projection WkW_k (GQA 2 heads)[D=512, n_kv*d_k=128]512×128512 \times 12865,536
Value Projection WvW_v (GQA 2 heads)[D=512, n_kv*d_k=128]512×128512 \times 12865,536
Attention Output WoW_o[n_q*d_k=512, D=512]512×512512 \times 512262,144
FFN RMSNorm Scale[D=512]512512512
SwiGLU Gate Projection WgateW_{gate}[D=512, d_ffn=1408]512×1408512 \times 1408720,896
SwiGLU Up Projection WupW_{up}[D=512, d_ffn=1408]512×1408512 \times 1408720,896
SwiGLU Down Projection WdownW_{down}[d_ffn=1408, D=512]1408×5121408 \times 512720,896
Single Block Subtotal--2,819,072 (2.82 M)
3. 12-Layer Stack12 Sequential Blocks12×Single Block12 \times \text{Single Block}12×2,819,07212 \times 2,819,07233,828,864 (33.83 M)
4. Final Normalizationnorm (Final RMSNorm)[D=512]512512512
5. Output Headlm_head (Tied Weights)[D=512, V=4096]Shared with Embedding0 (Reused)
Total Architecture ParametersMini-LLaMA-0.04B Golden Architecture-Exact Sum35,926,528 (35.93 M)

Physical Validation: Run in your terminal: python -c "from src.model import MiniLLaMAConfig, MiniLLaMAForCausalLM; print(sum(p.numel() for p in MiniLLaMAForCausalLM(MiniLLaMAConfig()).parameters()))", and the output displays precisely 35,926,528, exactly matching our manual calculation down to the last single parameter!

0.5 Memory and Compute Derivations: Calculating Training VRAM Footprints

Why do developers with 8GB graphics cards still encounter RuntimeError: CUDA out of memory? We divide training VRAM consumption into four distinct categories:

VRAM CategoryMemory Footprint (35.9M Model)Numerical Precision / BreakdownRole & Optimization Advice
1. Static Weights71.8 MBfp16 (2 bytes/param)Model weights loaded into GPU memory for forward passes.
2. Backward Gradients71.8 MBfp16 (2 bytes/param)First derivatives calculated during backprop matching weight shapes.
3. AdamW Optimizer State431.1 MBfp32 (12 bytes/param)<br>• Master fp32 weights (4B)<br>• First momentum (4B)<br>• Second momentum (4B)The largest static memory consumer during training; essential for numerical precision.
4. Dynamic Activations~800 MB ~ 1.2 GBScales with Batch Size & Sequence LengthStores intermediate layer outputs for backward pass computation.
Total Steady-State VRAM~1.5 GB ~ 2.0 GB-Runs smoothly on consumer laptops, integrated GPUs, and standard graphics cards.

0.5.1 Deep Dive: Why AdamW Requires 12 Bytes per Parameter

In Automatic Mixed Precision (AMP) training, forward and backward operations execute in 16-bit floats (2 bytes), but to prevent microscopic gradient updates from underflowing into zero, AdamW tracks states in full 32-bit floating point:

  • Master Weights: A full fp32 copy of model weights (4 bytes).
  • First Momentum mtm_t: Exponential moving average of past gradients (4 bytes).
  • Second Momentum vtv_t: Exponential moving average of squared gradients (4 bytes).
VRAMAdamW=35.93M×(4+4+4) bytes=35.93×12 MB431.1 MB\text{VRAM}_{\text{AdamW}} = 35.93\text{M} \times (4 + 4 + 4) \text{ bytes} = 35.93 \times 12\text{ MB} \approx \mathbf{431.1\text{ MB}}

Notice that optimizer state alone consumes 6 times more memory than the fp16 model weights themselves!

0.5.2 Compute Derivation: FLOPs Requirements and Training Time

Every multiply-accumulate operation (a×b+ca \times b + c) in a neural network counts as 2 floating-point operations (2 FLOPs).

  • Forward Pass: Each parameter participates in approximately one multiply-accumulate operation:
FLOPsfwd2×Nparams×Ttokens\text{FLOPs}_{fwd} \approx 2 \times N_{\text{params}} \times T_{\text{tokens}}
  • Backward Pass: Backpropagation computes gradients for both activations and weight tensors, requiring double the forward compute:
FLOPsbwd4×Nparams×Ttokens\text{FLOPs}_{bwd} \approx 4 \times N_{\text{params}} \times T_{\text{tokens}}
  • Total Forward + Backward Compute per Token:
FLOPstotal6×Nparams6×3.59×1072.16×108 FLOPs/token\text{FLOPs}_{total} \approx 6 \times N_{\text{params}} \approx 6 \times 3.59 \times 10^7 \approx \mathbf{2.16 \times 10^8 \text{ FLOPs/token}}

💡 Compute Budget Estimation: Training 3 Full Epochs on 100M Tokens

Total floating-point operations:

Total Compute=2.16×108×1082.16×1016 FLOPs\text{Total Compute} = 2.16 \times 10^8 \times 10^8 \approx \mathbf{2.16 \times 10^{16} \text{ FLOPs}}
  • On a consumer GPU running at ~7,000 tok/s:
Time=108 tokens7,000 tok/s14,285 seconds3.96 hours\text{Time} = \frac{10^8 \text{ tokens}}{7,000 \text{ tok/s}} \approx 14,285\text{ seconds} \approx \mathbf{3.96 \text{ hours}}
  • A quick 500-step preview run (16.38M tokens) finishes in just ~40 minutes!

0.6 Common Pitfalls and Conceptual FAQ

Q1: Why not stack layers deeper (e.g., 48 layers) with smaller hidden dimensions?

Answer: This embodies the classic trade-off between "deep & narrow" versus "shallow & wide".

  1. Parallel Efficiency: Each Transformer block must wait for preceding layer outputs to complete (sequential execution). Excessive depth increases GPU core idle times and kernel invocation latencies.
  2. Gradient Highway Friction: Despite Pre-LN and residual connections, signals in extremely deep networks suffer representation drift. For a 0.04B model, 12 layers with dmodel=512d_{model}=512 represents the empirically verified optimum.

Q2: Does pretraining actually instill commonsense knowledge, or just parrot text?

Answer: Autoregressive modeling appears to simply predict next characters, but accurately continuing complex passages forces the 12-layer attention network to construct structured concept representations in latent space. For example, predicting "frost" after "Ahead of bed bright moonlight" requires the model to align bed, moon, and Tang poetic imagery within high-dimensional space. This structured probabilistic modeling forms the genesis of language intelligence.

0.7 Chapter Summary and Next Steps

In this guide, we finalized the top-level architecture for our 0.04B golden model:

  • Specifications: V=4096,dmodel=512,L=12,dffn=1408,nq=8,nkv=2V=4096, d_{model}=512, L=12, d_{ffn}=1408, n_q=8, n_{kv}=2, locking in 35,926,528 parameters;
  • Hardware Profile: Steady-state VRAM requirements of 1.5GB ~ 2.0GB, training locally in 40 minutes to 3.5 hours;
  • Architecture Strategy: Fully aligned with modern open-weight architectures (RMSNorm + RoPE + SwiGLU + GQA).

The blueprints are drawn and tools prepared! In the next guide, we dive into raw byte streams and build our own tokenizer from scratch: Chapter 01 | Implementing a Byte-Level BPE Tokenizer from Scratch.

REFERENCES

References

  1. 01Training Compute-Optimal Large Language Models (Chinchilla Paper)
  2. 02Attention Is All You Need (Vaswani et al.)
  3. 03GLU Variants Improve Transformer (Shazeer)
  4. 04GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints

Series

Building an LLM from scratch

Next step

Continue with related topics

Continue along the same topic.

Browse latest news