TensoriumInterview preparation
← Home

Core concepts · 62 topics

A guided path through the ideas behind modern LLMs.

Read the stages in order: how text becomes a prediction, what happens inside the Transformer block, how a model learns, then serving, scale, and post-training. Every topic names what to know first, then gives the intuition, the math with concrete numbers, the common misconception, and a self-check. Already comfortable with the basics? Switch to By area and jump straight to a topic.

New to machine learning? Start here instead.

These topics work best as a refresher; they assume ideas like probability and loss. Begin with three short beginner lessons (tokens, embeddings, next-token prediction), then the visual walkthrough for attention and the Transformer.

Finish line: You can explain how text becomes a prediction and what training does to improve that prediction.

Topic 20 of 62 · How a model learns

Effective batch and accumulation

Easy

How does gradient accumulation emulate a larger batch size, and when does the equivalence break?

Why this mattersGradient accumulation lets you reach a target optimization batch size when activations do not fit in memory, though contrastive learning and uneven sequence lengths need special care.

1 · Start with the intuition

Because the gradient of an average loss is the average of the per-example gradients, you do not need to fit the entire optimization batch in GPU memory at once. Gradient accumulation splits a large target batch into a sequence of smaller microbatches: for each microbatch, you run forward and backward while keeping the weights θ fixed, let the gradients add up in .grad, and call optimizer.step() and zero_grad() only after all microbatches have been processed.

2 · How the mechanism works

Mechanism step 1 of 3 · no question to answer

Run one microbatch at a time

Forward and backward one microbatch of size b_micro so activation memory stays bounded by b_micro.

Next, we will look at: Scale & accumulate into .grad. Move on when this operation makes sense.

Visualizing the behavior

One optimizer step per 4 microbatches (DDP)

Illustrative
Forward + backward
mb 1
mb 2
mb 3
mb 4
Gradient all-reduce
all-reduce
Optimizer
step
time (illustrative)
Gradients add up in .grad while the weights stay fixed. With DDP, no_sync() skips the all-reduce on the first three microbatches, so communication happens once and overlaps the last backward pass.

Microbatch A: 10 tokens, mean loss 2.0 · B: 90 tokens, mean loss 1.0

Derived
Mean of microbatch means
1.50
Token-weighted mean
1.10
Averaging per-microbatch means gives the short microbatch 5× too much weight per token. Accumulate summed losses and divide by the total token count to match the full-batch objective.