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

Paddle 相比 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 | PaddlePaddle | 备注 |
| ------- | :----------: | :----------------------------------------------------------: |
| dimension | axis | 表示需要提取的维度,仅参数名不一致。 |
| size | size | 表示需要提取的窗口长度。 |
| step | step | 表示每次提取跳跃的步长。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## [ 参数不一致 ]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的用法

```

### [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 的形式。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

后面加一句 需要转写。

然后需要在下面加上转写示例

infoflow 2023-10-11 18-34-01

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## [ 参数完全一致 ]torch.Tensor.view_as

### [torch.Tensor.view](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](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 。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

你这个写了 仅参数名不一致 吗?

| 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,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 暂无转写方式。|
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个 需要转写 不用写

| out | - | 表示输出的 Tensor,Paddle 无此参数,需要转写。 |


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

Choose a reason for hiding this comment

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

interpolation 这个不能转写,写示例不用加上


# 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 @@ -917,7 +917,9 @@
| 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.is_pinned](https://pytorch.org/docs/stable/generated/torch.Tensor.is_pinned.html?highlight=is_pinned#torch.Tensor.is_pinned) | | 功能缺失 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个有办法组合替代实现吧,比如说判断x.place

Copy link
Contributor Author

@Li-fAngyU Li-fAngyU Oct 11, 2023

Choose a reason for hiding this comment

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

谢谢审查,经过查阅资料是可以组合替代实现判断张量是否在固定内存上,但是Paddle和Torch的pin_memory有一点不太一致。

Paddle 代码:

pin_memory_Tensor = paddle.to_tensor(1, place=paddle.CUDAPinnedPlace())
print(pin_memory_Tensor.place)
# Place(gpu_pinned)

Torch 代码:

x = torch.randn(4,4)
print(x.pin_memory().is_pinned())
# True
print(x.cuda().pin_memory().is_pinned())
# 会报错
# RuntimeError: cannot pin 'torch.cuda.FloatTensor' only dense CPU tensors can be pinned

即 Paddle 仅支持 CUDA Pin, 而 Torch 仅支持 dense CPU tensors to pin memory。

请问是否忽略这一点呢?

Copy link
Collaborator

Choose a reason for hiding this comment

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

这个点不用管,只需关注torch的代码转换为paddle如何呈现(前提是torch代码可正常运行),不关注其他的地方。上文中的 x.is_pinned() 如果转为 paddle的写法,'pinned' in x.place 这种写法有问题吗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的,可以通过 'pinned' in str(x.place) 去判断。

Copy link
Collaborator

Choose a reason for hiding this comment

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

这个已经给了你转写替代方案

| 304 | [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) | | 功能缺失 |
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

Choose a reason for hiding this comment

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

torch.sum_to_size 的实现是借助 sum_to 实现的其源码如下:

template <typename T>
inline Tensor _sum_to(
    Tensor tensor,
    const c10::ArrayRef<T> shape,
    bool always_return_non_view = false) {
  if (shape.size() == 0) {
    return tensor.sum();
  }

  auto sizes = at::symint::sizes<T>(tensor);
  c10::SmallVector<int64_t, 8> reduce_dims;
  const int64_t leading_dims = sizes.size() - shape.size();
  for (const auto i : c10::irange(leading_dims)) {
    reduce_dims.push_back(i);
  }
  for (int64_t i = leading_dims; i < static_cast<int64_t>(sizes.size()); ++i) {
    if (shape[i - leading_dims] == 1 && sizes[i] != 1) {
      reduce_dims.push_back(i);
    }
  }

  if (!reduce_dims.empty()) {
    tensor = tensor.sum(reduce_dims, /*keepdim=*/true);
  }

  if (always_return_non_view) {
    // This is only actually used by the functionalization pass.
    // We want to be able to guarantee that this function doesn't return a view
    // of the input.
    return leading_dims > 0 ? at::symint::view_copy<T>(tensor, shape)
                            : tensor.clone();
  } else {
    return leading_dims > 0 ? at::symint::view<T>(tensor, shape) : tensor;
  }
}

由于 Paddle 中没有 sum_to API 因此我不知道该如何去组合替代实现 sum_to_size.

Copy link
Collaborator

Choose a reason for hiding this comment

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

这个就先按功能缺失来吧

| 305 | [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