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.36】为 Paddle 代码转换工具新增 API 转换规则(第 3 组)2 -part #6890

Merged
merged 8 commits into from
Sep 29, 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,56 @@
## [ 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')
enkilee marked this conversation as resolved.
Show resolved Hide resolved
```

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

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

### 转写示例

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

# Paddle 写法
paddle.audio.functional.window._blackman(5, sym=False)
```

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

# Paddle 写法
x = paddle.audio.functional.window._blackman(5)
x.stop_gradient = False
```

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

# Paddle 写法
y = paddle.audio.functional.window._blackman(5)
y.cpu()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## [ 组合替代实现 ]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 写法
import array
enkilee marked this conversation as resolved.
Show resolved Hide resolved
a = array.array('b', [-1, 0, 0, 0])
torch.frombuffer(a, dtype=torch.int32)

# Paddle 写法
def paddle_frombuffer(buffer,dtype,count=-1,offset=0):
enkilee marked this conversation as resolved.
Show resolved Hide resolved
original = memoryview(buffer)
numpy_frombuffer = np.frombuffer(original, dtype=dtype, count=count, offset=offset)
paddle_tensor = paddle.to_tensor(numpy_frombuffer)
return paddle_tensor

import array
enkilee marked this conversation as resolved.
Show resolved Hide resolved
a = array.array('b', [-1, 0, 0, 0])
paddle_frombuffer(a, dtype='int32')
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## [ 功能缺失 ]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()
enkilee marked this conversation as resolved.
Show resolved Hide resolved
```

返回 CPU 上用于操作间并行的线程数 (例如,在 JIT 解释器中) 。

Paddle 当前无对应 API 功能。
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## [ 组合替代实现 ]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 写法
import multiprocessing
def get_num_threads():
enkilee marked this conversation as resolved.
Show resolved Hide resolved
device = paddle.device.get_device()
if 'cpu' in device:
return multiprocessing.cpu_count()

get_num_threads()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
## [ 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 | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| M | M | 输入窗口的长度。 |
| periodic | sym | 判断是否返回适用于过滤器设计的对称窗口,功能相反,需要转写。 |
| alpha | - | 窗函数中非线性部分的衰减速度,Paddle 无此参数,暂无转写方式。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个没有转写方式吗?看其他的窗函数std、alpha都能转写

Copy link
Contributor Author

Choose a reason for hiding this comment

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

paddle:

def _hamming(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
    """Compute a Hamming window.
    The Hamming window is a taper formed by using a raised cosine with
    non-zero endpoints, optimized to minimize the nearest side lobe.
    """
    return _general_hamming(M, 0.54, sym, dtype=dtype)

参数固定了。

| beta | - | 窗函数中线性部分的衰减速度,Paddle 无此参数,暂无转写方式。 |
| dtype | dtype | 返回 Tensor 的数据类型。 |
| layout | - | 表示布局方式, Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
| device | - | 表示 Tensor 存放设备位置,Paddle 无此参数,需要转写。 |
| requires_grad | - | 表示是否计算梯度, Paddle 无此参数,需要转写。 |

### 转写示例

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

# Paddle 写法
paddle.audio.functional.window._hamming(10, sym=False)
```

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

# Paddle 写法
x = paddle.audio.functional.window._hamming(10)
x.stop_gradient = False
```

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

# Paddle 写法
y = paddle.audio.functional.window._hamming(10)
y.cpu()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## [ 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 | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| M | M | 输入窗口的长度。 |
| periodic | sym | 判断是否返回适用于过滤器设计的对称窗口,功能相反,需要转写。 |
| dtype | dtype | 返回 Tensor 的数据类型。 |
| layout | - | 表示布局方式, Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
| device | - | 表示 Tensor 存放设备位置,Paddle 无此参数,需要转写。 |
| requires_grad | - | 表示是否计算梯度, Paddle 无此参数,需要转写。 |

### 转写示例

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

# Paddle 写法
paddle.audio.functional.window._hann(10, sym=False)
```

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

# Paddle 写法
x = paddle.audio.functional.window._hann(10)
x.stop_gradient = False
```

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

# Paddle 写法
y = paddle.audio.functional.window._hann(10)
y.cpu()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## [ 功能缺失 ]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 功能。
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## [ 参数默认值不一致 ]torch.set_num_threads
enkilee marked this conversation as resolved.
Show resolved Hide resolved

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

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

### [paddle.static.cpu_places](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/static/cpu_places_cn.html)

```python
paddle.static.cpu_places(device_count=None)
```

其中 PyTorch 和 Paddle 功能一致,参数默认值不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | -- |
| - | device_count | 要设置的 Cpu 线程数,参数默认值不一致,参数名不一致。 |