# Concept visuals: LLM Transformer ↔ Point Transformer (PTv3)

Lecture material for DSE411. Each concept is one HTML page that puts the GPT-style
transformer (code from `LLMs-from-scratch-pyare`) side by side with the point-cloud
transformer (PTv3), in this order on every page:

1. **Need** – what breaks without the mechanism, on each side
2. **Concept** – the mechanism in plain words and one formula
3. **Code + output** – the course notebook code, actually run, with its real output
4. **Visual** – an interactive view of the same numbers
5. **Parallel** – one table: what is the same, what changes for point clouds
6. **Check yourself**

## Pages

| # | Page | LLM side | Point-cloud side |
|---|------|----------|------------------|
| 1 | `01_tokenization.html` | regex tokenizer, vocab, BPE, sliding window | grid quantization, Z-order / Hilbert codes, sorting, patches |
| 2 | `02_embedding.html` | `nn.Embedding` lookup + absolute position table | linear embedding of (x,y,z,i); position from geometry (xCPE idea) |
| 3 | `03_attention.html` | simple → trainable scaled dot-product attention | dot-product attention inside one 64-point patch; vector attention |
| 4 | `04_mask_interaction.html` | causal mask, dropout, multi-head | no mask; Shift / Shuffle Order so patches talk; multi-head |
| 5 | `05_block_model.html` | LayerNorm, GELU, FFN, TransformerBlock, GPTModel | PTv3 block, grid pooling, mini U-Net forward on the car |

## Page anatomy (lesson style)

Every lesson opens with two maps (`fig/gen/roadmap_0N.svg`, `fig/gen/pipeline_0N.svg`), then a
chain of concept cards. A card has: **What** (definition) · **Why we need it** (amber strip) ·
the two instances, **LLM** (violet, with the book's figure) and **Point cloud** (teal, with a
figure drawn from the car's numbers), each with the real output, the ⚙ line and the code one
click away · an italic bridge to the next card. `FLOW.md` lists every card with its question.

Figures: `fig/book/` holds the book's figures for local use only (git-ignored, Manning copyright; `tools/fetch_book_figs.sh` restores them from the course notebooks, and the pages show a captioned placeholder when they are absent);
`fig/gen/` is drawn by `tools/make_figs.py` from `outputs/*.json`.

## How to present

Open a lesson locally (all figures present) and press **Present** in the top bar: the page goes
fullscreen and shows one card per screen. Keys: → / space / PageDown next · ← / PageUp previous ·
Home / End · Esc exit. Each concept card is three slides (what + why, the LLM instance, the
point-cloud instance); the bridge to the next card is shown at the bottom. The interactive demos
work inside Present mode. A slide can be linked directly: `03_attention.html#present=7`.

## How it is built

```
code/llm/c0*.py      LLM-side scripts   (import code/common_llm.py = notebook listings)
code/pc/c0*.py       point-cloud scripts (import code/common_pc.py = serialization + PTv3 pieces)
code/run_all.sh      runs every script, writes outputs/<script>_<side>.txt and outputs/*.json
tools/make_figs.py   draws fig/gen/*.svg (maps, pipelines, point-cloud concept figures) from outputs/
pages/*.tpl.html     page templates with {{CODE ...}} / {{OUT ...}} / {{JSON ...}} / {{INCLUDE ...}} tags
build.py             fills the templates -> *.html (open directly in a browser, no server needed)
assets/tutorial.css  the lesson stylesheet (cards, instances, figures); assets/common.js helpers
```

Workflow when you correct something:

```bash
# 1. edit a script in code/llm or code/pc, or a template in pages/
code/run_all.sh            # re-run the code (uses the LLMs-from-scratch-pyare .venv: torch + tiktoken)
python3 tools/make_figs.py # redraw the maps and the point-cloud figures from outputs/*.json
python3 build.py           # rebuild the HTML
```

Code sections are marked `# ---[ name ]---` in the scripts and printed outputs are marked
`## [name]`, so a template can quote any slice with `{{CODE code/llm/c03_attention.py simple}}`
and `{{OUT outputs/c03_attention_llm.txt simple}}`.

Data: `the-verdict.txt` and `car_pcloud.csv` are read from the LLMs-from-scratch-pyare repo
(paths in `code/paths.py`). The browser visuals use a 2 500-point subsample of the car,
written to `outputs/c01_pc.json` by `code/pc/c01_serialization.py`.

## Sources

- S. Raschka, *Build a Large Language Model (From Scratch)*; original notebooks at https://github.com/rasbt/LLMs-from-scratch (e.g. `ch02/01_main-chapter-code/ch02.ipynb`); course copies in `LLMs-from-scratch-pyare`
- X. Wu et al., *Point Transformer V3: Simpler, Faster, Stronger*, CVPR 2024
- H. Zhao et al., *Point Transformer*, ICCV 2021 (vector attention, used on page 3)
