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 Logical dtype Rank Shape Description Presence
indices I — — Integer or float index tensor; values outside [-depth, depth-1] produce all-off_value output rows. required
depth D — — Scalar (or length-1 rank-1) tensor specifying the number of classes and the size of the one-hot dimension. required
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 Logical dtype Rank Shape Description Presence
output T derived 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

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

const indicesData = Int16Array.of(-1);
const depthData = Int16Array.of(2);
const valuesData = Float32Array.of(-0.25, 1.5);

const { output } = await kernel({
  indices: { data: indicesData, shape: [1] },
  depth: { data: depthData, shape: [] },
  values: { data: valuesData, shape: [2] },
});
console.log({ output });
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.