ai.onnx.ScatterND

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

Description

Copies data and then scatters values from updates into the copy at positions specified by indices, producing an output with the same shape as data. Each entry in indices is a k-tuple that addresses either a single element (when k equals the rank of data) or a slice; an optional reduction attribute replaces the default overwrite with an elementwise add, mul, max, or min accumulation.

See the ONNX ScatterND spec for the reference semantics.

Inputs

Name Logical dtype WebGPU storage Rank Shape Description Presence
data T runtime-selected; narrow integers and bool use 32-bit slots — — Source tensor of rank r >= 1 that is copied to form the base of the output. required
indices I int32 — — Logical int64 index tensor of rank q >= 1 whose last dimension gives signed k-tuples that address into data; WebGPU stores it as int32. required
updates T runtime-selected; narrow integers and bool use 32-bit slots — — Values to scatter into the output, of rank q + r - k - 1, matching the leading (q-1) index dimensions and the trailing (r-k) data slice dimensions. required

Outputs

Name Logical dtype Rank Shape Description Presence
output T same as data same as data Result tensor of rank r with the same shape as data, with selected positions overwritten or reduced by updates. required

Attributes

Default values (overridable per request):

Attribute Default Description
reduction "none" Reduction to apply when writing each update value: none (overwrite, no duplicate index tuples), add, mul, max, or min.

Type constraints

Variable Allowed dtypes
T float32, float16, int32, uint32, int8, uint8, int16, bool, int64
I int64

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.

  • ordered_tile_add — Keeps point additions in input order. Tiles with one or two destinations reuse partial folds when the rounding grid permits it; other tiles group updates by destination and original position.
  • ordered_direct — Gathers matching updates in input order without temporary buffers. For long inputs, lanes cooperatively load consecutive indices and values into workgroup memory.

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

const dataData = Float32Array.of(0, 0.1, 0.1, 0.1);
const indicesData = Int32Array.of(1, 0);
const updatesData = Float32Array.of(0.2, 0.3, 0, 0.1);

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

Requires WebGPU support. See the compatibility table.