# isotope-typst Turn spoken-English math into Typst math with a small Transformer trained from scratch. ``` $ uv run isotope "the integral from 0 to infinity of e to the minus x squared dx" $integral_0^infinity e^(-x^2) dif x$ ``` ## Run it ```bash ./run.sh # build everything (skips finished steps) ./isotope "x squared plus y squared equals r squared" ./isotope # REPL: model stays loaded, :help for commands ``` `./isotope` is shorthand for `uv run isotope`. The trained model (`artifacts/model.pt`) is not in git. Run `./run.sh` to train one before using `./isotope`. `./run.sh` generates data, trains the tokenizer, runs the memorise-100-pairs sanity check, trains the model, and evaluates it. `./run.sh --quick` does a smoke run in a few minutes. `./run.sh --fresh` starts again from nothing. Translator options: `--beam 4` for beam search, `--check` to compile-check the output, `--raw` to leave off the `$ … $`, and `--no-constrain` to turn off paren-balanced decoding. ## Commands, one per stage of the guide | stage | command | file | |---|---|---| | §4 data | `uv run isotope-gen` (`--basic -n 10000` for milestone M1) | `ast.py`, `render.py`, `generate.py`, `validate.py` | | §5 tokens | `uv run isotope-tokenizer` | `tokenizer.py` | | §6 model | built-in `nn.Transformer`, or `--custom-attn` for the hand-written attention | `model.py` | | §7 training | `uv run isotope-train` (`--overfit 100`, `--config tiny/small/medium`) | `train.py` | | §8 inference | `uv run isotope "..."` (greedy, beam, constrained decoding) | `translate.py` | | §9 evaluation | `uv run isotope-eval` (exact / compiles / render match, synthetic and hand-written sets) | `evaluate.py` | | §10 Path B | `uv run --group pathb isotope-pathb all` (mlx-lm LoRA on Qwen2.5-0.5B), or `uv run --group byt5 isotope-pathb all --backend byt5` | `pathb.py` | | §4.1 paraphrase | `uv run --group pathb isotope-paraphrase -n 2000 --append` | `paraphrase.py` | | all of Path A | `uv run isotope-pipeline` (what `run.sh` calls) | `pipeline.py` | | tests | `uv run pytest` | `tests/` | Outputs go to: * `data/`: `train/valid/test.jsonl`, split by a hash of the Typst string, plus `stats.txt`, a histogram of node types * `artifacts/`: `tokenizer.json`, `model.pt`, `train_log.csv`, `eval.json`, `errors_*.txt` (failure dumps for error analysis) * `eval/hand_test.jsonl`: the hand-written test set, which is never generated and never trained on ## Conventions the generator uses * "e to the …" takes everything after it as the exponent, so "e to the minus x squared" means `e^(-x^2)`. * "the quantity …", "( … )" and "open paren … close paren" mark grouping. * Fractions render as `frac(a, b)`, functions as `sin(x)`, and derivatives as `frac(dif, dif x) …`. ## Results on an M4 MacBook Air Render match is the share of outputs whose compiled Typst looks identical to the reference. | model | synthetic test | hand-written test | training time | |---|---|---|---| | Path A, first data generator | 96.3% | 71.3% | ~18 min | | Path A, after error analysis (default) | **96.3%** | **83.2%** | ~18 min | | Path B, Qwen2.5-0.5B + LoRA, 1000 iters | 73.0% | 80.2% | ~20 min | The hand-written score rose from 71% to 83% by changing only the data, which is the guide's point (§9, Q7). Path B gets close on human phrasing with far less task data. It does worse on the synthetic set because it has seen only 20k of the 288k pairs. Turn off Low Power Mode before training: it makes training about 3× faster.