FDE Times dataFDE jobs open 932▼14 Change in open roles over 7 days, like-for-like: roles from companies we started tracking this week are left outNew in 7 days 62Companies hiring 202Remote-friendly 24%Median US pay $201kTop hirer Databricks 92
中文VI

The newspaper of the Forward Deployed Engineer

Books & courses

The 368-page book that lets you explain LLMs on a whiteboard

Sebastian Raschka's book won't make you a researcher, but it makes you write attention, fine-tuning and LoRA yourself, and the code runs on a laptop.

Cover of Build a Large Language Model (From Scratch)
Build a Large Language Model (From Scratch) · Sebastian Raschka · Cover: Open Library
ShareLinkedInFacebookX

In brief

  • The book runs to 368 pages and was published by Manning in September 2024. Chapters 2 to 7 move from text data and attention to assembling a GPT, pretraining and two kinds of fine-tuning.
  • The code in the official repo runs on an ordinary laptop. The KV cache is in the chapter 4 bonus material; LoRA is in appendix E.
  • The book is harder and slower going than you expect. Read chapters 2-4 closely, then choose chapter 6 or 7 depending on the problems your clients usually bring.
A large figure, 220,100 token passes without a KV cache, sits above a bar spanning the full width. Below it, 1,200 passes with a cache, shown as a thin bar drawn to the same scale. Both are for a 1,000-token prompt with 200 further tokens generated.
For a 1,000-token prompt and 200 generated tokens, a KV cache cuts token passes from 220,100 to 1,200, because the keys and values of earlier tokens need not be recomputed. Each new token must still attend to every stored key. Source: rough estimate in the article, based on the KV cache bonus material in chapter 4 of the rasbt/LLMs-from-scratch repo (Sebastian Raschka).

Double the length of a prompt and the attention computation grows fourfold. Clients notice the bill before they understand the cause, and sooner or later one of them asks why. If you have only ever called a model through an API, your answer will be vague. If you have written the attention mechanism yourself, you can draw it on a whiteboard in two minutes.

That is what Sebastian Raschka’s Build a Large Language Model (From Scratch) sets out to give you. The book runs to 368 pages and was published by Manning in September 2024 (ISBN 9781633437166). According to Manning, you write a base model yourself without relying on any existing LLM library, then turn it into a classifier and an instruction-following chatbot.

For engineers deploying LLMs for clients, this is a practical book. It helps you understand what you are deploying well enough to diagnose failures, estimate costs and choose the right way to customize a model.

A book to type, not just to read

The official repo, rasbt/LLMs-from-scratch, follows the order of someone who has to make a model actually run. Chapter 2 handles text data, chapter 3 covers attention, chapter 4 assembles a GPT model, chapter 5 is pretraining, and chapters 6 and 7 cover two kinds of fine-tuning. Appendix E deals with LoRA.

The code is written to run on an ordinary laptop in a reasonable time, and uses a GPU automatically if the machine has one. Manning asks only for intermediate Python and some machine learning background. Raschka also offers a free YouTube course that accompanies the book, useful when you get stuck on a chapter.

Why does a prompt twice as long cost four times as much attention?

The attention chapter makes you compute a score for every pair of tokens yourself. The arithmetic is simple: a sequence of n tokens produces an n×n matrix. At 1,000 tokens the matrix has 1 million cells. At 2,000 tokens it has 4 million.

The bonus material for chapter 4 in the repo includes a KV cache. Raschka explains that the cache stores the key and value vectors already computed, so they can be reused when generating new tokens.

A rough estimate shows the difference. Take a 1,000-token prompt from which the model generates another 200 tokens. Without a cache, each generation step reprocesses the whole sequence, from 1,001 up to 1,200 tokens, for a total of 220,100 token passes.

With a cache, the model processes the 1,000 tokens once, then takes 200 steps of one new token each: 1,200 passes in total. That figure is not the total compute cost. Each new token still has to attend to every stored key, so the saving comes from not recomputing keys and values for earlier tokens; the attention itself does not disappear.

Raschka is also candid about the price: the code becomes more complex, and memory grows with sequence length. So when a client complains about latency, you know what to ask: how long is the prompt, how long is the output, is the cache on, and how much memory does the machine have left.

Does the client need labels or generated text?

Chapters 6 and 7 separate two different jobs. Classification fine-tuning turns the model into a labeler. Instruction fine-tuning teaches the model to respond to requests. A client who wants support tickets sorted automatically into five groups has a chapter 6 problem, measured by accuracy on a test set.

If the client instead wants an assistant that drafts replies to tickets, you are in chapter 7, and evaluation gets much harder. The freer the output, the less there is a single correct answer to compare against. You therefore need a repeatable scoring method before you can claim the fine-tuned model beats the original.

On site with a client, before anyone discusses fine-tuning, write down whether their problem is labeling or text generation. The answer decides both the approach and the evaluation.

Two traps: skimming and rushing to fine-tune

An independent reader writing on DEV Community said the book demands a lot of effort and that finishing it may take longer than you expect. Reading only to know the material is not enough. The n×n arithmetic only really sinks in when you change the sequence length yourself and watch the matrix size change on screen.

The second trap appears when you bring the book to a client: jumping straight to instruction fine-tuning. You can lose a whole week on chapter 7 when all the client needed was a ticket router measured by accuracy.

It also helps to know what the book leaves out. Another review calls it a first-principles, line-by-line guide. But its author notes that some topics, such as top-p sampling and RLHF, are only mentioned in passing or dropped entirely.

What order should you read it in?

Read chapters 2 to 4 very closely and retype the code. Chapter 5 on pretraining can be read faster. Then choose chapter 6 or 7 depending on the kind of problem your clients usually bring, and finish with LoRA in appendix E.

The payoff comes late but it is real. The DEV Community reviewer said that after finishing, articles on attention or KV caches became far easier to follow. For an FDE, that is the skill of keeping up with new technical material every week.

For your career, do not let the work sit idle in a notebook. Put a model fine-tuned on public data on GitHub, with a page that sets out how you evaluated it and the results. On a CV, a line such as “implemented a GPT-style model from scratch, fine-tuned it for classification and measured accuracy on a test set” is far more convincing than “experience with LLMs”.

The next time a client asks why the model is slow, or why fine-tuning did not improve results, you will not have to guess. You will have written that very code yourself.

Was this article useful?

Use with your AI assistantAsk Claude ↗Ask ChatGPT ↗
7 sources
Read next on the roadmap · Stage 1: FoundationsA 2013 statistics textbook teaches the FDE skill most often skippedISLP, the 2023 Python edition, teaches honest evaluation of a model on client data, and a few of its chapters pay off in next week's project.