Skip to content

deepbox/core

Devices & Execution

Where tensors live, which backends exist, and what runs on the GPU. The CPU is the default. WebGPU and WASM are opt-in.

How devices work

Every tensor carries a device: cpu, webgpu or wasm. The CPU backend is always registered. The other two are opt-in: create the backend, call await backend.init(), check info().available, then pass it to registerBackend.

Operands of one operation must be on the same device, except that a 0-D host tensor is moved to the device of the other operand. An operation that a device does not implement throws a DeviceError that tells you to move the tensor with await t.cpu(). Deepbox never copies webgpu data back to the CPU on its own.

devices.ts
import { getBackend, listBackends, setConfig } from "deepbox/core"; setConfig({ defaultDevice: "cpu", defaultDtype: "float32" });console.log("Registered backends:", listBackends());console.log("CPU backend:", getBackend("cpu"));

WebGPU

The WebGPU backend runs these operations as WGSL compute kernels, on views, transposes and broadcasts without copying:

  • Element-wise binary operations (add, sub, mul, div, pow, maximum, minimum), the common unary operations and activations (exp, log, sqrt, relu, sigmoid, tanh, gelu, softplus and others), and where.
  • matmul and dot, including batched matmul with batch broadcasting.
  • sum, mean, max and min, over the whole tensor or along axes.
  • 2-D convolution (through im2col and col2im) and 2-D max and average pooling.

Supported element types are float32, float16 and bfloat16. float16 needs the WebGPU shader-f16 feature. bfloat16 is rounded to bfloat16 on upload and download and computed in float32. Reductions of empty tensors throw on device.

Autograd records through these operations, and the optimizers keep their state on the device when the parameters live there. LBFGS and SparseAdam are the exceptions: they throw a DeviceError for device parameters. Anything outside the list above needs await t.cpu() first.

Limits on a device

  • Only float32, float16 and bfloat16 tensors can be moved to webgpu. Convert other dtypes with astype("float32") first, otherwise to() throws DTypeError.
  • Reading .data, toArray() or item() on a device tensor throws DeviceError. Read the result back with await t.cpu() first.
  • Operands on different devices throw DeviceError, except a 0-D host tensor, which is uploaded for you.
  • Free large device tensors with t.dispose(). A disposed tensor throws DeviceError when you use it.
webgpu.ts
import { registerBackend, WebGpuBackend } from "deepbox/core";import { dot, relu, tensor } from "deepbox/ndarray"; // In a browser, navigator.gpu is used. In Node, pass a WebGPU binding: new WebGpuBackend({ gpu }).const gpu = new WebGpuBackend();await gpu.init();if (gpu.info().available) {  registerBackend("webgpu", gpu);   const a = tensor(    [      [1, 2],      [3, 4],    ],    { device: "webgpu" }  );  const y = relu(dot(a, a)); // runs as WGSL compute kernels  console.log((await y.cpu()).toString()); // read back to host memory} else {  console.log("WebGPU is not available in this runtime");}

WASM

The WASM backend is a host accelerator. Tensors on the wasm device keep ordinary host memory. Element-wise add, sub, mul and div run through embedded SIMD kernels when both operands are contiguous float32 tensors of the same shape with at least 512 elements. Every other operation runs the normal CPU code, and results are identical to the CPU. If the runtime lacks WASM SIMD, info().available is false.

wasm.ts
import { registerBackend, WasmBackend } from "deepbox/core";import { add, tensor } from "deepbox/ndarray"; const wasm = new WasmBackend();await wasm.init();if (wasm.info().available) {  registerBackend("wasm", wasm);   const a = tensor(new Array(4096).fill(1), { dtype: "float32", device: "wasm" });  const b = add(a, a); // SIMD kernel  console.log(b.device, b.at(0)); // wasm 2} else {  console.log("WASM SIMD is not available in this runtime");}

Training on a device

Module.to(device) returns a promise, so write await model.to("webgpu"). It moves float32, float16 and bfloat16 parameters and buffers once a backend is registered, and it throws DeviceError when none is. Layers with host-only kernels (Embedding, Conv3d, the transposed convolutions, the recurrent layers, PReLU and SpectralNorm) keep their weights on the CPU and keep working.

Autograd and the optimizer step then run on the device for the operations listed above. Trainer and clipGradNorm_ read values on the host, so use them with CPU tensors. For GPU training, write the loop yourself and read the loss with await loss.cpu(). Without a registered backend everything runs on the CPU.