Add clean Path A proof of concept

Spoken-English math to Typst with a small from-scratch encoder-decoder
Transformer, following the build guide: compile-checked synthetic data
generator, shared BPE tokenizer, nn.Transformer model, training loop with
an overfit sanity check, greedy decoding CLI, and evaluation on synthetic
and hand-written test sets. ./run.sh runs the whole pipeline.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
paultalandClaude Opus 5.5 committed 2026-11-27 00:14:06 +02:00
1 parent 5926731546
commit fa472f8abd
20 files changed
+2398 -1

No files matched your search

+55 -1
View File
@@ -2,4 +2,58 @@
**isotope-typst** is a Transformer model that translates math written in plain English into [Typst](https://typst.app) markup.
For example, `the integral from 0 to infinity of e to the minus x squared dx` becomes `$integral_0^infinity e^(-x^2) dif x$`.
For example, `the integral from 0 to infinity of e to the minus x squared dx` becomes `$integral_0^infinity e^(-x^2) dif x$`.
The current code is the **Path A proof of concept**: a small (~8M parameter) encoder–decoder Transformer trained from scratch on synthetic data, following the [build guide](https://git.paultal.online/paultal/generated-books/src/branch/main/isotope-typst-guide.pdf).
## Run it
Needs [uv](https://docs.astral.sh/uv/) and the Typst CLI (`brew install typst`).
```bash
./run.sh # everything, ~20 min on an M-series Mac
./run.sh --quick # smoke run, ~2 min, tiny model
uv run isotope "x squared plus y squared equals r squared"
uv run isotope # interactive prompt
```
Or one stage at a time:
| guide | command | what it does |
|---|---|---|
| §4 data | `uv run isotope-gen -n 200000` | random trees → English + Typst pairs, compile-checked, into `data/` |
| §5 tokens | `uv run isotope-tokenizer` | byte-level BPE shared by both sides → `artifacts/tokenizer.json` |
| §7 sanity | `uv run isotope-train --overfit 100` | a tiny model must memorise 100 pairs, or the pipeline has a bug |
| §7 training | `uv run isotope-train` | `--config tiny/small/medium`, `--steps`, Ctrl-C saves a checkpoint |
| §8 inference | `uv run isotope "..."` | greedy decoding; `--check` compiles the output |
| §9 evaluation | `uv run isotope-eval` | exact match and compile rate on synthetic test + `eval/hand_test.jsonl` |
| tests | `uv run pytest` | renderer compiles, English is unambiguous, tokenizer round-trips, causal mask works |
`data/` and `artifacts/` are generated and not tracked in git.
## Code
```
src/isotope_typst/
ast.py expression nodes: Var Num Neg Add Sub Mul Pow Frac Sqrt Func Integral Sum Lim Eq
render.py to_typst() (deterministic) and to_english() (random phrasings)
generate.py random trees -> data/{train,valid,test}.jsonl, split by a hash of the Typst string
validate.py `typst compile` checks
tokenizer.py BPE tokenizer
model.py Seq2Seq on nn.Transformer, with encode()/decode() for inference
train.py teacher forcing, AdamW, warmup + cosine, checkpoints, train_log.csv
translate.py greedy decoding + CLI
evaluate.py metrics + failure dumps in artifacts/errors_*.txt
eval/hand_test.jsonl hand-written test sentences: never generated, never trained on
```
## Conventions the English follows
* "e to the minus x squared" means `e^(-x^2)`: a bare exponent takes everything after it (guide Appendix A, Q1).
* Grouping is said with "( … )", "open paren … close paren", or, at the end of a phrase, "the quantity …".
* "sine of x squared" means `sin(x^2)`; `sin(x)^2` is "sine squared of x".
* Integrals, sums and limits appear only at the top level of an expression.
## Not in this PoC (yet)
Beam search, constrained decoding, render-match evaluation, a hand-written attention module, paraphrase augmentation, and Path B fine-tuning.