Skip to content

compute.ndarray#shape

Summary

compute.ndarray#shape lists the array's logical dimensions. Use stridesBytes and offsetBytes to locate the elements within its backing range. Treat the metadata arrays as read-only and use layout() when you need copies.

Syntax

readonly shape: number[]
readonly stridesBytes: number[]
readonly offsetBytes: number
readonly bytesPerElement: number
readonly numel: number
readonly byteLength: number
const { shape, stridesBytes, offsetBytes, bytesPerElement, numel, byteLength } = a;

Returns

Member Meaning
shape Axis lengths. [] is a scalar. Any zero-length axis makes the array empty.
stridesBytes Signed byte increments for advancing one element along each axis. A zero stride repeats elements.
offsetBytes Byte offset of the first logical element within this array's backing range.
bytesPerElement Size of one logical element in bytes, including both components for complex data.
numel Product of the axis lengths, with one element for a scalar.
byteLength Size of the backing range, including gaps and any leading bytes within that range. Empty arrays have a zero-byte range.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.zeros("f32", { shape: [2, 3] });
const b = nd.flip(a, 1);
console.log(b.shape, b.stridesBytes, b.offsetBytes); // [2, 3], [12, -4], 8
b.destroy();
a.destroy();

Notes

The logical byte address relative to the range is the offset plus the sum of each index times its stride. A view may move the start of the backing range and adjust the offset to match. Broadcasting can produce more logical bytes than the size of the backing range.

See Also