Skip to content

device类文档更新 #7244

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

Merged
merged 16 commits into from
Apr 27, 2025
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## [torch 参数更多]torch.Tensor.cuda
## [输入参数类型不一致]torch.Tensor.cuda

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

@@ -12,12 +12,41 @@ torch.Tensor.cuda(device=None, non_blocking=False, memory_format=torch.preserve_
paddle.Tensor.cuda(device_id=None, blocking=False)
```

PyTorch 相比 Paddle 支持更多其他参数,具体如下:
两者功能一致但参数类型不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------------------------ |
| device | device_id | 目标 GPU 设备,仅参数名不一致。 |
| device | device_id | 目标 GPU 设备,输入参数类型不一致,需要转写。 |
| non_blocking | blocking | 是否同步或异步拷贝,PyTorch 和 Paddle 取值相反,需要转写。 |
| memory_format | - | 表示内存格式, Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |


### 转写示例

#### non_blocking: 同步或异步拷贝

```python
# PyTorch 写法
tensor.cuda(non_blocking=True)

# Paddle 写法
tensor.cuda(blocking=False)
```

#### device: 目标 GPU 设备

```python
# PyTorch 写法
tensor.cuda("cuda:0")

# Paddle 写法
tensor.cuda(0)

# PyTorch 写法
tensor.cuda(0)

# Paddle 写法
tensor.cuda(0)
```
Original file line number Diff line number Diff line change
@@ -29,3 +29,27 @@ torch.tensor([-1, -2, 3]).size(0)
# Paddle 写法
paddle.to_tensor([-0.4, -0.2, 0.1, 0.3]).shape[0]
```

```python
# PyTorch 写法
torch.ones(10, 20, 30).size().count(30)

# Paddle 写法
tuple(paddle.ones([10, 20, 30]).shape).count(30)
```

```python
# PyTorch 写法
torch.ones(10, 20, 30).size().index(30, 1)

# Paddle 写法
tuple(paddle.ones([10, 20, 30]).shape).index(30, 1)
```

```python
# PyTorch 写法
torch.ones(10, 20, 30).size().numel()

# Paddle 写法
paddle.ones([10, 20, 30]).numel().item()
```
Original file line number Diff line number Diff line change
@@ -27,12 +27,24 @@ paddle.device.Stream(device=None, priority=None)

```python
# PyTorch 写法
high_priority = -1
default_priority = 0
y = torch.cuda.Stream(priority=default_priority)
torch.cuda.Stream(priority=0)

# Paddle 写法
high_priority = 1
default_priority = 2
y = paddle.device.Stream(priority=default_priority)
paddle.device.Stream(priority=2)
```

#### device: 希望分配 stream 的设备

```python
# PyTorch 写法
torch.cuda.Stream('cuda:0')

# Paddle 写法
paddle.device.Stream('gpu:0')

# PyTorch 写法
torch.cuda.Stream(2)

# Paddle 写法
paddle.device.Stream(device='gpu:2')
```
Original file line number Diff line number Diff line change
@@ -6,10 +6,10 @@
torch.cuda.current_device()
```

### [paddle.framework._current_expected_place]()
### [paddle.get_device](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/device/get_device_cn.html#get-device)

```python
paddle.framework._current_expected_place()
paddle.get_device()
```

功能一致,无参数。
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
## [参数完全一致]torch.cuda.current_stream
## [输入参数类型不一致]torch.cuda.current_stream

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

```python
torch.cuda.current_stream(device=None)
```

### [paddle.device.cuda.current_stream](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/device/cuda/current_stream_cn.html)
### [paddle.device.current_stream](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/device/cuda/current_stream_cn.html)

```python
paddle.device.cuda.current_stream(device=None)
paddle.device.current_stream(device=None)
```

功能一致,参数完全一致,具体如下:
功能一致,参数类型不一致,具体如下:
### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| device | device | 表示希望获取 stream 的设备或者设备 ID。如果为 None,则为当前的设备。默认值为 None。 |
| device | device | 表示希望获取 stream 的设备或者设备 ID。如果为 None,则为当前的设备。默认值为 None,需要转写。 |

### 转写示例
#### device: 特定的运行设备

```python
# PyTorch 写法
torch.cuda.current_stream('cuda:0')

# Paddle 写法
paddle.device.current_stream('gpu:0')

# PyTorch 写法
torch.cuda.current_stream(2)

# Paddle 写法
paddle.device.current_stream('gpu:2')

# PyTorch 写法
torch.cuda.current_stream()

# Paddle 写法
paddle.device.current_stream()
```
Original file line number Diff line number Diff line change
@@ -6,10 +6,10 @@
torch.cuda.device(device)
```

### [paddle.CUDAPlace](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/CUDAPlace_cn.html)
### [paddle.device._convert_to_place](https://github.com/PaddlePaddle/Paddle/blob/c8ccc9b154632ef41ade1b8e97b87d54fde7e8f8/python/paddle/device/__init__.py#L174)

```python
paddle.CUDAPlace(id)
paddle.device._convert_to_place(device)
```

其中 PyTorch 与 Paddle 的参数支持类型不一致,具体如下:
@@ -18,23 +18,23 @@ paddle.CUDAPlace(id)

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | -------------------------------------------------------------------------------- |
| device | id | GPU 的设备 ID, PyTorch 支持 torch.device 和 int,Paddle 支持 int,需要转写。 |
| device | device | GPU 的设备 ID, PyTorch 支持 torch.device 和 int,Paddle 支持 str,需要转写。 |

### 转写示例

#### device: 获取 device 参数,对其取 device.index 值
#### device: 特定的运行设备

```python
# PyTorch 写法
torch.cuda.device(torch.device('cuda'))
torch.cuda.device('cuda:0')

# Paddle 写法
paddle.CUDAPlace(0)
paddle.device._convert_to_place('gpu:0')

# 增加 index
# PyTorch 写法
torch.cuda.device(torch.device('cuda', index=index))
torch.cuda.device(2)

# Paddle 写法
paddle.CUDAPlace(index)
paddle.device._convert_to_place('gpu:2')

```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## [参数完全一致]torch.cuda.get_device_properties
## [ 输入参数类型不一致 ]torch.cuda.get_device_properties

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

@@ -12,9 +12,32 @@ torch.cuda.get_device_properties(device)
paddle.device.cuda.get_device_properties(device)
```

功能一致,参数完全一致,具体如下:
两者功能一致但参数类型不一致,具体如下:
### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| device | device | 表示设备、设备 ID 和类似于 gpu:x 的设备名称。如果 device 为空,则 device 为当前的设备。默认值为 None。|
| device | device | 表示设备、设备 ID 和类似于 gpu:x 的设备名称。默认值为 None,需要转写。|

### 转写示例
#### device: 设备

```python
# PyTorch 写法
torch.cuda.get_device_properties('cuda:0')
Copy link
Collaborator

Choose a reason for hiding this comment

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

device为空的情况需要转写吗

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 device不能为空


# Paddle 写法
paddle.device.cuda.get_device_properties('gpu:0')

# PyTorch 写法
torch.cuda.get_device_properties(2)

# Paddle 写法
paddle.device.cuda.get_device_properties('gpu:2')

# PyTorch 写法
torch.cuda.get_device_properties()

# Paddle 写法
paddle.device.cuda.get_device_properties()
```
Original file line number Diff line number Diff line change
@@ -6,10 +6,10 @@
torch.cuda.set_device(device)
```

### [paddle.device.set_device](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/device/set_device_cn.html)
### [paddle.set_device](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/device/set_device_cn.html)

```python
paddle.device.set_device(device)
paddle.set_device(device)
```

功能一致,参数类型不一致,具体如下:
@@ -18,3 +18,21 @@ paddle.device.set_device(device)
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ |------------------------------------------------|
| device | device | PyTorch 支持 torch.device 或 int。PaddlePaddle 支持 str。 |


### 转写示例
#### device: 特定的运行设备

```python
# PyTorch 写法
torch.cuda.set_device('cuda:0')

# Paddle 写法
paddle.set_device('gpu:0')

# PyTorch 写法
torch.cuda.set_device(2)

# Paddle 写法
paddle.set_device('gpu:2')
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## [参数完全一致]torch.cuda.synchronize
## [ 输入参数类型不一致 ]torch.cuda.synchronize

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

@@ -12,9 +12,27 @@ torch.cuda.synchronize(device)
paddle.device.cuda.synchronize(device)
```

功能一致,参数完全一致(PyTorch 参数是 PaddlePaddle 参数子集),具体如下:
两者功能一致但参数类型不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ |-----------------------------------------------------------------------|
| device | device | PyTorch 支持 torch.device 和 int。 PaddlePaddle 支持 paddle.CUDAPlace、int 、str。 |
| device | device | PyTorch 支持 torch.device 和 int。 PaddlePaddle 支持 paddle.CUDAPlace、int 、str,需要转写 |

### 转写示例
#### device: 特定的运行设备

```python
# PyTorch 写法
torch.cuda.synchronize('cuda:0')

# Paddle 写法
paddle.device.cuda.synchronize('gpu:0')

# PyTorch 写法
torch.cuda.synchronize(2)

# Paddle 写法
paddle.device.cuda.synchronize('gpu:2')
```
Original file line number Diff line number Diff line change
@@ -25,3 +25,30 @@ torch.Size([2, 3])
# Paddle 写法
(2, 3)
```

```python
# PyTorch 写法
torch.Size([2, 3]).count(2)

# Paddle 写法
(2, 3).count(2)
```

```python
# PyTorch 写法
torch.Size([2, 3]).index(3,0)

# Paddle 写法
(2, 3).index(3,0)
```

```python
# PyTorch 写法
torch.Size([2, 3]).numel()

# Paddle 写法
result = (2, 3)
out = 1
for x in result:
out *= x
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [ 无参数 ]torch.cpu.current_device

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

```python
torch.cpu.current_device()
```

### [paddle.get_device](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/device/get_device_cn.html#get-device)

```python
paddle.get_device()
```

功能一致,无参数。
Loading