Skip to content

compute.ndarray.pad

Summary

compute.ndarray.pad adds values before or after each selected axis, for example to prepare an input for filtering near its edges. Choose a constant fill or an edge, reflected or wrapped extension. The result has packed storage in the input dtype and residency.

Syntax

pad<T extends CPUndarray | GPUndarray>(a: T, options: NdarrayPadOptions): T
const border = nd.pad(a, { width: 1, mode: "edge" });

Parameters

Name Type Required Description
a CPUndarray | GPUndarray Yes Live input array, including a strided view.
options NdarrayPadOptions Yes Pad widths, boundary mode and optional GPU output settings.

Returns

T - Array with each axis enlarged by its before and after widths. Destroy it when finished unless writing to an existing GPU out.

Type Details

type NdarrayPadMode = "constant" | "edge" | "reflect" | "symmetric" | "wrap";
type NdarrayPadWidth = number | readonly [number, number] | ReadonlyArray<number | readonly [number, number]>;
type NdarrayPadOptions = NdarrayOperationOptions & {
    width: NdarrayPadWidth;
    mode?: NdarrayPadMode;
    value?: number | ComplexScalar;
};

Use a single width for both sides of every axis or a pair [before, after] for all axes. To specify separate widths for a two-axis array, use nested pairs such as [[1, 1], [2, 2]]. Widths are non-negative integers.

Mode Out-of-range values
"constant" (default) Fill with value, default 0. Integers must be exactly representable. Complex values may be given as a number or [re, im].
"edge" Repeat the nearest edge.
"reflect" Mirror without repeating the endpoint.
"symmetric" Mirror including the endpoint.
"wrap" Repeat periodically.

The non-constant modes require an input element on any axis extended from length zero. The GPU output and submission rules match compute.ndarray.contiguous.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("i32", [2], [1, 2]);
const b = nd.pad(a, { width: [1, 2], mode: "edge" });
console.log(b.data()); // [1, 1, 2, 2, 2]
b.destroy();
a.destroy();

See Also