ai.onnx.Reshape

ai.onnx · standard ONNX operator · ONNX opset ≥ 14

Description

Reshapes the input tensor to a new shape while preserving the total number of elements. In this preallocated kernel ABI, the caller must resolve and validate the target shape—including -1, zero dimensions, and allowzero—before allocating reshaped; the kernel copies the elements and requires the input and output element counts to match.

See the ONNX Reshape spec for the reference semantics.

Inputs

Name Bind key Logical dtype WebGPU storage Rank Shape Description Presence
data data T runtime-selected; narrow integers and bool use 32-bit slots The input tensor to reshape. required
shape shape S int32 1 Logical int64 1-D ONNX target-shape tensor, stored as int32 by WebGPU so -1 remains representable. This standalone kernel does not inspect its values; the caller must validate them and allocate reshaped with the resolved shape. required

Outputs

Name Bind key Logical dtype Rank Shape Description Presence
reshaped reshaped T derived The reshaped output tensor with the caller-resolved target shape and the same elements and data type as the input. required

Attributes

Default values (overridable per request):

Attribute Default Description
allowzero 0 Controls caller-side target-shape resolution: 0 copies the corresponding input dimension for a zero entry, while 1 preserves a literal zero dimension. The standalone kernel does not interpret this attribute or the shape tensor.

Type constraints

Variable Allowed dtypes
T float32, float16, int32, int16, uint32, uint8, int8, bool
S int64

Files

Use with @huggingface/kernels

The loader automatically allocates outputs whose metadata it can derive from the manifest contract and this call.

The explicit outputs entries provide shape and logical dtype metadata for the results listed below:

  • reshaped

Each entry either requests an optional result or supplies metadata that cannot be inferred from the inputs.

The version: 1 option selects the published kernel contract; it is independent of any operator opset, contrib since_version, or model version.

Replace each *Data placeholder with a typed array containing the corresponding input data.

import { getKernel } from "@huggingface/kernels";

const kernel = await getKernel("webgpu-kernels/ai.onnx.Reshape", { version: 1 });
// Explicit destinations request optional results or supply metadata that cannot be inferred.
const { reshaped } = await kernel({
  data: { data: dataData, shape: [2] },
  shape: { data: shapeData, shape: [2] },
}, {
  outputs: { reshaped: { shape: [1, 2], dtype: "int8" } },
});
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.