A TypeScript neural network framework

Build neural networks in TypeScript.

Use Tensor operations and Module classes to define models. TypeNN records those operations in a deferred graph; TypeShade compiles and runs their kernels on CPU and supported GPU backends.

Pre-release · source and examples are the current usage reference.

Tensor APIexample.ts
import * as nn from "typenn";

const device = await nn.device();
const input = device.tensor([1, 2, 3, 4], { shape: [2, 2] });
const weight = device.tensor([5, 6, 7, 8], { shape: [2, 2] });
const output = input.matmul(weight).relu();

console.log(await output.data());
// Float32Array [19, 22, 43, 50]
await output.disposeGraph();
await device.dispose();
Float32Array[19, 22, 43, 50]Example output

Execution

How TypeNN executes Tensor operations

How execution works
01

Define

Tensor calls define shapes and operations. TypeNN records each call as a graph node.

tensor.matmul(weight).relu()
02

Call kernel functions

Each node holds a TypeShade kernel function. When you call data(), TypeNN runs the function with that node's input and output arrays.

TypeShade kernel
03

Run the graph

When needed, data() runs the kernels for the requested result on the selected tier.

CPU · WebGPU · WebGL2*

* WebGL2 and the CPU tier run the same kernel functions apart. Only WebGPU fuses an elementwise chain into one dispatch.

Try TypeNN

Try TypeNN in your browser

Run matrix operations in the browser, then read the guides to learn about graph execution, automatic differentiation, and model training.

Current implementation

Training and execution

When data() computes a new result, it runs the required kernels. After a plan runs, inspect device.lastReport for its execution tier and dispatches. SGD and Adam update parameters on the device; tiled MatMul remains future work.

Read implementation status