--- library_name: kernels license: apache-2.0 tags: - kernel - webgpu - wgsl --- # 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](https://onnx.ai/onnx/operators/onnx__Unique.html) 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 - [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, provenance) - [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth) - [`test.json`](build/webgpu/test.json) — correctness cases - [`bench.json`](build/webgpu/bench.json) — benchmark + tuning cases - [`unique-axis-compact-sort.wgsl.jinja`](build/webgpu/unique-axis-compact-sort.wgsl.jinja) - [`unique-axis-dedup.wgsl.jinja`](build/webgpu/unique-axis-dedup.wgsl.jinja) - [`unique-axis-hash.wgsl.jinja`](build/webgpu/unique-axis-hash.wgsl.jinja) - [`unique-axis-scatter.wgsl.jinja`](build/webgpu/unique-axis-scatter.wgsl.jinja) - [`unique-axis.wgsl.jinja`](build/webgpu/unique-axis.wgsl.jinja) - [`unique-compact-sort.wgsl.jinja`](build/webgpu/unique-compact-sort.wgsl.jinja) - [`unique-dedup.wgsl.jinja`](build/webgpu/unique-dedup.wgsl.jinja) - [`unique-hash-build.wgsl.jinja`](build/webgpu/unique-hash-build.wgsl.jinja) - [`unique-hash-collect.wgsl.jinja`](build/webgpu/unique-hash-collect.wgsl.jinja) - [`unique-hash-init.wgsl.jinja`](build/webgpu/unique-hash-init.wgsl.jinja) - [`unique-hash-mark.wgsl.jinja`](build/webgpu/unique-hash-mark.wgsl.jinja) - [`unique-hash-sort-collected-key-only.wgsl.jinja`](build/webgpu/unique-hash-sort-collected-key-only.wgsl.jinja) - [`unique.wgsl.jinja`](build/webgpu/unique.wgsl.jinja) ## 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. ```js 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" } }, }); ```