PyTorch, by name
nn.Linear, AdamW, backward(). What you learn here is what you will read in papers and in PyTorch code.
Simien is a TypeScript runtime with PyTorch's API and a graph database built in. Write a model, train it, and see exactly what you built. One binary, no Python.
$ curl -fsSL https://simien.dev/install.sh | sh$ simien run model.tsimport torch, { F, nn, optim, type Tensor } from "simien:torch"; torch.manual_seed(0); // A small language model: 2 layers, 128 numbers wide, 8,000 words.const WIDTH = 128, HEADS = 4, CONTEXT = 64, WORDS = 8000; class Layer extends nn.Module { attention_norm = new nn.LayerNorm(WIDTH); query_key_value = new nn.Linear(WIDTH, 3 * WIDTH); attention_output = new nn.Linear(WIDTH, WIDTH); feed_forward_norm = new nn.LayerNorm(WIDTH); expand = new nn.Linear(WIDTH, 4 * WIDTH); contract = new nn.Linear(4 * WIDTH, WIDTH); forward(stream: Tensor): Tensor { const [batch, length] = stream.shape; // every word looks back at the words before it const [q, k, v] = this.query_key_value.forward(this.attention_norm.forward(stream)).chunk(3, -1) .map((t) => t.view(batch, length, HEADS, WIDTH / HEADS).transpose(1, 2)); const gathered = F.scaled_dot_product_attention(q, k, v, { is_causal: true }) .transpose(1, 2).reshape(batch, length, WIDTH); stream = stream.add(this.attention_output.forward(gathered)); return stream.add(this.contract.forward(F.gelu(this.expand.forward(this.feed_forward_norm.forward(stream))))); }} class TinyModel extends nn.Module { words = new nn.Embedding(WORDS, WIDTH); positions = new nn.Embedding(CONTEXT, WIDTH); layers = new nn.ModuleList([new Layer(), new Layer()]); final_norm = new nn.LayerNorm(WIDTH); forward(ids: Tensor): Tensor { const length = ids.shape[1]; let stream = this.words.forward(ids).add(this.positions.forward(torch.arange(length))); for (const layer of this.layers) stream = layer.forward(stream); // scores for every word: the word table, read backwards return this.final_norm.forward(stream).matmul(this.words.weight.t()); }} // One made-up sentence of 17 words, four times: learn to say the next word.const model = new TinyModel();const ids = torch.randint(0, WORDS, [1, 17]).expand(4, 17);const inputs = ids.narrow(1, 0, 16), targets = ids.narrow(1, 1, 16);const parameters = model.parameters();const optimizer = new optim.AdamW(parameters, { lr: 1e-3 }); for (let step = 1; step <= 20; step++) { const loss = torch.scope(() => { const loss = F.cross_entropy(model.forward(inputs).flatten(0, 1), targets.flatten()); optimizer.zero_grad(); loss.backward(); nn.utils.clip_grad_norm_(parameters, 1.0); optimizer.step(); return loss.item(); }); if (step === 1 || step % 5 === 0) console.log(`step ${step} loss ${loss.toFixed(3)}`);} Lesson 5, a tiny transformer, drawn by the playground from a run of simien v0.63.2. Run live trains it here, in your browser.
import torch from "simien:torch";
// A tensor is a grid of numbers with a shape.
const a = torch.tensor([[1, 2, 3], [4, 5, 6]], { dtype: torch.float32 });
console.log("shape", a.shape);
// Operations work on the whole grid at once.
const b = a.mul(10);
console.log(b.tolist());
console.log("sum", a.sum().item());
shape [ 2, 3 ] [ [ 10, 20, 30 ], [ 40, 50, 60 ] ] sum 21
b has shape [2, 3]; a.sum() is 21
A tensor is a grid of numbers with a shape. Math applies to every cell at once, which is what makes models fast.
The same simien:torch, in your browser: on your GPU through WebGPU, or on the CPU through WebAssembly. Edit a model, run it, and watch it train.
open the playgroundA grid of numbers with a shape, and math on all of it at once.
2 · LESSONrequires_grad and backward(): which way is uphill, and how steep.
3 · LESSONA training loop by hand: the loss, a step downhill, repeat.
4 · LESSONA hidden layer with nn.Module, nn.Linear and AdamW.
5 · LESSONEmbeddings, attention and a feed-forward, trained on a sentence.
DOCSOne command on macOS or Linux, then simien run, check and the rest.
nn.Linear, AdamW, backward(). What you learn here is what you will read in papers and in PyTorch code.
simien check reads every program with the TypeScript compiler before it runs, so a misspelt name or a wrong argument shows up in your editor.
simien:torch runs on libtorch, PyTorch’s own C++ engine. One install on macOS or Linux. Windows support coming soon.