Skip to content

compute.kernels.argmax

Summary

compute.kernels.argmax locates the first non-NaN maximum in logical C order. The result is a scalar u32 GPU ndarray, so another GPU operation can consume the index directly. Map it through the input's logical shape when you need coordinates.

Syntax

argmax(a: GPUndarray, opts?: KernelOutputOptions): GPUndarray
const index = kernels.argmax(a);

Parameters

Name Type Required Description
a GPUndarray Yes Non-empty f32 array of any supported shape and layout.
opts KernelOutputOptions No Kernel output and dispatch settings.

Returns

GPUndarray - One u32 at shape []. The index follows logical C order and picks the first occurrence on ties, including signed zero. All-NaN input gives 0xffffffff. Empty input throws.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const { ndarray: nd, kernels } = wgpu.compute;
const a = nd.fromArray("f32", [2, 2], [NaN, 3, 3, 1], { residency: "gpu" });
const index = kernels.argmax(a);
const cpu = await nd.toCPU(index);
console.log(cpu.get()); // 1
cpu.destroy();
index.destroy();
a.destroy();

See Also