fn
scatter_add
→Tensorscatter_add(input: Tensor, dim: int, index: Tensor, src: Tensor)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
inputTensorDestination tensor.
dimintScatter axis.
indexTensorInteger index tensor; same shape as
src.srcTensorValues to accumulate.
Returns
Tensorinput + 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.])