Files
paultal 98e88cac04 Add clean Path A proof of concept (#1)
Spoken-English math to Typst with a small (~8M parameter) encoder-decoder Transformer trained from scratch, following the build guide (Path A only).

## What's in it
- `generate.py` / `render.py` / `ast.py`: random expression trees rendered to Typst (deterministic) and English (random phrasings); every Typst string is compile-checked, split by a hash of the Typst string
- `tokenizer.py`: byte-level BPE shared by both sides
- `model.py`: `nn.Transformer` seq2seq with `encode()` / `decode()` for inference
- `train.py`: teacher forcing, AdamW, warmup + cosine, overfit-100 sanity check, checkpoints
- `translate.py`: greedy decoding CLI (`uv run isotope "..."`)
- `evaluate.py`: exact match + compile rate on the synthetic test split and `eval/hand_test.jsonl`
- `run.sh`: whole pipeline; `./run.sh --quick` is a 2-minute smoke run
- `AGENTS.md`: commit and workflow rules for agents

## Checked
- `uv run pytest` (8 tests: Typst compiles, English is unambiguous, tokenizer round-trips, causal mask) and `ruff check` pass
- `./run.sh --quick` and the full `./run.sh` run end to end on an M-series Mac

Reviewed-on: #1
Co-authored-by: Paul Tal <[email protected]>
2026-11-27 00:24:05 +02:00

3.0 KiB
Raw Permalink Blame History

isotope-typst

isotope-typst is a Transformer model that translates math written in plain English into Typst 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$.

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.

Run it

Needs uv and the Typst CLI (brew install typst).

./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.