ai.onnx.OneHot

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

Description

Produces a one-hot tensor from an indices input: positions matching each index are filled with on_value and all other positions with off_value, where both are taken from the two-element values tensor [off_value, on_value]. The output rank is one greater than indices, with the new dimension of size depth inserted at the position given by axis; indices outside [-depth, depth-1] yield all-off_value rows.

See the ONNX OneHot spec for the reference semantics.

Inputs

Name Bind key Logical dtype Rank Shape Description Presence
indices indices I Integer or float index tensor; values outside [-depth, depth-1] produce all-off_value output rows. required
depth depth D Scalar (or length-1 rank-1) tensor specifying the number of classes and the size of the one-hot dimension. required
values values T 1 Rank-1 tensor of exactly two elements [off_value, on_value] giving the values written to inactive and active positions respectively. required

Outputs

Name Bind key Logical dtype Rank Shape Description Presence
output output T derived One-hot tensor with rank equal to rank(indices) + 1, same element type as values. required

Attributes

Default values (overridable per request):

Attribute Default Description
axis -1 Axis along which the one-hot dimension is inserted; default -1 appends it as the last dimension. Negative values count from the back; accepted range is [-r-1, r] where r = rank(indices).

Type constraints

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

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:

  • output

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.OneHot", { version: 1 });
// Explicit destinations request optional results or supply metadata that cannot be inferred.
const { output } = await kernel({
  indices: { data: indicesData, shape: [1] },
  depth: { data: depthData, shape: [] },
  values: { data: valuesData, shape: [2] },
}, {
  outputs: { output: { shape: [1, 2], dtype: "float32" } },
});
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.