Skip to content

WasmGPUInterop

Summary

WasmGPUInterop is the Python helper for moving NumPy arrays into and out of a WasmGPU runtime in Pyodide or JupyterLite. Load python/interop.py into the Python environment and pass it the JavaScript WasmGPU instance.

Syntax

interop = WasmGPUInterop(wgpu)
gpu = interop.to_gpu(values)
snapshot = await interop.from_gpu(gpu)

Available APIs

Method Behavior
to_cpu(x, *, dtype=None, order="K", copy="if_needed", casting="safe") Import into an owning CPU ndarray.
to_gpu(x, *, dtype=None, order="K", copy="if_needed", casting="safe", copy_src=True, copy_dst=True, usage=None, label=None) Import into an owning GPU ndarray. The GPU options set storage copy flags, add usage flags and label the allocation.
copy_into(dst, x, *, copy="if_needed", casting="safe") Broadcast a NumPy-compatible source into an existing CPU or GPU ndarray and cast to the destination dtype under the requested policy.
from_cpu(src, *, dtype=None, order="K", out=None, copy="if_needed", casting="safe") Copy into NumPy synchronously.
from_gpu(src, *, dtype=None, order="K", out=None, copy="if_needed", casting="safe") Start a GPU snapshot and return an awaitable for the NumPy result.
view(x, index) Select an ndarray view with Python basic indexing. Use integers, slices, a tuple, ... or None. The view shares the root's storage.

Returns

Imported arrays and views are JavaScript ndarray objects. Call destroy() on each when finished and keep roots live while using their views. Exported NumPy arrays hold copied bytes. When supplied, out must be writable and have the same shape as the source. Use the default order and omit dtype when passing out. Conversion or writing a non-contiguous out can require a temporary.

Type Details

Order = Literal["K", "C", "F", "A", "preserve"]
CopyPolicy = Literal["never", "if_needed", "always"]
CastingPolicy = Literal["no", "equiv", "safe", "same_kind", "unsafe"]

"K" packs in source memory order as closely as possible. Use "C" for row-major, "F" for column-major, and "A" to select C or Fortran order from the input. "preserve" copies the byte extent exactly, including gaps, and retains signed or zero strides and the offset within the extent. It requires the source dtype and a representable layout.

"never" allows direct transfers and rejects additional layout materialization, conversion or staging. "if_needed" and "always" allow required materialization. NumPy's casting policy controls dtype conversion. Imports must use one of the engine's supported dtypes after conversion. For complex-to-real workflows, select x.real or x.imag before converting.

Example

import numpy as np
from interop import WasmGPUInterop

interop = WasmGPUInterop(wgpu)
values = np.arange(6, dtype=np.float32).reshape(2, 3)
gpu = interop.to_gpu(values[:, ::-1])
first = interop.view(gpu, (0, slice(None)))
try:
    print(await interop.from_gpu(first))  # [2. 1. 0.]
finally:
    first.destroy()
    gpu.destroy()

Notes

from_gpu captures the data when called. Submit GPU writes before starting it. The source can be modified or destroyed after the call returns. Python validation errors occur at call time and GPU transfer errors surface when awaited. The helper reports transfer and indexing failures as WasmGPUInteropError.

See Also