Skip to content

compute.kernels.scaleExtract

Summary

compute.kernels.scaleExtract turns rows of GPU components into one scalar each, using a selected component or the row magnitude. It also returns u32 validity flags, which can feed compact to keep finite values for statistics.

Syntax

scaleExtract(src: GPUndarray, opts?: ScaleExtractOptions): ScaleExtractResult
const { values, flags } = kernels.scaleExtract(src, { valueMode: "magnitude" });

Parameters

Name Type Required Description
src GPUndarray Yes f32 array shaped [count] or [count, components] with one to four components. Strided sources are accepted.
opts ScaleExtractOptions No Selection mode, optional outputs and dispatch settings.

Returns

ScaleExtractResult - Two arrays of shape [count]. values stores the selected f32 scalars. flags stores u32 ones where the selected value is finite. Non-finite selections yield zero in both outputs.

Type Details

type ScaleExtractOptions = KernelDispatchOptions & NdarrayGPUOptions & {
    componentIndex?: number;
    valueMode?: "component" | "magnitude";
    outValues?: GPUndarray;
    outFlags?: GPUndarray;
};
type ScaleExtractResult = { values: GPUndarray; flags: GPUndarray };

valueMode defaults to "component". The component index defaults to zero and must be an integer from zero to three. Missing components read as zero. "magnitude" uses the Euclidean norm of the row. Supplied output ranges must be disjoint. Omitted outputs are allocated.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const { ndarray: nd, kernels } = wgpu.compute;
const src = nd.fromArray("f32", [2, 2], [3, 4, 1, Infinity], { residency: "gpu" });
const result = kernels.scaleExtract(src, { valueMode: "magnitude" });
const cpu = await nd.toCPU(result.values);
console.log(cpu.data()); // [5, 0]
for (const array of [cpu, result.values, result.flags, src]) array.destroy();

See Also