Skip to content

TypeScript API Reference

The WAVE TypeScript SDK provides GPU compute bindings for Node.js and Deno. It exposes array creation, device detection, kernel compilation, and async dispatch.

Terminal window
npm install wave-gpu
type DType = "f16" | "f32" | "f64" | "i32" | "u32";
type GpuVendor = "AMD" | "NVIDIA" | "Intel" | "Unknown";
type Language = "python" | "rust" | "cpp" | "typescript";
interface DeviceInfo {
vendor: GpuVendor;
name: string;
}
interface WaveArray {
readonly length: number;
readonly data: ArrayBuffer;
readonly dtype: DType;
toArray(): number[];
toBuffer(): Buffer;
}
interface CompiledKernel {
launch(options: {
device?: DeviceInfo;
buffers: WaveArray[];
scalars?: number[];
grid: [number, number, number];
workgroup: [number, number, number];
}): Promise<void>;
}

function array(data: number[], dtype?: DType): WaveArray;

Create a device array from a JavaScript array.

Parameters:

ParameterTypeDefaultDescription
datanumber[](required)Source data to upload.
dtypeDType"f32"Element type.

Returns: WaveArray

import { array } from "wave-gpu";
const a = array([1.0, 2.0, 3.0, 4.0]);
const b = array([1, 2, 3], "u32");

function zeros(n: number, dtype?: DType): WaveArray;

Create a device array of n zeros.

Parameters:

ParameterTypeDefaultDescription
nnumber(required)Number of elements.
dtypeDType"f32"Element type.

Returns: WaveArray

const buf = zeros(1024);
const ints = zeros(256, "i32");

function ones(n: number, dtype?: DType): WaveArray;

Create a device array of n ones.

Parameters:

ParameterTypeDefaultDescription
nnumber(required)Number of elements.
dtypeDType"f32"Element type.

Returns: WaveArray


function device(): DeviceInfo;

Detect the first available GPU and return its information.

Returns: DeviceInfo

Throws: Error if no supported GPU is found.

import { device } from "wave-gpu";
const dev = device();
console.log(`${dev.vendor}: ${dev.name}`);

function kernel(source: string, lang?: Language): CompiledKernel;

Compile a kernel from source code.

Parameters:

ParameterTypeDefaultDescription
sourcestring(required)Kernel source code.
langLanguage"typescript"Source language.

Returns: CompiledKernel

Throws: Error if compilation fails.

import { kernel } from "wave-gpu";
const add = kernel(`
export function add(a: f32[], b: f32[], out: f32[]): void {
const tid = threadId();
out[tid] = a[tid] + b[tid];
}
`);

Copy the device buffer to the host and return as a JavaScript array.

const a = array([1.0, 2.0, 3.0]);
console.log(a.toArray()); // [1, 2, 3]

Copy the device buffer to the host and return as a Node.js Buffer of raw bytes.

const a = array([1.0, 2.0], "f32");
const buf = a.toBuffer(); // 8 bytes (2 x 4-byte f32)

The number of elements in the buffer.

const a = zeros(512);
console.log(a.length); // 512

The raw ArrayBuffer backing the device data. Accessing this triggers a device-to-host copy.

The element type string (e.g., "f32", "u32").


Dispatch a compiled kernel on the GPU. Returns a Promise that resolves when execution completes.

await add.launch({
buffers: [a, b, out],
grid: [4, 1, 1],
workgroup: [4, 1, 1],
});

Options:

FieldTypeRequiredDescription
deviceDeviceInfoNoTarget device. If omitted, uses the default detected device.
buffersWaveArray[]YesDevice buffers bound as kernel arguments, in declaration order.
scalarsnumber[]NoScalar values passed as 32-bit constants.
grid[number, number, number]YesGlobal grid dimensions [x, y, z].
workgroup[number, number, number]YesWorkgroup dimensions [x, y, z].

import { array, zeros, device, kernel } from "wave-gpu";
const dev = device();
console.log(`Running on ${dev.name}`);
const a = array([1.0, 2.0, 3.0, 4.0]);
const b = array([5.0, 6.0, 7.0, 8.0]);
const out = zeros(4);
const add = kernel(`
export function add(a: f32[], b: f32[], out: f32[]): void {
const tid = threadId();
out[tid] = a[tid] + b[tid];
}
`);
await add.launch({
buffers: [a, b, out],
grid: [4, 1, 1],
workgroup: [4, 1, 1],
});
console.log(out.toArray()); // [6, 8, 10, 12]