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]>
This commit was merged in pull request #1.
This commit is contained in:
paultal authored and paultal committed 2026-11-27 00:24:05 +02:00
1 parent 5926731546
commit 98e88cac04
21 files changed
+2411 -1

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
import torch
from isotope_typst.model import Seq2Seq, causal_mask
def test_causal_mask():
m = causal_mask(4)
assert not m[2, :3].any() and m[2, 3] # row 2 sees 0..2, not 3
def test_forward_shape():
model = Seq2Seq(vocab=50, d=32, heads=4, layers=2, ff=64, drop=0.0)
src, tgt = torch.randint(4, 50, (3, 7)), torch.randint(4, 50, (3, 5))
assert model(src, tgt).shape == (3, 5, 50)
def test_decoder_cannot_see_the_future():
"""Changing a later target token must not change the logits at earlier positions."""
torch.manual_seed(0)
model = Seq2Seq(vocab=50, d=32, heads=4, layers=2, ff=64, drop=0.0).eval()
src = torch.randint(4, 50, (1, 6))
a = torch.randint(4, 50, (1, 5))
b = a.clone()
b[0, 4] = (a[0, 4] + 1) % 46 + 4
with torch.no_grad():
assert torch.allclose(model(src, a)[0, :4], model(src, b)[0, :4], atol=1e-5)