Skip to content

compute.kernels.sub

Summary

compute.kernels.sub subtracts the second GPU ndarray from the first at their broadcast shape. Subtract a shared reference row from a batch to form residuals. Both arrays must use the same dtype, and negative u32 differences wrap modulo 2 ** 32.

Syntax

sub(a: GPUndarray, b: GPUndarray, opts?: ElementwiseOptions): GPUndarray
const difference = kernels.sub(a, b);

Parameters

Name Type Required Description
a, b GPUndarray Yes Strided or packed arrays of the same f32, u32 or c64 dtype. Shape compatibility follows add.
opts ElementwiseOptions No Kernel output and dispatch settings.

Returns

GPUndarray - Difference with the broadcast shape and input dtype. Underflow for u32 wraps modulo 2 ** 32. A matching C-contiguous input can be used as out.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const { ndarray: nd, kernels } = wgpu.compute;
const a = nd.fromArray("f32", [2], [8, 10], { residency: "gpu" });
const baseline = nd.fromArray("f32", [], [3], { residency: "gpu" });
kernels.sub(a, baseline, { out: a });
const cpu = await nd.toCPU(a);
console.log(cpu.data()); // [5, 7]
cpu.destroy();
baseline.destroy();
a.destroy();

See Also