ai.onnx.Unique

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

Description

Finds unique values or subtensors along an optional axis. Without an axis, X is flattened; results are sorted or retain first-occurrence order. Sub-32-bit integers and booleans use lossless widened 32-bit storage. Metadata outputs remain logical int64 but use lossless uint32 storage because all values are bounded by an addressable tensor extent. Callers supply exact data-dependent output shapes. ONNX-permitted uint16, 64-bit, string, and complex inputs remain unsupported because the runtime lacks matching WebGPU storage.

See the ONNX Unique spec for the reference semantics.

Inputs

Name Bind key Logical dtype Rank Shape Description Presence
X x T The N-D input tensor from which unique values or subtensors are extracted. When axis is omitted, tensors of any rank are flattened in row-major order. required

Outputs

Name Bind key Logical dtype WebGPU storage Rank Shape Description Presence
Y y T runtime-selected; narrow integers and bool use 32-bit slots derived Tensor containing all unique values or subtensors of X, sorted or in first-occurrence order. required
indices indices I uint32 1 Optional logical int64 indices of each Y value or slice's first occurrence in X; stored as bounded uint32 values by WebGPU. optional
inverse_indices inverse_indices I uint32 1 Optional logical int64 mapping from each flattened input value, or each input-axis slice, to its corresponding index in Y; stored as bounded uint32 values by WebGPU. optional
counts counts I uint32 1 Optional logical int64 occurrence count for each unique value or slice in Y; stored as bounded uint32 values by WebGPU. optional

Attributes

Attributes and default values (overridable per request):

Attribute Default Description
sorted 1 Whether to sort unique elements in ascending order before output; 1 (default) sorts, 0 retains first-occurrence order.
axis Optional axis along which unique subtensors are identified. Negative values count from the back; when omitted, the input is flattened.

Type constraints

Variable Allowed dtypes
T float32, float16, uint32, int32, int16, uint8, int8, bool
I 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:

  • y

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.Unique", { version: 1 });
// Explicit destinations request optional results or supply metadata that cannot be inferred.
const { y } = await kernel({ x: { data: xData, shape: [1] } }, {
  outputs: { y: { shape: [1], dtype: "float32" } },
});
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.