New Material: Autoregressive (x, y) Batches and get_batch
Agent-Assisted Engineering Check
Prerequisites
Tokenization from Days 01-03, cross-entropy from Day 36, and the forward pass from Day 48.
Learning Objective
Build causal input-target pairs from a flat token array, sample batches with get_batch, size the batch tensors, and check the first loss against ln(V).
Instructions
This is the only new topic today; the rest of the day reviews Week 7. A decoder predicts token t+1 from tokens up to t. For context length L, slice a window of L + 1 tokens, chunk = tokens[i : i + L + 1], then set x = chunk[:-1] and y = chunk[1:]. Both have shape (L,). A batch stacks B random windows into x and y of shape (B, T). The model returns logits of shape (B, T, V); F.cross_entropy wants (N, V) and (N,), so flatten with logits.view(-1, V) and y.view(-1). Before training, every token is equally likely, so the first loss should be close to ln(V).
Practice & Prediction Log
1. For tokens = [502, 1204, 33, 991, 88, 412] and L = 4 starting at index 0, write x and y. Why does the window need L + 1 tokens?
2. Write get_batch(data, batch_size, block_size, device).
3. How many bytes do x and y take together for B = 64, T = 1024 as int64? As int32?
4. Write the cross_entropy call with the right flattening, and state the expected first loss for V = 50,257.
5. What goes wrong if y = x (no shift)?
x takes tokens[0:4] and y takes tokens[1:5]. A batch holds 64 * 1024 = 65,536 tokens per tensor. The expected first loss is ln(50257).
1. x = [502, 1204, 33, 991], y = [1204, 33, 991, 88]. L supervision pairs (x_t, y_t) need L inputs plus one extra token for the last target.
2. get_batch:
def get_batch(data, batch_size, block_size, device):
ix = torch.randint(len(data) - block_size, (batch_size,))
x = torch.stack([torch.from_numpy(data[i:i + block_size].astype(np.int64)) for i in ix])
y = torch.stack([torch.from_numpy(data[i + 1:i + 1 + block_size].astype(np.int64)) for i in ix])
return x.to(device), y.to(device)3. 65,536 tokens * 8 bytes = 524,288 bytes per tensor, so x and y take 1,048,576 bytes (1.0 MiB) as int64 and 524,288 bytes (512 KiB) as int32.
4. loss = F.cross_entropy(logits.view(-1, logits.size(-1)), y.view(-1)). The expected first loss is ln(50257) ≈ 10.8249. A first loss far above this, or NaN, points to broken initialization or a shape bug.
5. With y = x the model only has to copy its input, the loss falls toward zero, and it learns nothing about predicting the next token.
Learner derives the shifted (x, y) pairs, writes a working get_batch, sizes the batch tensors exactly, and uses ln(V) to sanity-check the first loss.
Authoritative Source Resources & Provenance
nanoGPT: train.py pretraining loop
Relevant Section / Range: train.py: get_batch(split): x = data[i:i+block_size], y = data[i+1:i+1+block_size]
Verified Version & Date: Git master commit 71c3905 (Verified: 2026-10-30)
Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.
Python, PyTorch, and Day 46-48 Transformer model architecture.
Runnable pretraining scripts and step-by-step tensor mechanics are fully self-contained in the curriculum lessons.
Compared Candidate Sources & Selection Analysis (2)
Heavy abstraction layer wrapping distributed training; nanoGPT exposes raw PyTorch loops without framework overhead.
High-performance enterprise distributed framework; unsuitable for local educational first-principles comprehension.
CS336 Assignment 1: Basics (Spring 2025 tagged release)
Relevant Section / Range: README setup; tests/adapters.py; tests/test_optimizer.py; tests/test_nn_utils.py; tests/test_serialization.py
Verified Version & Date: spring2025 tag; README, tests and MIT LICENSE checked 2026-11-01
Lead assignment-driven source for pretraining component practice and tests. nanoGPT remains a compact historical code-reading companion, not the current recommended training curriculum.
Fluent Python, PyTorch, and linear algebra
Local self-contained pretraining scripts run on standard local CPUs or budget GPUs without Stanford cluster dependency.
Compared Candidate Sources & Selection Analysis (2)
Simpler code layout, useful complementary reference for minimal PyTorch Transformer.
High-level API focus rather than building from raw tensor fundamentals.