Skip to content

Commit

Permalink
【Hackathon 7th No.35】为 Paddle 代码转换工具新增 API 转换规则(第 2 组)-part (#6880)
Browse files Browse the repository at this point in the history
* 【Hackathon 7th No.35】为 Paddle 代码转换工具新增 API 转换规则(第 2 组)

* update some api

* fix codestyle

* change paddle.assign to inplace api
  • Loading branch information
ccsuzzh authored Sep 29, 2024
1 parent 07f9137 commit cbbc029
Show file tree
Hide file tree
Showing 16 changed files with 356 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## [ 组合替代实现 ]torch.Tensor.addbmm_

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

```python
torch.Tensor.addbmm_(batch1, batch2, *, beta=1, alpha=1)
```

用于实现矩阵 `batch1` 与矩阵 `batch2` 相乘,将结果按 `axis=0` 求和之后与`alpha`相乘,再加上输入 `input``beta`,公式为:

$$
out = \beta \, input + \alpha \left( \sum_{i=0}^{b-1} batch1_i \, @ \, batch2_i \right)
$$

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

### 转写示例

```python
# PyTorch 写法
input.addbmm_(batch1, batch2, beta=beta, alpha=alpha)

# Paddle 写法
input.multiply_(paddle.to_tensor(beta, dtype=input.dtype)).add_(alpha * paddle.sum(paddle.bmm(batch1, batch2), axis=0))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## [ 组合替代实现 ]torch.Tensor.addcdiv_

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

```python
torch.Tensor.addcdiv_(tensor1, tensor2, *, value=1)
```

用于实现矩阵 `tensor1` 与矩阵 `tensor2` 相除,再加上输入 `input` ,公式为:

$ out = input + value * (tensor1 / tensor2) $

PaddlePaddle 目前无对应 API,可使用如下代码组合实现该 API。

### 转写示例

```python
# PyTorch 写法
input.addcdiv_(tensor1, tensor2, value=value)

# Paddle 写法
input.add_(value * tensor1 / tensor2)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## [ 组合替代实现 ]torch.Tensor.addmv_

### [torch.Tensor.addmv_](https://pytorch.org/docs/stable/generated/torch.Tensor.addmv_.html#torch.Tensor.addmv_)
```python
torch.Tensor.addmv_(mat, vec, beta=1, alpha=1, out=None)
```

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

### 转写示例

```python
# PyTorch 写法
input.addmv_(mat, vec, beta=beta, alpha=alpha)

# Paddle 写法
input.multiply_(paddle.to_tensor(beta, dtype=input.dtype)).add_(alpha * paddle.mm(mat, vec))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.Tensor.addr_

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

```python
torch.Tensor.addr_(vec1, vec2, beta=1, alpha=1)
```

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

### 转写示例

```python
# PyTorch 写法
y = input.addr_(vec1, vec2, beta=beta, alpha=alpha)

# Paddle 写法
input.multiply_(paddle.to_tensor(beta, dtype=input.dtype)).add_(alpha * paddle.outer(vec1, vec2))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## [ 组合替代实现 ]torch.Tensor.baddbmm_

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

```python
torch.Tensor.baddbmm_(batch1, batch2, beta=1, alpha=1)
```
Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
input.baddbmm_(batch1, batch2, beta=beta, alpha=alpha)

# Paddle 写法
input.multiply_(paddle.to_tensor(beta, dtype=input.dtype)).add_(alpha * paddle.bmm(batch1, batch2))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## [ 输入参数类型不一致 ] torch.Tensor.copysign

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

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

### [paddle.Tensor.copysign]()

```python
paddle.Tensor.copysign(y, name=None)
```

其中,PyTorch 与 Paddle 的 `other` 参数所支持类型不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ----------------------------- |
| other | y | 表示输入的 Tensor ,PyTorch 支持 Python Number 和 Tensor 类型, Paddle 仅支持 Tensor 类型。当输入为 Python Number 类型时,需要转写。 |

### 转写示例
#### other
```python
# PyTorch 写法
result = x.copysign(other=2.)

# Paddle 写法
result = x.copysign(y=paddle.to_tensor(2.))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## [ 输入参数类型不一致 ] torch.Tensor.copysign_

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

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

### [paddle.Tensor.copysign_]()

```python
paddle.Tensor.copysign_(y, name=None)
```

其中,PyTorch 与 Paddle 的 `other` 参数所支持类型不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ----------------------------- |
| other | y | 表示输入的 Tensor ,PyTorch 支持 Python Number 和 Tensor 类型, Paddle 仅支持 Tensor 类型。当输入为 Python Number 类型时,需要转写。 |

### 转写示例
#### other
```python
# PyTorch 写法
result = x.copysign_(other=2.)

# Paddle 写法
result = x.copysign_(y=paddle.to_tensor(2.))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.Tensor.erfc_

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

```python
torch.Tensor.erfc_()
```

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

### 转写示例

```python
# PyTorch 写法
x.erfc_()

# Paddle 写法
paddle.erf_(x).multiply_(paddle.to_tensor(-1.)).add_(paddle.to_tensor(1.))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [ 无参数 ]torch.Tensor.fix_

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

```python
torch.Tensor.fix_()
```

### [paddle.Tensor.trunc_]()

```python
paddle.Tensor.trunc_()
```

两者功能一致,均无参数。
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## [ 输入参数类型不一致 ]torch.Tensor.fmod_

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

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

### [paddle.Tensor.mod_]()

```python
paddle.Tensor.mod_(y, name=None)
```

其中,PyTorch 与 Paddle 的 `other` 参数所支持类型不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ----------------------------- |
| other | y | 多维 Tensor,PyTorch 支持 Tensor 和 Python Number,Paddle 仅支持 Tensor,需要转写。 |

### 转写示例
#### other
```python
# PyTorch 写法
x.fmod_(other=2.)

# Paddle 写法
x.mod_(y=paddle.to_tensor(2.))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## [ 功能缺失 ] torch.Tensor.int_repr

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

```python
torch.Tensor.int_repr()
```

### []()

Paddle 当前无对应功能,功能缺失。
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## [ 输入参数类型不一致 ] torch.Tensor.mul_

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

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

### [paddle.Tensor.multiply_]()

```python
paddle.Tensor.multiply_(y,
axis=-1,
name=None)
```

其中,Paddle 与 PyTorch 的 `other` 参数所支持类型不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ----------------------------------------------- |
| other | y | 相乘的元素,PyTorch 支持 Tensor 和 Python Number,Paddle 仅支持 Tensor,需要转写。 |
| - | axis | 计算的维度,PyTorch 无此参数, Paddle 保持默认即可。|

### 转写示例
#### other:相乘的元素
```python
# PyTorch 写法
x.mul_(other=2)

# Paddle 写法
x.multiply_(y=paddle.to_tensor(2))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [ 无参数 ]torch.Tensor.sinc_

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

```python
torch.Tensor.sinc_()
```

### [paddle.Tensor.sinc_]()

```python
paddle.Tensor.sinc_()
```

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

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

```python
torch.Tensor.t_()
```

### [paddle.Tensor.t_]()

```python
paddle.Tensor.t_()
```

两者功能一致,无参数。
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## [ 输入参数用法不一致 ]torch.Tensor.transpose_

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

```python
torch.Tensor.transpose_(dim0, dim1)
```

### [paddle.Tensor.transpose_]()

```python
paddle.Tensor.transpose_(perm, name=None)
```

PyTorch 的 `dim0, dim1` 与 Paddle 的 `perm` 用法不同,具体如下:
### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| dim0, dim1 | perm | torch 的 dim0 与 dim1 表示要交换的两个维度, 为整数。 paddle 的 perm 表示重排的维度序列,为 list/tuple 。需要转写。|

### 转写示例
#### dim0, dim1: 表示要交换的两个维度
```python
# pytorch
x = torch.randn(2, 3, 5)
x.transpose_(0, 1)

# paddle
x = paddle.randn([2, 3, 5])
x.transpose_(perm=[1, 0, 2])
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.Tensor.xlogy_

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

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

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

### 转写示例

```python
# PyTorch 写法
a.xlogy_(b)

# Paddle 写法
a.multiply_(paddle.log(paddle.to_tensor(b)))
```

0 comments on commit cbbc029

Please sign in to comment.