Skip to content

Commit 84a39dd

Browse files
authored
【Hackthon 5 No.42】 为Paddle代码转换工具新增API转换规则 (第1组 编号1-20) (#6212)
* Hackthon5 No 42. paddle/docs first commit. * first commit. * remove empty file * fix torch.Tensor.cummin.md torch.Tensor.cummax.md. * update * fix torch.as_strided and torch.Tensor.as_strided * update
1 parent b0e6786 commit 84a39dd

15 files changed

+362
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## [ 仅参数名不一致 ]torch.Tensor.as_strided
2+
### [torch.Tensor.as_strided](https://pytorch.org/docs/stable/generated/torch.Tensor.as_strided.html?highlight=as_strided#torch.Tensor.as_strided)
3+
4+
```python
5+
torch.Tensor.as_strided(size,
6+
stride,
7+
storage_offset=None)
8+
```
9+
10+
### [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)
11+
12+
```python
13+
paddle.Tensor.as_strided(shape,
14+
stride,
15+
offset=0,
16+
name=None)
17+
```
18+
19+
两者功能一致且参数用法一致,仅参数名不一致,具体如下:
20+
### 参数映射
21+
| PyTorch | PaddlePaddle | 备注 |
22+
| ------------- | ------------ | ------------------------------------------------------ |
23+
| size | shape | 表示输出 Tensor 的维度, 仅参数名不一致。 |
24+
| stride | stride | 表示输出 Tensor 的 stride。 |
25+
| storage_offset | offset | 表示偏移量, 仅参数名不一致。 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## [ 仅 paddle 参数更多 ]torch.Tensor.cummax
2+
3+
### [torch.Tensor.cummax](https://pytorch.org/docs/stable/generated/torch.Tensor.cummax.html?highlight=cummax#torch.Tensor.cummax)
4+
5+
```python
6+
torch.Tensor.cummax(dim)
7+
```
8+
9+
### [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)
10+
11+
```python
12+
paddle.Tensor.cummax(axis=None, dtype=None, name=None)
13+
```
14+
15+
两者功能一致,其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下:
16+
17+
### 参数映射
18+
19+
| PyTorch | PaddlePaddle | 备注 |
20+
| ------- | ------------ | ------------------------------ |
21+
| dim | axis | 需要累加的维度,仅参数名不一致。 |
22+
| - | dtype | 输出 Tensor 的数据类型。PyTorch 无此参数, Paddle 保持默认即可。 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## [ 仅 paddle 参数更多 ]torch.Tensor.cummin
2+
3+
### [torch.Tensor.cummin](https://pytorch.org/docs/stable/generated/torch.Tensor.cummin.html?highlight=cummin#torch.Tensor.cummin)
4+
5+
```python
6+
torch.Tensor.cummin(dim)
7+
```
8+
9+
### [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)
10+
11+
```python
12+
paddle.Tensor.cummin(axis=None, dtype=None, name=None)
13+
```
14+
15+
两者功能一致,其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下:
16+
17+
### 参数映射
18+
19+
| PyTorch | PaddlePaddle | 备注 |
20+
| ------- | ------------ | ------------------------------ |
21+
| dim | axis | 需要累加的维度,仅参数名不一致。 |
22+
| - | dtype | 输出 Tensor 的数据类型。PyTorch 无此参数, Paddle 保持默认即可。 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## [ 无参数 ]torch.Tensor.i0
2+
3+
### [torch.Tensor.i0](https://pytorch.org/docs/stable/generated/torch.Tensor.i0.html?highlight=i0#torch.Tensor.i0)
4+
5+
```python
6+
torch.Tensor.i0()
7+
```
8+
9+
### [paddle.Tensor.i0](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#i0-x-name-none)
10+
11+
```python
12+
paddle.Tensor.i0(name=None)
13+
```
14+
15+
两者功能一致,无参数。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## [ 仅参数名不一致 ]torch.Tensor.index_put
2+
### [torch.Tensor.index_put](https://pytorch.org/docs/stable/generated/torch.Tensor.index_put.html?highlight=index_put#torch.Tensor.index_put)
3+
4+
```python
5+
torch.Tensor.index_put(indices, values, accumulate=False)
6+
```
7+
8+
### [paddle.Tensor.index_put](https://pytorch.org/docs/stable/generated/torch.Tensor.index_put.html?highlight=index_put#torch.Tensor.index_put)
9+
10+
```python
11+
paddle.Tensor.index_put(indices, value, accumulate=False, name=None)
12+
```
13+
14+
两者功能一致且参数用法一致,仅参数名不一致,具体如下:
15+
16+
### 参数映射
17+
18+
| PyTorch | PaddlePaddle | 备注 |
19+
| ------------- | ------------ | ------------------------------------------------------ |
20+
| indices | indices | 包含用来索引的 tensors 的元组。数据类型为 int32,int64,bool。 |
21+
| values | value | 用来给 x 赋值的 Tensor,仅参数名不一致。 |
22+
| accumulate | accumulate | 指定是否将 value 加到 x 的参数。 默认值为 False。 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## [ 仅参数名不一致 ]torch.Tensor.unfold
2+
3+
### [torch.Tensor.unfold](https://pytorch.org/docs/stable/generated/torch.Tensor.unfold.html?highlight=unfold#torch.Tensor.unfold)
4+
5+
```python
6+
torch.Tensor.unfold(dimension,
7+
size,
8+
step)
9+
```
10+
11+
### [paddle.Tensor.unfold](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#unfold-x-axis-size-step-name-none)
12+
13+
```python
14+
paddle.Tensor.unfold(axis,
15+
size,
16+
step,
17+
name=None)
18+
```
19+
20+
两者功能一致且参数用法一致,仅参数名不一致,具体如下:
21+
22+
### 参数映射
23+
24+
| PyTorch | PaddlePaddle | 备注 |
25+
| ------- | :----------: | :----------------------------------------------------------: |
26+
| dimension | axis | 表示需要提取的维度,仅参数名不一致。 |
27+
| size | size | 表示需要提取的窗口长度。 |
28+
| step | step | 表示每次提取跳跃的步长。 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## [ 参数不一致 ]torch.Tensor.view
2+
3+
### [torch.Tensor.view](https://pytorch.org/docs/stable/generated/torch.Tensor.view.html?highlight=view#torch.Tensor.view)
4+
5+
```python
6+
torch.Tensor.view(*shape)
7+
torch.Tensor.view(dtype)
8+
```
9+
10+
### [paddle.Tensor.view](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#view-x-shape-or-dtype-name-none)
11+
12+
```python
13+
paddle.Tensor.view(shape_or_dtype, name=None)
14+
```
15+
16+
两者功能一致, 但 pytorch 的 `*shape` 和 paddle 的 `shape_or_dtype` 参数用法不一致,具体如下:
17+
18+
### 参数映射
19+
20+
| PyTorch | PaddlePaddle | 备注 |
21+
| ------- | ------------ | ------------------------------ |
22+
| *shape | shape_or_dtype | 指定的维度。 Pytorch 参数 shape 既可以是可变参数,也可以是 list/tuple/torch.Size/dtype 的形式, Paddle 参数 shape_or_dtype 为 list/tuple/dtype 的形式。对于可变参数的用法,需要进行转写。 |
23+
24+
### 转写示例
25+
26+
```python
27+
# Pytorch 写法
28+
x = torch.randn(4, 4)
29+
x.view(2, 2, 4)
30+
31+
# Paddle 写法
32+
x = paddle.randn(4, 4)
33+
x.view([2, 2, 4])
34+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## [ 参数完全一致 ]torch.Tensor.view_as
2+
3+
### [torch.Tensor.view_as](https://pytorch.org/docs/stable/generated/torch.Tensor.view_as.html?highlight=view_as#torch.Tensor.view_as)
4+
5+
```python
6+
torch.Tensor.view_as(other)
7+
```
8+
9+
### [paddle.Tensor.view_as](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#view-as-x-other-name-none)
10+
11+
```python
12+
paddle.Tensor.view_as(other, name=None)
13+
```
14+
15+
两者功能一致,参数完全一致,具体如下:
16+
17+
### 参数映射
18+
19+
| PyTorch | PaddlePaddle | 备注 |
20+
| ------- | ------------ | ------------------------------ |
21+
| other | other | 与返回 Tensor shape 相同的 Tensor |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## [ 仅参数名不一致 ]torch.as_strided
2+
### [torch.as_strided](https://pytorch.org/docs/stable/generated/torch.as_strided.html?highlight=as_strided#torch.as_strided)
3+
4+
```python
5+
torch.as_strided(input,
6+
size,
7+
stride,
8+
storage_offset=None)
9+
```
10+
11+
### [paddle.as_strided](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/as_strided_cn.html#as-strided)
12+
13+
```python
14+
paddle.as_strided(x,
15+
shape,
16+
stride,
17+
offset=0,
18+
name=None)
19+
```
20+
21+
两者功能一致且参数用法一致,仅参数名不一致,具体如下:
22+
### 参数映射
23+
| PyTorch | PaddlePaddle | 备注 |
24+
| ------------- | ------------ | ------------------------------------------------------ |
25+
| input | x | 表示输入的 Tensor, 仅参数名不一致。 |
26+
| size | shape | 表示输出 Tensor 的维度, 仅参数名不一致。 |
27+
| stride | stride | 表示输出 Tensor 的 stride。 |
28+
| storage_offset | offset | 表示偏移量, 仅参数名不一致。 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## [ torch 参数更多 ]torch.clamp_max
2+
### [torch.clamp_max]()
3+
4+
```python
5+
torch.clamp_max(input,
6+
max=None,
7+
*,
8+
out=None)
9+
```
10+
11+
### [paddle.clip](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/clip_cn.html#clip)
12+
13+
```python
14+
paddle.clip(x,
15+
min=None,
16+
max=None,
17+
name=None)
18+
```
19+
20+
Pytorch 相比 Paddle 支持更多其他参数,具体如下:
21+
### 参数映射
22+
| PyTorch | PaddlePaddle | 备注 |
23+
| ------------- | ------------ | ------------------------------------------------------ |
24+
| input | x | 表示输入的 Tensor ,仅参数名不一致。 |
25+
| - | min | 表示裁剪的最小值。Pytorch 无此参数, Paddle 保持默认即可。 |
26+
| max | max | 表示裁剪的最大值。 |
27+
| out | - | 表示输出的 Tensor , Paddle 无此参数,需要转写。 |
28+
29+
30+
### 转写示例
31+
#### out:指定输出
32+
```python
33+
# Pytorch 写法
34+
torch.clamp_max(input, max=0.5, out=y)
35+
36+
# Paddle 写法
37+
paddle.assign(paddle.clip(input, max=0.5), y)
38+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## [ torch 参数更多 ]torch.cummax
2+
3+
### [torch.cummax](https://pytorch.org/docs/stable/generated/torch.cummax.html?highlight=cummax#torch.cummax)
4+
5+
```python
6+
torch.cummax(input,
7+
dim,
8+
*,
9+
out=None)
10+
```
11+
12+
### [paddle.cummax](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/cummax_cn.html#cummax)
13+
14+
```python
15+
paddle.cummax(x,
16+
axis=None,
17+
dtype='int64',
18+
name=None)
19+
```
20+
21+
两者功能一致,torch 参数更多,具体如下:
22+
23+
### 参数映射
24+
| PyTorch | PaddlePaddle | 备注 |
25+
| ------------- | ------------ | ------------------------------------------------------ |
26+
| input | x | 表示输入的 Tensor,仅参数名不一致。 |
27+
| dim | axis | 用于指定 index 获取输入的维度,仅参数名不一致。 |
28+
| - | dtype | 指定输出索引的数据格式,PyTorch 无此参数,Paddle 保持默认即可。 |
29+
| out | - | 表示输出的 Tensor,Paddle 无此参数,需要转写。 |
30+
31+
### 转写示例
32+
#### out:指定输出
33+
34+
```python
35+
# Pytorch 写法
36+
torch.cummax(x,1, out=(values, indices))
37+
38+
# Paddle 写法
39+
paddle.assign(paddle.cummax(x,1), (values, indices))
40+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## [ 组合替代实现 ]torch.Tensor.is_pinned
2+
3+
### [torch.Tensor.is_pinned](https://pytorch.org/docs/stable/generated/torch.Tensor.is_pinned.html?highlight=is_pinned#torch.Tensor.is_pinned)
4+
5+
```python
6+
torch.Tensor.is_pinned()
7+
```
8+
9+
返回张量是否在固定内存上; Paddle 无此 API,需要组合实现。
10+
11+
### 转写示例
12+
13+
```python
14+
# Pytorch 写法
15+
y = a.is_pinned(b)
16+
17+
# Paddle 写法
18+
y = 'pinned' in str(a.place)
19+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## [ torch 参数更多 ]torch.nanquantile
2+
3+
### [torch.nanquantile](https://pytorch.org/docs/stable/generated/torch.nanquantile.html?highlight=nanquantile#torch.nanquantile)
4+
5+
```python
6+
torch.nanquantile(input,
7+
q,
8+
dim=None,
9+
keepdim=False,
10+
*,
11+
interpolation='linear',
12+
out=None)
13+
```
14+
15+
### [paddle.nanquantile]()
16+
17+
```python
18+
paddle.nanquantile(x,
19+
q,
20+
axis=None,
21+
keepdim=False,
22+
name=None)
23+
```
24+
25+
Pytorch 相比 Paddle 支持更多其他参数,具体如下:
26+
### 参数映射
27+
| PyTorch | PaddlePaddle | 备注 |
28+
| ------------- | ------------ | ------------------------------------------------------ |
29+
| input | x | 表示输入的 Tensor,仅参数名不一致。 |
30+
| q | q | 待计算的分位数。 |
31+
| dim | axis| 指定对 x 进行计算的轴,仅参数名不一致。 |
32+
|keepdim|keepdim| 是否在输出 Tensor 中保留减小的维度。|
33+
|interpolation| - | 当所需分位数位于两个数据点之间时使用的插值方法,Paddle 无此参数,Paddle 暂无转写方式。|
34+
| out | - | 表示输出的 Tensor,Paddle 无此参数,需要转写。 |
35+
36+
37+
### 转写示例
38+
#### out:指定输出
39+
```python
40+
# Pytorch 写法
41+
torch.nanquantile(torch.tensor([float('nan'), 1., 2., 3.]), 0.6, out=y)
42+
43+
# Paddle 写法
44+
paddle.assign(paddle.nanquantile(paddle.to_tensor([float('nan'), 1., 2., 3.]), 0.6), y)
45+
```

docs/guides/model_convert/convert_from_pytorch/api_difference/pytorch_api_mapping_format_cn.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Paddle API 签名
5151

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

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

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

docs/guides/model_convert/convert_from_pytorch/pytorch_api_mapping_cn.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,8 @@
917917
| 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)|
918918
| 301 | [torch.Tensor.copysign](https://pytorch.org/docs/1.13/generated/torch.Tensor.copysign.html?highlight=torch+tensor+copysign#torch.Tensor.copysign) | | 功能缺失 |
919919
| 302 | [torch.Tensor.dequantize](https://pytorch.org/docs/1.13/generated/torch.Tensor.dequantize.html?highlight=torch+tensor+dequantize#torch.Tensor.dequantize) | | 功能缺失 |
920-
920+
| 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) | | 功能缺失 |
921+
| 304 | [torch.Tensor.resize_](https://pytorch.org/docs/stable/generated/torch.Tensor.resize_.html?highlight=resize#torch.Tensor.resize_) | | 功能缺失 |
921922
***持续更新...***
922923

923924
## torch.nn.init.XX API 映射列表

0 commit comments

Comments
 (0)