Skip to content

Commit

Permalink
【Hackathon 7th No.36】为 Paddle 代码转换工具新增 API 转换规则(第 3 组)- part2 (#6890)
Browse files Browse the repository at this point in the history
* fix temp

* fix

* fix

* fix

* fix

* fix

* fix

* fix
  • Loading branch information
enkilee authored Sep 29, 2024
1 parent a1e39a0 commit 8887e8d
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## [ torch 参数更多 ] torch.blackman_window

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

```python
torch.blackman_window(window_length, periodic=True, *, dtype=None, layout=torch.strided, device=None, requires_grad=False)
```

### [paddle.audio.functional.get_window](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.6/api/paddle/audio/functional/get_window_cn.html#get-window)

```python
paddle.audio.functional.get_window(window, win_length, fftbins=True, dtype='float64')
```

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

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| - | window | 窗函数类型,Pytorch 无此参数,Paddle 需设置为 `blackman`|
| window_length | win_length | 输入窗口的长度,仅参数名不同。 |
| periodic | fftbins | 判断是否返回适用于过滤器设计的对称窗口,功能相反,Pytorch 默认值为 True 时,Paddle 须设置为 False,需要转写。 |
| dtype | dtype | 返回 Tensor 的数据类型。 |
| layout | -| 表示布局方式, Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
| device | - | 表示 Tensor 存放设备位置,Paddle 无此参数,需要转写。 |
| requires_grad | - | 表示是否计算梯度, Paddle 无此参数,需要转写。 |

### 转写示例

#### window:窗函数类型
```python
# PyTorch 写法
torch.blackman_window(5)

# Paddle 写法
paddle.audio.functional.get_window('blackman', 5, fftbins = False)
```

#### periodic:判断是否返回适用于过滤器设计的对称窗口
```python
# PyTorch 写法
torch.blackman_window(5, periodic = False)

# Paddle 写法
paddle.audio.functional.get_window('blackman', 5, fftbins = True)
```

#### requires_grad:是否需要求反向梯度,需要修改该 Tensor 的 stop_gradient 属性
```python
# PyTorch 写法
torch.blackman_window(5, requires_grad = True)

# Paddle 写法
x = paddle.audio.functional.get_window('blackman', 5, fftbins = False)
x.stop_gradient = False
```

#### device: Tensor 的设备
```python
# PyTorch 写法
torch.blackman_window(5, device = torch.device('cpu'))

# Paddle 写法
y = paddle.audio.functional.get_window('blackman', 5, fftbins = False)
y.cpu()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.frombuffer

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

```python
torch.frombuffer(buffer, *, dtype, count=-1, offset=0, requires_grad=False)
```

把 Python 缓冲区创建的的对象变成一维 Tensor,Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch.frombuffer(a, dtype=torch.int32)

# Paddle 写法
paddle.to_tensor(np.frombuffer(a, dtype='int32'))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.get_num_interop_threads

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

```python
torch.get_num_interop_threads()
```

返回 CPU 上用于操作间并行的线程数 (例如,在 JIT 解释器中),Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch.get_num_interop_threads()

# Paddle 写法
os.environ['OMP_NUM_THREADS']
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.get_num_threads

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

```python
torch.get_num_threads()
```

返回用于并行化 CPU 操作的线程数,Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch.get_num_threads()

# Paddle 写法
os.environ['CPU_NUM']
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
## [ torch 参数更多 ]torch.hamming_window
### [torch.hamming_window](https://pytorch.org/docs/stable/generated/torch.hamming_window.html)

```python
torch.hamming_window(window_length, periodic=True, alpha=0.54, beta=0.46, *, dtype=None, layout=torch.strided, device=None, requires_grad=False)
```

### [paddle.audio.functional.get_window](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.6/api/paddle/audio/functional/get_window_cn.html#get-window)

```python
paddle.audio.functional.get_window(window, win_length, fftbins=True, dtype='float64')
```

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

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| - | window | 窗函数类型,Pytorch 无此参数,Paddle 需设置为 `hamming`|
| window_length | win_length | 输入窗口的长度,仅参数名不同。 |
| periodic | fftbins | 判断是否返回适用于过滤器设计的对称窗口,功能相反,Pytorch 默认值为 True 时,Paddle 须设置为 False,需要转写。 |
| alpha | - | 窗函数中非线性部分的衰减速度,Paddle 无此参数,暂无转写方式。 |
| beta | - | 窗函数中线性部分的衰减速度,Paddle 无此参数,暂无转写方式。 |
| dtype | dtype | 返回 Tensor 的数据类型。 |
| layout | - | 表示布局方式, Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
| device | - | 表示 Tensor 存放设备位置,Paddle 无此参数,需要转写。 |
| requires_grad | - | 表示是否计算梯度, Paddle 无此参数,需要转写。 |

### 转写示例

#### window:窗函数类型
```python
# PyTorch 写法
torch.hamming_window(10, periodic = True)

# Paddle 写法
paddle.audio.functional.get_window('hamming', 10, fftbins = False)
```

#### periodic:判断是否返回适用于过滤器设计的对称窗口
```python
# PyTorch 写法
torch.hamming_window(10, periodic = False)

# Paddle 写法
paddle.audio.functional.get_window('hamming', 10, fftbins = True)
```

#### requires_grad:是否需要求反向梯度,需要修改该 Tensor 的 stop_gradient 属性
```python
# PyTorch 写法
torch.hamming_window(10, requires_grad=True)

# Paddle 写法
x = paddle.audio.functional.get_window('hamming', 10, fftbins = False)
x.stop_gradient = False
```

#### device: Tensor 的设备
```python
# PyTorch 写法
torch.hamming_window(10, device=torch.device('cpu'))

# Paddle 写法
y = paddle.audio.functional.get_window('hamming', 10, fftbins = False)
y.cpu()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## [ torch 参数更多 ]torch.hann_window
### [torch.hann_window](https://pytorch.org/docs/stable/generated/torch.hann_window.html)

```python
torch.hann_window(window_length, periodic=True, *, dtype=None, layout=torch.strided, device=None, requires_grad=False)
```

### [paddle.audio.functional.get_window](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.6/api/paddle/audio/functional/get_window_cn.html#get-window)

```python
paddle.audio.functional.get_window(window, win_length, fftbins=True, dtype='float64')
```

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

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| - | window | 窗函数类型,Pytorch 无此参数,Paddle 需设置为 `hann`|
| window_length | win_length | 输入窗口的长度,仅参数名不同。 |
| periodic | fftbins | 判断是否返回适用于过滤器设计的对称窗口,功能相反,Pytorch 默认值为 True 时,Paddle 须设置为 False,需要转写。 |
| dtype | dtype | 返回 Tensor 的数据类型。 |
| layout | - | 表示布局方式, Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
| device | - | 表示 Tensor 存放设备位置,Paddle 无此参数,需要转写。 |
| requires_grad | - | 表示是否计算梯度, Paddle 无此参数,需要转写。 |

### 转写示例

#### window:窗函数类型
```python
# PyTorch 写法
torch.hann_window(5)

# Paddle 写法
paddle.audio.functional.get_window('hann', 5, fftbins = False)
```

#### periodic:判断是否返回适用于过滤器设计的对称窗口
```python
# PyTorch 写法
torch.blackman_window(10, periodic = False)

# Paddle 写法
paddle.audio.functional.get_window('hann', 10, fftbins = True)
```

#### requires_grad:是否需要求反向梯度,需要修改该 Tensor 的 stop_gradient 属性
```python
# PyTorch 写法
torch.hann_window(10, requires_grad=True)

# Paddle 写法
x = paddle.audio.functional.get_window('hann', 10, fftbins = False)
x.stop_gradient = False
```

#### device: Tensor 的设备
```python
# PyTorch 写法
torch.hann_window(10, device=torch.device('cpu'))

# Paddle 写法
y = paddle.audio.functional.get_window('hann', 10, fftbins = False)
y.cpu()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.set_num_interop_threads

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

```python
torch.set_num_interop_threads()
```

设置 CPU 上用于操作间并行的线程数 (例如,在 JIT 解释器中),Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch.set_num_interop_threads(2)

# Paddle 写法
os.environ['OMP_NUM_THREADS'] = '2'
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.set_num_threads

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

```python
torch.set_num_threads(int)
```

设置用于 CPU 上的内部操作并行性的线程数,Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch.set_num_threads(2)

# Paddle 写法
os.environ['CPU_NUM'] = '2'
```

0 comments on commit 8887e8d

Please sign in to comment.