Skip to content

新增映射文档 #7104

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 4 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -1,8 +1,8 @@
## [ torch 参数更多 ]torch.nn.modules.module.register_module_forward_hook
## [ 组合替代实现 ]torch.nn.modules.module.register_module_forward_hook
### [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)
torch.nn.modules.module.register_module_forward_hook(hook, *, always_call=False)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个PyTorch官方文档上面有,但我实际使用应该没有

```

### [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)
Expand All @@ -11,12 +11,28 @@ torch.nn.modules.module.register_module_forward_hook(hook, *, prepend=False, wit
paddle.nn.Layer.register_forward_post_hook(hook)
```

PyTorch 相比 Paddle 支持更多其他参数,具体如下:
其中,PyTorch 为给全局所有 module 注册 hook,而 Paddle 为给单个 Layer 注册 hook。PyTorch 相比 Paddle 支持更多其他参数,具体如下:
### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| hook | hook | 被注册为 forward pre-hook 的函数。 |
| prepend | - | 钩子执行顺序控制,Paddle 无此参数,暂无转写方式。 |
| with_kwargs | - | 是否传递关键字参数,Paddle 无此参数,暂无转写方式。 |
| always_call | - | 是否强制调用钩子,Paddle 无此参数,暂无转写方式。 |
| hook | hook | 被注册为 forward post-hook 的函数。 |
| always_call | - | 是否强制调用钩子,Paddle 无此参数,一般对训练结果影响不大,可直接删除。 |

### 转写示例

```python
# PyTorch 写法
Linear = torch.nn.Linear(2, 4)
Conv2d = torch.nn.Conv2d(3, 16, 3)
Batch2d = torch.nn.BatchNorm2d(10)
torch.nn.modules.module.register_module_forward_hook(hook)

# Paddle 写法
Linear = paddle.nn.Linear(2, 4)
Conv2d = paddle.nn.Conv2d(3, 16, 3)
Batch2d = paddle.nn.BatchNorm2D(10)
Linear.register_forward_post_hook(hook)
Conv2d.register_forward_post_hook(hook)
Batch2d.register_forward_post_hook(hook)
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## [ 参数完全一致 ]torch.nn.modules.module.register_module_forward_pre_hook
## [ 组合替代实现 ]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)

Expand All @@ -12,10 +12,28 @@ torch.nn.modules.module.register_module_forward_pre_hook(hook)
paddle.nn.Layer.register_forward_pre_hook(hook)
```

功能一致,参数完全一致,具体如下:
其中,PyTorch 为给全局所有 module 注册 hook,而 Paddle 为给单个 Layer 注册 hook, 具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
|---------|--------------|-----------------------------------------------------------------------------------------------|
| hook | hook | 被注册为 forward pre-hook 的函数。 |

### 转写示例

```python
# PyTorch 写法
Linear = torch.nn.Linear(2, 4)
Conv2d = torch.nn.Conv2d(3, 16, 3)
Batch2d = torch.nn.BatchNorm2d(10)
torch.nn.modules.module.register_module_forward_pre_hook(hook)

# Paddle 写法
Linear = paddle.nn.Linear(2, 4)
Conv2d = paddle.nn.Conv2d(3, 16, 3)
Batch2d = paddle.nn.BatchNorm2D(10)
Linear.register_forward_pre_hook(hook)
Conv2d.register_forward_pre_hook(hook)
Batch2d.register_forward_pre_hook(hook)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_abs

### [torch.\_foreach_abs](https://pytorch.org/docs/stable/generated/torch._foreach_abs.html#torch-foreach-abs)

```python
torch._foreach_abs(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_abs(tensors)

# Paddle 写法
[paddle.abs(x) for x in tensors]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_abs_

### [torch.\_foreach_abs_](https://pytorch.org/docs/stable/generated/torch._foreach_abs_.html#torch-foreach-abs)

```python
torch._foreach_abs_(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_abs_(tensors)

# Paddle 写法
[paddle.abs_(x) for x in tensors]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_acos

### [torch.\_foreach_acos](https://pytorch.org/docs/stable/generated/torch._foreach_acos.html#torch-foreach-acos)

```python
torch._foreach_acos(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_acos(tensors)

# Paddle 写法
[paddle.acos(x) for x in tensors]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_acos_

### [torch.\_foreach_acos_](https://pytorch.org/docs/stable/generated/torch._foreach_acos_.html#torch-foreach-acos)

```python
torch._foreach_acos_(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_acos_(tensors)

# Paddle 写法
[paddle.acos_(x) for x in tensors]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_asin

### [torch.\_foreach_asin](https://pytorch.org/docs/stable/generated/torch._foreach_asin.html#torch-foreach-asin)

```python
torch._foreach_asin(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_asin(tensors)

# Paddle 写法
[paddle.asin(x) for x in tensors]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_asin_

### [torch.\_foreach_asin_](https://pytorch.org/docs/stable/generated/torch._foreach_asin_.html#torch-foreach-asin)

```python
torch._foreach_asin_(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_asin_(tensors)

# Paddle 写法
[paddle.asin_(x) for x in tensors]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_atan

### [torch.\_foreach_atan](https://pytorch.org/docs/stable/generated/torch._foreach_atan.html#torch-foreach-atan)

```python
torch._foreach_atan(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_atan(tensors)

# Paddle 写法
[paddle.atan(x) for x in tensors]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_atan_

### [torch.\_foreach_atan_](https://pytorch.org/docs/stable/generated/torch._foreach_atan_.html#torch-foreach-atan)

```python
torch._foreach_atan_(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_atan_(tensors)

# Paddle 写法
[paddle.atan_(x) for x in tensors]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_ceil

### [torch.\_foreach_ceil](https://pytorch.org/docs/stable/generated/torch._foreach_ceil.html#torch-foreach-ceil)

```python
torch._foreach_ceil(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_ceil(tensors)

# Paddle 写法
[paddle.ceil(x) for x in tensors]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_cos

### [torch.\_foreach_cos](https://pytorch.org/docs/stable/generated/torch._foreach_cos.html#torch-foreach-cos)

```python
torch._foreach_cos(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_cos(tensors)

# Paddle 写法
[paddle.cos(x) for x in tensors]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_cos_

### [torch.\_foreach_cos_](https://pytorch.org/docs/stable/generated/torch._foreach_cos_.html#torch-foreach-cos)

```python
torch._foreach_cos_(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_cos_(tensors)

# Paddle 写法
[paddle.cos_(x) for x in tensors]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_cosh

### [torch.\_foreach_cosh](https://pytorch.org/docs/stable/generated/torch._foreach_cosh.html#torch-foreach-cosh)

```python
torch._foreach_cosh(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_cosh(tensors)

# Paddle 写法
[paddle.cosh(x) for x in tensors]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_cosh_

### [torch.\_foreach_cosh_](https://pytorch.org/docs/stable/generated/torch._foreach_cosh_.html#torch-foreach-cosh)

```python
torch._foreach_cosh_(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_cosh_(tensors)

# Paddle 写法
[paddle.cosh_(x) for x in tensors]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_erf

### [torch.\_foreach_erf](https://pytorch.org/docs/stable/generated/torch._foreach_erf.html#torch-foreach-erf)

```python
torch._foreach_erf(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_erf(tensors)

# Paddle 写法
[paddle.erf(x) for x in tensors]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.\_foreach_erf_

### [torch.\_foreach_erf_](https://pytorch.org/docs/stable/generated/torch._foreach_erf_.html#torch-foreach-erf)

```python
torch._foreach_erf_(self)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch._foreach_erf_(tensors)

# Paddle 写法
[paddle.erf_(x) for x in tensors]
```
Loading