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.33】为 Paddle 新增 atleast_1d / atleast_2d / atleast_3d API #6317

Merged
merged 11 commits into from
Nov 29, 2023
3 changes: 3 additions & 0 deletions docs/api/paddle/Overview_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ tensor 元素操作相关(如:转置,reshape 等)
" :ref:`paddle.gather <cn_api_paddle_gather>` ", "根据索引 index 获取输入 x 的指定 aixs 维度的条目,并将它们拼接在一起"
" :ref:`paddle.gather_nd <cn_api_paddle_gather_nd>` ", "paddle.gather 的高维推广"
" :ref:`paddle.reshape <cn_api_paddle_reshape>` ", "在保持输入 x 数据不变的情况下,改变 x 的形状"
" :ref:`paddle.atleast_1d <cn_api_paddle_atleast_1d>` ", "将输入转换为张量并返回至少为 ``1`` 维的视图"
" :ref:`paddle.atleast_2d <cn_api_paddle_atleast_2d>` ", "将输入转换为张量并返回至少为 ``2`` 维的视图"
" :ref:`paddle.atleast_3d <cn_api_paddle_atleast_3d>` ", "将输入转换为张量并返回至少为 ``3`` 维的视图"
" :ref:`paddle.roll <cn_api_paddle_roll>` ", "沿着指定维度 axis 对输入 x 进行循环滚动,当元素移动到最后位置时,会从第一个位置重新插入"
" :ref:`paddle.scatter <cn_api_paddle_distributed_scatter>` ", "通过基于 updates 来更新选定索引 index 上的输入来获得输出"
" :ref:`paddle.scatter_nd <cn_api_paddle_scatter_nd>` ", "根据 index ,将 updates 添加到一个新的张量中,从而得到输出的 Tensor"
Expand Down
24 changes: 24 additions & 0 deletions docs/api/paddle/Tensor_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3081,3 +3081,27 @@ masked_fill_(x, mask, value, name=None)
:::::::::

Inplace 版本的 :ref:`cn_api_paddle_masked_fill` API,对输入 `x` 采用 Inplace 策略。

atleast_1d(name=None)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有个疑问,这种多输入的Tensor,为什么会有类成员函数

:::::::::
将输入转换为张量并返回至少为 ``1`` 维的视图。 ``1`` 维或更高维的输入会被保留。

返回至少为 ``1`` 维视图的 Tensor 。

请参考 :ref:`cn_api_paddle_atleast_1d`

atleast_2d(name=None)
:::::::::
将输入转换为张量并返回至少为 ``2`` 维的视图。 ``2`` 维或更高维的输入会被保留。

返回至少为 ``2`` 维视图的 Tensor 。

请参考 :ref:`cn_api_paddle_atleast_2d`

atleast_3d(name=None)
:::::::::
将输入转换为张量并返回至少为 ``3`` 维的视图。 ``3`` 维或更高维的输入会被保留。

返回至少为 ``3`` 维视图的 Tensor 。

请参考 :ref:`cn_api_paddle_atleast_3d`
23 changes: 23 additions & 0 deletions docs/api/paddle/atleast_1d_cn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.. _cn_api_paddle_atleast_1d:

atleast_1d
-------------------------------

.. py:function:: paddle.atleast_1d(*inputs, name=None)
将输入转换为张量并返回至少为 ``1`` 维的视图。标量输入会被转换, ``1`` 维或更高维的输入则会被保留。

参数
::::::::::::

- **inputs** (Tensor|list(Tensor)) - 一个或多个 Tensor,数据类型为: ``float16``, ``float32``, ``float64``, ``int16``, ``int32``, ``int64``, ``int8``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16`` 或 ``bool``。
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。

返回
::::::::::::
Tensor 或者由 Tensor 组成的 list。当只有一个输入的时候返回一个 Tensor,当有多个输入的时候返回由 Tensor 组成的 list。

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

COPY-FROM: paddle.atleast_1d
23 changes: 23 additions & 0 deletions docs/api/paddle/atleast_2d_cn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.. _cn_api_paddle_atleast_2d:

atleast_2d
-------------------------------

.. py:function:: paddle.atleast_2d(*inputs, name=None)
将输入转换为张量并返回至少为 ``2`` 维的视图。 ``2`` 维或更高维的输入会被保留。

参数
::::::::::::

- **inputs** (Tensor|list(Tensor)) - 一个或多个 Tensor,数据类型为: ``float16``, ``float32``, ``float64``, ``int16``, ``int32``, ``int64``, ``int8``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16`` 或 ``bool``。
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。

返回
::::::::::::
Tensor 或者由 Tensor 组成的 list。当只有一个输入的时候返回一个 Tensor,当有多个输入的时候返回由 Tensor 组成的 list。

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

COPY-FROM: paddle.atleast_2d
23 changes: 23 additions & 0 deletions docs/api/paddle/atleast_3d_cn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.. _cn_api_paddle_atleast_3d:

atleast_3d
-------------------------------

.. py:function:: paddle.atleast_3d(*inputs, name=None)
将输入转换为张量并返回至少为 ``3`` 维的视图。 ``3`` 维或更高维的输入会被保留。

参数
::::::::::::

- **inputs** (Tensor|list(Tensor)) - 一个或多个 Tensor,数据类型为: ``float16``, ``float32``, ``float64``, ``int16``, ``int32``, ``int64``, ``int8``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16`` 或 ``bool``。
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。

返回
::::::::::::
Tensor 或者由 Tensor 组成的 list。当只有一个输入的时候返回一个 Tensor,当有多个输入的时候返回由 Tensor 组成的 list。

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

COPY-FROM: paddle.atleast_3d
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## [ 参数不一致 ]torch.atleast_1d

### [torch.atleast_1d](https://pytorch.org/docs/stable/generated/torch.atleast_1d.html#torch-atleast-1d)

```python
torch.atleast_1d(*tensors)
```

### [paddle.atleast_1d](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/atleast_1d_cn.html#atleast_1d)

```python
paddle.atleast_1d(*inputs, name=None)
```

PyTorch 与 Paddle 参数形式上一致,具体如下:

### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| <font color='red'> tensors </font> | <font color='red'> inputs </font> | 输入的 Tensor ,仅参数名不一致。 |

PyTorch 与 Paddle 功能一致,但,对于多个 Tensor 输入的处理方式略有不同,具体请看转写示例。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PyTorch 与 Paddle 功能一致,但,对于多个 Tensor 输入的处理方式略有不同,具体请看转写示例。
PyTorch 与 Paddle 功能一致,但对于多个 Tensor 输入的处理方式略有不同,具体请看转写示例。


### 转写示例

#### 多个 Tensor
```python
# Pytorch 写法
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

写不一致的case就行,这里写伪代码,不用写多了

x = torch.tensor(0.3)
y = torch.tensor(0.4)
# 可以写为多个输入的方式
torch.atleast_1d(x, y)
# 或者组合为一个 tuple,这两种方式是等价的
torch.atleast_1d((x, y))

# Paddle 写法
x = paddle.to_tensor(0.3)
y = paddle.to_tensor(0.4)
# 这里只能分别传入
paddle.atleast_1d(x, y)
# 下面这种方式会将 (x, y) 经过 broadcast 后作为单个输入
paddle.atleast_1d((x, y))

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## [ 参数不一致 ]torch.atleast_2d

### [torch.atleast_2d](https://pytorch.org/docs/stable/generated/torch.atleast_2d.html#torch-atleast-2d)

```python
torch.atleast_2d(*tensors)
```

### [paddle.atleast_2d](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/atleast_2d_cn.html#atleast_2d)

```python
paddle.atleast_2d(*inputs, name=None)
```

PyTorch 与 Paddle 参数形式上一致,具体如下:

### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| <font color='red'> tensors </font> | <font color='red'> inputs </font> | 输入的 Tensor ,仅参数名不一致。 |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上


PyTorch 与 Paddle 功能一致,但,对于多个 Tensor 输入的处理方式略有不同,具体请看转写示例。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PyTorch 与 Paddle 功能一致,但,对于多个 Tensor 输入的处理方式略有不同,具体请看转写示例。
PyTorch 与 Paddle 功能一致,但对于多个 Tensor 输入的处理方式略有不同,具体请看转写示例。


### 转写示例

#### 多个 Tensor
```python
# Pytorch 写法
x = torch.tensor(0.3)
y = torch.tensor(0.4)
# 可以写为多个输入的方式
torch.atleast_2d(x, y)
# 或者组合为一个 tuple,这两种方式是等价的
torch.atleast_2d((x, y))

# Paddle 写法
x = paddle.to_tensor(0.3)
y = paddle.to_tensor(0.4)
# 这里只能分别传入
paddle.atleast_2d(x, y)
# 下面这种方式会将 (x, y) 经过 broadcast 后作为单个输入
paddle.atleast_2d((x, y))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前是 参数不一致,因为涉及到某些case的用法不同

但有点疑问:API针对这种情况为什么不和torch一致呢?broadcast 后作为单个输入是更合理的行为吗

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PaddlePaddle/Paddle#58323 (comment)

这里是与 numpy 对齐 ~

torch 的这种处理方式至少在 python 的编程习惯上存在歧义 ~

paddle.atleast_1d(*inputs)

这里本身就是处理多个输入的情况,而在 python 中,paddle.atleast_1d(x, y)paddle.atleast_1d((x, y)) 是两种不同的输入,前者是两个 positional 输入,python 的 *inputs 会展开,而后者只是一个输入,类型是 tuple 的输入 ~

后者 合适 的输入方式应该是 paddle.atleast_1d(*(x, y)),参考 numpy :

In [1]: import numpy as np

In [2]: np.atleast_1d((123, 234)) # 一个 tuple 输入
Out[2]: array([123, 234])

In [3]: np.atleast_1d((np.array(123), np.array(234))) # 一个 tuple 输入,包含两个 array
Out[3]: array([123, 234])

In [4]: np.atleast_1d(*(np.array(123), np.array(234))) # 将 tuple 展开
Out[4]: [array([123]), array([234])]

In [5]: np.atleast_1d(123, 234) # 两个输入
Out[5]: [array([123]), array([234])]

torch 之所以这么做,个人感觉是因为,这几个 api 是直接用底层 c++ 处理的,上层封装没有进行区分 ... ...

因此

broadcast 后作为单个输入是更合理的行为吗

个人感觉,不是 ~ 因为这种输入本身就不合理 ~

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PaddlePaddle/Paddle#58323 (comment)

这里是与 numpy 对齐 ~

torch 的这种处理方式至少在 python 的编程习惯上存在歧义 ~

paddle.atleast_1d(*inputs)

这里本身就是处理多个输入的情况,而在 python 中,paddle.atleast_1d(x, y)paddle.atleast_1d((x, y)) 是两种不同的输入,前者是两个 positional 输入,python 的 *inputs 会展开,而后者只是一个输入,类型是 tuple 的输入 ~

后者 合适 的输入方式应该是 paddle.atleast_1d(*(x, y)),参考 numpy :

In [1]: import numpy as np

In [2]: np.atleast_1d((123, 234)) # 一个 tuple 输入
Out[2]: array([123, 234])

In [3]: np.atleast_1d((np.array(123), np.array(234))) # 一个 tuple 输入,包含两个 array
Out[3]: array([123, 234])

In [4]: np.atleast_1d(*(np.array(123), np.array(234))) # 将 tuple 展开
Out[4]: [array([123]), array([234])]

In [5]: np.atleast_1d(123, 234) # 两个输入
Out[5]: [array([123]), array([234])]

torch 之所以这么做,个人感觉是因为,这几个 api 是直接用底层 c++ 处理的,上层封装没有进行区分 ... ...

因此

broadcast 后作为单个输入是更合理的行为吗

个人感觉,不是 ~ 因为这种输入本身就不合理 ~

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是broadcast为单个输入,还是concat为单个输入?


```
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## [ 参数不一致 ]torch.atleast_3d
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个算 仅参数不一致 嘛,还是 参数不一致,不确定捏,孙师傅快来 @sunzhongkai588
是因为 两者对 (x,y) 的处理不一致
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯 很纠结 不知道该咋写 😂 ~~~

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个算 仅参数不一致 嘛,还是 参数不一致,不确定捏,孙师傅快来 @sunzhongkai588
是因为 两者对 (x,y) 的处理不一致

@zhwesky2010 麻烦看一下, atleast_1d / atleast_2d / atleast_3d 和 torch 属于那种不一致


### [torch.atleast_3d](https://pytorch.org/docs/stable/generated/torch.atleast_3d.html#torch-atleast-3d)

```python
torch.atleast_3d(*tensors)
```

### [paddle.atleast_3d](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/atleast_3d_cn.html#atleast_3d)

```python
paddle.atleast_3d(*inputs, name=None)
```

PyTorch 与 Paddle 参数形式上一致,具体如下:

### 参数映射
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| <font color='red'> tensors </font> | <font color='red'> inputs </font> | 输入的 Tensor ,仅参数名不一致。 |

PyTorch 与 Paddle 功能一致,但,对于多个 Tensor 输入的处理方式略有不同,具体请看转写示例。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PyTorch 与 Paddle 功能一致,但,对于多个 Tensor 输入的处理方式略有不同,具体请看转写示例。
PyTorch 与 Paddle 功能一致,但对于多个 Tensor 输入的处理方式略有不同,具体请看转写示例。


### 转写示例

#### 多个 Tensor
```python
# Pytorch 写法
x = torch.tensor(0.3)
y = torch.tensor(0.4)
# 可以写为多个输入的方式
torch.atleast_3d(x, y)
# 或者组合为一个 tuple,这两种方式是等价的
torch.atleast_3d((x, y))

# Paddle 写法
x = paddle.to_tensor(0.3)
y = paddle.to_tensor(0.4)
# 这里只能分别传入
paddle.atleast_3d(x, y)
# 下面这种方式会将 (x, y) 经过 broadcast 后作为单个输入
paddle.atleast_3d((x, y))

```
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@
| REFERENCE-MAPPING-ITEM(`torch.is_grad_enabled`, https://github.com/PaddlePaddle/docs/tree/develop/docs/guides/model_convert/convert_from_pytorch/api_difference/others/torch.is_grad_enabled.md) |
| REFERENCE-MAPPING-ITEM(`torch.manual_seed`, https://github.com/PaddlePaddle/docs/tree/develop/docs/guides/model_convert/convert_from_pytorch/api_difference/others/torch.manual_seed.md) |
| [torch.can_cast](https://pytorch.org/docs/stable/generated/torch.can_cast.html#torch-can-cast) | 功能缺失 |
| REFERENCE-MAPPING-ITEM(`torch.atleast_1d`, https://github.com/PaddlePaddle/docs/tree/develop/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.atleast_1d.md) |
| REFERENCE-MAPPING-ITEM(`torch.atleast_2d`, https://github.com/PaddlePaddle/docs/tree/develop/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.atleast_2d.md) |
| REFERENCE-MAPPING-ITEM(`torch.atleast_3d`, https://github.com/PaddlePaddle/docs/tree/develop/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.atleast_3d.md) |


***持续更新...***
Expand Down Expand Up @@ -926,6 +929,9 @@
| 303 | [torch.Tensor.sum_to_size](https://pytorch.org/docs/stable/generated/torch.Tensor.sum_to_size.html?highlight=sum_to_size#torch.Tensor.sum_to_size) | | 功能缺失 |
| 304 | [torch.Tensor.resize_](https://pytorch.org/docs/stable/generated/torch.Tensor.resize_.html?highlight=resize#torch.Tensor.resize_) | | 功能缺失 |
| 305 | [torch.Tensor.masked_fill_](https://pytorch.org/docs/stable/generated/torch.Tensor.masked_fill_.html?highlight=resize#torch.Tensor.masked_fill_) | [paddle.Tensor.masked_fill_](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/Tensor_cn.html#id25) | 功能完全一致 |
| 306 | | [paddle.Tensor.atleast_1d](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/atleast_1d_cn.html#atleast_1d) | PyTorch 功能缺失 |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

中间多了一个 | ,渲染出来没问题,看着有点奇怪~

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

撤回,刚刚没看清,sorry

| 307 | | [paddle.Tensor.atleast_2d](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/atleast_2d_cn.html#atleast_2d) | PyTorch 功能缺失 |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个如果pytorch没有,paddle的也不用写,只用写pytorch能对应上的

| 308 | | [paddle.Tensor.atleast_3d](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/atleast_3d_cn.html#atleast_3d) | PyTorch 功能缺失 |


| 序号 | PyTorch API | PaddlePaddle API | 备注 |
Expand Down Expand Up @@ -1234,6 +1240,7 @@
| NOT-IMPLEMENTED-ITEM(`torch.Tensor.dequantize`, https://pytorch.org/docs/1.13/generated/torch.Tensor.dequantize.html?highlight=torch+tensor+dequantize#torch.Tensor.dequantize) |
| NOT-IMPLEMENTED-ITEM(`torch.Tensor.sum_to_size`, https://pytorch.org/docs/stable/generated/torch.Tensor.sum_to_size.html?highlight=sum_to_size#torch.Tensor.sum_to_size) |
| NOT-IMPLEMENTED-ITEM(`torch.Tensor.resize_`, https://pytorch.org/docs/stable/generated/torch.Tensor.resize_.html?highlight=resize#torch.Tensor.resize_) |

***持续更新...***

## torch.nn.init.XX API 映射列表
Expand Down