Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Hackathon 5th No.2】为 Paddle 新增 index_fill API #6284

Merged
merged 1 commit into from
Nov 6, 2023
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 docs/api/paddle/Overview_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ tensor 数学操作原位(inplace)版本
" :ref:`paddle.lerp_ <cn_api_paddle_lerp_>` ", "Inplace 版本的 lerp API,对输入 x 采用 Inplace 策略"
" :ref:`paddle.hypot_ <cn_api_paddle_hypot_>` ", "Inplace 版本的 hypot API,对输入 x 采用 Inplace 策略"
" :ref:`paddle.masked_fill_ <cn_api_paddle_masked_fill_>` ", "Inplace 版本的 masked_fill API,对输入 x 采用 Inplace 策略"
" :ref:`paddle.index_fill_ <cn_api_paddle_index_fill_>` ", "Inplace 版本的 index_fill API,对输入 x 采用 Inplace 策略"

.. _tensor_logic:

Expand Down Expand Up @@ -386,6 +387,7 @@ tensor 元素操作相关(如:转置,reshape 等)
" :ref:`paddle.view_as <cn_api_paddle_view_as>` ", "使用 other 的 shape,返回 x 的一个 view Tensor"
" :ref:`paddle.unfold <cn_api_paddle_unfold>` ", "返回 x 的一个 view Tensor。以滑动窗口式提取 x 的值"
" :ref:`paddle.masked_fill <cn_api_paddle_masked_fill>` ", "根据 mask 信息,将 value 中的值填充到 x 中 mask 对应为 True 的位置。"
" :ref:`paddle.index_fill <cn_api_paddle_index_fill>` ", "沿着指定轴 axis 将 index 中指定位置的 x 的值填充为 value"

.. _tensor_manipulation_inplace:

Expand Down
28 changes: 28 additions & 0 deletions docs/api/paddle/index_fill__cn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.. _cn_api_paddle_index_fill_:

index_fill\_
-------------------------------

.. py:function:: paddle.index_fill_(x, index, axis, value, name=None)

依据指定的轴 ``axis`` 和索引 ``indices`` 将指定位置的 ``x`` 填充为 ``value`` 。

参数
:::::::::

- **x** (Tensor)– 输入 Tensor。 ``x`` 的数据类型可以是 float16, float32,float64,int32,int64。
- **index** (Tensor)– 包含索引下标的 1-D Tensor。数据类型可以是 int32,int64。
- **axis** (int) – 索引轴。数据类型为 int。
- **value** (float)– 用于填充目标张量的值。
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。

返回
:::::::::

Tensor,返回一个数据类型同输入的 Tensor。


代码示例
::::::::::::

COPY-FROM: paddle.index_fill_
17 changes: 17 additions & 0 deletions docs/api/paddle/index_fill_cn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.. _cn_api_paddle_index_fill:

index_fill
-------------------------------

.. py:function:: paddle.index_fill(x, index, axis, value, name=None)

Outplace 版本的 :ref:`cn_api_paddle_index_fill_` API

代码示例
::::::::::::

COPY-FROM: paddle.index_fill

更多关于 outplace 操作的介绍请参考 `3.1.3 原位(Inplace)操作和非原位(Outplace)操作的区别`_ 了解详情。

.. _3.1.3 原位(Inplace)操作和非原位(Outplace)操作的区别: https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/guides/beginner/tensor_cn.html#id3
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ paddle.index_add(x, index, axis, value, name=None)
#### alpha:source 的缩放倍数
```python
# Pytorch 写法
x.index_add_(dim=1, index=index, source=source, alpha=alpha)
x.index_add(dim=1, index=index, source=source, alpha=alpha)

# Paddle 写法
paddle.index_add(x, index=index, axis=1, value=alpha*source)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## [ 仅参数名不一致 ]torch.Tensor.index_fill
### [torch.Tensor.index_fill](https://pytorch.org/docs/stable/generated/torch.Tensor.index_fill.html?highlight=index_fill#torch.Tensor.index_fill)

```python
torch.Tensor.index_fill(dim, index, value)
```

### [paddle.Tensor.index_fill](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/index_fill_cn.html#index-fill)

```python
paddle.Tensor.index_fill(index, axis, value, name=None)
```

两者功能一致且参数用法一致,仅参数名不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| <font color='red'> dim </font> | <font color='red'> axis </font> | 表示进行运算的轴,仅参数名不一致。 |
| <font color='red'> index </font> | <font color='red'> index </font> | 包含索引下标的 1-D Tensor。 |
| <font color='red'> value </font> | <font color='red'> value </font> | 填充的值。 |

### 转写示例
#### alpha:source 的缩放倍数
```python
# Pytorch 写法
x.index_fill(dim=1, index=index, value=1)

# Paddle 写法
x.index_fill(index=index, axis=1, value=1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## [ 仅参数名不一致 ]torch.Tensor.index_fill_
### [torch.Tensor.index_fill_](https://pytorch.org/docs/stable/generated/torch.Tensor.index_fill_.html?highlight=index_fill_#torch.Tensor.index_fill_)

```python
torch.Tensor.index_fill_(dim, index, value)
```

### [paddle.Tensor.index_fill_]()

```python
paddle.Tensor.index_fill_(index, axis, value, name=None)
```

两者功能一致且参数用法一致,仅参数名不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| <font color='red'> dim </font> | <font color='red'> axis </font> | 表示进行运算的轴,仅参数名不一致。 |
| <font color='red'> index </font> | <font color='red'> index </font> | 包含索引下标的 1-D Tensor。 |
| <font color='red'> value </font> | <font color='red'> value </font> | 填充的值。 |

### 转写示例
#### alpha:source 的缩放倍数
```python
# Pytorch 写法
x.index_fill_(dim=1, index=index, value=1)

# Paddle 写法
x.index_fill_(index=index, axis=1, value=1)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
torch.Tensor.index_put(indices, values, accumulate=False)
```

### [paddle.Tensor.index_put](https://pytorch.org/docs/stable/generated/torch.Tensor.index_put.html?highlight=index_put#torch.Tensor.index_put)
### [paddle.Tensor.index_put](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/index_put_cn.html#index-put)

```python
paddle.Tensor.index_put(indices, value, accumulate=False, name=None)
Expand Down