Language Model (From Scratch) Sebastian Raschka, Manning, 2024 Local scripts Book chapters Focus 01–09 Chapter 2 Text data and embeddings 10–14 Chapter 3 Attention mechanisms 15–29 Chapters 4–5 GPT, training and pretrained weights This lecture follows the local study scripts 01–29. 02
Meaning Example x Input value 2 a Weight that multiplies the input 2 b Bias added to the result 1 y Prediction computed by the model 5 The model computes y from x. Parameters a and b define its rule. 03
a = 2 12 12 10 10 8 8 Output y Output y Changing a with b = 1 6 4 6 4 2 2 0 0 0 1 a=1, b=1 2 Input x 3 4 a=2, b=1 0 1 a=2, b=1 2 Input x 3 4 a=2, b=3 a controls how fast y increases. b is the output at x = 0. 04
predictions At x = 2 10 Output y 8 First prediction: 2 6 Target: 5 4 2 0 0 1 2 Input x Before: y = x After: y = 2x + 1 3 4 After adjustment: 5 Observed targets Training adjusts weights and biases to improve predictions. 05
b Input x = [2, 1] Output Multiply inputs by weights, then add Result First value 2×2+1×1+1 6 Second value −1 × 2 + 3 × 1 + 0 1 The same idea extends from one number to a vector. In a linear layer, W contains weights and b contains biases. 06
question Time Basics and text 01~09 How does text become numbers? ~30 min Token relationships 10~14 Which tokens contribute information? ~30 min Building GPT 15~23 How do the components connect? ~25 min Training and generation 24~29 How do we improve and use predictions? ~30 min Part 1 ends on slide 39. Continue with Part 2 next time if needed. 08
one batch 2 sequences T Tokens in each sequence 4 tokens C Features representing each token 768 numbers [B, T, C] Sequences × tokens × features [2, 4, 768] B, T and C keep the same meaning throughout this lecture. 09
is a test. After splitting Hello , world . This , is a test . Characters Total tokens Unique tokens 20,479 4,690 1,130 Total tokens count repeats. Unique tokens count each kind once. 10
Process ! 0 Deduplicate, sort, then assign IDs I 53 Deduplicate, sort, then assign IDs the 988 Deduplicate, sort, then assign IDs you 1126 Deduplicate, sort, then assign IDs vocab = {token: integer for integer, token in enumerate(all_words)} An ID is a lookup index. Its size does not express meaning. 11
painted Encoded IDs 988 602 533 746 Decoded text the last he painted Text to IDs IDs to text encode() decode() Encoding maps text pieces to IDs. Decoding maps IDs back to text. 12
Known token Its token ID Its token ID Unknown token Lookup error ID of <|unk|> Joining documents Handle boundaries manually Use <|endoftext|> Document A <|endoftext|> Document B Each special token also has its own vocabulary ID. 13
Start None yet aaabdaaabac 1 aa becomes X XabdXabac 2 ab becomes Y XYdXYac 3 XY becomes Z ZdZac Script 05 uses the existing GPT-2 merges. It does not train new BPE rules. 15
2 3 Input x Every effort moves you Target y effort moves you forward Input IDs 6109 3626 6100 345 Target IDs 3626 6100 345 2651 At each position, the model predicts the next token. 16
target Every effort Every effort moves Every effort moves you Every effort moves you forward A sequence of length 4 provides four next-token training targets. 17
vector Sum Position 0 [0.2, 0.4, 0.1] [0.1, 0.0, 0.3] [0.3, 0.4, 0.4] Position 1 [0.2, 0.4, 0.1] [0.0, 0.5, 0.1] [0.2, 0.9, 0.2] [8, 4, 256] + [4, 256] = [8, 4, 256] Position embeddings give the same token different inputs at different positions. 21
integer IDs 2. Embedding lookup Retrieve one vector per ID 3. Add positions Vectors now include order Look up token vectors, then add information about their positions. 22
piece of text ID 6109 Index into the embedding table Vector [0.2, −0.5, …] Numbers for attention and neural layers IDs select rows. Vectors contain the numbers used in model calculations. 23
Feature 2 Feature 3 Your 0.43 0.15 0.89 journey 0.55 0.87 0.66 starts 0.57 0.85 0.64 with 0.22 0.58 0.33 6 tokens Your journey starts with one step 3 numbers per token Input shape [6, 3] Query token one 0.77 0.25 0.10 Second row: journey step 0.05 0.80 0.55 Teaching example: fixed vectors, before Q/K/V projections Fixed example vectors: one token per row, one feature per column. 25
dot-product scores for journey 1.8 1.6 1.4 1.2 1.0 0.8 0.6 0.4 0.2 0.0 1.50 1.48 1.09 0.95 Your 0.84 journey starts with 0.71 one step score(journey, starts) = 0.55×0.57 + 0.87×0.85 + 0.66×0.64 This toy example scores input vectors directly. Learned Q and K come next. 26
GPT learns how to compare and combine. Operation Script 10 teaching example GPT attention Representations Fixed example input vectors Learned Q, K and V projections Scores xᵢ · xⱼ qᵢ · kⱼ / √dₖ Content to mix Original input vectors X Projected value vectors V Visible positions All positions in the example Current and earlier positions GPT head: Z = softmax(QKᵀ / √dₖ + M)V Q = XW_Q K = XW_K V = XW_V M masks future positions Direct dot products of input embeddings are a teaching simplification. 29
scaling 35% 30% 25% 20% 15% 10% 5% 0% Your journey starts Before scaling with one step After dividing by √2 Attention weights = softmax((Q × Kᵀ) / √dₖ) Divide scores by √dₖ before applying softmax. 32
starts with one step Purple: allowed Your 1 × × × × × Current and past tokens journey 1 1 × × × × starts 1 1 1 × × × with 1 1 1 1 × × Gray: blocked Future tokens Diagonal stays open one 1 1 1 1 1 × A token can use itself step 1 1 1 1 1 1 Each position can use itself and earlier tokens. 33
Item Value Meaning Total feature width 768 Features per token Number of heads 12 Groups computed in parallel Features per head 64 768 ÷ 12 Maximum context length 1,024 Positions in one model input Each of the 12 heads works with 64 of the 768 features. 36
of Q, K and V [2, 4, 768] Batch, tokens, features Split and transpose heads [2, 12, 4, 64] Batch, heads, tokens, head features Q × Kᵀ [2, 12, 4, 4] 2 batches × 12 heads, each with a 4 × 4 tokenscore table Weighted sum, then join heads [2, 4, 768] 768 features per token After combining heads, the output shape returns to [B, T, 768]. 38
IDs become vectors with positions Attention adds context from earlier tokens Next: stacking these components to build GPT This is a natural place to pause and continue in the next session. 39
Shape Role in this example Input IDs [2, 4] 2 sequences of 4 tokens Embeddings [2, 4, 768] Combine token and position vectors Dummy blocks and normalization [2, 4, 768] Pass-through placeholders Output logits [2, 4, 50257] Scores for 50,257 candidates at each position Scripts 16–21 replace the placeholder blocks with real components. 40
one token 2.0 1.0 0.0 -1.0 -2.0 Feature 1 Feature 2 Feature 3 Before normalization Feature 4 Feature 5 Feature 6 After normalization Layer normalization operates across the features of each token. 41
value = (x − mean) / √(variance + ε) Output = scale × normalized value + shift Parameter Initial value Role ε 0.00001 Stabilizes the denominator scale / shift 1/0 Learns the output scale and offset After normalization, apply learned scale and shift parameters. 42
features Input width 3,072 features Expand × 4, then apply GELU 768 features Return to the original width The same FFN acts on each position separately and keeps the token count. 44
B Tokens T Feature width C Input 2 3 768 First Linear 2 3 3,072 GELU 2 3 3,072 Second Linear 2 3 768 Both the input and final output have shape [2, 3, 768]. 45
20_example_deep_neural_network.py 13 − 10 = 3 Input and output with illustrative numbers 15 3 10 5 Input x = 10 Residual F(x) = +3 10 10 Output = 13 0 Input x Original input Output x + F(x) Added change In this illustrative example, adding a change of +3 turns 10 into 13. 46
With shortcuts Layer 1 0.000202 0.221698 Layer 2 0.000120 0.206941 Layer 3 0.000715 0.328970 Layer 4 0.001399 0.266573 Layer 5 0.005050 1.325854 First layer: 0.000202 / 0.221698 In this small experiment, the first layer's mean gradient is about 1,099× larger. 48
the block Output [2, 4, 768] Context mixing + feature transformation [2, 4, 768] Same shape, updated vector values A consistent shape lets us stack several blocks of the same kind. 50
IDs with shape [B, T] 12 blocks Shape stays [B, T, 768] Output scores [B, T, 50257] Vector values in the diagram are illustrative. Embeddings feed 12 blocks, a final normalization and an output layer. 51
we count Parameter count Explanation Actual total in this code 163,009,536 Separate embedding and output weights Total assuming weight tying 124,412,160 Count the shared matrix once fp32 weights only 621.8 MiB Training needs additional memory One embedding matrix: 50,257 × 768 This GPTModel stores token embeddings and output weights separately. 52
in this implementation (MiB) 7,000 6,248 6,000 5,000 4,000 3,198 3,000 2,000 1,000 1,550 622 0 small medium large XL These figures count fp32 weights only. 53
from their scale. 6.5 = (+1) × 1.625 × 2² Part Example Meaning Sign +1 Positive or negative Significand (mantissa) 1.625 Significant digits, including the leading 1 Exponent 2 2² = 4 sets the magnitude Normal binary value: significand = 1 + stored fraction More fraction bits give finer steps. More exponent bits allow a wider range. 55
stores a sign, a biased exponent and fraction bits. Sign s Stored exponent E Stored fraction bits 0 10000001 = 129 10100000000000000000000 FP32 bias = 127: e = 129 − 127 = 2 f = 0.625 6.5 = (+1) × (1 + 0.625) × 2² For normal FP32 values, the hidden leading 1 gives 24 bits of significand precision. 56
bits: 1 sign bit, 8 exponent bits and 7 fraction bits. Next number above 1 FP32 BF16 Value 1.0000001192… 1.0078125 BF16 keeps a similar magnitude range to FP32, with coarser spacing between values. 57
stored fraction bits. Both include 1 sign bit. Raw format E4M3FN E5M2 Largest finite magnitude 448 57,344 Next number above 1 1.125 1.25 E4M3 has finer precision. E5M2 covers a wider magnitude range. 58
formats and floating-point formats use their bits differently. Type Bit layout Raw values INT8 8-bit signed integer −128 to 127 INT4 4-bit signed integer −8 to 7 FP4 E2M1 1 sign + 2 exponent + 1 fraction 0, ±0.5, ±1, ±1.5, ±2, ±3, ±4, ±6 INT4 example: q = 3 Its meaning as a weight depends on the accompanying scale factor. The bit width tells you the storage size. The format tells you which values it can store. 59
Here, scale s means: reconstruct x̂ = q × s. Store: q = cast(x / s) Reconstruct: x̂ = q × s The scale extends the usable range. Rounding can still lose information. 60
= 0.01, INT4 s = 0.1. Zero point = 0. Original x INT8 q INT8 x̂ INT4 q INT4 x̂ −0.63 −63 −0.63 −6 −0.60 −0.24 −24 −0.24 −2 −0.20 0.33 33 0.33 3 0.30 0.68 68 0.68 7 0.70 q = clip(round(x / s), qmin, qmax) INT4 with s = 0.1 covers reconstructed values from −0.8 to +0.7. A smaller scale gives finer steps, but also reduces the range before clipping. 61
can lose small values when another value is much larger. Data / encoding Small group Large group Original values 0.01, 0.03, 0.05, 0.07 1, 3, 5, 7 One scale: s = 1 Stored INT4 q 0, 0, 0, 0 1, 3, 5, 7 Two scales Stored INT4 q s = 0.01 1, 3, 5, 7 s=1 1, 3, 5, 7 Same stored q = 7 7 × 0.01 = 0.07 7×1=7 More local scales can preserve detail, while adding scale metadata. 62
generate_text_simple.py Full output [B, T, 50257] Last position only [:, −1, :] Pick the largest score One token with argmax 50,257 candidates have IDs 0–50,256. Generation uses only the logits at the last input position. 63
probability p. Vertical: loss 4.0 3.5 3.0 2.5 2.0 1.5 1.0 0.5 0.0 0 0.2 p = 0.1 gives loss 2.30 0.4 0.6 0.8 1 1.2 p = 0.8 gives loss 0.22 A higher probability for the correct token means a lower loss. 65
After flattening Predicted logits [2, 3, 50257] [6, 50257] Target IDs [2, 3] [6] Loss Compare targets at each position Average over 6 predictions cross_entropy(logits.flatten(0, 1), targets.flatten()) CrossEntropyLoss takes logits as its input. 66
20,000 Training data 18,431 Used to update weights 15,000 Validation data 10,000 Used to check performance 5,000 2,048 0 Training characters Validation characters Context length: 256 Reduced from 1,024 here The code splits the original text by characters: first 90%, last 10%. 67
1 Clear previous gradients optimizer.zero_grad() 2 Compute the current loss loss = calc_loss_batch(...) 3 Compute gradients for the weights loss.backward() 4 Update the weights optimizer.step() Compute the loss, then update weights in a direction that reduces it. 68
same logits and different temperatures 100% 80% 60% 40% 20% 0% closer every effort forward T = 0.1 inches T=1 moves pizza toward you T=5 Divide logits by temperature before computing probabilities. 71
after filtering 70% 60% 50% 40% 30% 20% 10% 0% closer every effort forward inches Original probabilities moves pizza toward you After Top-3 Set logits outside the top k to −∞, then recompute probabilities. 72
or actions in script 26 Training 10 epochs with a new model Generation Up to 15 tokens, Top-k 25, temperature 1.4 Save model state model_state_dict Save optimizer state optimizer_state_dict This script trains a new model before generating samples. 73
restore Why it matters 1 Same GPT_CONFIG Match the weight shapes 2 Model state_dict Restore trained weights 3 Optimizer state_dict Restore accumulated optimizer state 4 model.train(), then 1 epoch Continue training Recreate the architecture, then restore model and optimizer states. 74
or loads Next step Script 28 The code in gpt_download.py Prepare the download helper Script 29 GPT-2 settings and weights Load values into GPTModel 28: urlretrieve(...) 29: download_and_load_gpt2(...) Script 28 downloads gpt_download.py. Script 29 requests the model weights. 75
Number of heads Width per head small 768 12 12 64 medium 1024 24 16 64 large 1280 36 20 64 xl 1600 48 25 64 Script 29 also sets context_length = 1024 and qkv_bias = True The current choice is GPT-2 XL, with context length 1,024 and QKV biases. 76
Destination in our code Required operation wte / wpe tok_emb / pos_emb Copy matching shapes c_attn weights and bias W_query / W_key / W_value Split into Q, K and V Linear-layer weights Linear layers in attention / FFN Transpose where needed All values assign(left, right) Error if shapes differ Match matrix names and axis order, check shapes, then copy the values. 77
What the script demonstrates Script 23 Random initialization How the generation loop works Scripts 24 and 26 Trained on the-verdict.txt Loss reduction and generation changes Script 29 Public pretrained GPT-2 weights Weight conversion and loading The generation procedure stays the same after loading pretrained weights. 78
Scripts Tokenization Text Integer IDs 01~05 Input representation IDs and positions Token vector 06~09 Context computation Token vector Updated token vectors 10~22 Generation Vocabulary scores One next token 23~29 Follow the text as it becomes IDs, vectors, scores and a new token. 79
changes in temperature. Brendan Bycroft's LLM Visualization https://bbycroft.net/llm Zoom into the matrices and repeated blocks of a language model. black7375's explanation (Korean) https://black7375.tistory.com/101 Review tokenizers, embeddings and the background of transformers. Explore the same computations interactively in these web visualizations. 80
become a vector? Why do we mask future tokens? Which numbers change during training? Why does generation repeat one token at a time? The appendix contains a script index and a glossary. 81
06_sliding_window.py 07_data_loader.py 08_token_embedding.py 09_position_encoding.py 10_self_attention.py 11_scaled_dot_product_attention.py 12_causal_attention.py 13_dropout.py 14_gpt_size.py 15_dummy_gpt_model.py Main concept Reading text and regex splitting Vocabulary and token IDs Encoding and decoding Unknown tokens and document boundaries GPT-2 BPE Inputs and targets shifted by one Windows and batches Token embeddings Learned position embeddings Simplified self-attention Q/K/V and scaling Causal masking Dropout Multi-head attention at GPT scale Dummy GPT architecture The concepts build on one another in script order. 82
21_transformer_block_test.py 22_gpt_model_test.py 23_text_generate.py 24_text_eval.py 25_sampling.py 26_sampling2.py 27_model_load.py 28_download_gpt_download.py 29_load_pretrained_gpt.py Main concept Normalization intuition LayerNorm class GELU and ReLU Feature transformation in the FFN Residual connections and gradients Transformer block Complete GPT model and size Generating one token at a time Loss, validation and training Temperature and Top-k Training, sampling and saving Checkpoint restoration Downloading the helper code Loading public GPT-2 weights The concepts build on one another in script order. 83
of text that the model processes Tensor An array of numbers organized along several axes Embedding A numeric vector associated with a token ID Attention A weighted combination of token information Logit A candidate-token score before softmax Loss A measure of prediction error for the target Gradient A derivative used to decide how to adjust weights Checkpoint A saved model or training state These terms connect the lecture to names used in the code. 84
uv sync, then choose a standalone script 22 / 24 / 26 Prepare large-model or training runs before the lecture 27 Requires model_and_optimizer.pth from script 26 28 / 29 Prepare the helper, then download GPT-2 weights uv run python 05_bpe.py Run commands from the repository root, where the-verdict.txt is located. 85
(From Scratch) https://www.manning.com/books/build-a-large-language-model-from-scratch Official book code by Sebastian Raschka https://github.com/rasbt/LLMs-from-scratch Transformer Explainer https://poloclub.github.io/transformer-explainer/ Brendan Bycroft's LLM Visualization https://bbycroft.net/llm black7375's explanation (Korean) https://black7375.tistory.com/101 Click an underlined title or URL to open the source. 86