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:
1 parent
5926731546
commit
fa472f8abd
20 files changed
+2398
-1
No files matched your search
@@ -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)
|
||||
@@ -0,0 +1,35 @@
|
||||
import collections
|
||||
import random
|
||||
|
||||
from isotope_typst.ast import Frac, Integral, Neg, Num, Pow, Var
|
||||
from isotope_typst.generate import TreeGen, split_of
|
||||
from isotope_typst.render import to_english, to_typst
|
||||
from isotope_typst.validate import compiles_many
|
||||
|
||||
|
||||
def test_guide_example():
|
||||
t = Integral(Pow(Var("e"), Neg(Pow(Var("x"), Num(2)))), "x", Num(0), Var("infinity"))
|
||||
assert to_typst(t) == "integral_0^infinity e^(-x^2) dif x"
|
||||
assert to_typst(Frac(Num(1), Var("x"))) == "frac(1, x)"
|
||||
|
||||
|
||||
def test_generated_typst_compiles():
|
||||
gen = TreeGen(random.Random(0))
|
||||
maths = [to_typst(gen.tree()) for _ in range(500)]
|
||||
assert all(compiles_many(maths))
|
||||
|
||||
|
||||
def test_english_is_unambiguous():
|
||||
"""No sentence may come from two trees with different Typst: that would teach the model to guess."""
|
||||
rng = random.Random(0)
|
||||
gen = TreeGen(rng)
|
||||
seen = collections.defaultdict(set)
|
||||
for _ in range(20000):
|
||||
t = gen.tree()
|
||||
seen[to_english(t, rng, noise=False)].add(to_typst(t))
|
||||
bad = {en: typs for en, typs in seen.items() if len(typs) > 1}
|
||||
assert not bad, list(bad.items())[:5]
|
||||
|
||||
|
||||
def test_split_is_by_structure():
|
||||
assert split_of("x^2") == split_of("x^2")
|
||||
@@ -0,0 +1,17 @@
|
||||
import random
|
||||
|
||||
from isotope_typst.generate import TreeGen
|
||||
from isotope_typst.render import to_english, to_typst
|
||||
from isotope_typst.tokenizer import BOS, EOS, PAD, train
|
||||
|
||||
|
||||
def test_round_trip(tmp_path):
|
||||
rng = random.Random(0)
|
||||
gen = TreeGen(rng)
|
||||
trees = [gen.tree() for _ in range(2000)]
|
||||
texts = [s for t in trees for s in (to_english(t, rng), to_typst(t))]
|
||||
tok = train(texts, str(tmp_path / "tok.json"), vocab=1000)
|
||||
assert (tok.token_to_id("[PAD]"), tok.token_to_id("[BOS]"), tok.token_to_id("[EOS]")) == (PAD, BOS, EOS)
|
||||
for t in trees[:1000]: # guide §5: decode(encode(s)) == s, or nothing downstream can be right
|
||||
s = to_typst(t)
|
||||
assert tok.decode(tok.encode(s).ids) == s
|
||||
Reference in new issue
Block a user