Skip to content
Open
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
18 changes: 15 additions & 3 deletions python/paddle/tensor/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8313,16 +8313,28 @@ def diagonal_scatter(
return fill_diagonal_tensor(x, y, offset, axis1, axis2, name)


@ParamAliasDecorator(
{
"x": ["input"],
"values": ["src"],
"axis": ["dim"],
}
)
def select_scatter(
x: Tensor, values: Tensor, axis: int, index: int, name: str | None = None
) -> Tensor:
"""
Embeds the values of the values tensor into x at the given index of axis.

.. note::
Alias Support: The parameter name ``input`` can be used as an alias for ``x``.
Alias Support: The parameter name ``src`` can be used as an alias for ``values``.
Alias Support: The parameter name ``dim`` can be used as an alias for ``axis``.

Args:
x (Tensor) : The Destination Tensor. Supported data types are `bool`, `float16`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `bfloat16`, `complex64`, `complex128`.
values (Tensor) : The tensor to embed into x. Supported data types are `bool`, `float16`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `bfloat16`, `complex64`, `complex128`.
axis (int) : the dimension to insert the slice into.
x (Tensor) : The Destination Tensor. Supported data types are `bool`, `float16`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `bfloat16`, `complex64`, `complex128`. Alias: ``input``.
values (Tensor) : The tensor to embed into x. Supported data types are `bool`, `float16`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `bfloat16`, `complex64`, `complex128`. Alias: ``src``.
axis (int) : the dimension to insert the slice into. Alias: ``dim``.
index (int) : the index to select with.
name (str|None, optional): Name for the operation (optional, default is None).

Expand Down
38 changes: 38 additions & 0 deletions test/legacy_test/test_select_scatter_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,36 @@ def get_out_ref(self, out_ref, index, value_np):
out_ref[i, j, index, k, w] = value_np[i, j, k, w]


class TestSelectScatterAliasAPI(unittest.TestCase):
def setUp(self):
self.place = get_places()

def test_api_alias_dygraph(self):
def run(place):
paddle.disable_static(place)
try:
x_tensor = paddle.zeros([2, 3], dtype='float32')
value_tensor = paddle.to_tensor([1.0, 2.0], dtype='float32')
out1 = paddle.select_scatter(
x=x_tensor, values=value_tensor, axis=1, index=0
)
out2 = paddle.select_scatter(
input=x_tensor, src=value_tensor, dim=1, index=0
)
expected = np.array(
[[1.0, 0.0, 0.0], [2.0, 0.0, 0.0]], dtype='float32'
)
np.testing.assert_allclose(
out1.numpy(), out2.numpy(), rtol=0.001
)
np.testing.assert_allclose(out2.numpy(), expected, rtol=0.001)
finally:
paddle.enable_static()

for place in self.place:
run(place)


class TestSelectScatterAPIError(unittest.TestCase):
def setUp(self):
np.random.seed(0)
Expand All @@ -151,6 +181,14 @@ def test_one_of_size_not_equal_error(self):
value_tensor = paddle.to_tensor([[2, 2], [2, 2]]).astype(np.float32)
res = paddle.select_scatter(x_tensor, value_tensor, 1, 1)

def test_axis_and_dim_conflict_error(self):
with self.assertRaises(ValueError):
x_tensor = paddle.to_tensor(self.x_np)
value_tensor = paddle.to_tensor(self.value_np)
res = paddle.select_scatter(
x=x_tensor, values=value_tensor, axis=1, dim=1, index=1
)


if __name__ == "__main__":
paddle.enable_static()
Expand Down
Loading