ai.onnx.Constant

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

Description

Produces a numeric constant from ONNX's tensor-valued value attribute or its float scalar/list shorthands. Sparse, string, and int64-only shorthand attributes are unsupported.

See the ONNX Constant spec for the reference semantics.

Differences from the ONNX spec

  • Attribute sparse_value: Sparse tensors are not supported.
  • Attribute value_int, attribute value_ints: This shorthand produces an int64 tensor, which this package cannot store losslessly.
  • Attribute value_string, attribute value_strings: String tensors are not supported.

Outputs

Name Logical dtype Rank Shape Description Presence
output T derived derived Output tensor containing the constant value(s) specified by the attribute: the value tensor's shape and dtype, a float32 vector for value_floats or a float32 scalar for value_float. required

Attributes

Attributes and default values (overridable per request):

Attribute Default Description
value — Tensor-valued constant { dtype, shape, values }: the output's dtype and shape, and its elements in row-major order.
value_float — A single float32 value producing a scalar tensor.
value_floats — Float32 values producing a one-dimensional tensor.

Type constraints

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

Files

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.Constant", { version: 1 });

const { output } = await kernel({}, {
  attrs: { value_float: -2.5 },
});
console.log({ output });
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.