teaching_llm_applications

CS 336 Stanford Language modelling from scratch

Week 1

image

image

image

When a Large Language Model (LLM) generates a response, the process (called inference) doesn’t happen all at once. It is split into two distinct steps: the Prefill Phase and the Decode Phase.

The Prefill Phase is the very first step where the model reads and processes your entire input prompt.

What Happens During the Prefill Phase?

1. Parallel Processing of the Prompt

Unlike generating text (which happens one word at a time), the model reads your entire prompt all at once. Because the input tokens are already known, the GPU processes them in parallel. This step is highly efficient and relies heavily on the sheer computational power of the GPU (it is “compute-bound”).

2. Building the KV Cache

As the model processes your prompt, it calculates and stores mathematical representations of every word to understand their context and relationships. It saves these calculations in what is known as the Key-Value (KV) Cache. This is a crucial step because it acts as the model’s short-term memory for your prompt.

3. Generating the First Token

The culmination of the prefill phase is the model predicting and outputting the very first token (word or sub-word) of its response.


Contrast: The Decode Phase

Once the first token is generated, the prefill phase ends and the Decode Phase begins.


A Simple Analogy

Imagine you are handed a piece of paper with a complex math word problem on it.

image

Remedies

Week 1 assignment/practical

Divine Benevolence

image