File size: 4,266 Bytes
5d5e27b
75441f6
5d5e27b
75441f6
 
 
 
5d5e27b
75441f6
 
 
 
 
 
 
 
 
 
 
 
b7c227f
 
 
 
 
75441f6
 
 
b7c227f
 
 
75441f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7c227f
75441f6
 
 
 
 
b7c227f
75441f6
 
 
 
 
 
 
 
b7c227f
 
 
 
 
75441f6
 
b7c227f
75441f6
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
library_name: kernels
license: apache-2.0
tags:
- kernel
- webgpu
- wgsl
---
# ai.onnx.ScatterElements

`ai.onnx`  ·  standard ONNX operator  ·  ONNX opset ≥ 18

## Description

Produces a copy of `data` with values updated at positions given by `indices` along the specified `axis`. For each entry in `updates`, the axis coordinate comes from `indices` while all other coordinates come from the entry's own position in `updates`. An optional `reduction` (`add`, `mul`, `max`, `min`) combines updates with existing values instead of overwriting; with `none`, duplicate indices are not allowed.

See the [ONNX `ScatterElements` spec](https://onnx.ai/onnx/operators/onnx__ScatterElements.html) for the reference semantics.

## Inputs

| Name | Logical dtype | Rank | Shape | Description | Presence |
| --- | --- | --- | --- | --- | --- |
| `data` | `T` | — | — | Input tensor of rank r >= 1 that is copied to form the output base. | required |
| `indices` | `I` | — | — | Integer index tensor of the same rank as `data`; each value selects a position along `axis`. | required |
| `updates` | `T` | — | — | Values to scatter, same rank and shape as `indices`. | required |

## Outputs

| Name | Logical dtype | Rank | Shape | Description | Presence |
| --- | --- | --- | --- | --- | --- |
| `output` | `T` | same as `data` | same as `data` | Copy of `data` with scattered updates applied; same shape as `data`. | required |

## Attributes

Default values (overridable per request):

| Attribute | Default | Description |
| --- | --- | --- |
| `axis` | `0` | Which axis to scatter on; negative values count from the back. Accepted range is `[-r, r-1]` where `r = rank(data)`. |
| `reduction` | `"none"` | Reduction to apply when writing updates: `none` (overwrite, no duplicate indices), `add`, `mul`, `max`, or `min`. |

## Type constraints

| Variable | Allowed dtypes |
| --- | --- |
| `T` | `float32`, `float16`, `int32`, `uint32`, `int8`, `uint8`, `int16`, `bool` |
| `I` | `int32` |

## Device requirements

Some implementation variants require `subgroups`. These are route-specific capabilities, not package-wide requirements; availability also depends on the request shape and dtype.

## Files

- [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, per-variant templates, 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
- [`scatter-elements-f32-add-axis0-histogram.wgsl.jinja`](build/webgpu/scatter-elements-f32-add-axis0-histogram.wgsl.jinja)
- [`scatter-elements-reduction-atomic.wgsl.jinja`](build/webgpu/scatter-elements-reduction-atomic.wgsl.jinja)
- [`scatter-elements-reduction-slab.wgsl.jinja`](build/webgpu/scatter-elements-reduction-slab.wgsl.jinja)
- [`scatter-elements-reduction.wgsl.jinja`](build/webgpu/scatter-elements-reduction.wgsl.jinja)
- [`scatter-elements.wgsl.jinja`](build/webgpu/scatter-elements.wgsl.jinja)
- [`scatter-f16-f32-convert.wgsl.jinja`](build/webgpu/scatter-f16-f32-convert.wgsl.jinja)
- [`scatter-flat-copy.wgsl.jinja`](build/webgpu/scatter-flat-copy.wgsl.jinja)
- [`scatter-narrow-wrap.wgsl.jinja`](build/webgpu/scatter-narrow-wrap.wgsl.jinja)

## Use with `@huggingface/kernels`

```sh
npm install --save-exact @huggingface/kernels@0.0.1-preview.2
```

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`.

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.ScatterElements", { version: 1 });
const { output } = await kernel({
  data: { data: dataData, shape: [3] },
  indices: { data: indicesData, shape: [2] },
  updates: { data: updatesData, shape: [2] },
});
```