Skip to content

新增映射文档 #7081

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 15 commits into from
Mar 13, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
### [torch.Tensor.where](https://pytorch.org/docs/stable/generated/torch.Tensor.where.html#torch.Tensor.where)

```python
torch.Tensor.where(condition, y)
torch.Tensor.where(condition, other)
```

### [paddle.Tensor.where](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#where-y-name-none)
### [paddle.where](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/where_cn.html)

```python
paddle.Tensor.where(x, y, name=None)
paddle.where(condition, x=None, y=None, name=None)
```

两者功能一致,参数名和参数用法不一致,具体如下:
Pytorch 为 Tensor 类方法,Paddle 为普通函数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| condition | - | condition 为判断条件。Paddle 无此参数,需要转写。|
| - | x | 当 condition 为 true 时,选择 x 中元素。|
| y | y | 当 condition 为 false 时,选择 y 中元素。|

| condition | condition | 判断条件。|
| self | x | 当 condition 为 true 时,选择的元素,调用 torch.Tensor 类方法的 self Tensor 传入。|
| other | y | 当 condition 为 false 时,选择的元素,仅参数名不一致。|

### 转写示例

Expand All @@ -30,7 +31,5 @@ b = torch.tensor([2, 3, 0])
c = a.where(a > 0, b)

# paddle 写法
a = paddle.to_tensor([0, 1, 2])
b = paddle.to_tensor([2, 3, 0])
c = (a > 0).where(a, b)
paddle.where(a > 0, a, b)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## [ 返回参数类型不一致 ]torch.cuda.get_rng_state
### [torch.cuda.get_rng_state](https://pytorch.org/docs/stable/generated/torch.cuda.get_rng_state.html#torch-cuda-get-rng-state)

```python
torch.cuda.get_rng_state(device='cuda')
```

### [paddle.get_cuda_rng_state](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/get_cuda_rng_state_cn.html#get-cuda-rng-state)

```python
paddle.get_cuda_rng_state()
```

torch 参数更多,并且 torch 与 paddle 的返回参数类型不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| device | - | 返回 RNG 状态的设备,Paddle 无此参数,需要转写。 |
| 返回值 | 返回值 | 返回参数类型不一致, PyTorch 返回 torch.ByteTensor,Paddle 返回 GeneratorState 对象列表。 |

### 转写示例

#### 返回参数类型不同

```python
# PyTorch 写法,返回 torch.ByteTensor
x = torch.cuda.get_rng_state(device='cuda:0')
Copy link
Collaborator

@zhwesky2010 zhwesky2010 Mar 12, 2025

Choose a reason for hiding this comment

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

这个是对device的转写,再加一个对返回参数的转写。对返回参数的转写,不需要传入device参数。


# Paddle 写法,返回 GeneratorState 对象
x = paddle.get_cuda_rng_state()[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无输入的话,应该是取第一个值吗?paddle.get_cuda_rng_state()[0]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个在Matcher部分又重新修改了一下

```

```python
# PyTorch 写法,返回 torch.ByteTensor
x = torch.cuda.get_rng_state()

# Paddle 写法,返回 GeneratorState 对象
x = paddle.get_cuda_rng_state()[paddle.framework._current_expected_place().get_device_id()]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.cuda.set_per_process_memory_fraction

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

```python
torch.cuda.set_per_process_memory_fraction(fraction, device=None)
```

限制当前进程在指定 GPU 上最多能分配的显存比例,Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch.cuda.set_per_process_memory_fraction(0.5)

# Paddle 写法
os.environ['FLAGS_fraction_of_gpu_memory_to_use'] = '0.5'
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [无参数]torch.distributed.is_available

### [torch.distributed.is_available](https://pytorch.org/docs/stable/distributed.html#torch.distributed.is_available)

```python
torch.distributed.is_available()
```

### [paddle.distributed.is_available](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/distributed/is_available_cn.html#cn-api-paddle-distributed-is-available)

```python
paddle.distributed.is_available()
```

两者功能一致,无参数。
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [无参数]torch.distributed.is_nccl_available

### [torch.distributed.is_nccl_available](https://pytorch.org/docs/stable/distributed.html#torch.distributed.is_nccl_available)

```python
torch.distributed.is_nccl_available()
```

### [paddle.core.is_compiled_with_nccl](https://github.com/PaddlePaddle/Paddle/blob/61de6003525166856157b6220205fe53df638376/python/paddle/jit/sot/utils/paddle_api_config.py#L159)

```python
paddle.core.is_compiled_with_nccl()
```

两者功能一致,无参数。
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## [ torch 参数更多 ] torch.distributed.monitored_barrier
### [torch.distributed.monitored_barrier](https://pytorch.org/docs/stable/distributed.html#torch.distributed.monitored_barrier)

```python
torch.distributed.monitored_barrier(group=None, timeout=None, wait_all_ranks=False)
```

### [paddle.distributed.barrier](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/distributed/barrier_cn.html)

```python
paddle.distributed.barrier(group=None)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------|
| group | group | 进程组编号。 |
| timeout | - | 超时时间,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
| wait_all_ranks | - | 是否等待所有进程超时后才报错,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## [ torch 参数更多 ]torch.nn.modules.module.register_module_forward_hook
Copy link
Collaborator

@zhwesky2010 zhwesky2010 Mar 13, 2025

Choose a reason for hiding this comment

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

有几处错误:

  1. torch的这两个API是给全局所有layer注册的hook,而paddle是单个layer的,这个差异必须要强调
    torch.nn.modules.module.register_module_forward_hook
    torch.nn.modules.module.register_module_forward_pre_hook

  2. 我查阅文档,没有发现register_module_forward_hook的prepend参数,这个参数是怎么来的?
    https://pytorch.org/docs/main/generated/torch.nn.modules.module.register_module_forward_hook.html#torch-nn-modules-module-register-module-forward-hook

  3. always_call我觉得不影响计算的结果,可以算作直接删除

这么看这两个API是无法自动转换的,可以写映射文档但Matcher是无法实现的(因为paddle没有类似的全局注册的API,只有单个注册的,没法将全局自动转为单个的逐个注册)。而PaConvert写的也有问题,由于单测是错的,直接用的Module.register_forward_hook,压根就没跑torch.nn.modules.module.register_module_forward_hook,导致两个错误Matcher没有测试到。

后续还是要保证代码的正确性。这些API只是初筛认为有对应功能,如果没有对应功能,就按你思考与判断的结论来处理,而不是一定要弄个错误的上去。

### [torch.nn.modules.module.register_module_forward_hook](https://pytorch.org/docs/stable/generated/torch.nn.modules.module.register_module_forward_hook.html)

```python
torch.nn.modules.module.register_module_forward_hook(hook, *, prepend=False, with_kwargs=False, always_call=False)
```

### [paddle.nn.Layer.register_forward_post_hook](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/nn/Layer_cn.html#register-forward-post-hook-hook)

```python
paddle.nn.Layer.register_forward_post_hook(hook)
```

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

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| hook | hook | 被注册为 forward pre-hook 的函数。 |
| prepend | - | 钩子执行顺序控制,Paddle 无此参数,暂无转写方式。 |
| with_kwargs | - | 是否传递关键字参数,Paddle 无此参数,暂无转写方式。 |
| always_call | - | 是否强制调用钩子,Paddle 无此参数,暂无转写方式。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

prepend会控制pre、post吗?always_call会导致不调用吗?with_kwargs影响参数传递方式吗?

得看看这三个参数的影响是什么,是否会导致paddle的运行结果不同,如果对运行结果无影响就不用管 直接删除了

Copy link
Contributor Author

@Xuxuanang Xuxuanang Mar 12, 2025

Choose a reason for hiding this comment

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

这三个参数应该都有可能会导致运行结果不同,prepend控制当前注册的 hook 相对于已有 hook 的调用顺序,always_call如果设置为True的话,则在抛出异常会调用,with_kwargs会影响传递给hook的参数

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## [ 参数完全一致 ]torch.nn.modules.module.register_module_forward_pre_hook

### [torch.nn.modules.module.register_module_forward_pre_hook](https://pytorch.org/docs/stable/generated/torch.nn.modules.module.register_module_forward_pre_hook.html)

```python
torch.nn.modules.module.register_module_forward_pre_hook(hook)
```

### [paddle.nn.Layer.register_forward_pre_hook](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/nn/Layer_cn.html#register-forward-pre-hook-hook)

```python
paddle.nn.Layer.register_forward_pre_hook(hook)
```

功能一致,参数完全一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
|---------|--------------|-----------------------------------------------------------------------------------------------|
| hook | hook | 被注册为 forward pre-hook 的函数。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## [ 输入参数用法不一致 ]torch.optim.Optimizer.zero_grad

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

```python
torch.optim.Optimizer.zero_grad(set_to_none=True)
```

### [paddle.optimizer.Optimizer.clear_gradients](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/optimizer/Optimizer_cn.html#clear-grad)

```python
paddle.optimizer.Optimizer.clear_gradients(set_to_zero=True)
```

PyTorch 的 `Optimizer.zero_grad` 参数与 Paddle 的 `Optimizer.clear_gradients` 参数用法刚好相反,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ----------- | ------------ | ------------------------------------------------ |
| set_to_none | set_to_zero | 设置如何清空梯度,PyTorch 默认 set_to_none 为 True,Paddle 默认 set_to_zero 为 True,两者功能刚好相反,Paddle 需设置为 False。 |

### 转写示例

```python
# PyTorch 写法
torch.optim.Optimizer.zero_grad(set_to_none=True)

# Paddle 写法
paddle.optimizer.Optimizer.clear_gradients(set_to_zero=False)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [无参数]torch.get_default_device

### [torch.get_default_device](https://pytorch.org/docs/stable/generated/torch.get_default_device.html#torch-get-default-device)

```python
torch.get_default_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()
```

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

### [torch.set_default_device](https://pytorch.org/docs/stable/generated/torch.set_default_device.html#torch-set-default-device)

```python
torch.set_default_device(device)
```

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

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

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

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ |------------------------------------------------|
| device | device | PyTorch 支持 torch.device 。PaddlePaddle 支持 str。 |
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@
| ALIAS-REFERENCE-ITEM(`torch.utils.data.sampler.WeightedRandomSampler`, `torch.utils.data.WeightedRandomSampler`) |
| ALIAS-REFERENCE-ITEM(`torch.igamma`, `torch.special.gammainc`) |
| ALIAS-REFERENCE-ITEM(`torch.igammac`, `torch.special.gammaincc`) |
| ALIAS-REFERENCE-ITEM(`torch.distributions.multivariate_normal.MultivariateNormal`, `torch.distributions.MultivariateNormal`) |

## <span id="id25">功能缺失的 API 列表</span>

Expand Down Expand Up @@ -1034,22 +1035,15 @@
| NOT-IMPLEMENTED-ITEM(`torch.cuda.memory_usage`, https://pytorch.org/docs/stable/generated/torch.cuda.memory_usage.html#torch-cuda-memory-usage, 可新增,且框架底层有相关设计,成本低) |
| NOT-IMPLEMENTED-ITEM(`torch.layout`, https://pytorch.org/docs/stable/tensor_attributes.html#torch.layout, 可新增,但框架底层无相关设计,成本高) |
| NOT-IMPLEMENTED-ITEM(`torch.cuda.is_current_stream_capturing`, https://pytorch.org/docs/stable/generated/torch.cuda.is_current_stream_capturing.html#torch-cuda-is-current-stream-capturing, 可新增,且框架底层有相关设计,成本低) |
| NOT-IMPLEMENTED-ITEM(`torch.cuda.device_of`, https://pytorch.org/docs/stable/generated/torch.cuda.device_of.html, 可新增,且框架底层有相关设计,成本低) |

## <span id="id26">映射关系开发中的 API 列表</span>

| 序号 | Pytorch 最新 release | Paddle develop | 映射关系分类 | 备注 |
| ----- | ----------- | ----------------- | ----------- | ------- |
| IN-DEVELOPMENT-PATTERN(`torch.nn.parameter.UninitializedParameter`, https://pytorch.org/docs/stable/generated/torch.nn.parameter.UninitializedParameter.html#torch.nn.parameter.UninitializedParameter) |
| IN-DEVELOPMENT-PATTERN(`torch.nn.modules.module.register_module_forward_pre_hook`, https://pytorch.org/docs/stable/generated/torch.nn.modules.module.register_module_forward_pre_hook.html#torch-nn-modules-module-register-module-forward-pre-hook) |
| IN-DEVELOPMENT-PATTERN(`torch.nn.modules.module.register_module_forward_hook`, https://pytorch.org/docs/stable/generated/torch.nn.modules.module.register_module_forward_hook.html#torch-nn-modules-module-register-module-forward-hook) |
| IN-DEVELOPMENT-PATTERN(`torch.cuda.device_of`, https://pytorch.org/docs/stable/generated/torch.cuda.device_of.html#torch.cuda.device_of) |
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个分类不对,另外放到功能缺失里,你得细分一下类。

功能缺失也有好几类。

| IN-DEVELOPMENT-PATTERN(`torch.cuda.get_rng_state`, https://pytorch.org/docs/stable/generated/torch.cuda.get_rng_state.html#torch-cuda-get-rng-state) |
| IN-DEVELOPMENT-PATTERN(`torch.cuda.set_per_process_memory_fraction`, https://pytorch.org/docs/stable/generated/torch.cuda.set_per_process_memory_fraction.html#torch-cuda-set-per-process-memory-fraction) |
| IN-DEVELOPMENT-PATTERN(`torch.distributed.Backend`, https://pytorch.org/docs/stable/distributed.html#torch.distributed.Backend) |
| IN-DEVELOPMENT-PATTERN(`torch.distributed.is_available`, https://pytorch.org/docs/stable/distributed.html#torch.distributed.is_available) |
| IN-DEVELOPMENT-PATTERN(`torch.distributed.is_nccl_available`, https://pytorch.org/docs/stable/distributed.html#torch.distributed.is_nccl_available) |
| IN-DEVELOPMENT-PATTERN(`torch.distributed.gather_object`, https://pytorch.org/docs/stable/distributed.html#torch.distributed.gather_object) |
| IN-DEVELOPMENT-PATTERN(`torch.distributions.multivariate_normal.MultivariateNormal`, https://pytorch.org/docs/stable/distributions.html#torch.distributions.multivariate_normal.MultivariateNormal) |
| IN-DEVELOPMENT-PATTERN(`torch.jit.script`, https://pytorch.org/docs/stable/generated/torch.jit.script.html#torch-jit-script) |
| IN-DEVELOPMENT-PATTERN(`torch.jit.trace`, https://pytorch.org/docs/stable/generated/torch.jit.trace.html#torch-jit-trace) |
| IN-DEVELOPMENT-PATTERN(`torch.jit.save`, https://pytorch.org/docs/stable/generated/torch.jit.save.html#torch-jit-save) |
Expand All @@ -1058,11 +1052,8 @@
| IN-DEVELOPMENT-PATTERN(`torch.utils.checkpoint.checkpoint_sequential`, https://pytorch.org/docs/stable/checkpoint.html#torch.utils.checkpoint.checkpoint_sequential) |
| IN-DEVELOPMENT-PATTERN(`torch.utils.tensorboard.writer.SummaryWriter`, https://pytorch.org/docs/stable/tensorboard.html#torch.utils.tensorboard.writer.SummaryWriter) |
| IN-DEVELOPMENT-PATTERN(`torch.nn.parameter.UninitializedBuffer`, https://pytorch.org/docs/stable/generated/torch.nn.parameter.UninitializedBuffer.html#torch.nn.parameter.UninitializedBuffer) |
| IN-DEVELOPMENT-PATTERN(`torch.optim.Optimizer.zero_grad`, https://pytorch.org/docs/stable/generated/torch.optim.Optimizer.zero_grad.html#torch-optim-optimizer-zero-grad) |
| IN-DEVELOPMENT-PATTERN(`torch.distributed.monitored_barrier`, https://pytorch.org/docs/stable/distributed.html#torch.distributed.monitored_barrier) |
| IN-DEVELOPMENT-PATTERN(`torch.autograd.Function.jvp`, https://pytorch.org/docs/stable/generated/torch.autograd.Function.jvp.html#torch-autograd-function-jvp) |
| IN-DEVELOPMENT-PATTERN(`torch.memory_format`, https://pytorch.org/docs/stable/tensor_attributes.html#torch.memory_format) |
| IN-DEVELOPMENT-PATTERN(`torch.set_default_device`, https://pytorch.org/docs/stable/generated/torch.set_default_device.html#torch-set-default-device) |
| IN-DEVELOPMENT-PATTERN(`torch.concatenate`, https://pytorch.org/docs/stable/generated/torch.concatenate.html#torch-concatenate) |
| IN-DEVELOPMENT-PATTERN(`torch._foreach_abs`, https://pytorch.org/docs/stable/generated/torch._foreach_abs.html#torch-foreach-abs) |
| IN-DEVELOPMENT-PATTERN(`torch._foreach_abs_`, https://pytorch.org/docs/stable/generated/torch._foreach_abs_.html#torch-foreach-abs) |
Expand Down Expand Up @@ -1137,7 +1128,6 @@
| IN-DEVELOPMENT-PATTERN(`torch.distributed.reduce_scatter_tensor`, https://pytorch.org/docs/stable/distributed.html#torch.distributed.reduce_scatter_tensor) |
| IN-DEVELOPMENT-PATTERN(`torch.distributed.all_to_all_single`, https://pytorch.org/docs/stable/distributed.html#torch.distributed.all_to_all_single) |
| IN-DEVELOPMENT-PATTERN(`torch.utils.set_module`, https://pytorch.org/docs/stable/generated/torch.utils.set_module.html#torch-utils-set-module) |
| IN-DEVELOPMENT-PATTERN(`torch.get_default_device`, https://pytorch.org/docs/stable/generated/torch.get_default_device.html#torch-get-default-device) |
| IN-DEVELOPMENT-PATTERN(`torch.nn.utils.fuse_conv_bn_eval`, https://pytorch.org/docs/stable/generated/torch.nn.utils.fuse_conv_bn_eval.html#torch-nn-utils-fuse-conv-bn-eval) |
| IN-DEVELOPMENT-PATTERN(`torch.nn.utils.fuse_conv_bn_weights`, https://pytorch.org/docs/stable/generated/torch.nn.utils.fuse_conv_bn_weights.html#torch-nn-utils-fuse-conv-bn-weights) |
| IN-DEVELOPMENT-PATTERN(`torch.nn.utils.fuse_linear_bn_eval`, https://pytorch.org/docs/stable/generated/torch.nn.utils.fuse_linear_bn_eval.html#torch-nn-utils-fuse-linear-bn-eval) |
Expand Down