Module 20. The complete LLM: from tokens to training and generation
Try each exercise first. Expand worked answers or project guidance when needed; their open state is shared between RU/EN.
Exercises 1–3 belong to the first route and Exercises 4–5 to the second. Read Section 19 before attempting Exercise 6: the SSM and diffusion comparison appears there to close the module with the scope of the construction.
Download course files (.zip) 2588.1 KBThe archive preserves the source folder structure, both languages, and supporting files.
Exercise 1. Reconstruct the final attention row
Use , the keys, and the values in Section 5 to calculate and . Why is the final key unavailable to row 1 even though the complete array already exists in memory?
Show answer and solution
Solution. Divide the three dot products by , then normalize their exponentials. The result is . Their weighted value sum is approximately . Multiplication by followed by addition of gives . For row 1, the score of key 2 is replaced by . Visibility is a property of the causal mask, not of whether an array has already been allocated.
Exercise 2. Count parameters and cached values
Use with an independent output projection. How many K/V scalars are stored after three tokens? What changes if ?
Show answer and solution
Solution. The two vocabulary tables contribute 32 parameters, Q and O contribute 32, K and V contribute 16, the FFN contributes 36, and the norms contribute 12: 128 in total. The cache holds scalars. A second K/V head adds 16 parameters, bringing the total to 144, and doubles the cache to 24 scalars. The concatenated width of the query-head outputs stays unchanged.
Exercise 3. Separate the model from its decoder
Apply top-2 and temperature 0.5 to the final logit row in Section 9. Does the model assign zero probability to EOS? What is the loss if only the EOS target is selected before any weight update?
Show answer and solution
Solution. The two retained logits differ by about , or after temperature scaling. The probability of A within the pair is therefore , leaving for B. The decoder assigns zero to EOS, while the model assigns 0.183502. With only that target selected, the loss is ; the mask denominator is one.
Exercise 4. Follow the gradient into a shared table
With tied embeddings, why can the EOS row receive a gradient when EOS is absent from the inputs ? Where do the two computational roles meet?
Show answer and solution
Solution. The output operation uses every row of , including EOS, so its logit derivative contributes to the EOS row. Input lookup contributes only to rows selected by input IDs. Both paths accumulate into one parameter tensor before the optimizer acts. The notebook's finite-difference check for tied embeddings includes both roles simultaneously.
Exercise 5. Explain the first update and the router
Why is the first AdamW step with zero initial moments independent of after bias correction? Why does that reasoning fail later? Check the selected experts and weights in Section 15.
Show answer and solution
Solution. The factors and cancel in the bias-corrected first moments, leaving and . Later moments contain earlier gradients, so the same cancellation is unavailable. The MoE offsets select experts 2 and 0, but the mixture normalizes the original scores 0.268941 and 0.5. Their weights are 0.349755 and 0.650245. Normalizing offset-adjusted scores would change the routing rule.
Exercise 6. Compare the two changes in Section 19
Calculate the states of with and inputs . Then verify that the final probabilities of the two-step reverse chain in Section 19 sum to one. Which index visits token positions, and which index refines an entire sequence?
Show answer and solution
Solution. The states are ; visits successive input positions. The reverse-chain probabilities sum to one. Its index changes the complete two-position sequence. Both computations repeat an operation, but they do not organize generation in the same way.