ai.onnx.ConstantOfShape
ai.onnx · standard ONNX operator · ONNX opset ≥ 9
Description
Generates a tensor filled with a constant value and an input-specified shape. The shape is provided as a 1-D integer tensor; if empty, the output is a scalar. The fill value and its dtype are taken from the value attribute, defaulting to 0 of type float32.
See the ONNX ConstantOfShape spec for the reference semantics.
Inputs
| Name | Logical dtype | WebGPU storage | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|---|
input |
S |
uint32 |
1 |
— | Logical int64 1-D tensor whose elements define the output shape; all values must be non-negative and use uint32 WebGPU storage. | required |
Outputs
| Name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|
output |
T |
derived | derived | Output tensor of the shape given by input, filled with the constant value. |
required |
Attributes
Attributes and default values (overridable per request):
| Attribute | Default | Description |
|---|---|---|
value |
— | Optional one-element tensor { dtype, shape, values } giving the fill value and the output dtype; omitted, the output is float32 zeros. |
Type constraints
| Variable | Allowed dtypes |
|---|---|
S |
int64 |
T |
float32, float16, int32, int16, uint32, int8, uint8, bool |
Implementation variants
One implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.
fill— Fills with vec4 stores when the output has at least one full vector; a count not divisible by four adds a one-invocation scalar tail pass. Float fills read the value from a uniform, integer and boolean fills bake it into the shader.
Files
metadata.json— kernel metadata (id, file digests, manifest spec, provenance)manifest.json— the op contract (source of truth)test.json— correctness casesbench.json— benchmark casesconstant-of-shape.wgsl.jinja
Use with @huggingface/kernels
npm install --save-exact @huggingface/kernels@0.0.1-preview.4
Paste the example into a browser app that resolves npm imports. Serve the app over HTTPS or localhost with WebGPU available.
Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
The version: 1 option selects the published kernel contract; it is independent of any operator opset, contrib since_version, or model version.
It follows the v1 branch as fixes land. To pin exact artifact bytes, pass a 40-character commit revision instead of version.
import { getKernel } from "@huggingface/kernels";
const kernel = await getKernel("webgpu-kernels/ai.onnx.ConstantOfShape", { version: 1 });
const inputData = Uint32Array.of(2, 3);
const { output } = await kernel({ input: { data: inputData, shape: [2] } });
console.log({ output });
- Downloads last month
- -
Requires WebGPU support. See the compatibility table.