Skip to content

WasmGPU.python

Summary

WasmGPU.python provides ndarray-oriented data transfer utilities for Python-JS interop workflows. It supports sending arrays into Wasm memory, receiving typed views/copies back, in-place updates, and freeing heap-owned transfers. This API is designed to pair with Pyodide or any bridge that exposes Python buffers.

Syntax

WasmGPU.python: PythonInterop
const py = wgpu.python;

Parameters

This accessor does not take parameters.

Returns

PythonInterop - Object with send/view/bytes/copy/receive/free methods.

Type Details

type WasmNdarrayHandle = {
    kind: "heap" | "frame" | "arena";
    dtype: "i8" | "u8" | "i16" | "u16" | "i32" | "u32" | "f32" | "f64";
    shape: number[];
    ptr: number;
    length: number;
    byteLength: number;
    epoch?: number;
};

type PythonInterop = {
    sendNdarray(src: PythonArraySource, options?: SendNdarrayOptions): WasmNdarrayHandle;
    view(handle: WasmNdarrayHandle): NumberTypedArray;
    bytes(handle: WasmNdarrayHandle): Uint8Array;
    copyInto(handle: WasmNdarrayHandle, src: PythonArraySource, options?: { dtype?: DType }): void;
    receiveNdarray(handle: WasmNdarrayHandle, options?: { copy?: boolean }): { dtype: DType; shape: number[]; data: NumberTypedArray };
    free(handle: WasmNdarrayHandle): void;
};

Example

const canvas = document.querySelector("canvas");
const wgpu = await WasmGPU.create(canvas);

const source = new Float32Array([1, 2, 3, 4, 5, 6]);
const handle = wgpu.python.sendNdarray(source, { dtype: "f32", shape: [2, 3], allocator: "heap" });
const view = wgpu.python.view(handle);
view[0] = 99;

const transfer = wgpu.python.receiveNdarray(handle, { copy: true });
console.log(transfer.dtype, transfer.shape, transfer.data[0]);
wgpu.python.free(handle);

See Also