Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@
row_stack,
scatter,
scatter_,
scatter_add,
scatter_nd,
scatter_nd_add,
select_scatter,
Expand Down Expand Up @@ -1180,6 +1181,7 @@
'renorm_',
'take_along_axis',
'put_along_axis',
'scatter_add',
'select_scatter',
'multigammaln',
'multigammaln_',
Expand Down
2 changes: 2 additions & 0 deletions python/paddle/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
row_stack,
scatter,
scatter_,
scatter_add,
scatter_nd,
scatter_nd_add,
select_scatter,
Expand Down Expand Up @@ -801,6 +802,7 @@
'repeat_interleave',
'take_along_axis',
'put_along_axis',
'scatter_add',
'select_scatter',
'put_along_axis_',
'bernoulli_',
Expand Down
41 changes: 41 additions & 0 deletions python/paddle/tensor/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6692,6 +6692,47 @@ def infer_broadcast_shape(
return broadcast_shape


def scatter_add(
input: Tensor,
dim: int,
index: Tensor,
src: Tensor,
) -> Tensor:
"""
Scatter the values of the source tensor to the target tensor according to the given indices, and perform a add operation along the designated axis.

Args:
input (Tensor) : The Input Tensor. Supported data types are bfloat16, float16, float32, float64,
int32, int64, uint8.
dim (int) : The axis to scatter 1d slices along.
index (Tensor) : Indices to scatter along each 1d slice of input. This must match the dimension of input,
Supported data type are int32 and int64.
src (Tensor) : The value element(s) to scatter. The data types should be same as input.

Returns:
Tensor, The indexed element, same dtype with input

Examples:
.. code-block:: python

>>> import paddle

>>> x = paddle.to_tensor([[10, 20, 30], [40, 50, 60]])
>>> indices = paddle.zeros((2,3)).astype("int32")
>>> values = paddle.to_tensor([[1, 2, 3],[4, 5, 6]]).astype(x.dtype)
>>> result = paddle.scatter_add(x, 0, indices, values)
>>> print(result)
Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[15, 27, 39],
[40, 50, 60]])

"""

return put_along_axis(
input, index, src, dim, 'add', include_self=True, broadcast=False
)


@ParamAliasDecorator({"arr": ["input"], "axis": ["dim"]})
def take_along_axis(
arr: Tensor, indices: Tensor, axis: int, broadcast: bool = True
Expand Down
Loading