Getting started
The core API is a beta preview and may change between minor versions.
runntime/core is the engine the zoo is built on: graph ops (matmul,
sdpa, rope, rmsNorm, …), PyTorch-style nn modules and a safetensors
loader. Use it to run a model the zoo does not have.
Ops do not touch the GPU. Each one checks its inputs and returns a pending
Value. toArray() runs everything the value needs in one submit and reads
it back.
import { function add(a: Value, b: Value | number): Value
add, function initRunntime(root: TgpuRoot): RunntimeExecutor
initRunntime, function matmul(a: Value, b: Value, opts?: MatmulOpts): Value
matmul, function tensor(data: Float32Array, shape: readonly [number, number] | ValueMeta): Value (+1 overload)
tensor, function toArray(value: Value, ex?: Executor & Readback): Promise<Float32Array>
toArray } from 'runntime/core';import const tgpu: { const: { <TDataType extends AnyData>(dataType: TDataType, value: InferGPU<TDataType>): TgpuConst<TDataType>; <TElement extends AnyWgslData>(dataType: (elementCount: number) => WgslArray<TElement>, value: InferGPU<WgslArray<TElement>>): TgpuConst<WgslArray<TElement>>; }; fn: { <Args extends BaseData[] | []>(argTypes: Args, returnType?: undefined): TgpuFnShell<Args, Void>; <Args extends BaseData[] | [], Return extends BaseData>(argTypes: Args, returnType: Return): TgpuFnShell<Args, Return>; <T extends AnyFn>(inner: T): TgpuGenericFn<T>; }; ... 17 more ...; '~unstable': typeof import("/home/runner/work/runntime/runntime/node_modules/.pnpm/typegpu@0.12.5/node_modules/typegpu/tgpuUnstable");}
tgpu from 'typegpu';
// 1. Connect the engine to a WebGPU device, once per page.function initRunntime(root: TgpuRoot): RunntimeExecutor
initRunntime(await const tgpu: { const: { <TDataType extends AnyData>(dataType: TDataType, value: InferGPU<TDataType>): TgpuConst<TDataType>; <TElement extends AnyWgslData>(dataType: (elementCount: number) => WgslArray<TElement>, value: InferGPU<WgslArray<TElement>>): TgpuConst<WgslArray<TElement>>; }; fn: { <Args extends BaseData[] | []>(argTypes: Args, returnType?: undefined): TgpuFnShell<Args, Void>; <Args extends BaseData[] | [], Return extends BaseData>(argTypes: Args, returnType: Return): TgpuFnShell<Args, Return>; <T extends AnyFn>(inner: T): TgpuGenericFn<T>; }; ... 17 more ...; '~unstable': typeof import("/home/runner/work/runntime/runntime/node_modules/.pnpm/typegpu@0.12.5/node_modules/typegpu/tgpuUnstable");}
tgpu.init: (options?: InitOptions) => Promise<TgpuRoot>
init({ device?: (GPUDeviceDescriptor & { optionalFeatures?: Iterable<GPUFeatureName>;}) | undefined
device: { optionalFeatures?: Iterable<GPUFeatureName> | undefined
optionalFeatures: ['subgroups', 'shader-f16'] } }));
// 2. Upload inputs as [rows, columns] matrices.const const x: Value
x = function tensor(data: Float32Array, shape: readonly [number, number] | ValueMeta): Value (+1 overload)
tensor(new var Float32Array: Float32ArrayConstructornew (elements: Iterable<number>) => Float32Array<ArrayBuffer> (+6 overloads)
Float32Array([1, 2, 3, 4]), [2, 2]);const const w: Value
w = function tensor(data: Float32Array, shape: readonly [number, number] | ValueMeta): Value (+1 overload)
tensor(new var Float32Array: Float32ArrayConstructornew (elements: Iterable<number>) => Float32Array<ArrayBuffer> (+6 overloads)
Float32Array([1, 0, 0, 1]), [2, 2]);
// 3. Build the graph, then run it and read the result.const const y: Value
y = function add(a: Value, b: Value | number): Value
add(function matmul(a: Value, b: Value, opts?: MatmulOpts): Value
matmul(const x: Value
x, const w: Value
w), 1);var console: Console
console.Console.log(...data: any[]): void
log(await function toArray(value: Value, ex?: Executor & Readback): Promise<Float32Array>
toArray(const y: Value
y));The zoo and core share one engine, so initRunntime() from either entry
point sets up both. The source of every zoo model, in
src/zoo,
shows how to compose ops into a model and map a checkpoint onto it.
To load your own fine-tune or a quantized checkpoint, see Exporting weights.