# Build a tiny GPT

> Train a small GPT on your laptop to write SAP-style process notes, and see first-hand how language models learn, invent details and memorize training data.

- Course: Orchestrate, Unit 19 · Attention, transformers and LLMs
- Page: https://erpfde.com/topics/build-a-tiny-gpt/
- Updated: 2026-11-13
- Reading time: Foundational 8 min, Deep 40 min

Independent learning resource. Not affiliated with, sponsored by or endorsed by SAP SE. SAP and other SAP products mentioned are trademarks of SAP SE in Germany and other countries.

## Foundational layer

### The 60-second version

A **GPT** is a transformer that has learned one skill: guess the next piece of text. In this topic, learners build a tiny one and train it on their own laptop.

The tiny GPT reads made-up SAP process notes, such as "Sales order 14212 for customer C-1032 blocked: credit limit exceeded". It sees one character at a time. Its only task is to guess the next character. At first it guesses at random and writes gibberish. After a few minutes of practice, it writes new notes that look real.

That is how large language models (LLMs) are made, too. The design is the same as the [transformer](https://erpfde.com/topics/the-transformer-architecture/) from the previous topic. The task is the same: guess the next token. What differs is scale. The tiny GPT has under a million learned numbers and reads about 320,000 characters. Large models have billions of numbers and read trillions of tokens.

Three lessons come out of the tiny GPT that matter to every leader:

1. **It learns patterns, not facts.** It writes notes that look right, with numbers that are often wrong.
2. **It can memorize.** Trained too long on too little data, it repeats training notes word for word, customer numbers included.
3. **Training from scratch is the wrong default.** Even this toy takes minutes. Real models take data centres.

### Why it matters to the business

**Fluent is not the same as correct.** In our test, the trained model wrote an invoice note with a goods receipt of 1,460 pieces against an invoice of 230. Every note it saw had a receipt slightly below the invoice. The sentence was fluent; the number was invented. Large models do the same thing with more polish. This is why SAP processes such as three-way match need checks against system data, not trust in generated text.

**Models can leak what they learned.** Trained on only 100 notes, our tiny GPT copied 42% of its outputs straight from the training data, customer numbers and amounts included. Researchers showed in 2021 that GPT-2 could be made to repeat names, phone numbers and email addresses from its training data. Some appeared in just one document. If a team proposes training or fine-tuning a model on customer data, ask what stops it from repeating that data to the wrong person.

**Scale drives cost.** A 2022 study trained over 400 models and concluded that training data should grow with model size. Their 70-billion-parameter model, Chinchilla, read 1.4 trillion tokens. Very few companies should pay for that. Most SAP projects call a model someone else trained, or use one SAP pretrained.

**A concrete order-to-cash example.** A team wants drafts of customer messages about blocked sales orders. Option one: train a model on five years of order notes. Option two: call a hosted LLM through SAP's generative AI hub, and give it the facts of each order at request time. The tiny GPT shows why option two is the usual starting point. Training needs data, hardware and time. It also bakes customer details into the model, where no authorization check can reach them.

### How SAP does it

SAP doesn't ask customers to pretrain language models. As of October 2026, SAP offers three paths:

- **Pretrained models in the generative AI hub.** The generative AI hub, part of SAP AI Core, gives access to LLMs from several providers. SAP's AI Core guide says it is available only in the **extended** service plan. Unit 3 sets up access.
- **SAP's own pretrained models.** SAP's release highlights for Q4 2025 describe **SAP-ABAP-1**, trained on more than 250 million lines of ABAP code, 30 million lines of CDS code and technical documentation. They also describe **SAP-RPT-1**, a pretrained model for business tables. Both are offered through the generative AI hub. SAP-ABAP-1 can be tried in the hub's trial; SAP-RPT-1 has a separate free web playground ([SAP News, January 2026](https://news.sap.com/2026/01/sap-business-ai-release-highlights-q4-2025/)). SAP did the expensive training; customers call the result.
- **Your own training jobs in SAP AI Core.** SAP AI Core runs training pipelines as batch jobs. You package your code, choose a resource plan with the CPUs, GPUs and memory you need, and SAP AI Core stores the trained model. This suits small custom models on your own data, such as a table model or a classifier, more than pretraining an LLM.

### What it takes to train a language model

| | Tiny GPT (this topic) | nanoGPT's "baby GPT" | Chinchilla (2022) |
| --- | --- | --- | --- |
| Learned numbers (parameters) | 0.81 million | About 10.8 million, in our count | 70 billion |
| Training text | About 320,000 characters of made-up notes | About 1.1 million characters of Shakespeare | 1.4 trillion tokens |
| Hardware and time | A laptop CPU, about 3.5 minutes in our test | One A100 data-centre GPU, about 3 minutes | Not something a project team repeats |
| Who does this | A learner, once | A learner with a GPU | A few large labs |

The pattern is clear. Pretraining a useful LLM is a research-lab activity. A project team's job is to choose a model, give it the right context, and check its output.

### Questions to ask

- Are we training a model, fine-tuning one, or calling a pretrained one? Why that choice, and what would the simpler option cost?
- If we train or fine-tune on our data, which records go in? Could the model repeat customer, supplier or employee data to a user who isn't allowed to see it?
- How do we check generated numbers, such as quantities, amounts and order numbers, against the SAP system before anyone acts on them?
- Which SAP service plan does the proposal need: standard for training jobs in SAP AI Core, extended for the generative AI hub?
- How was the model tested on data it didn't train on, and what was the result?
- Who owns a model we train ourselves: retraining, monitoring and deletion when data must be removed?

### Common misconceptions

- **"The model looks up answers."** A GPT stores no records to look up. It learned patterns in its weights and writes text that fits them, which is why it invents plausible numbers.
- **"A model that writes fluent notes understands the process."** Our tiny GPT writes perfect-looking three-way match notes and breaks the rule behind them.
- **"Training data stays private inside the model."** A model can repeat training text word for word, especially with little data or long training.
- **"Training our own LLM gives us control at a fair price."** Pretraining needs vast data and compute. Most teams get better value from a pretrained model plus their own data at request time.
- **"Lower training loss always means a better model."** In the exercise, training loss keeps falling while the model gets worse on new text and starts copying.

### Key terms

- **GPT (generative pre-trained transformer):** a decoder-only transformer trained to predict the next token.
- **Token:** the unit a model reads and writes; one character in the tiny GPT, a word piece in most LLMs.
- **Training:** repeatedly adjusting the parameters so the model's guesses improve.
- **Loss:** a number for how wrong the guesses are; lower is better.
- **Pretraining:** the first, expensive training on a huge general text collection.
- **Fine-tuning:** further training of a pretrained model on a smaller, specific data set.
- **Overfitting:** the model fits its training data so closely that it gets worse on new data.
- **Memorization:** the model reproduces training examples word for word.
- **Hallucination:** fluent output that is not true, such as an invented quantity.

### Check yourself

1. What is the one task a GPT is trained to do?
   - A. Look up the right answer in a database of documents
   - B. Guess the next piece of text, given the text so far
   - C. Translate business rules into program code
   - D. Sort documents into categories chosen by the vendor

   Answer: B. A GPT is trained only to predict the next token. Writing whole notes, answers or emails comes from repeating that one guess many times. It has no database to look things up in.

2. The tiny GPT wrote an invoice note with a goods receipt of 1,460 pieces against an invoice of 230. What does that show?
   - A. The training data must have contained that exact same note
   - B. The model needs a bigger vocabulary to write large numbers
   - C. The laptop was too slow to train the model properly in time
   - D. Fluent text can contain invented details that break the pattern

   Answer: D. Every training note had a receipt slightly below the invoice, yet the model wrote a fluent note that breaks that pattern. Language models learn what text looks like, not the facts behind it. Generated numbers need checks against system data.

3. A team plans to fine-tune a model on five years of customer order notes. What risk should you raise first?
   - A. The model could repeat customer details to users who shouldn't see them
   - B. The model will refuse to write about any customer it hasn't seen
   - C. Fine-tuning deletes the model's general language skills completely
   - D. Order notes are far too short for any model to learn from

   Answer: A. Models can memorize and repeat training text. The tiny GPT copied 42% of its outputs from 100 training notes, and researchers extracted names and phone numbers from GPT-2. No authorization check works inside a model's weights.

4. Why is pretraining your own LLM the wrong default for most SAP projects?
   - A. SAP's license terms forbid customers from training their own models
   - B. Language models can only be trained on public text from the web
   - C. It costs vast data, compute and time, and pretrained models exist
   - D. Pretrained models can't be combined with data from SAP systems

   Answer: C. The Chinchilla study trained a 70-billion-parameter model on 1.4 trillion tokens. Few companies should pay for that. The generative AI hub and SAP's own pretrained models offer the result of that work as a service.

5. Which path does SAP offer for running your own small training job?
   - A. Training jobs run inside the S/4HANA system on the ABAP stack
   - B. The generative AI hub trial trains a private copy of each model
   - C. Joule trains a new model for every customer automatically
   - D. SAP AI Core runs training pipelines on a resource plan you choose

   Answer: D. SAP AI Core runs pipelines as batch jobs, for example to train models, with resource plans that differ in CPUs, GPUs and memory. The generative AI hub, in the extended plan, is for calling pretrained models.

6. In the exercise, training loss keeps falling while loss on new text rises. What is happening?
   - A. The model is learning the general pattern better with every step
   - B. Overfitting: it fits the training data and gets worse on new data
   - C. The new text must use a different alphabet from the training text
   - D. The model has run out of memory and has stopped learning

   Answer: B. Falling training loss with rising loss on unseen text is the sign of overfitting. In the exercise, the same model also started copying training notes word for word. Judge a model on data it didn't train on.

## Deep layer

### Mental model: one guess, repeated

A GPT does exactly one thing: given some text, it scores every possible next token. Everything else is a loop around that one guess.

- **Training** is the loop of guessing, measuring how wrong the guess was, and nudging the weights. You did this in [Gradient descent by hand](https://erpfde.com/topics/gradient-descent-by-hand/). Here the model and the data are bigger, but the loop is the same.
- **Writing** is the loop of guessing, picking one token, adding it to the text, and guessing again.

The trick that makes training efficient is the causal mask from the [transformer](https://erpfde.com/topics/the-transformer-architecture/). Because each position sees only earlier tokens, one window of 64 characters gives 64 guesses at once. Each position predicts the character after it. One forward pass, 64 lessons.

> **Note** New words. **Token**: the unit the model reads; here, one character. **Vocabulary**: the list of all tokens the model knows. **Logits**: the raw scores the model gives each vocabulary token; softmax turns them into probabilities. **Loss**: how wrong the guesses are, averaged; lower is better. **Batch**: several text windows processed together. **Step**: one round of guess, measure and nudge. **Checkpoint**: a file holding the trained weights. **Overfitting**: fitting the training data so closely that results on new data get worse.

### How it works

#### From text to numbers: the tokenizer

The tiny GPT uses the simplest tokenizer there is: one token per character. It collects every different character in the training file, sorts them and numbers them. Our made-up SAP notes use 57 different characters, so the vocabulary has 57 entries. Tiny Shakespeare, a classic practice text, uses 65.

Real LLMs split text into word pieces instead. GPT-2 had a vocabulary of 50,257 pieces, as you saw in the previous topic's parameter count. Word pieces make text shorter in tokens, so the same context window holds more meaning. Characters keep this topic simple: no extra library, and every output is readable.

#### Inputs and targets: the same text, shifted by one

Training takes random windows of the text. The target for each window is the same text, moved one character to the right.

```mermaid
flowchart LR
  T["Text: Order 4711 on hold"] --> X["Input: Order 4711 on hol"]
  T --> Y["Target: rder 4711 on hold"]
  X --> M[Tiny GPT]
  M --> P[A guess at every position]
  P --> L[Loss: guesses vs targets]
  Y --> L
```

At the position of `O`, the target is `r`. At `Order 47`, the target is `1`. With the causal mask, no position can peek at its own target. So one window teaches as many lessons as it has characters.

#### The loss, and a number to expect before training

The loss is **cross-entropy**: for each position, minus the log of the probability the model gave the correct next character. You met its yes/no version in [Neural networks from scratch](https://erpfde.com/topics/neural-networks-from-scratch/). Here it covers 57 choices instead of two.

That gives a useful check before training. An untrained model spreads its probability about evenly, about 1/57 for each character. Its loss should then be close to `ln(57)`, which is 4.04. The script prints this number, and the first loss it measures is 4.09. If your first loss is far above `ln(vocabulary size)`, something is wrong with the setup, before any training happens.

The loss also has a floor. Our notes contain random order numbers and amounts. No model can predict a random digit, so the loss can never reach zero. On the made-up notes, it settles near 0.31. On Shakespeare it stays far higher, because real language is less predictable than templates.

#### The training loop

Each step does five things:

1. Take a batch of 32 random windows of 64 characters from the training part of the text.
2. Run the model to get logits of shape `(32, 64, 57)`: a score for every vocabulary character, at every position, in every window.
3. Compute the cross-entropy loss against the shifted targets.
4. Run `backward()` to get the gradients, and **clip** them to a maximum overall size of 1.0, a common guard against a single wild step.
5. Let the **AdamW** optimizer update the weights. AdamW is a refined form of gradient descent that adapts the step size for each weight. It is the usual choice for transformers.

Every few hundred steps, the script measures the loss on 20 batches from the training part and 20 from a **check** part the model never trains on. That second number is the honest one.

#### Train and check: spotting overfitting

The script keeps the first 90% of the text for training and the last 10% for checking, the same split nanoGPT uses. Two curves come out:

| What you see | What it means |
| --- | --- |
| Both losses fall together | The model is learning patterns that carry over to new text |
| Both flatten at the same level | The model has learned what it can at this size; more steps add little |
| Training loss falls, check loss rises | Overfitting: the model fits its training text and gets worse on new text |

With 3,000 notes, both losses flatten together near 0.31. With 100 notes, training loss keeps falling to 0.13 while check loss climbs above 0.6. The exercise shows what the model does in that state: it copies.

#### Writing text: the generation loop

Once trained, the model writes by repeating its one guess:

```mermaid
flowchart LR
  P[Prompt] --> C[Keep the last<br/>64 characters]
  C --> M[Tiny GPT]
  M --> S[Scores for the<br/>next character]
  S --> D[Draw one character<br/>by probability]
  D --> A[Append to the text]
  A -->|repeat| C
  A -->|line ends| E[Done]
```

Two details matter:

- **The context limit.** The model has 64 position slots. Before each guess, the script keeps only the last 64 characters. nanoGPT's `generate` does the same: if the text grows too long, it is cropped to the context size. Anything earlier is gone from the model's view. Large models have the same limit, just bigger.
- **Drawing, not picking the top.** The script draws the next character at random, weighted by the probabilities. That is why two runs give different notes. The `--temperature` option sharpens or flattens the probabilities before the draw. The next topic, How LLMs generate text, explores these choices in depth.

#### Saving and loading safely

The trained weights go into a checkpoint file with `torch.save`. The script saves the weights (the **state dict**), the character list and the model size, as PyTorch's serialization notes recommend, rather than the whole Python object.

Loading uses `torch.load(..., weights_only=True)`. PyTorch's documentation explains that this setting, the default since version 2.6, restricts loading to what a state dict needs. A normal Python "pickle" file can run code when it is loaded. So a model file from an unknown source is a security risk unless it is loaded this way. Treat model files like any other software you install.

#### How big is "tiny"?

The two sizes match the timing script from [Set up for Unit 19](https://erpfde.com/topics/set-up-for-unit-19/):

| Size | Blocks | Width | Heads | Context | Batch | Parameters (notes vocabulary) |
| --- | --- | --- | --- | --- | --- | --- |
| `small` | 4 | 128 | 4 | 64 | 32 | 0.81 million |
| `medium` | 6 | 384 | 6 | 256 | 64 | 10.77 million |

The `small` size is close to nanoGPT's suggested CPU settings, which its README says reach a validation loss of about 1.88 on Tiny Shakespeare in about 3 minutes. The `medium` size has the shape of nanoGPT's "baby GPT", which the README reports reaches 1.4697 in about 3 minutes on one A100 GPU. The README now points readers to a newer project, nanochat, for anything beyond learning.

### Build it yourself: train a tiny GPT on SAP-style notes

You will generate a file of made-up SAP process notes, train the transformer from the previous topic on it, and make it write new notes. Then you will check whether the new notes are original or copied. Everything runs on your laptop with made-up data.

**Before you start:** complete [Set up your computer for this course](https://erpfde.com/topics/set-up-your-computer/) and [Set up for Unit 19](https://erpfde.com/topics/set-up-for-unit-19/). You also need `transformer_block.py` from [The transformer architecture](https://erpfde.com/topics/the-transformer-architecture/), saved in your `unit19` folder: this topic imports its `TinyTransformer` class. Your `timing_notes.md` from the setup topic tells you whether the small size is quick enough on your computer.

```mermaid
flowchart LR
  G[make-data] --> N[sap_notes.txt<br/>3,000 made-up notes]
  N --> T[train] --> K[tiny_gpt.pt]
  K --> W[generate] --> O[New notes]
  K --> C[copies] --> R[Copied or original?]
```

#### What you need

- The course folder, its `.venv` with PyTorch, and `unit19/transformer_block.py`.
- About 45 minutes. Training the small model takes 3 to 4 minutes on our two-core test machine; a recent laptop is often faster.
- No accounts, no API keys, no cost. The optional Shakespeare text needs internet once; everything else runs offline.

> **Warning** Use only the made-up data in this topic. Don't train on real SAP notes, customer names or anything from your company's systems. The exercise shows why: a model can repeat its training data word for word.

#### Step 1: Open the course folder and turn on the environment

1. Open VS Code, choose **File > Open Folder** and open `orchestrate-course`.
2. Open a terminal: **Terminal > New Terminal**.
3. Turn on the virtual environment if the prompt doesn't start with `(.venv)`:

   - **Windows (PowerShell):**

     ```powershell
     .venv\Scripts\Activate.ps1
     ```

   - **macOS / Linux:**

     ```bash
     source .venv/bin/activate
     ```

4. Go into the Unit 19 folder:

   ```bash
   cd unit19
   ```

5. Check that the file from the previous topic is there:

   - **Windows (PowerShell):**

     ```powershell
     Get-ChildItem transformer_block.py
     ```

   - **macOS / Linux:**

     ```bash
     ls transformer_block.py
     ```

   If the file is missing, go back to [The transformer architecture](https://erpfde.com/topics/the-transformer-architecture/), Step 2, and save it first.

#### Step 2: Save the script

1. In VS Code's file list, right-click `unit19`, choose **New File** and name it `tiny_gpt.py`.
2. Paste the code below and save with **File > Save**. The next topic loads the model this script trains, so keep the name.

```python
"""Unit 19: build a tiny GPT. Train the transformer from transformer_block.py to write text.

The model reads characters and learns one thing: predict the next character.
Trained on made-up SAP-style process notes, it starts writing notes of its own.

How to run (from the unit19 folder, with the course .venv turned on):
    python tiny_gpt.py make-data                      # write sap_notes.txt: 3,000 made-up SAP process notes
    python tiny_gpt.py train                          # train the small model on sap_notes.txt, save tiny_gpt.pt
    python tiny_gpt.py generate                       # write 5 new notes with the trained model
    python tiny_gpt.py generate --prompt "Invoice"    # start the notes with your own text
    python tiny_gpt.py copies                         # how many generated notes copy a training note word for word

Optional:
    python tiny_gpt.py train --steps 300              # a quick run to see it work (about a minute)
    python tiny_gpt.py train --size medium            # a bigger model; use a GPU notebook for this one
    python tiny_gpt.py get-shakespeare                # download a classic practice text (needs internet)
    python tiny_gpt.py train --data shakespeare.txt --model shakespeare.pt --prompt "ROMEO:"
    python tiny_gpt.py generate --model shakespeare.pt --prompt "ROMEO:" --no-stop --chars 300
"""
import argparse
import math
import random
import time
import urllib.request
from pathlib import Path

import torch
import torch.nn.functional as F

from transformer_block import TinyTransformer

SIZES = {  # the same two sizes as bench_tiny_gpt.py in the Unit 19 setup topic
    "small": dict(n_layer=4, n_embd=128, n_head=4, context=64, batch=32),
    "medium": dict(n_layer=6, n_embd=384, n_head=6, context=256, batch=64),
}
SHAKESPEARE_URL = "https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt"


# ---------------------------------------------------------------- 1. data: made-up SAP-style notes
def make_note(r: random.Random) -> str:
    """One made-up process note. Numbers, names and rules are invented for practice."""
    customer, vendor = f"C-{r.randint(1000, 1999)}", f"V-{r.randint(2000, 2999)}"
    material, plant = f"M-{r.randint(4000, 4999)}", r.choice(["1010", "1020", "1710", "2010"])
    amount = f"{r.randint(1, 99)},{r.randint(0, 999):03d}.{r.choice(['00', '50'])} {r.choice(['EUR', 'USD'])}"
    qty_inv = r.randint(5, 50) * 10
    qty_gr = qty_inv - r.randint(1, 4) * 10
    kind = r.choice(["o2c", "p2p", "mrp"])
    if kind == "o2c":   # order-to-cash: blocked sales orders
        so = f"{r.randint(10000, 19999)}"
        reason = r.choice(["credit limit exceeded by " + amount, "overdue items on account",
                           "missing export license", "price below minimum"])
        action = r.choice(["Credit team to review before release.", "Sales rep to call the customer.",
                           "Release after payment arrives.", "Check with the credit manager today."])
        start = r.choice([f"Sales order {so} for customer {customer} blocked: ",
                          f"Order {so} ({customer}) on hold, ",
                          f"Delivery block on sales order {so}, customer {customer}: "])
        return start + reason + ". " + action
    if kind == "p2p":   # procure-to-pay: three-way match exceptions
        inv, po = f"{r.randint(510000, 519999)}", f"45{r.randint(10000, 99999)}"
        issue = r.choice([f"invoice quantity {qty_inv} PC, goods receipt {qty_gr} PC",
                          f"price differs from the purchase order by {amount}",
                          "no goods receipt posted yet"])
        action = r.choice([f"Buyer to confirm with supplier {vendor}.", "AP clerk to park the invoice.",
                           "Warehouse to check the receipt.", f"Ask {vendor} for a credit memo."])
        start = r.choice([f"Invoice {inv} for PO {po} held: ", f"Three-way match failed on PO {po}, ",
                          f"Payment block on invoice {inv} (PO {po}): "])
        return start + issue + ". " + action
    order = f"{r.randint(1000000, 1999999)}"   # plan-to-produce: MRP exceptions
    issue = r.choice(["start date in the past", "stock below safety stock",
                      "reschedule in", "reschedule out", "opening date in the past"])
    action = r.choice([f"Planner to reschedule order {order}.", "Expedite with the supplier.",
                       f"Check capacity at plant {plant}.", "Convert the planned order today."])
    start = r.choice([f"MRP exception for material {material} at plant {plant}: ",
                      f"Material {material}, plant {plant}, exception: "])
    return start + issue + ". " + action


def make_data(path: Path, notes: int, seed: int) -> None:
    r = random.Random(seed)
    lines = [make_note(r) for _ in range(notes)]
    path.write_text("\n".join(lines) + "\n", encoding="utf-8")
    print(f"Wrote {notes:,} made-up notes ({path.stat().st_size:,} characters) to {path}")
    print("First three:")
    for line in lines[:3]:
        print("  " + line)


def get_shakespeare(path: Path) -> None:
    print(f"Downloading {SHAKESPEARE_URL}")
    with urllib.request.urlopen(SHAKESPEARE_URL, timeout=30) as response:
        path.write_bytes(response.read())
    print(f"Saved {path.stat().st_size:,} characters to {path}")


# ---------------------------------------------------------------- 2. tokenizer: one token per character
class CharTokenizer:
    def __init__(self, chars: list):
        self.chars = chars
        self.stoi = {c: i for i, c in enumerate(chars)}

    def encode(self, text: str) -> list:
        return [self.stoi[c] for c in text]

    def decode(self, ids: list) -> str:
        return "".join(self.chars[i] for i in ids)


# ---------------------------------------------------------------- 3. device and batches
def pick_device(choice: str) -> torch.device:
    if choice != "auto":
        return torch.device(choice)
    if torch.cuda.is_available():
        return torch.device("cuda")
    if torch.backends.mps.is_available():
        return torch.device("mps")
    return torch.device("cpu")


def get_batch(data: torch.Tensor, context: int, batch: int, device: torch.device):
    """Random windows of text. The target is the same window shifted one character to the right."""
    starts = torch.randint(0, len(data) - context - 1, (batch,))
    x = torch.stack([data[s:s + context] for s in starts])
    y = torch.stack([data[s + 1:s + context + 1] for s in starts])
    return x.to(device), y.to(device)


def loss_on(model, data, cfg, device, batches: int = 20) -> float:
    model.eval()
    total = 0.0
    with torch.no_grad():
        for _ in range(batches):
            x, y = get_batch(data, cfg["context"], cfg["batch"], device)
            logits = model(x)
            total += F.cross_entropy(logits.reshape(-1, logits.shape[-1]), y.reshape(-1)).item()
    model.train()
    return total / batches


# ---------------------------------------------------------------- 4. generation: one character at a time
@torch.no_grad()
def generate(model, tok: CharTokenizer, prompt: str, max_chars: int, context: int,
             temperature: float, device: torch.device, stop_at_newline: bool = True) -> str:
    model.eval()
    idx = torch.tensor([tok.encode(prompt)], device=device)
    for _ in range(max_chars):
        window = idx[:, -context:]                       # the model only has `context` position slots
        logits = model(window)[:, -1, :] / temperature   # scores for the next character only
        probs = F.softmax(logits, dim=-1)
        nxt = torch.multinomial(probs, num_samples=1)    # draw one character at random, by probability
        idx = torch.cat([idx, nxt], dim=1)
        if stop_at_newline and tok.chars[nxt.item()] == "\n":
            break                                        # one note per line: stop at the line end
    return tok.decode(idx[0].tolist())


# ---------------------------------------------------------------- 5. training
def indent(text: str) -> str:
    return "\n".join("  | " + line for line in text.strip("\n").splitlines())


def train(args) -> None:
    path = Path(args.data)
    if not path.exists():
        raise SystemExit(f"{path} not found. Run: python tiny_gpt.py make-data")
    text = path.read_text(encoding="utf-8")
    tok = CharTokenizer(sorted(set(text)))
    data = torch.tensor(tok.encode(text), dtype=torch.long)
    split = int(0.9 * len(data))                       # first 90% to learn from, last 10% to check on
    train_data, val_data = data[:split], data[split:]
    cfg = SIZES[args.size]
    device = pick_device(args.device)

    torch.manual_seed(args.seed)
    model = TinyTransformer(len(tok.chars), cfg["context"], cfg["n_embd"], cfg["n_head"],
                            cfg["n_layer"]).to(device)
    opt = torch.optim.AdamW(model.parameters(), lr=args.lr)
    params = sum(p.numel() for p in model.parameters())
    print(f"Data: {path} ({len(text):,} characters, {len(tok.chars)} different ones)")
    print(f"Model '{args.size}': {params / 1e6:.2f} million parameters, {cfg['n_layer']} blocks, "
          f"context {cfg['context']} characters, on {device.type}")
    print(f"Loss if guessing evenly: ln({len(tok.chars)}) = {math.log(len(tok.chars)):.2f}")
    before = generate(model, tok, args.prompt, 80, cfg["context"], 1.0, device, stop_at_newline=False)
    print("Before training, it writes:\n" + indent(before))

    every = max(1, args.steps // 5)
    start = time.time()
    for step in range(args.steps + 1):
        if step % every == 0 or step == args.steps:
            tr, va = loss_on(model, train_data, cfg, device), loss_on(model, val_data, cfg, device)
            print(f"  step {step:>5}  train loss {tr:.2f}  check loss {va:.2f}  [{time.time() - start:.0f} s]")
        if step == args.steps:
            break
        x, y = get_batch(train_data, cfg["context"], cfg["batch"], device)
        logits = model(x)                                # (batch, context, vocabulary)
        loss = F.cross_entropy(logits.reshape(-1, logits.shape[-1]), y.reshape(-1))
        opt.zero_grad()
        loss.backward()
        torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
        opt.step()

    after = generate(model, tok, args.prompt, 240, cfg["context"], 1.0, device, stop_at_newline=False)
    print("After training, it writes:\n" + indent(after))
    torch.save({"model": model.state_dict(), "chars": tok.chars, "size": args.size,
                "data": str(path)}, args.model)
    print(f"Saved the trained model to {args.model}")


def load(model_path: str, device: torch.device):
    if not Path(model_path).exists():
        raise SystemExit(f"{model_path} not found. Run: python tiny_gpt.py train")
    ckpt = torch.load(model_path, map_location=device, weights_only=True)   # numbers and text only, no code
    cfg = SIZES[ckpt["size"]]
    tok = CharTokenizer(ckpt["chars"])
    model = TinyTransformer(len(tok.chars), cfg["context"], cfg["n_embd"], cfg["n_head"],
                            cfg["n_layer"]).to(device)
    model.load_state_dict(ckpt["model"])
    return model, tok, cfg, ckpt


def generate_cmd(args) -> None:
    device = pick_device(args.device)
    model, tok, cfg, _ = load(args.model, device)
    unknown = sorted(set(args.prompt) - set(tok.chars))
    if unknown:
        raise SystemExit(f"The prompt uses characters the model never saw: {unknown}")
    torch.manual_seed(args.seed)
    for _ in range(args.count):
        print(generate(model, tok, args.prompt, args.chars, cfg["context"], args.temperature, device,
                       stop_at_newline=not args.no_stop).strip())


def copies_cmd(args) -> None:
    device = pick_device(args.device)
    model, tok, cfg, ckpt = load(args.model, device)
    text = Path(ckpt["data"]).read_text(encoding="utf-8")
    split = int(0.9 * len(text))
    seen = set(text[:split].splitlines())                # notes the model trained on
    torch.manual_seed(args.seed)
    copies = []
    for _ in range(args.count):
        note = generate(model, tok, "\n", args.chars, cfg["context"], 1.0, device).strip()
        if note in seen:
            copies.append(note)
    print(f"Generated {args.count} notes; {len(copies)} are word-for-word copies of a training note "
          f"({len(copies) / args.count:.0%}).")
    print(f"The training part of {ckpt['data']} holds {len(seen):,} different notes.")
    for note in sorted(set(copies))[:2]:
        print(f"  copied: {note}")


def main() -> None:
    p = argparse.ArgumentParser(description="Build, train and sample a tiny character-level GPT.")
    sub = p.add_subparsers(dest="command", required=True)
    d = sub.add_parser("make-data", help="write made-up SAP-style notes to a text file")
    d.add_argument("--notes", type=int, default=3000)
    d.add_argument("--out", default="sap_notes.txt")
    d.add_argument("--seed", type=int, default=0)
    s = sub.add_parser("get-shakespeare", help="download the Tiny Shakespeare practice text")
    s.add_argument("--out", default="shakespeare.txt")
    t = sub.add_parser("train", help="train the model and save it")
    t.add_argument("--data", default="sap_notes.txt")
    t.add_argument("--size", choices=sorted(SIZES), default="small")
    t.add_argument("--steps", type=int, default=2000)
    t.add_argument("--lr", type=float, default=1e-3, help="learning rate")
    t.add_argument("--prompt", default="Sales order", help="text to start the before/after samples")
    t.add_argument("--model", default="tiny_gpt.pt", help="file to save the trained model in")
    t.add_argument("--device", choices=["auto", "cpu", "cuda", "mps"], default="auto")
    t.add_argument("--seed", type=int, default=0)
    g = sub.add_parser("generate", help="write new text with a trained model")
    g.add_argument("--prompt", default="\n", help="text to start with (default: a new line)")
    g.add_argument("--count", type=int, default=5)
    g.add_argument("--chars", type=int, default=200, help="maximum characters per note")
    g.add_argument("--temperature", type=float, default=1.0)
    g.add_argument("--no-stop", action="store_true", help="keep writing past the end of a line")
    c = sub.add_parser("copies", help="count generated notes that copy a training note exactly")
    c.add_argument("--count", type=int, default=200)
    c.add_argument("--chars", type=int, default=200)
    for sp in (g, c):
        sp.add_argument("--model", default="tiny_gpt.pt")
        sp.add_argument("--device", choices=["auto", "cpu", "cuda", "mps"], default="auto")
        sp.add_argument("--seed", type=int, default=0)
    args = p.parse_args()

    if args.command == "make-data":
        make_data(Path(args.out), args.notes, args.seed)
    elif args.command == "get-shakespeare":
        get_shakespeare(Path(args.out))
    elif args.command == "train":
        train(args)
    elif args.command == "generate":
        generate_cmd(args)
    else:
        copies_cmd(args)


if __name__ == "__main__":
    main()
```

#### Step 3: Make the training data

```bash
python tiny_gpt.py make-data
```

**What success looks like:**

```text
Wrote 3,000 made-up notes (319,555 characters) to sap_notes.txt
First three:
  Three-way match failed on PO 4538631, no goods receipt posted yet. AP clerk to park the invoice.
  Material M-4097, plant 1710, exception: reschedule out. Planner to reschedule order 1346236.
  Order 16534 (C-1444) on hold, credit limit exceeded by 71,488.50 USD. Check with the credit manager today.
```

The notes mix three running examples of this course: blocked sales orders (order-to-cash), three-way match exceptions (procure-to-pay) and MRP exceptions (plan-to-produce). Customer numbers, amounts and the rules are invented. One rule is hidden in the data on purpose: in every quantity note, the goods receipt is 10 to 40 pieces **below** the invoice quantity. Watch whether the model learns it.

Open `sap_notes.txt` in VS Code to look at it. It is plain text, one note per line.

#### Step 4: Train the model

```bash
python tiny_gpt.py train
```

**What success looks like** (times and sample notes differ between computers):

```text
Data: sap_notes.txt (319,555 characters, 57 different ones)
Model 'small': 0.81 million parameters, 4 blocks, context 64 characters, on cpu
Loss if guessing evenly: ln(57) = 4.04
Before training, it writes:
  | Sales order2D  vlD.0aywBTC99qhunB:Catol
  | h
  | Pq)fhnTck37cEfuq0pVPl9Br-qE-esvy)B6cSs)WkO00C1C64
  step     0  train loss 4.09  check loss 4.09  [1 s]
  step   400  train loss 0.39  check loss 0.39  [41 s]
  step   800  train loss 0.32  check loss 0.33  [81 s]
  step  1200  train loss 0.31  check loss 0.32  [121 s]
  step  1600  train loss 0.30  check loss 0.32  [161 s]
  step  2000  train loss 0.31  check loss 0.31  [203 s]
After training, it writes:
  | Sales order 16161, customer C-1398: overdue items on account. Sales rep to call the customer.
  | Delivery block on sales order 13710, customer C-1978: overdue items on account. Release after payment arrives.
  | MRP exception for material M-4048 at plant 171
Saved the trained model to tiny_gpt.pt
```

How to read it:

- **The first loss, 4.09, is close to ln(57) = 4.04.** The untrained model guesses evenly, as expected. Its "before" text is random characters after the prompt.
- **Both losses drop fast, then flatten near 0.31.** The train and check numbers stay together, so the model learns patterns that carry over to new notes. The floor comes from the random digits no model can predict.
- **The "after" text reads like real notes.** Look closely at the first one: "Sales order 16161, customer C-1398:" isn't one of the templates in `make_note`. The model mixed two openings it had seen into a new one.
- **The last note stops mid-word** because the sample is capped at 240 characters.

> **Tip** In a hurry? Run `python tiny_gpt.py train --steps 300` first. It takes about 40 seconds and already writes rough notes. Then run the full training when you have four minutes.

#### Step 5: Write new notes

```bash
python tiny_gpt.py generate
```

**What success looks like** (your notes will differ):

```text
Material M-4805, plant 2010, exception: stock below safety stock. Expedite with the supplier.
Payment block on invoice 513371 (PO 4503891): price differs from the purchase order by 21,117.00 EUR. AP clerk to park the invoice.
Material M-4169, plant 1010, exception: reschedule out. Expedite with the supplier.
Material M-4769, plant 2010, exception: stock below safety stock.
MRP exception for material M-4112 at plant 1020: opening date in the past. Convert the planned order today.
```

Now start the notes with your own words:

```bash
python tiny_gpt.py generate --prompt "Invoice" --count 3
```

```text
Invoice 519921 for PO 4570287 held: invoice quantity 230 PC, goods receipt 1460 PC. AP clerk to park the invoice.
Invoice 511680 for PO 4518851 held: invoice quantity 210 PC, goods receipt 110 PC.
Invoice 517902 for PO 4536921 held: invoice quantity 200 PC, goods receipt 110 PC. Buyer to confirm with supplier V-2707.
```

Check them against the hidden rule from Step 3. In the training data, the goods receipt is always 10 to 40 pieces below the invoice. All three notes break it: 1,460 against 230, and 110 against 210 and 200. The sentences are perfect; the numbers are invented. The model learned what a three-way match note **looks like**, not the rule behind it. That is a hallucination, in miniature.

If the prompt uses a character the model never saw, such as `é`, the script stops with a clear message. A character-level model can only write the 57 characters it knows.

#### Step 6: Check whether the notes are copied

```bash
python tiny_gpt.py copies
```

**What success looks like:**

```text
Generated 200 notes; 0 are word-for-word copies of a training note (0%).
The training part of sap_notes.txt holds 2,699 different notes.
```

The script writes 200 notes and looks each one up in the training part of the file. With 3,000 notes, a model this small learns the patterns but copies none of them. Remember this number; the exercise changes it.

#### Step 7 (optional): Train on Tiny Shakespeare

Tiny Shakespeare is the classic practice text for character-level models: 1,115,394 characters, 65 different ones. It needs internet once.

```bash
python tiny_gpt.py get-shakespeare
python tiny_gpt.py train --data shakespeare.txt --model shakespeare.pt --prompt "ROMEO:"
python tiny_gpt.py generate --model shakespeare.pt --prompt "ROMEO:" --no-stop --chars 300 --count 1
```

In our run, the check loss reached 1.72 after 2,000 steps, in about 3.5 minutes. The model writes play-shaped text with line breaks, capitals and many invented words:

```text
ROMEO:
Why moves had selfess Hate exal's,
Nake to the Moccius your namerer? hear this being suigness
Bustints, your wake a whats oard; and not my father?
```

Compare 1.72 with the 0.31 on the SAP notes. Real language is far less predictable than templates. A model needs much more size and data to write it well, which is the scale lesson of this unit in one number.

#### Step 8 (optional): The medium size on a free GPU

Use this only if your `timing_notes.md` says the medium size is too slow on your laptop. On our two-core test machine, 2,000 medium steps would take hours.

1. Open Google Colab and switch on a GPU as in [Set up for Unit 19](https://erpfde.com/topics/set-up-for-unit-19/), Step 6, items 1 to 3.
2. In the first code cell, type `%%writefile transformer_block.py` on the first line, paste the whole of your `transformer_block.py` below it, and run the cell.
3. Click **+ Code**, type `%%writefile tiny_gpt.py` on the first line, paste the whole of `tiny_gpt.py` below it, and run the cell.
4. Click **+ Code**, paste these lines and run the cell:

   ```text
   !python tiny_gpt.py make-data
   !python tiny_gpt.py train --size medium --model tiny_gpt_medium.pt
   !python tiny_gpt.py generate --model tiny_gpt_medium.pt
   ```

**What success looks like:** the second line of the training output ends with `on cuda`, and the losses fall as in Step 4. Colab deletes its machine when you leave. To keep the model, download `tiny_gpt_medium.pt` from the **Files** pane on the left before you close the tab.

#### Step 9: Save your work in Git

From the course folder:

```bash
cd ..
git add unit19/tiny_gpt.py
git commit -m "Train a tiny GPT on made-up SAP notes"
```

Don't commit `tiny_gpt.pt` or the text files: they are rebuilt by the script in minutes, and model files don't belong in a code history. If you use a `.gitignore` file, add the line `*.pt` to it.

#### What each part of the script does

| Part | What it does |
| --- | --- |
| `SIZES` | The two model sizes from the Unit 19 setup's timing script |
| `make_note`, `make_data` | Made-up SAP-style notes from three processes, with random numbers and a hidden quantity rule |
| `get_shakespeare` | Downloads Tiny Shakespeare from the address nanoGPT uses |
| `CharTokenizer` | One token per character: `encode` turns text into numbers, `decode` turns them back |
| `pick_device` | Uses an NVIDIA GPU (`cuda`), then an Apple GPU (`mps`), then the CPU |
| `get_batch` | Random 64-character windows; the targets are the same windows shifted one character |
| `loss_on` | Average loss on 20 batches, without training; used for both train and check parts |
| `generate` | Crops to the context, scores the next character, draws one by probability, repeats until the line ends |
| `train` | 90/10 split, `TinyTransformer` from the previous topic, AdamW with gradient clipping, losses printed five times, checkpoint saved |
| `load` | Rebuilds the model and loads the checkpoint with `weights_only=True` |
| `copies_cmd` | Generates notes and counts exact copies of training notes |

#### If something goes wrong

| What you see | What it means | What to do |
| --- | --- | --- |
| `python` is not recognized, or `command not found` | Python isn't installed, or the terminal can't find it | Windows: repeat Step 1 of [Set up your computer](https://erpfde.com/topics/set-up-your-computer/), then open a new terminal. macOS/Linux: use `python3` until `.venv` is active |
| `ModuleNotFoundError: No module named 'torch'` | PyTorch isn't in the Python you're using | Check for `(.venv)` in the prompt. If it's there, follow [Set up for Unit 18](https://erpfde.com/topics/set-up-for-unit-18/), Step 2 |
| `ModuleNotFoundError: No module named 'transformer_block'` | `transformer_block.py` isn't in the folder you're running from | Run from `unit19`, and save `transformer_block.py` there (previous topic, Step 2) |
| `TypeError: ... unexpected keyword argument` from `TinyTransformer` | Your `transformer_block.py` differs from the published one | Replace it with the version from [The transformer architecture](https://erpfde.com/topics/the-transformer-architecture/) |
| `sap_notes.txt not found` or `tiny_gpt.pt not found` | A step was skipped | Run `make-data` before `train`, and `train` before `generate` or `copies` |
| `get-shakespeare` shows `URLError`, a proxy error or a timeout | Your network or company proxy blocks GitHub downloads | Skip Step 7, or try another network. Everything else works offline |
| Asked for an API key | Nothing in this topic uses a key or account | Check you are running `tiny_gpt.py` |
| Training is very slow (over 10 minutes) | Your computer is slower than our test machine | Use `--steps 800`; the loss is already near its floor by then |
| The losses stay near 4 | The model isn't learning | Check that you didn't change `--lr`; the default `0.001` works for this model |

### Where this shows up in SAP

Unit 19 is a mechanics unit, so this section is short. As of October 2026:

#### You call models SAP and its partners trained

The generative AI hub gives access to pretrained LLMs from several providers. SAP's AI Core guide says it is available only in the extended service plan. SAP's own models follow the same pattern. SAP's Q4 2025 release highlights describe SAP-ABAP-1, trained on more than 250 million lines of ABAP code, 30 million lines of CDS code and technical documentation, and available on the generative AI hub. They describe SAP-RPT-1 as pretrained, to spare customers costly training. SAP doesn't publish the training recipes behind these models, so don't assume they match this topic's code. The idea is the same: an expensive training run once, then many cheap calls. Check the generative AI hub's current model list before planning around a specific model.

#### When you do train: SAP AI Core pipelines

For your own small models, SAP AI Core runs training as a pipeline. SAP Learning describes the flow: put the training data in an object store, create a configuration with the parameters for one run, start an execution, and collect the trained model as an artifact that SAP AI Core registers. A **resource plan** picks the hardware. The AI Core guide of September 2026 lists plans from Starter (one CPU core) up to Train-L (an "advanced GPU"). The guide also says SAP AI Core integrates with Argo Workflow, an open-source workflow engine.

Your code goes into a Docker image, and an Argo `WorkflowTemplate` tells SAP AI Core how to run it. This is how `tiny_gpt.py` could look as a training pipeline:

> **Note** This is a **sketch**, not something to run now. It needs an SAP AI Core instance with a service plan that allows training (SAP's tutorial uses the standard plan for this), a Docker registry, and a connected Git repository. Unit 10 covers running AI workloads on BTP. The labels and annotations follow SAP's "Build a House Price Predictor" tutorial; the image name and command are ours.

```yaml
# Sketch: tiny_gpt.py as an SAP AI Core training pipeline (not tested)
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: tiny-gpt-train
  annotations:
    scenarios.ai.sap.com/name: "Tiny GPT (course)"
    scenarios.ai.sap.com/description: "Character-level GPT on made-up SAP notes"
    executables.ai.sap.com/name: "tiny-gpt-train"
    executables.ai.sap.com/description: "Trains the Unit 19 tiny GPT"
  labels:
    scenarios.ai.sap.com/id: "tiny-gpt"
    ai.sap.com/version: "1.0"
spec:
  imagePullSecrets:
    - name: <your-registry-secret>
  entrypoint: train
  templates:
  - name: train
    container:
      image: docker.io/<YOUR_DOCKER_USERNAME>/tiny-gpt:01
      command: ["/bin/sh", "-c"]
      args:
        - "python tiny_gpt.py make-data && python tiny_gpt.py train"
```

A real pipeline would also declare the checkpoint as an output artifact so it lands in the object store, and choose a resource plan. SAP documents both; Unit 10 walks through them.

### Build vs. SAP

| Situation | Train your own (this topic) | Train in SAP AI Core | Call a pretrained model through SAP |
| --- | --- | --- | --- |
| Learning how LLMs work | Best: every line visible | Overkill | Shows behavior, not mechanics |
| Drafting text in an SAP process | No: far too small | Rarely worth pretraining | Best: generative AI hub, with facts given at request time |
| Explaining ABAP code | No | No | SAP-ABAP-1, if it fits your case and is in your model list |
| Predictions on SAP tables | No | Possible for a custom model | SAP-RPT-1 first; it needs no training step |
| A small custom model on your data | Prototype here | Yes, as a pipeline with a resource plan | Not applicable |

### Production concerns

- **Memorization is a data protection issue.** Carlini and colleagues extracted names, phone numbers and email addresses from GPT-2, some from a single document, and found larger models more vulnerable. The exercise reproduces this in miniature. Keep personal and confidential data out of training sets unless you have a tested plan for it. Unit 11 covers data security and PII.
- **Authorizations don't reach inside weights.** SAP authorizations decide who may see a sales order. Once order details are in a model's weights, any user of the model might get them back. Give data to a model at request time, filtered by the user's authorizations, instead. Unit 6 covers this.
- **Generated numbers need verification.** The tiny GPT broke the quantity rule while writing fluent notes. Check every generated amount, quantity and document number against the system of record before anyone acts on it.
- **Judge on held-out data.** Training loss says little. Always keep a check set the model never trains on, and evaluate on it. Unit 7 builds an evaluation harness.
- **Model files are code-adjacent.** Load checkpoints with `weights_only=True`, as PyTorch recommends, and only from sources you trust.
- **Training cost scales fast.** The Chinchilla study says data should grow with model size. Budget hardware, time and data together, and compare with the cost of calling a pretrained model.
- **Clean core still applies.** Training jobs run beside S/4HANA, in SAP AI Core or elsewhere, never inside it. Read SAP data through released APIs, as in [Calling your first SAP API](https://erpfde.com/topics/calling-your-first-sap-api/).

### Pitfalls

- **Trusting the training loss.** It can keep falling while the model gets worse on new text. Watch the check loss.
- **Forgetting the context limit.** The model sees only its last 64 characters. Anything earlier in a long prompt is gone.
- **Expecting zero loss.** Random parts of the data, such as order numbers, set a floor no model can beat.
- **Training on real data "just to test".** A test model can still memorize and be copied around. Use made-up data.
- **Loading an unknown checkpoint with `weights_only=False`.** That can run code from the file.
- **Comparing losses across tokenizers.** A character-level loss and a word-piece loss measure different things. Compare like with like.
- **Committing model files to Git.** They are large and rebuildable. Commit the code and the data recipe instead.

### Exercise: make the model memorize, then stop it

The tiny GPT copied nothing when it trained on 3,000 notes. Now give it only 100 notes, train it too long, and watch it start copying. Then find the fix. Your notes from this exercise feed Unit 11, where data security and PII come back.

```mermaid
flowchart LR
  F[100 notes] --> L[Train 2,000 steps]
  F --> S[Train 400 steps]
  L --> C1[copies]
  S --> C2[copies]
  C1 --> N[memorization_notes.md]
  C2 --> N
```

**Before you start:** finish **Build it yourself** above. You need `tiny_gpt.py` and `transformer_block.py` in `unit19`, and the terminal in `unit19` with `.venv` on. Each training run takes 1 to 4 minutes on a laptop CPU.

1. Make a small data set of 100 notes:

   ```bash
   python tiny_gpt.py make-data --notes 100 --out few_notes.txt
   ```

   **What success looks like:** `Wrote 100 made-up notes (10,495 characters) to few_notes.txt`, followed by the same first three notes as before.

2. Train on it for the full 2,000 steps, saving to a separate model file:

   ```bash
   python tiny_gpt.py train --data few_notes.txt --model few.pt
   ```

   **What success looks like** (your numbers may differ a little):

   ```text
     step     0  train loss 4.09  check loss 4.09  [1 s]
     step   400  train loss 0.34  check loss 0.43  [43 s]
     step   800  train loss 0.19  check loss 0.52  [83 s]
     step  1200  train loss 0.14  check loss 0.55  [123 s]
     step  1600  train loss 0.13  check loss 0.63  [163 s]
     step  2000  train loss 0.13  check loss 0.62  [204 s]
   ```

   Training loss falls below the 0.31 floor of Step 4, while check loss climbs. That is overfitting: the model has learned the random digits of these exact 90 training notes.

3. Count the copies:

   ```bash
   python tiny_gpt.py copies --model few.pt
   ```

   **What success looks like:**

   ```text
   Generated 200 notes; 85 are word-for-word copies of a training note (42%).
   The training part of few_notes.txt holds 91 different notes.
     copied: Delivery block on sales order 10693, customer C-1281: credit limit exceeded by 33,136.50 EUR. Sales rep to call the customer.
     copied: Delivery block on sales order 11727, customer C-1369: price below minimum. Check with the credit manager today.
   ```

   > **Note** These loss and copy numbers come from made-up notes and a toy model. They show the method for measuring memorization, not how often a real LLM, or a model trained on real SAP data, would copy its training text.

   Open `few_notes.txt` and search for `10693` with **Edit > Find**. The note appears once. The model repeats a made-up customer's number and exact open amount, from a single training line.

4. Now stop training early. Train on the same 100 notes for 400 steps only:

   ```bash
   python tiny_gpt.py train --data few_notes.txt --steps 400 --model few400.pt
   python tiny_gpt.py copies --model few400.pt
   ```

   In our run, the check loss ended at 0.44 and the model copied 0 of 200 notes. Stopping when the check loss stops improving is called **early stopping**. It doesn't make memorization impossible, but it removes the worst of it here.

5. In VS Code, create `unit19/memorization_notes.md` with this table, filled in with your numbers:

   ```text
   # Memorization in my tiny GPT

   | Notes | Steps | Final train loss | Final check loss | Copies out of 200 |
   | --- | --- | --- | --- | --- |
   | 3,000 | 2,000 |  |  |  |
   | 100 | 2,000 |  |  |  |
   | 100 | 400 |  |  |  |
   ```

   Below the table, write three sentences: why the 100-note model copied, what early stopping changed, and what this means for training a model on real customer data.

6. Save your work:

   ```bash
   cd ..
   git add unit19/memorization_notes.md
   git commit -m "Measure memorization in a tiny GPT"
   ```

If the 100-note model copies far fewer notes on your computer, run step 2 again with `--seed 1` and note the seed in your file. Keep `tiny_gpt.pt` from Step 4 of **Build it yourself**: the next topic, How LLMs generate text, uses it to explore temperature and other ways of choosing the next token.

**Done when:** `tiny_gpt.py generate` writes readable notes from your `tiny_gpt.pt`, `memorization_notes.md` holds the three-row table with your numbers and your three sentences, and both `tiny_gpt.py` and `memorization_notes.md` are committed in Git.

### Check yourself

1. Why can one 64-character window give the model 64 lessons in a single forward pass?
   - A. The tokenizer copies the window 64 times, each with a different target
   - B. AdamW updates the weights once for every character in the window it reads
   - C. The causal mask lets each position predict the next one without peeking
   - D. The model reads the window backwards and forwards in the same pass

   Answer: C. The targets are the inputs shifted by one, and the causal mask stops each position from seeing later characters. So every position makes a fair guess at its own next character, and the loss averages all of them at once.

2. Your untrained model with a 57-character vocabulary reports a first loss of 9.5. What should you conclude?
   - A. That is normal, since untrained losses vary widely between runs
   - B. The setup has a bug, since even guessing gives about ln(57) = 4.04
   - C. The model is already overfitting its training data badly
   - D. The learning rate is too low, so training should simply run longer

   Answer: B. An untrained model spreads its probability about evenly, so its loss should be close to ln(vocabulary size). A first loss far above that means the model starts out confidently wrong, which points to a bug in the setup, not a training problem.

3. On the made-up notes, both losses flatten near 0.31 and never reach zero. Why?
   - A. The model is too small to learn any of the templates in the notes
   - B. Gradient clipping stops the loss from going any lower than that
   - C. The check part of the data uses different characters
   - D. The notes contain random numbers that no model can predict

   Answer: D. Order numbers, customer numbers and amounts are random digits, so some guesses must stay uncertain. That sets a floor. A training loss well below it, as in the exercise, means the model has memorized specific numbers.

4. What does `generate` do before each guess, and why?
   - A. Keeps the last 64 characters, as the model has only 64 position slots
   - B. Removes the prompt, because the model has already read it once before
   - C. Sorts the characters, because the tokenizer expects them in order
   - D. Adds a newline, because every note must be exactly one line long

   Answer: A. The tiny GPT learned position embeddings for 64 slots, so a longer input can't be processed. nanoGPT crops the same way. Anything earlier than the window is invisible to the model, the same limit large models have at a larger size.

5. The model writes an invoice note with a goods receipt of 1,460 against an invoice of 230. What explains it best?
   - A. The note was copied word for word from the training data
   - B. The tokenizer can't represent four-digit numbers like 1,460 correctly
   - C. It learned the shape of the notes, not the rule linking the numbers
   - D. The temperature setting forced it to pick unlikely digits

   Answer: C. Every training note had a receipt slightly below the invoice, and this note breaks that rule while looking perfect. The model predicts plausible characters, not facts. That is why generated numbers must be checked against the system of record.

6. Why does the script load checkpoints with `weights_only=True`?
   - A. It makes the model load faster when a GPU is available
   - B. It skips the tokenizer so that only the model weights are restored
   - C. It is required to load a model trained on another device
   - D. It limits loading to what a state dict needs, so no code can run

   Answer: D. PyTorch's documentation explains that `weights_only=True`, the default since 2.6, restricts the unpickler to what state dicts need. A normal pickle file can run code when loaded, so an untrusted model file is a security risk otherwise.

7. A team trained a model on 500 real service notes for 20,000 steps. Training loss is tiny and check loss has risen for hours. What do you do?
   - A. Ship it, since a tiny training loss shows it learned the notes well
   - B. Treat it as overfit and memorizing: test for copies, stop earlier
   - C. Train longer, since check loss always rises before it falls
   - D. Add more blocks so it can separate training and check data

   Answer: B. Falling training loss with rising check loss is overfitting. In the exercise, the same pattern came with 42% word-for-word copies, while stopping early cut copies to zero. With real notes, those copies could contain customer data.

8. Your company wants a model that drafts replies about blocked sales orders. Which approach fits SAP's offering best?
   - A. Pretrain a GPT on all order notes in SAP AI Core on a Train-L plan
   - B. Train this topic's tiny GPT on the order notes, then deploy it to users
   - C. Ask SAP to add your order notes to SAP-ABAP-1's training data
   - D. Call a pretrained model in the generative AI hub, with facts per request

   Answer: D. Pretraining needs vast data and compute, and puts customer data into the weights where authorizations can't reach. The generative AI hub, in the extended plan, gives access to pretrained models, and facts filtered by the user's authorizations can be given with each request.

## Sources

- [nanoGPT README (karpathy/nanoGPT on GitHub)](https://github.com/karpathy/nanoGPT): character-level Shakespeare quick start; baby GPT with context 256, 384 channels, 6 layers, 6 heads reaches best validation loss 1.4697 in about 3 minutes on one A100; CPU settings with 4 layers, width 128, context 64 reach about 1.88 in about 3 minutes; nanoGPT points to its newer cousin nanochat
- [nanoGPT data/shakespeare_char/prepare.py (karpathy/nanoGPT on GitHub)](https://github.com/karpathy/nanoGPT/blob/master/data/shakespeare_char/prepare.py): Tiny Shakespeare download URL; 1,115,394 characters; 65 unique characters; 90% train and 10% validation split
- [Improving Language Understanding by Generative Pre-Training (Radford et al., OpenAI, 2018), Semantic Scholar](https://www.semanticscholar.org/paper/Improving-Language-Understanding-by-Generative-Radford-Narasimhan/cd18800a0fe0b668a1cc19f2ec95b5003d0a5035): generative pre-training of a language model on a diverse unlabeled corpus, then fine-tuning for each task
- [Training Compute-Optimal Large Language Models (Hoffmann et al., 2022), arXiv](https://arxiv.org/pdf/2203.15556): over 400 models from 70 million to over 16 billion parameters on 5 to 500 billion tokens; for every doubling of model size, training tokens should also double; Chinchilla has 70B parameters trained on 1.4T tokens
- [Extracting Training Data from Large Language Models (Carlini et al., USENIX Security 2021), ar5iv](https://ar5iv.arxiv.org/html/2012.07805): hundreds of verbatim training sequences extracted from GPT-2, including names, phone numbers and email addresses, some found in just one document; larger models more vulnerable than smaller ones
- [Serialization semantics (PyTorch main documentation)](https://docs.pytorch.org/docs/main/notes/serialization.html): since 2.6, torch.load uses weights_only=True by default; it restricts unpickling to what state dicts need; saving the state dict rather than the whole module is recommended
- [SAP AI Core (SAP Help Portal PDF, version of 2026-09-04)](https://help.sap.com/doc/c31b38b32a5d4e07a4488cb0f8bb55d9/CLOUD/en-US/f17fa8568d0448c685f2a0301061a6ee.pdf): executes pipelines as batch jobs, for example to train models; integrates with Argo Workflow and KServe; free, standard and extended plans; generative AI hub only in the extended plan; resource plans from Starter (1 CPU core) to Train-L (advanced GPU)
- [Training an ML model (SAP Learning, Introduction to SAP AI Core)](https://learning.sap.com/courses/introduction-to-ai-core/training-an-ml-model_fe6540c3-bad0-4855-b8df-ff6674597177): dataset in an object store, configuration, execution, artifacts registered automatically; resource plans differ in CPUs, GPUs and memory; APIs to record metrics
- [Build a House Price Predictor with SAP AI Core (SAP Developers tutorial)](https://developers.sap.com/tutorials/ai-core-code.html): Argo WorkflowTemplate with scenarios.ai.sap.com and executables.ai.sap.com annotations, scenarios.ai.sap.com/id and ai.sap.com/version labels; Docker image built and pushed to a registry; standard plan for narrow AI, extended plan for the generative AI hub
- [SAP Business AI: Release Highlights Q4 2025 (SAP News, 14 January 2026)](https://news.sap.com/2026/01/sap-business-ai-release-highlights-q4-2025/): SAP-ABAP-1 trained on more than 250 million lines of ABAP code, 30 million lines of CDS code and technical documentation, available on the generative AI hub and free to try in its trial; SAP-RPT-1 comes pretrained, is on the generative AI hub for productive use, and has a separate free web playground
