fn

scatter_add

Tensor
scatter_add(input: Tensor, dim: int, index: Tensor, src: Tensor)
source

Accumulate src into input along dim at positions index.

Differs from scatter in that collisions are summed rather than overwritten — useful for histogramming and sparse gradient accumulation.

Parameters

inputTensor
Destination tensor.
dimint
Scatter axis.
indexTensor
Integer index tensor; same shape as src.
srcTensor
Values to accumulate.

Returns

Tensor

input + scatter(src) along the indexed dim.

Notes

The accumulation is non-deterministic in floating point when several threads target the same destination on accelerators; sum the same input twice and you may get bitwise-different results.

Examples

>>> import lucid
>>> base = lucid.zeros(5)
>>> idx = lucid.tensor([0, 1, 1, 2, 2])
>>> src = lucid.tensor([1.0, 2.0, 3.0, 4.0, 5.0])
>>> lucid.scatter_add(base, dim=0, index=idx, src=src)
Tensor([1., 5., 9., 0., 0.])