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.42】 为Paddle代码转换工具新增API转换规则 (第1组 编号1-20)-part #6212

Merged
merged 10 commits into from
Oct 19, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## [ 仅参数名不一致 ]torch.Tensor.as_strided
### [torch.Tensor.as_strided](https://pytorch.org/docs/stable/generated/torch.Tensor.as_strided.html?highlight=as_strided#torch.Tensor.as_strided)

```python
torch.Tensor.as_strided(size,
stride,
storage_offset=None)
```

### [paddle.Tensor.as_strided](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#as-strided-x-shape-stride-offset-0-name-none)

```python
paddle.Tensor.as_strided(shape,
stride,
offset=0,
name=None)
```

两者功能一致且参数用法一致,仅参数名不一致,具体如下:
### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| size | shape | 表示输出 Tensor 的维度, 仅参数名不一致。 |
| stride | stride | 表示输出 Tensor 的 stride。 |
| storage_offset | offset | 表示偏移量, 仅参数名不一致。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## [ 仅 paddle 参数更多 ]torch.Tensor.cummax

### [torch.Tensor.cummax](https://pytorch.org/docs/stable/generated/torch.Tensor.cummax.html?highlight=cummax#torch.Tensor.cummax)

```python
torch.Tensor.cummax(dim)
```

### [paddle.Tensor.cummax](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#cummax-x-axis-none-dtype-int64-name-none)

```python
paddle.Tensor.cummax(axis=None, dtype=None, name=None)
```

两者功能一致,其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ------------------------------ |
| dim | axis | 需要累加的维度,仅参数名不一致。 |
| - | dtype | 输出 Tensor 的数据类型。PyTorch 无此参数, Paddle 保持默认即可。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## [ 仅 paddle 参数更多 ]torch.Tensor.cummin

### [torch.Tensor.cummin](https://pytorch.org/docs/stable/generated/torch.Tensor.cummin.html?highlight=cummin#torch.Tensor.cummin)

```python
torch.Tensor.cummin(dim)
```

### [paddle.Tensor.cummin](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#cummin-x-axis-none-dtype-int64-name-none)

```python
paddle.Tensor.cummin(axis=None, dtype=None, name=None)
```

两者功能一致,其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ------------------------------ |
| dim | axis | 需要累加的维度,仅参数名不一致。 |
| - | dtype | 输出 Tensor 的数据类型。PyTorch 无此参数, Paddle 保持默认即可。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [ 无参数 ]torch.Tensor.i0

### [torch.Tensor.i0](https://pytorch.org/docs/stable/generated/torch.Tensor.i0.html?highlight=i0#torch.Tensor.i0)

```python
torch.Tensor.i0()
```

### [paddle.Tensor.i0](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#i0-x-name-none)

```python
paddle.Tensor.i0(name=None)
```

两者功能一致,无参数。
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## [ 仅参数名不一致 ]torch.Tensor.index_put
### [torch.Tensor.index_put](https://pytorch.org/docs/stable/generated/torch.Tensor.index_put.html?highlight=index_put#torch.Tensor.index_put)

```python
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)

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

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| indices | indices | 包含用来索引的 tensors 的元组。数据类型为 int32,int64,bool。 |
| values | value | 用来给 x 赋值的 Tensor,仅参数名不一致。 |
| accumulate | accumulate | 指定是否将 value 加到 x 的参数。 默认值为 False。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## [ 仅参数名不一致 ]torch.Tensor.unfold

### [torch.Tensor.unfold](https://pytorch.org/docs/stable/generated/torch.Tensor.unfold.html?highlight=unfold#torch.Tensor.unfold)

```python
torch.Tensor.unfold(dimension,
size,
step)
```

### [paddle.Tensor.unfold](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#unfold-x-axis-size-step-name-none)

```python
paddle.Tensor.unfold(axis,
size,
step,
name=None)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | :----------: | :----------------------------------------------------------: |
| dimension | axis | 表示需要提取的维度,仅参数名不一致。 |
| size | size | 表示需要提取的窗口长度。 |
| step | step | 表示每次提取跳跃的步长。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## [ 参数不一致 ]torch.Tensor.view

### [torch.Tensor.view](https://pytorch.org/docs/stable/generated/torch.Tensor.view.html?highlight=view#torch.Tensor.view)

```python
torch.Tensor.view(*shape)
Copy link
Collaborator

Choose a reason for hiding this comment

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

torch.Tensor.view 有多种用法,还有dtype的用法

torch.Tensor.view(dtype)
```

### [paddle.Tensor.view](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#view-x-shape-or-dtype-name-none)

```python
paddle.Tensor.view(shape_or_dtype, name=None)
```

两者功能一致, 但 pytorch 的 `*shape` 和 paddle 的 `shape_or_dtype` 参数用法不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ------------------------------ |
| *shape | shape_or_dtype | 指定的维度。 Pytorch 参数 shape 既可以是可变参数,也可以是 list/tuple/torch.Size/dtype 的形式, Paddle 参数 shape_or_dtype 为 list/tuple/dtype 的形式。对于可变参数的用法,需要进行转写。 |

### 转写示例

```python
# Pytorch 写法
x = torch.randn(4, 4)
x.view(2, 2, 4)

# Paddle 写法
x = paddle.randn(4, 4)
x.view([2, 2, 4])
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## [ 参数完全一致 ]torch.Tensor.view_as

### [torch.Tensor.view_as](https://pytorch.org/docs/stable/generated/torch.Tensor.view_as.html?highlight=view_as#torch.Tensor.view_as)

```python
torch.Tensor.view_as(other)
```

### [paddle.Tensor.view_as](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#view-as-x-other-name-none)

```python
paddle.Tensor.view_as(other, name=None)
```

两者功能一致,参数完全一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ------------------------------ |
| other | other | 与返回 Tensor shape 相同的 Tensor |
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## [ 仅参数名不一致 ]torch.as_strided
### [torch.as_strided](https://pytorch.org/docs/stable/generated/torch.as_strided.html?highlight=as_strided#torch.as_strided)

```python
torch.as_strided(input,
size,
stride,
storage_offset=None)
```

### [paddle.as_strided](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/as_strided_cn.html#as-strided)

```python
paddle.as_strided(x,
shape,
stride,
offset=0,
name=None)
```

两者功能一致且参数用法一致,仅参数名不一致,具体如下:
### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| input | x | 表示输入的 Tensor, 仅参数名不一致。 |
| size | shape | 表示输出 Tensor 的维度, 仅参数名不一致。 |
| stride | stride | 表示输出 Tensor 的 stride。 |
| storage_offset | offset | 表示偏移量, 仅参数名不一致。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## [ torch 参数更多 ]torch.clamp_max
### [torch.clamp_max]()

```python
torch.clamp_max(input,
max=None,
*,
out=None)
```

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

```python
paddle.clip(x,
min=None,
max=None,
name=None)
```

Pytorch 相比 Paddle 支持更多其他参数,具体如下:
### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| input | x | 表示输入的 Tensor ,仅参数名不一致。 |
| - | min | 表示裁剪的最小值。Pytorch 无此参数, Paddle 保持默认即可。 |
| max | max | 表示裁剪的最大值。 |
| out | - | 表示输出的 Tensor , Paddle 无此参数,需要转写。 |


### 转写示例
#### out:指定输出
```python
# Pytorch 写法
torch.clamp_max(input, max=0.5, out=y)

# Paddle 写法
paddle.assign(paddle.clip(input, max=0.5), y)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## [ torch 参数更多 ]torch.cummax

### [torch.cummax](https://pytorch.org/docs/stable/generated/torch.cummax.html?highlight=cummax#torch.cummax)

```python
torch.cummax(input,
dim,
*,
out=None)
```

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

```python
paddle.cummax(x,
axis=None,
dtype='int64',
name=None)
```

两者功能一致,torch 参数更多,具体如下:

### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| input | x | 表示输入的 Tensor,仅参数名不一致。 |
| dim | axis | 用于指定 index 获取输入的维度,仅参数名不一致。 |
| - | dtype | 指定输出索引的数据格式,PyTorch 无此参数,Paddle 保持默认即可。 |
| out | - | 表示输出的 Tensor,Paddle 无此参数,需要转写。 |

### 转写示例
#### out:指定输出

```python
# Pytorch 写法
torch.cummax(x,1, out=(values, indices))

# Paddle 写法
paddle.assign(paddle.cummax(x,1), (values, indices))
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
Contributor Author

@Li-fAngyU Li-fAngyU Oct 18, 2023

Choose a reason for hiding this comment

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

这里是参考了其他文档, 比如 torch.histc.md

# Pytorch 写法
torch.histc(x, out=y)

# Paddle 写法
paddle.assign(paddle.histogram(x).astype('float32'), y)

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.Tensor.is_pinned

### [torch.Tensor.is_pinned](https://pytorch.org/docs/stable/generated/torch.Tensor.is_pinned.html?highlight=is_pinned#torch.Tensor.is_pinned)

```python
torch.Tensor.is_pinned()
```

返回张量是否在固定内存上; Paddle 无此 API,需要组合实现。

### 转写示例

```python
# Pytorch 写法
y = a.is_pinned(b)

# Paddle 写法
y = 'pinned' in str(a.place)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
## [ torch 参数更多 ]torch.nanquantile

### [torch.nanquantile](https://pytorch.org/docs/stable/generated/torch.nanquantile.html?highlight=nanquantile#torch.nanquantile)

```python
torch.nanquantile(input,
q,
dim=None,
keepdim=False,
*,
interpolation='linear',
out=None)
```

### [paddle.nanquantile]()

```python
paddle.nanquantile(x,
q,
axis=None,
keepdim=False,
name=None)
```

Pytorch 相比 Paddle 支持更多其他参数,具体如下:
### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| input | x | 表示输入的 Tensor,仅参数名不一致。 |
| q | q | 待计算的分位数。 |
| dim | axis| 指定对 x 进行计算的轴,仅参数名不一致。 |
|keepdim|keepdim| 是否在输出 Tensor 中保留减小的维度。|
|interpolation| - | 当所需分位数位于两个数据点之间时使用的插值方法,Paddle 无此参数,Paddle 暂无转写方式。|
| out | - | 表示输出的 Tensor,Paddle 无此参数,需要转写。 |


### 转写示例
#### out:指定输出
```python
# Pytorch 写法
torch.nanquantile(torch.tensor([float('nan'), 1., 2., 3.]), 0.6, out=y)

# Paddle 写法
paddle.assign(paddle.nanquantile(paddle.to_tensor([float('nan'), 1., 2., 3.]), 0.6), y)
```
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Paddle API 签名

* 如果 `无参数`,则没有参数映射这一栏。

* 如果 `参数完全一致`,无需转写示例,需要在备注栏里对该参数加一句 `仅参数名不一致`
* 如果 `参数完全一致`,无需转写示例。

* 如果 `仅参数名不一致`,无需转写示例,需要在备注栏里对该参数加一句 `仅参数名不一致`。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,8 @@
| 300 | [torch.ShortTensor](https://pytorch.org/docs/stable/tensors.html) |[paddle.to_tensor](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/to_tensor_cn.html#to-tensor)| 仅 paddle 参数更多 , [差异对比](https://github.com/PaddlePaddle/docs/tree/develop/docs/guides/model_convert/convert_from_pytorch/api_difference/Tensor/torch.ShortTensor.md)|
| 301 | [torch.Tensor.copysign](https://pytorch.org/docs/1.13/generated/torch.Tensor.copysign.html?highlight=torch+tensor+copysign#torch.Tensor.copysign) | | 功能缺失 |
| 302 | [torch.Tensor.dequantize](https://pytorch.org/docs/1.13/generated/torch.Tensor.dequantize.html?highlight=torch+tensor+dequantize#torch.Tensor.dequantize) | | 功能缺失 |

| 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_) | | 功能缺失 |
***持续更新...***

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