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 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, int64
I int32

Implementation variants

One implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.

  • reduction_f16_ordered_global — Builds bounded destination chains, locally sorts short chains by original update position, and scans long chains in original order. Consecutive column batches reuse the same workspace; output half pairs have a single writer.
  • reduction_f16_ordered_chunks — Stages a slice of destinations and rounds to half after each update in input order. Without subgroups, smaller slices keep the direct destination table within workgroup storage. Subgroup devices can use a bounded hash table for larger slices.
  • reduction_f16_ordered_list — Builds destination-owned update chains, sorts their original positions, and applies each additive update with float16 rounding. Uses no global accumulator.
  • reduction_f32_add_axis0_histogram — Fold the updates axis into workgroup bins per 32-column tile, then add each touched bin into the copied output with one device atomic. Striping the updates axis widens the grid a column-only dispatch caps at ceil(columns / 32) workgroups.
  • reduction_f16_atomic_f32 — Reduces half minima or maxima through float32 atomic storage and converts the result back to half. Additive updates use ordered per-update half rounding.
  • reduction_f16_slab — Stages half slabs as float32 bits and combines extrema or products in workgroup memory on compatible fixed-width subgroups. Additive updates use ordered per-update half rounding.
  • reduction_owned_row — Owns one complete aligned short half row, applies additive updates in original order with per-update rounding, and writes the packed row once.

Device requirements

Some implementation variants require subgroups. Other variants run without it; which variant runs also depends on the request's shapes and dtypes.

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

const dataData = Uint32Array.of(1, 2, 3);
const indicesData = Int32Array.of(2, 0);
const updatesData = Uint32Array.of(9, 8);

const { output } = await kernel({
  data: { data: dataData, shape: [3] },
  indices: { data: indicesData, shape: [2] },
  updates: { data: updatesData, shape: [2] },
});
console.log({ output });
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.