Skip to content

Commit

Permalink
add sinc etc. (#5894)
Browse files Browse the repository at this point in the history
* model_convert add torch.is_nonzero .etc

* model_convert add is_nonzero .etc

* model_convert add is_nonzero .etc

* model_convert add is_nonzero .etc

* add xlogy.etc

* add randn_like .etc

* comment

* add vdot etc.

* add aminmax etc.

* fix parameter

* add bucketizr etc.

* add bucketizr etc.

* add bucketizr etc.

* add sinc etc.

* add sinc etc.

* add sinc etc.
  • Loading branch information
zpceng314 authored May 29, 2023
1 parent 38e205a commit c599bdf
Show file tree
Hide file tree
Showing 20 changed files with 423 additions and 43 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## [ 参数完全一致 ]torch.Tensor.histc
## [ 参数不一致 ]torch.Tensor.histc

### [torch.Tensor.histc](https://pytorch.org/docs/1.13/generated/torch.Tensor.histc.html?highlight=torch+tensor+histc#torch.Tensor.histc)

Expand All @@ -12,7 +12,7 @@ torch.Tensor.histc(bins=100, min=0, max=0)
paddle.Tensor.histogram(bins=100, min=0, max=0, name=None)
```

两者功能一致且参数完全一致
返回 Tensor 的数据类型不一致,Pytorch 返回数据类型与输入 Tensor 一致, Paddle 默认返回 int64 类型

### 参数映射

Expand All @@ -21,3 +21,13 @@ paddle.Tensor.histogram(bins=100, min=0, max=0, name=None)
| <font color='red'> bins </font> | <font color='red'> bins </font> | 直方图 bins(直条)的个数,默认为 100。 |
| <font color='red'> min </font> | <font color='red'> min </font> | range 的下边界(包含),默认为 0。 |
| <font color='red'> max </font> | <font color='red'> max </font> | range 的上边界(包含),默认为 0。 |

### 转写示例

```python
# Pytorch 写法
y = a.histc(bins=3, min=2, max=4)

# Paddle 写法
y = a.histogram(bins=3, min=2, max=4).astype(a.dtype)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.Tensor.hypot

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

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

给定直角三角形的直角边,求斜边; Paddle 无此 API,需要组合实现。

### 转写示例

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

# Paddle 写法
y = (a**2 + b**2) ** (1/2)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## [ 参数完全一致 ]torch.Tensor.istft
### [torch.Tensor.istft](https://pytorch.org/docs/stable/generated/torch.Tensor.istft.html#torch.Tensor.istft)

```python
torch.Tensor.istft(n_fft, hop_length=None, win_length=None, window=None, center=True, normalized=False, onesided=None, length=None, return_complex=False)
```

### [paddle.signal.istft](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/signal/istft_cn.html#istft)

```python
paddle.signal.istft(x,
n_fft,
hop_length=None,
win_length=None,
window=None,
center=True,
normalized=False,
onesided=True,
length=None,
return_complex=False,
name=None)
```

两者功能一致且参数完全一致,具体如下:
### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| n_fft | n_fft | 表示离散傅里叶变换的样本点个数。 |
| hop_length | hop_length | 表示相邻两帧偏移的样本点个数。 |
| win_length | win_length | 表示信号窗的长度。 |
| window | window | 表示长度为 win_length 的 Tensor 。 |
| center | center | 表示是否将输入信号进行补长。 |
| normalized | normalized | 表示是否将傅里叶变换的结果乘以值为 1/sqrt(n) 的缩放系数。 |
| onesided | onesided | 表示是否返回一个实信号。 |
| length | length | 表示输出信号的长度。 |
| return_complex | return_complex | 表示输出的重构信号是否为复信号。 |
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
## [ 无参数 ] torch.Tensor.logdet
## [ 组合替代实现 ]torch.Tensor.logdet

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

```python
torch.Tensor.logdet()
```
Paddle 无此 API,需要组合实现。

### [paddle.linalg.slogdet](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/linalg/slogdet_cn.html)
### 转写示例

```python
paddle.linalg.slogdet(x)
```
# Pytorch 写法
y = input.logdet()

两者功能一致,用于计算矩阵的对数行列式。
# Paddle 写法
y = paddle.log(paddle.linalg.det(input))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## [ 仅 Paddle 参数更多 ]torch.Tensor.pinverse
### [torch.Tensor.pinverse](https://pytorch.org/docs/stable/generated/torch.Tensor.pinverse.html#torch.Tensor.pinverse)

```python
torch.Tensor.pinverse()
```

### [paddle.linalg.pinv](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/linalg/pinv_cn.html#pinv)

```python
paddle.linalg.pinv(x,
rcond=1e-15,
hermitian=False,
name=None)
```

其中 Paddle 相比 Pytorch 支持更多参数,具体如下:
### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| - | rcond | 奇异值(特征值)被截断的阈值,Pytorch 无此参数,Paddle 保持默认即可。 |
| - | hermitian | 是否为 hermitian 矩阵或者实对称矩阵,Pytorch 无此参数,Paddle 保持默认即可。|
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.Tensor.reshape_as

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

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

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

### 转写示例

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

# Paddle 写法
y = paddle.reshape(a, b.shape)
```
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
## [ 仅参数名不一致 ]torch.Tensor.select
## [ 组合替代实现 ]torch.Tensor.select

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

```python
torch.Tensor.select(dim, index)
```

### [paddle.index_select](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#index-select-index-axis-0-name-none)
Paddle 无此 API,需要组合实现。

```python
paddle.Tensor.index_select(index, axis=0, name=None)
```

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

### 参数映射
```python
# Pytorch 写法
y = a.select(dim=dim, index=index)

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| dim | axis | 指定进行运算的轴,仅参数名不同。 |
| index | index | 包含索引下标的 1-D Tensor |
# Paddle 写法
y = paddle.index_select(a, index=paddle.to_tensor([index]), axis=dim).squeeze(dim)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## [ 组合替代实现 ]torch.Tensor.sinc

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

```python
torch.Tensor.sinc()
```

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

### 转写示例

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

# Paddle 写法
import numpy
y = paddle.where(a==0, x=paddle.to_tensor([1], dtype=a.dtype), y=paddle.sin(numpy.pi*a)/(numpy.pi*a))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## [ 参数不一致 ]torch.linalg.slogdet
### [torch.linalg.slogdet](https://pytorch.org/docs/stable/generated/torch.linalg.slogdet.html#torch.linalg.slogdet)

```pythonpa
torch.linalg.slogdet(A, *, out=None)
```

### [paddle.linalg.slogdet](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/linalg/slogdet_cn.html#slogdet)

```python
paddle.linalg.slogdet(x)
```

两者功能一致但参数类型不一致,Pytorch 返回 named tuple,Paddle 返回 Tensor,需要转写。具体如下:
### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| <font color='red'> A </font> | <font color='red'> x </font> | 表示输入的 Tensor ,仅参数名不一致。 |
| <font color='red'> out </font> | - | 表示输出的 Tuple ,Paddle 无此参数,暂无转写方式。 |


### 转写示例
#### 返回类型不一致
```python
# Pytorch 写法
y = torch.linalg.slogdet(a)

# Paddle 写法
result = paddle.linalg.slogdet(a)
y = tuple([result[0], result[1]])
```
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ paddle.nn.LayerNorm(normalized_shape,
torch.nn.LayerNorm(normalized_shape=(256, 256), eps=1e-05, elementwise_affine=False)

# paddle 写法
paddle.nn.GroupNorm(normalized_shape=(256, 256), epsilon=1e-05, weight_attr=paddle.ParamAttr(learning_rate=0.0), bias_attr=paddle.ParamAttr(learning_rate=0.0))
paddle.nn.GroupNorm(normalized_shape=(256, 256), epsilon=1e-05, weight_attr=False, bias_attr=False)

# 当 PyTorch 的 elementwise_affine 为`True`,torch 写法
torch.nn.LayerNorm(normalized_shape=(256, 256), eps=1e-05, elementwise_affine=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
## [ 参数不一致 ]torch.nn.SyncBatchNorm
### [torch.nn.SyncBatchNorm](https://pytorch.org/docs/stable/generated/torch.nn.SyncBatchNorm.html#torch.nn.SyncBatchNorm)

```python
torch.nn.SyncBatchNorm(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True, process_group=None, device=None, dtype=None)
```

### [paddle.nn.SyncBatchNorm](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/nn/SyncBatchNorm_cn.html#syncbatchnorm)

```python
paddle.nn.SyncBatchNorm(num_features, epsilon=1e-5, momentum=0.9, weight_attr=None, bias_attr=None, data_format='NCHW', name=None)
```

两者功能一致但参数不一致,部分参数名不同,具体如下:
### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| num_features | num_features | 表示输入 Tensor 通道数。 |
| eps | epsilon | 为了数值稳定加在分母上的值。 |
| momentum | momentum | 表示归一化函数中的超参数, PyTorch 和 Paddle 公式实现细节不一致,两者正好是相反的,需要进行转写。 |
| - | weight_attr | 指定权重参数属性的对象。如果为 False, 则表示每个通道的伸缩固定为 1,不可改变。默认值为 None,表示使用默认的权重参数属性。 |
| - | bias_attr | 指定偏置参数属性的对象。如果为 False, 则表示每一个通道的偏移固定为 0,不可改变。默认值为 None,表示使用默认的偏置参数属性。 |
| - | data_format | 指定输入数据格式, Pytorch 无此参数,Paddle 保持默认即可。 |
| affine | - | 是否进行反射变换, PaddlePaddle 无此参数,需要进行转写。 |
| track_running_stats | use_global_stats | 表示是否已加载的全局均值和方差。 |
| process_group | - | 统计信息的同步分别在每个进程组内发生, PaddlePaddle 无此参数,暂无转写方式。 |
| device | - | 设备类型,PaddlePaddle 无此参数。可直接删除。 |
| dtype | - | 参数类型,PaddlePaddle 无此参数。需要转写 |

### 转写示例
#### affine:是否进行反射变换
```python
affine=False 时,表示不更新:

# PyTorch 写法
m = torch.nn.SyncBatchNorm(24, affine=False)

# Paddle 写法
m = paddle.nn.SyncBatchNorm(24, weight_attr=False, bias_attr=False)

affine=True 时,表示更新:

# PyTorch 写法
m = torch.nn.SyncBatchNorm(24)

# Paddle 写法
m = paddle.nn.SyncBatchNorm(24)
```

#### momentum:
```python
# PyTorch 写法
m = torch.nn.SyncBatchNorm(24, momentum=0.2)

# Paddle 写法
m = paddle.nn.SyncBatchNorm(24, momentum=0.8)
```

#### dtype:
```python
# PyTorch 写法
m = torch.nn.SyncBatchNorm(24, momentum=0.2dtype=torch.float32)
y = m(x)

# Paddle 写法
m = paddle.nn.SyncBatchNorm(24, momentum=0.8)
y = m(x).astype(paddle.float32)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.hypot

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

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

给定直角三角形的直角边,求斜边; Paddle 无此 API,需要组合实现。

### 转写示例

```python
# Pytorch 写法
y = torch.hypot(a, b)

# Paddle 写法
y = (a**2 + b**2) ** (1/2)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.select

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

```python
torch.select(input, dim, index)
```

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

### 转写示例

```python
# Pytorch 写法
y = torch.select(a, dim=dim, index=index)

# Paddle 写法
y = paddle.index_select(a, index=paddle.to_tensor([index]), axis=dim).squeeze(dim)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## [ 组合替代实现 ]torch.sinc

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

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

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

### 转写示例

```python
# Pytorch 写法
y = torch.sinc(a)

# Paddle 写法
import numpy
y = paddle.where(a==0, x=paddle.to_tensor([1], dtype=a.dtype), y=paddle.sin(numpy.pi*a)/(numpy.pi*a))
```
Loading

0 comments on commit c599bdf

Please sign in to comment.