compute.kernels.matmul¶
Summary¶
compute.kernels.matmul contracts the shared axis of two GPU matrices or vectors. Use alpha to scale the product and supply an existing out with nonzero beta to add a scaled version of that output. Two vector operands produce a scalar.
Syntax¶
matmul(a: GPUndarray, b: GPUndarray, opts?: MatmulOptions): GPUndarray
const product = kernels.matmul(a, b);
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
a, b |
GPUndarray |
Yes | Each input has rank one or two and the same f32, u32 or c64 dtype. The last dimension of a must match the first of b. Strided inputs are accepted. |
opts |
MatmulOptions |
No | Scalar coefficients and kernel output and dispatch settings. |
Returns¶
GPUndarray - Product in the input dtype. Shapes [m, k] and [k, n] yield [m, n]. A left vector is treated as one row and a right vector as one column, with the corresponding length-one axes removed from the result. Two vectors yield a scalar.
Type Details¶
The operation computes alpha * (a @ b) + beta * out. alpha defaults to one and beta to zero. A nonzero beta requires out. Scalars follow scl dtype rules. For a zero shared dimension, the product term is zero and any supplied accumulation term still applies. Unsigned arithmetic wraps modulo 2 ** 32.
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const { ndarray: nd, kernels } = wgpu.compute;
const a = nd.fromArray("f32", [2, 2], [1, 2, 3, 4], { residency: "gpu" });
const b = nd.fromArray("f32", [2], [1, 1], { residency: "gpu" });
const result = kernels.matmul(a, b);
const cpu = await nd.toCPU(result);
console.log(cpu.data()); // [3, 7]
cpu.destroy();
result.destroy();
b.destroy();
a.destroy();