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
44 changes: 44 additions & 0 deletions swe-paddle/tasks/PaddlePaddle__Paddle-74586/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# PaddlePaddle__Paddle-74586

This directory converts Paddle PR #74586 into a SWE-Paddle community task candidate.

## Source

| Field | Value |
| --- | --- |
| Repo | `PaddlePaddle/Paddle` |
| PR | [74586](https://github.com/PaddlePaddle/Paddle/pull/74586) |
| PR title | `[API compatibility] add scatter_add api` |
| Base commit | `e5c11eb4ab20851a6ab76bd0a85c8650b20b0692` |
| Merged at | `2025-08-21` |
| Task type | `feature_enhancement` |
| Resource | CPU |

## Summary

Add the public `paddle.scatter_add` API so indexed source values are accumulated into an input tensor along a selected dimension, while preserving existing tensor-manipulation behavior.

## Why This Is A Good SWE-Paddle Candidate

- The change comes from a merged Paddle API-compatibility PR with a compact Python production scope.
- The target behavior is externally observable through indexed additive updates and public namespace exposure.
- The task has a clear fail-to-pass boundary because the API is absent at the base commit.
- Existing manipulation behavior can be protected with a stable P2P regression test.

## Files

- `proposal.md`: candidate proposal for maintainer triage.
- `instruction.md`: self-contained problem statement for the coding agent.
- `solution/code.patch`: production-only gold patch derived from the merged PR.
- `tests/test.patch`: test patch exposing the target behavior.
- `tests/test.sh`: minimal target test command.
- `environment/README.md`: environment notes for reproduction.
- `README.md`: task overview and verification entrypoint.

## Verification

```bash
bash tests/test.sh
```

Expected behavior: applying `tests/test.patch` to the base commit should fail on the new `scatter_add` behavior while the P2P case remains valid; applying both `tests/test.patch` and `solution/code.patch` should pass all target tests.
27 changes: 27 additions & 0 deletions swe-paddle/tasks/PaddlePaddle__Paddle-74586/environment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Environment Notes

This candidate is part of the SWE-Paddle community task set.

## Expected Environment

- Repository: `PaddlePaddle/Paddle`
- Base commit: `e5c11eb4ab20851a6ab76bd0a85c8650b20b0692`
- Resource: CPU
- GPU required: no
- Build path: Paddle source checkout at the base commit. This Python-only task may be verified with an AST overlay and controlled doubles; source build is not required.

## Run Order

1. Check out `PaddlePaddle/Paddle` at the base commit.
2. Apply `tests/test.patch`.
3. Run `bash tests/test.sh`; the target behavior should fail before the fix.
4. Apply `solution/code.patch`.
5. Run `bash tests/test.sh` again; the target behavior should pass after the gold patch.

## Minimal Test Command

```bash
bash tests/test.sh
```

The verifier is responsible for deriving stable F2P and P2P node IDs from repeated runs.
35 changes: 35 additions & 0 deletions swe-paddle/tasks/PaddlePaddle__Paddle-74586/instruction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 新增 `paddle.scatter_add` API

## 详细描述

Paddle 目前缺少 `scatter_add` 接口。

现在需要新增 `paddle.scatter_add(input, dim, index, src)`,以 `input` 为初始结果,按照 `index` 指定的位置,将 `src` 中的值沿 `dim` 维累加进去。

当多个索引指向同一位置时,这些值都应累加,不能相互覆盖。该接口返回新的 Tensor,不修改输入的 `input`。

同时支持以下调用方式:

```python
paddle.scatter_add(input, dim, index, src)
paddle.tensor.scatter_add(input, dim, index, src)
input.scatter_add(dim, index, src)
````

## 验收说明

* 支持 `input`、`dim`、`index` 和 `src` 参数
* 应在 `input` 原有数据的基础上累加 `src`
* 多个索引指向同一位置时,应正确累加所有对应值
* 返回结果的 shape 和 dtype 应与 `input` 保持一致
* `index` 支持 `int32` 和 `int64`
* `src` 的 dtype 应与 `input` 一致
* 调用后不应修改 `input`
* 三种调用方式应得到相同结果
* 现有 Tensor 操作接口的行为保持不变

## 技术要求

* 熟悉 Python
* 了解 Paddle Tensor API
* 了解按索引进行数据更新和累加的方式
54 changes: 54 additions & 0 deletions swe-paddle/tasks/PaddlePaddle__Paddle-74586/proposal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Task Proposal: PaddlePaddle__Paddle-74586

## 1. 来源信息

* Instance ID:`PaddlePaddle__Paddle-74586`
* PR 链接:https://github.com/PaddlePaddle/Paddle/pull/74586
* PR 标题:`[API compatibility] add scatter_add api`
* `base_commit`:`e5c11eb4ab20851a6ab76bd0a85c8650b20b0692`
* merged 时间:`2025-08-21`
* 你的身份:熟悉该模块的 contributor
* 后续联系人:TBD

## 2. 问题一句话

为 Paddle 新增公开的 `scatter_add` API,使调用方能够根据索引在指定维度上,将源 Tensor 中的值累加到输入 Tensor 的对应位置。

## 3. 为什么适合作为 SWE-Paddle 样本

* **真实性**:该任务来自已合入 Paddle `develop` 分支的 API compatibility PR,不是人工构造的需求。
* **代表性**:该任务涉及常用的 Tensor 索引累加操作、公共 API 导出,以及与其他深度学习框架的接口兼容。
* **边界清楚**:Base 中尚未提供 `scatter_add`,新增接口后的公共可见性和索引累加结果均可通过独立测试直接验证。
* **非平凡性**:任务不仅需要新增公共接口,还要正确处理指定维度、索引映射、重复索引累加,以及在输入 Tensor 原有值基础上的更新语义。

## 4. 任务类型和标签

* 任务类型:`feature_enhancement`
* 执行后端:`cpu`
* 设备范围:`cpu_only`
* 模块标签:`[api_compatibility, tensor, manipulation, scatter]`

## 5. 验证思路

* 目标测试命令:`bash tests/test.sh`
* 目标测试文件:`test/swe_paddle/test_pr74586_scatter_add.py`
* 修复前预期:现有 Tensor manipulation 接口的 P2P 测试应通过;`scatter_add` 的公共导出和索引累加行为测试应失败。
* 修复后预期:继续应用 `solution/code.patch` 后,P2P 与全部 F2P 均应通过;`scatter_add` 应能够从两个公共命名空间访问,并正确执行索引累加。
* P2P 候选:现有 `index_add` 在动态图路径下的参数传递、底层调用和返回行为保持不变。
* F2P 设计:验证 `scatter_add` 能够在输入 Tensor 原有值的基础上进行累加,并正确处理不同维度和重复索引;同时验证该接口能够从 `paddle` 和 `paddle.tensor` 公共命名空间访问。

## 6. 环境与资源

* 资源需求:CPU
* Paddle 来源:`PaddlePaddle/Paddle` source checkout at `base_commit`
* 是否能提供 Docker:暂无
* patch 类型:Python-only
* 最小测试命令:`bash tests/test.sh`
* 是否有 oracle 日志:由 SWE-Paddle verifier 结果另行维护

## 7. 风险自查

* 泄露风险:`instruction.md` 只描述 `scatter_add` 的公共接口、索引累加语义和兼容性要求,不透露 Gold patch 使用的内部函数、参数组合或具体修改位置。
* 环境风险:通过 AST overlay 隔离 source checkout 与当前运行环境之间的版本差异,避免依赖历史 Paddle package 的完整导入和 native extension。
* flaky 风险:测试使用固定的 NumPy 输入、索引和确定性的 Tensor 操作替身,不依赖随机数、GPU、并发或异步时序。
* 拆分风险:`scatter_add` 的函数实现和两个公共命名空间导出共同构成一项完整的 API compatibility 能力,适合作为单个任务。
92 changes: 92 additions & 0 deletions swe-paddle/tasks/PaddlePaddle__Paddle-74586/solution/code.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py
index 3ebe1ebb0fdddcba22c6435fc85ea93ad2d3cc21..ef49fc73690d71db7a13bb333f9bbb1f2d4b83bf 100644
--- a/python/paddle/__init__.py
+++ b/python/paddle/__init__.py
@@ -368,6 +368,7 @@ from .tensor.manipulation import (
row_stack,
scatter,
scatter_,
+ scatter_add,
scatter_nd,
scatter_nd_add,
scatter_reduce,
@@ -1262,6 +1263,7 @@ __all__ = [
'take_along_axis',
'scatter_reduce',
'put_along_axis',
+ 'scatter_add',
'select_scatter',
'multigammaln',
'multigammaln_',
diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py
index 61d2d9913846cad681072cca2a38ff770f8bf0b8..6b7f497615d8045126243e80f8130565ad2093b6 100644
--- a/python/paddle/tensor/__init__.py
+++ b/python/paddle/tensor/__init__.py
@@ -205,6 +205,7 @@ from .manipulation import ( # noqa: F401
row_stack,
scatter,
scatter_,
+ scatter_add,
scatter_nd,
scatter_nd_add,
scatter_reduce,
@@ -822,6 +823,7 @@ tensor_method_func = [
'take_along_axis',
'scatter_reduce',
'put_along_axis',
+ 'scatter_add',
'select_scatter',
'put_along_axis_',
'bernoulli_',
diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py
index 9497ab0f7568fc2a151f344ddb1e8dbf9c97942e..403f48d17c23343f4b2ddf95d3e543b7db48d8f3 100644
--- a/python/paddle/tensor/manipulation.py
+++ b/python/paddle/tensor/manipulation.py
@@ -6866,6 +6866,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
Loading