1 Understanding large language models

Language Models · v1.0.3

2026-11-03 01:40:24

Where we are

Where this fits

Built already

  • the transformer layer: self-attention, positional encoding, multiple heads
  • the position-wise MLP, residual connections, LayerNorm

Built here

  • the system assembled out of that layer
  • no code in this unit — it is the map for the units that follow

A GPT-class model carrying out tasks nobody trained it on is the phenomenon this unit explains.

Outline of key topics

  • What a large language model is, and how it departs from earlier NLP
  • The lifecycle: pretraining on unlabeled text, then fine-tuning
  • The transformer underneath, and the GPT branch of it
  • The scale of the data, and the emergent behaviour that arrives with it
  • The roadmap for the model built over the rest of the module

What is an LLM?

Definition and the two senses of “large”

LLMs as an application of deep learning, nested inside machine learning and AI, overlapping generative AI.

A deep neural network trained on massive text to understand, generate and respond to human-like text.

  • the model is large — tens to hundreds of billions of parameters
  • the dataset is large — often large portions of the public internet

Every weight is optimized for one task: predict the next word.

Departure from earlier NLP

Earlier NLP

  • features hand-crafted by experts — trigger words, all-caps, suspicious links
  • one narrow system per task
  • strong at categorization, weak at generation

Deep learning and LLMs

  • features learned directly from raw text
  • one pretrained model, many downstream tasks
  • broad proficiency across NLP tasks

Labels are still required for a supervised task in either approach; what changes is where the representation comes from.

Where LLMs are applied

  • Generation and transformation — translation, summarization, novel text, code
  • Understanding — sentiment analysis and other classification of a passage
  • Interaction — chatbots and assistants augmenting search
  • Knowledge retrieval — sifting and answering over specialized text in medicine or law

Each application takes unstructured text in and produces text out, posed as the continuation of a prompt.

Pretraining, then fine-tuning

The two-stage lifecycle

Pretraining on a large unlabeled corpus, then fine-tuning with a smaller labeled dataset.

Pretraining

  • large, diverse corpus of raw, unlabeled text
  • self-supervised: the label comes from the input itself
  • yields a base or foundation model

Capable of completion and limited few-shot behaviour, and nothing task-specific.

Fine-tuning, and the case for building one

Two forms

  • instruction fine-tuning — instruction and answer pairs
  • classification fine-tuning — texts carrying class labels

Both train the pretrained model further on a smaller labeled dataset.

Why a custom model

  • domain performance beyond a general model
  • data privacy — nothing leaves the organization
  • latency and cost — small models run on-device
  • autonomy over updates

The transformer behind LLMs

Encoder and decoder

The encoder maps input text to contextual vectors; the decoder generates output one word at a time.
  • introduced in 2017 for machine translation
  • both submodules joined by self-attention
  • attention weighs tokens against each other, capturing long-range dependence

Two branches of the architecture

The encoder segment yields BERT; the decoder segment yields GPT.
  • BERT — encoder only, masked word prediction, classification
  • GPT — decoder only, generative tasks, zero-shot and few-shot ability
  • not all transformers are LLMs, and not all LLMs are transformers

This module follows the GPT branch.

Scale and what emerges

The pretraining corpus

The GPT-3 pretraining mixture, by raw token count.

  • web crawl dominates — 410 billion tokens, about 570 GB
  • 499 billion tokens available, 300 billion actually trained on
  • sampling proportions differ from raw size: WebText2 supplies 22% of training from 19 billion tokens
  • scale and diversity drive syntax, semantics and general knowledge

Next-word prediction

GPT keeps the decoder alone: unidirectional, left to right, one word at a time.
for _ in range(n_new):
    logits = model(ids)[:, -1]
    nxt = logits.argmax(-1)
    ids = cat([ids, nxt], dim=1)
  • the next word in the text is the label, created on the fly
  • outputs feed back as inputs, so the model is autoregressive

Emergent behaviour

Completion, zero-shot and few-shot behaviour, distinguished only by what the prompt contains.
  • emergent behavior: a task the model was never explicitly trained for
  • zero-shot — the instruction alone
  • few-shot — a handful of examples inside the prompt
  • no retraining, no fine-tuning, no architecture change

The roadmap for this module

Three stages

Building an LLM: architecture and data, then pretraining, then fine-tuning.
  1. Building — data preparation and sampling, attention, the architecture
  2. Foundation model — training loop, evaluation, loading pretrained weights
  3. Fine-tuning — into an instruction-following assistant, and into a classifier

Summary

Summary

  • LLMs replace narrow, rule-based NLP with one broadly capable pretrained model
  • The lifecycle has two stages: pretrain on unlabeled text, fine-tune on labeled data
  • LLMs rest on the transformer; GPT-like models keep the decoder alone
  • Pretraining demands billions of tokens and serious compute
  • Next-word prediction alone yields emergent zero-shot and few-shot behaviour

Next: 2 Working with text data — text split into pieces, mapped to integer IDs, cut into input-target pairs, embedded as vectors.