Skip to content

Commit cb783bb

Browse files
authored
device类文档更新 (#7244)
* update docs * update docs * update P2POp * fix docs * fix bugs * update docs * update cuda docs * add torch.Size * update docs * fix docs with cuda * fix docs * fix torch.cuda.current_stream * fix some docs * fix cuda.get_device_properties * fix docs
1 parent e67174a commit cb783bb

15 files changed

+315
-36
lines changed

docs/guides/model_convert/convert_from_pytorch/api_difference/Tensor/torch.Tensor.cuda.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## [torch 参数更多]torch.Tensor.cuda
1+
## [输入参数类型不一致]torch.Tensor.cuda
22

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

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

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

1717
### 参数映射
1818

1919
| PyTorch | PaddlePaddle | 备注 |
2020
| ------------- | ------------ | ------------------------------------------------------------------------ |
21-
| device | device_id | 目标 GPU 设备,仅参数名不一致|
21+
| device | device_id | 目标 GPU 设备,输入参数类型不一致,需要转写|
2222
| non_blocking | blocking | 是否同步或异步拷贝,PyTorch 和 Paddle 取值相反,需要转写。 |
2323
| memory_format | - | 表示内存格式, Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
24+
25+
26+
### 转写示例
27+
28+
#### non_blocking: 同步或异步拷贝
29+
30+
```python
31+
# PyTorch 写法
32+
tensor.cuda(non_blocking=True)
33+
34+
# Paddle 写法
35+
tensor.cuda(blocking=False)
36+
```
37+
38+
#### device: 目标 GPU 设备
39+
40+
```python
41+
# PyTorch 写法
42+
tensor.cuda("cuda:0")
43+
44+
# Paddle 写法
45+
tensor.cuda(0)
46+
47+
# PyTorch 写法
48+
tensor.cuda(0)
49+
50+
# Paddle 写法
51+
tensor.cuda(0)
52+
```

docs/guides/model_convert/convert_from_pytorch/api_difference/Tensor/torch.Tensor.size.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,27 @@ torch.tensor([-1, -2, 3]).size(0)
2929
# Paddle 写法
3030
paddle.to_tensor([-0.4, -0.2, 0.1, 0.3]).shape[0]
3131
```
32+
33+
```python
34+
# PyTorch 写法
35+
torch.ones(10, 20, 30).size().count(30)
36+
37+
# Paddle 写法
38+
tuple(paddle.ones([10, 20, 30]).shape).count(30)
39+
```
40+
41+
```python
42+
# PyTorch 写法
43+
torch.ones(10, 20, 30).size().index(30, 1)
44+
45+
# Paddle 写法
46+
tuple(paddle.ones([10, 20, 30]).shape).index(30, 1)
47+
```
48+
49+
```python
50+
# PyTorch 写法
51+
torch.ones(10, 20, 30).size().numel()
52+
53+
# Paddle 写法
54+
paddle.ones([10, 20, 30]).numel().item()
55+
```

docs/guides/model_convert/convert_from_pytorch/api_difference/cuda/torch.cuda.Stream__upper.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,24 @@ paddle.device.Stream(device=None, priority=None)
2727

2828
```python
2929
# PyTorch 写法
30-
high_priority = -1
31-
default_priority = 0
32-
y = torch.cuda.Stream(priority=default_priority)
30+
torch.cuda.Stream(priority=0)
3331

3432
# Paddle 写法
35-
high_priority = 1
36-
default_priority = 2
37-
y = paddle.device.Stream(priority=default_priority)
33+
paddle.device.Stream(priority=2)
34+
```
35+
36+
#### device: 希望分配 stream 的设备
37+
38+
```python
39+
# PyTorch 写法
40+
torch.cuda.Stream('cuda:0')
41+
42+
# Paddle 写法
43+
paddle.device.Stream('gpu:0')
44+
45+
# PyTorch 写法
46+
torch.cuda.Stream(2)
47+
48+
# Paddle 写法
49+
paddle.device.Stream(device='gpu:2')
3850
```

docs/guides/model_convert/convert_from_pytorch/api_difference/cuda/torch.cuda.current_device.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
torch.cuda.current_device()
77
```
88

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

1111
```python
12-
paddle.framework._current_expected_place()
12+
paddle.get_device()
1313
```
1414

1515
功能一致,无参数。
Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
1-
## [参数完全一致]torch.cuda.current_stream
1+
## [输入参数类型不一致]torch.cuda.current_stream
22

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

55
```python
66
torch.cuda.current_stream(device=None)
77
```
88

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

1111
```python
12-
paddle.device.cuda.current_stream(device=None)
12+
paddle.device.current_stream(device=None)
1313
```
1414

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

1818
| PyTorch | PaddlePaddle | 备注 |
1919
| ------------- | ------------ | ------------------------------------------------------ |
20-
| device | device | 表示希望获取 stream 的设备或者设备 ID。如果为 None,则为当前的设备。默认值为 None。 |
20+
| device | device | 表示希望获取 stream 的设备或者设备 ID。如果为 None,则为当前的设备。默认值为 None,需要转写。 |
21+
22+
### 转写示例
23+
#### device: 特定的运行设备
24+
25+
```python
26+
# PyTorch 写法
27+
torch.cuda.current_stream('cuda:0')
28+
29+
# Paddle 写法
30+
paddle.device.current_stream('gpu:0')
31+
32+
# PyTorch 写法
33+
torch.cuda.current_stream(2)
34+
35+
# Paddle 写法
36+
paddle.device.current_stream('gpu:2')
37+
38+
# PyTorch 写法
39+
torch.cuda.current_stream()
40+
41+
# Paddle 写法
42+
paddle.device.current_stream()
43+
```

docs/guides/model_convert/convert_from_pytorch/api_difference/cuda/torch.cuda.device.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
torch.cuda.device(device)
77
```
88

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

1111
```python
12-
paddle.CUDAPlace(id)
12+
paddle.device._convert_to_place(device)
1313
```
1414

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

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

2323
### 转写示例
2424

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

2727
```python
2828
# PyTorch 写法
29-
torch.cuda.device(torch.device('cuda'))
29+
torch.cuda.device('cuda:0')
3030

3131
# Paddle 写法
32-
paddle.CUDAPlace(0)
32+
paddle.device._convert_to_place('gpu:0')
3333

34-
# 增加 index
3534
# PyTorch 写法
36-
torch.cuda.device(torch.device('cuda', index=index))
35+
torch.cuda.device(2)
3736

3837
# Paddle 写法
39-
paddle.CUDAPlace(index)
38+
paddle.device._convert_to_place('gpu:2')
39+
4040
```

docs/guides/model_convert/convert_from_pytorch/api_difference/cuda/torch.cuda.get_device_properties.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## [参数完全一致]torch.cuda.get_device_properties
1+
## [ 输入参数类型不一致 ]torch.cuda.get_device_properties
22

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

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

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

1818
| PyTorch | PaddlePaddle | 备注 |
1919
| ------------- | ------------ | ------------------------------------------------------ |
20-
| device | device | 表示设备、设备 ID 和类似于 gpu:x 的设备名称。如果 device 为空,则 device 为当前的设备。默认值为 None。|
20+
| device | device | 表示设备、设备 ID 和类似于 gpu:x 的设备名称。默认值为 None,需要转写。|
21+
22+
### 转写示例
23+
#### device: 设备
24+
25+
```python
26+
# PyTorch 写法
27+
torch.cuda.get_device_properties('cuda:0')
28+
29+
# Paddle 写法
30+
paddle.device.cuda.get_device_properties('gpu:0')
31+
32+
# PyTorch 写法
33+
torch.cuda.get_device_properties(2)
34+
35+
# Paddle 写法
36+
paddle.device.cuda.get_device_properties('gpu:2')
37+
38+
# PyTorch 写法
39+
torch.cuda.get_device_properties()
40+
41+
# Paddle 写法
42+
paddle.device.cuda.get_device_properties()
43+
```

docs/guides/model_convert/convert_from_pytorch/api_difference/cuda/torch.cuda.set_device.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
torch.cuda.set_device(device)
77
```
88

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

1111
```python
12-
paddle.device.set_device(device)
12+
paddle.set_device(device)
1313
```
1414

1515
功能一致,参数类型不一致,具体如下:
@@ -18,3 +18,21 @@ paddle.device.set_device(device)
1818
| PyTorch | PaddlePaddle | 备注 |
1919
| ------------- | ------------ |------------------------------------------------|
2020
| device | device | PyTorch 支持 torch.device 或 int。PaddlePaddle 支持 str。 |
21+
22+
23+
### 转写示例
24+
#### device: 特定的运行设备
25+
26+
```python
27+
# PyTorch 写法
28+
torch.cuda.set_device('cuda:0')
29+
30+
# Paddle 写法
31+
paddle.set_device('gpu:0')
32+
33+
# PyTorch 写法
34+
torch.cuda.set_device(2)
35+
36+
# Paddle 写法
37+
paddle.set_device('gpu:2')
38+
```

docs/guides/model_convert/convert_from_pytorch/api_difference/cuda/torch.cuda.synchronize.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## [参数完全一致]torch.cuda.synchronize
1+
## [ 输入参数类型不一致 ]torch.cuda.synchronize
22

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

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

15-
功能一致,参数完全一致(PyTorch 参数是 PaddlePaddle 参数子集),具体如下:
15+
两者功能一致但参数类型不一致,具体如下:
16+
1617
### 参数映射
1718

1819
| PyTorch | PaddlePaddle | 备注 |
1920
| ------------- | ------------ |-----------------------------------------------------------------------|
20-
| device | device | PyTorch 支持 torch.device 和 int。 PaddlePaddle 支持 paddle.CUDAPlace、int 、str。 |
21+
| device | device | PyTorch 支持 torch.device 和 int。 PaddlePaddle 支持 paddle.CUDAPlace、int 、str,需要转写 |
22+
23+
### 转写示例
24+
#### device: 特定的运行设备
25+
26+
```python
27+
# PyTorch 写法
28+
torch.cuda.synchronize('cuda:0')
29+
30+
# Paddle 写法
31+
paddle.device.cuda.synchronize('gpu:0')
32+
33+
# PyTorch 写法
34+
torch.cuda.synchronize(2)
35+
36+
# Paddle 写法
37+
paddle.device.cuda.synchronize('gpu:2')
38+
```

docs/guides/model_convert/convert_from_pytorch/api_difference/torch/torch.Size.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,30 @@ torch.Size([2, 3])
2525
# Paddle 写法
2626
(2, 3)
2727
```
28+
29+
```python
30+
# PyTorch 写法
31+
torch.Size([2, 3]).count(2)
32+
33+
# Paddle 写法
34+
(2, 3).count(2)
35+
```
36+
37+
```python
38+
# PyTorch 写法
39+
torch.Size([2, 3]).index(3,0)
40+
41+
# Paddle 写法
42+
(2, 3).index(3,0)
43+
```
44+
45+
```python
46+
# PyTorch 写法
47+
torch.Size([2, 3]).numel()
48+
49+
# Paddle 写法
50+
result = (2, 3)
51+
out = 1
52+
for x in result:
53+
out *= x
54+
```

0 commit comments

Comments
 (0)