GitHub

Build AI models in TypeScript.

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.

installBETA · v0.63.2License: MIT
install, on macOS or Linux
$ curl -fsSL https://simien.dev/install.sh | sh
run a program
$ simien run model.ts
Snapshot
5-tiny-transformer.tshover or select lines to light what they madeOpen in playground
import 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.

How it works, in four stages

from a grid of numbers to a network that trains; each program ran as shown

The playground

live at playground.simien.dev

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 playground
  • Run any lesson, or your own program, with no install.
  • See the model redrawn as you change the code, with the run's numbers on it.
  • Share a link: the code is in the link itself, and nothing is stored.

The learning path

each lesson opens in the playground

Why Simien

the short version

PyTorch, by name

nn.Linear, AdamW, backward(). What you learn here is what you will read in papers and in PyTorch code.

Types you can check

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.

No Python to set up

simien:torch runs on libtorch, PyTorch’s own C++ engine. One install on macOS or Linux. Windows support coming soon.

What it is made of

the datasheet
Runtime
Rust · V8 · web-standard APIs
Tensors
libtorch 2.2.2 natively; in the browser, WebGPU or the CPU through WebAssembly
Graph
simien:graph, Zega built in
Platforms
Linux x64 · macOS x64 · macOS arm64
Release
v0.63.2
License
MIT