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 7th No.38】为Paddle代码转换工具新增API转换规则(第5组)-part #6885

Merged
merged 18 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [无参数]torch.Tensor.isneginf

### [torch.Tensor.isneginf ](https://pytorch.org/docs/stable/generated/torch.Tensor.isneginf.html#torch.Tensor.isneginf)

```python
torch.Tensor.isneginf()
```

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

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

两者功能一致,无参数。
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [无参数]torch.Tensor.isposinf

### [torch.Tensor.isposinf](https://pytorch.org/docs/stable/generated/torch.Tensor.isposinf.html#torch.Tensor.isposinf)

```python
torch.Tensor.isposinf()
```

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

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

两者功能一致,无参数。
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [无参数]torch.Tensor.isreal

### [torch.Tensor.isreal](https://pytorch.org/docs/stable/generated/torch.Tensor.isreal.html#torch.Tensor.isreal)

```python
Tensor.isreal()
```

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

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

两者功能一致,无参数。
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## [功能缺失]torch.Tensor.positive
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个想办法组合实现吧,简单的功能,不用判定为功能缺失。只有必须新增API 的才被认定为功能缺失


[torch.Tensor.positive](https://pytorch.org/docs/stable/generated/torch.Tensor.positive.html#torch.Tensor.positive)

```python
torch.Tensor.positive()
```

判断 `input` 是否是 bool 类型的 Tensor,如果是则抛出 RuntimeError 异常,否则返回 `input` 。

Paddle 当前无对应 API 功能。
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## [paddle 参数更多]torch.Tensor.scatter_reduce

### [torch.Tensor.scatter_reduce](https://pytorch.org/docs/stable/generated/torch.Tensor.scatter_reduce.html#torch-tensor-scatter-reduce)

```python
Tensor.scatter_reduce(dim, index, src, reduce, *, include_self=True)
Copy link
Collaborator

Choose a reason for hiding this comment

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

API签名要写全名:torch.Tensor.scatter_reduce

```

### [paddle.Tensor.put_along_axis](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#put-along-axis-indices-value-axis-reduce-assign-include-self-true-broadcast-true)

```python
paddle.Tensor.put_along_axis(indices, values, axis, reduce="assign", include_self=True, broadcast=True)
```

其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------ | ------------ | ------------------------------------------------------------ |
| dim | axis | 表示 scatter 的维度,仅参数名不一致。 |
| index | indices | 表示输入的索引张量,仅参数名不一致。 |
| src | values | 表示需要插入的值,仅参数名不一致。 |
| reduce | reduce | 表示插入 values 时的计算方式,参数默认值不一致。PyTorch 中该参数无默认值,需要输入,Paddle 中默认值为 `assign`,应设置为与 PyTorch 一致,其中 PyTorch 的 `prod` 对应 Paddle 中 `multiple`。 |
| include_self | include_self | 表示插入 values 时是否包含输入元素中的值。 |
| - | broadcast | 表示是否需要广播索引张量矩阵,PyTorch 无此参数,Paddle 应设置为 `False` 才与 PyTorch 一致 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## [输入参数类型不一致]torch.block_diag

### [torch.block_diag](https://pytorch.org/docs/stable/generated/torch.block_diag.html#torch-block-diag)

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

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

```python
paddle.block_diag(inputs, name=None)
```

二者功能一致但参数类型不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| -------- | ------------ | ------------------------------------------------------------ |
| *tensors | inputs | 一组输入 Tensor,PyTorch 参数 tensors 为可变参数,Paddle 参数 inputs 为 list(Tensor) 或 tuple(Tensor) 的形式。 |

### 转写示例

#### *tensors:一组输入 Tensor

```python
# PyTorch 写法
torch.block_diag(x, y, z)

# Paddle 写法
paddle.block_diag([x, y, z])
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## [功能缺失]torch.can_cast

### [torch.can_cast](https://pytorch.org/docs/stable/generated/torch.can_cast.html#torch-can-cast)

```python
torch.can_cast(from_, to)
```

判断类型的转换在 PyTorch 的[casting 规则](https://pytorch.org/docs/stable/tensor_attributes.html#type-promotion-doc)中是否被允许,暂未发现 Paddle 中有 api 能实现该功能。
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个有无办法组合实现?

简单的功能,不用判定为功能缺失。只有必须新增API 的才被认定为功能缺失

Paddle也是支持类型提升的 https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/advanced/auto_type_promotion_cn.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

需要写一个方法实现逻辑功能,在api_matcher中通过generate_aux_code方法实现转写,文档的转写示例里直接把方法写出来吗?另外tensor和paddle的类型提升支持范围有差异。
paddle_about_cast
torch_about_cast

Copy link
Collaborator

@zhwesky2010 zhwesky2010 Sep 24, 2024

Choose a reason for hiding this comment

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

需要写一个方法实现逻辑功能,在api_matcher中通过generate_aux_code方法实现转写,文档的转写示例里直接把方法写出来吗?另外tensor和paddle的类型提升支持范围有差异。 paddle_about_cast torch_about_cast

可以这样做,简单组合直接走辅助函数,然后辅助函数的逻辑也需要在映射文档中呈现

Copy link
Contributor Author

@inaomIIsfarell inaomIIsfarell Sep 24, 2024

Choose a reason for hiding this comment

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

  1. 从源码看,pytorch的无符号整数的类型支持uint8,uint16,uint32,uint64,但paddle关于无符号整数的类型只支持uint8;
  2. paddle.Tensor.dtype共支持12种数据类型,我个人想到实现的方式是通过字典存储各类型的转换关系,通过转换前后的类型参数直接索引(事实上pytorch也是这么实现的)。但是这样代码行数太多,写在映射文档种是否不太合适?

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## [输入参数类型不一致]torch.cartesian_prod

### [torch.cartesian_prod](https://pytorch.org/docs/stable/generated/torch.cartesian_prod.html#torch-cartesian-prod)

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

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

```python
paddle.cartesian_prod(x, name=None)
```

两者功能一致但参数类型不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| -------- | ------------ | ------------------------------------------------------------ |
| *tensors | x | 一组输入 Tensor , PyTorch 参数 tensors 为可变参, Paddle 参数 x 为 list(Tensor) 或 tuple(Tensor) 的形式。 |

### 转写示例

#### *tensors:一组输入 Tensor

```python
# PyTorch 写法
torch.cartesian_prod(a, b)

# Paddle 写法
paddle.cartesian_prod([a, b])
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## [torch 参数更多]torch.concatenate

### [torch.concatenate](https://pytorch.org/docs/stable/generated/torch.concatenate.html#torch-concatenate)

```python
torch.concatenate(tensors, dim=0, out=None)
```

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

```python
paddle.concat(x, axis=0, 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.

这个就按API alias别名来处理吧,配置在 https://github.com/PaddlePaddle/PaConvert/blob/master/paconvert/api_alias_mapping.json ,无需新增映射文档。

```

PyTorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射:

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ---------------------------------------------------------- |
| tensors | x | 待联结的 list(Tensor) 或者 tuple(Tensor),仅参数名不一致。 |
| dim | axis | 指定对输入进行运算的轴,仅参数名不一致。 |
| out | - | 表示输出的 Tensor,Paddle 无此参数,需要转写。 |

### 转写示例:

#### out:输出的 Tensor

```python
# PyTorch 写法:
torch.concatenate(x, y)

# Paddle 写法:
paddle.assign(paddle.concat(x), y)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.float_power

### [torch.float_power](https://pytorch.org/docs/stable/generated/torch.float_power.html#torch-float-power)

```python
torch.float_power(input, exponent, *, out=None)
```

Paddle 无此 API,需要组合实现。
Copy link
Collaborator

Choose a reason for hiding this comment

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

你这个需要写两个转写示例,先写 正常情况下如何组合实现,再写 out如何转写


### 转写示例

```python
# PyTorch 写法
torch.float_pow(x, y, out)
Copy link
Collaborator

Choose a reason for hiding this comment

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

API签名写错?


# Paddle 写法
paddle.assgin(x.cast(paddle.float64).pow(y), out)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## [仅参数名不一致]torch.isin

### [torch.isin](https://pytorch.org/docs/stable/generated/torch.isin.html#torch.isin)

```python
torch.isin(elements, test_elements, *, assume_unique=False, invert=False)
```

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

```python
paddle.isin(x, test_x, assume_unique=False, invert=False, name=None)
```

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

### 参数映射:

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------- | ------------------------------------------------ |
| elements | x | 输入的 tensor,仅参数名不一致。 |
| test_elements | test_x | 用于检测的 tensor,仅参数名不一致。 |
| assume_unique | assume_unique | 表示输入的 tensor 和用于检测的 tensor 元素是否唯一。 |
| invert | invert | 表示是否输出反转的结果。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## [torch 参数更多]torch.isneginf

### [torch.isneginf](https://pytorch.org/docs/stable/generated/torch.isneginf.html#torch-isneginf)

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

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

```python
paddle.isneginf(x, name=None)
```

Pytorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------ | ----------- | ------------------------------------------------ |
| input | x | 输入的 Tensor,仅参数名不一致。 |
| out | - | 表示输出的 Tensor , Paddle 无此参数,需要转写。 |

### 转写示例

#### out:指定输出

```python
# Pytorch 写法
torch.isneginf(a, out=b)

# Paddle 写法
paddle.assign(paddle.isneginf(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.

不要改参数名,上下对应,这样读者容易比对差异

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## [torch 参数更多]torch.isposinf

### [torch.isposinf](https://pytorch.org/docs/stable/generated/torch.isposinf.html#torch-isposinf)

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

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

```python
paddle.isposinf(x, name=None)
```

Pytorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------ | ----------- | ------------------------------------------------ |
| input | x | 输入的 Tensor,仅参数名不一致。 |
| out | - | 表示输出的 Tensor , Paddle 无此参数,需要转写。 |

### 转写实例

#### out 参数:指定输出

```python
# Pytorch 写法
torch.isposinf(a, out=b)

# Paddle 写法
paddle.assign(paddle.isposinf(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.

不要改参数名,上下对应,这样读者容易比对差异

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## [仅参数名不一致]torch.isreal

### [torch.isreal](https://pytorch.org/docs/stable/generated/torch.isreal.html#torch-isreal)

```python
torch.isreal(input)
```

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

```python
paddle.isreal(x, name=None)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ---------------------------------- |
| input | x | 表示输入的 Tensor,仅参数名不一致。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## [功能缺失]torch.positive

### [torch.positive](https://pytorch.org/docs/stable/generated/torch.positive.html#torch-positive)

```python
torch.positive(input)
```

判断 `input` 是否是 bool 类型的 Tensor,如果是则抛出 RuntimeError 异常,否则返回 `input` 。
Copy link
Collaborator

Choose a reason for hiding this comment

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

同上,简单的功能尽可能找方法去组合实现,只有必须要Paddle 新增API的才计做功能缺失


Paddle 当前无对应 API 功能。
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## [paddle 参数更多]torch.scatter_reduce

### [torch.scatter_reduce](https://pytorch.org/docs/stable/generated/torch.scatter_reduce.html#torch-scatter-reduce)

```python
torch.scatter_reduce(input, dim, index, src, reduce, *, include_self=True)
```

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

```python
paddle.put_along_axis(arr, indices, values, axis, reduce='assign', include_self=True, broadcast=True)
```

其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------ | ------------ | ------------------------------------------------------------ |
| input | arr | 表示输入 Tensor,仅参数名不一致。 |
| dim | axis | 表示沿着哪个维度 scatter,仅参数名不一致。 |
| index | indices | 表示输入的索引张量,仅参数名不一致。 |
| src | values | 表示要插入的值,仅参数名不一致。 |
| reduce | reduce | 表示插入 values 时的计算方式,参数默认值不一致。PyTorch 中该参数无默认值,需要输入,Paddle 中默认值为 `assign`,应设置为与 PyTorch 一致,其中 PyTorch 的 `prod` 对应 Paddle 中 `multiple`。 |
| include_self | include_self | 表示插入 values 时是否包含输入 Tensor 中的元素。 |
| - | broadcast | 表示是否需要广播输入的索引张量,PyTorch 无此参数,Paddle 应设置为 `False` 结果才与 pytorch 一致。 |