Skip to content

Commit

Permalink
Add doc (#6075)
Browse files Browse the repository at this point in the history
  • Loading branch information
co63oc authored Aug 14, 2023
1 parent 30f55af commit 5b05ce7
Show file tree
Hide file tree
Showing 15 changed files with 376 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## [参数完全一致]torch.cuda.set_stream

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

```python
torch.cuda.set_stream(stream)
```

### [paddle.device.set_stream](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/device/set_stream_cn.html#set-stream)

```python
paddle.device.set_stream(stream=None)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ------------------- |
| stream | stream | 希望设置的 stream。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## [ torch 参数更多 ] torch.linalg.svdvals

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

```python
torch.linalg.svdvals(A, *, driver=None, out=None)
```

### [paddle.linalg.svd](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/linalg/svd_cn.html)

```python
paddle.linalg.svd(x, full_matrices=False, name=None)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------- | ------------------------------------------------------------------------------------ |
| A | x | 输入 Tensor,仅参数名不一致。 |
| driver | - | cuSOLVER 方法名,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
| - | full_matrices | 是否计算完整的 U 和 V 矩阵,Paddle 为 False,Pytorch 无此参数,Paddle 使用默认即可。 |
| out | - | 表示输出的 Tensor,Paddle 无此参数,需要转写。 |
| 返回值 | 返回值 | PyTorch 返回值为 S,Paddle 返回 U、S、VH,需要转写。 |

### 转写示例

#### out 参数:输出的 Tensor

```python
# PyTorch 写法:
torch.linalg.svdvals(x, out=y)

# Paddle 写法:
paddle.assign(paddle.linalg.svd(x)[1], y)
```

#### 返回值

```python
# PyTorch 写法:
y = torch.linalg.svdvals(x)

# Paddle 写法:
y = paddle.linalg.svd(x)[1]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## [仅 Paddle 参数更多]torch.autograd.Function.backward

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

```python
torch.autograd.Function.backward(ctx, *grad_outputs)
```

### [paddle.autograd.PyLayer.backward](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/autograd/PyLayer_cn.html#backward-ctx-args-kwargs)

```python
paddle.autograd.PyLayer.backward(ctx, *args, **kwargs)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------ | ------------ | ------------------------------------------------------------------- |
| ctx | ctx | 上下文对象。 |
| grad_outputs | args | forward 输出 Tensor 的梯度,仅参数名不一致。 |
| - | kwargs | forward 输出 Tensor 的梯度,PyTorch 无此参数,Paddle 保持默认即可。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## [参数完全一致]torch.autograd.Function.forward

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

```python
torch.autograd.Function.forward(ctx, *args, **kwargs)
```

### [paddle.autograd.PyLayer.forward](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/autograd/PyLayer_cn.html#forward-ctx-args-kwargs)

```python
paddle.autograd.PyLayer.forward(ctx, *args, **kwargs)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ------------------ |
| ctx | ctx | 上下文对象。 |
| args | args | 自定义算子的输入。 |
| kwargs | kwargs | 自定义算子的输入。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## [torch 参数更多]torch.autograd.Function

### [torch.autograd.Function](https://pytorch.org/docs/stable/autograd.html#torch.autograd.Function)

```python
torch.autograd.Function(*args, **kwargs)
```

### [paddle.autograd.PyLayer](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/autograd/PyLayer_cn.html#paddle.autograd.PyLayer)

```python
paddle.autograd.PyLayer()
```

其中 PyTorch 相比 Paddle 支持更多其他参数,但该类一般用于继承实现,不会调用其参数。

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ----------------------------------------- |
| args | - | 自定义参数,Paddle 无此参数,可直接删除。 |
| kwargs | - | 自定义参数,Paddle 无此参数,可直接删除。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## [仅参数名不一致]torch.autograd.function.FunctionCtx.mark_non_differentiable

### [torch.autograd.function.FunctionCtx.mark_non_differentiable](https://pytorch.org/docs/stable/generated/torch.autograd.function.FunctionCtx.mark_non_differentiable.html#torch.autograd.function.FunctionCtx.mark_non_differentiable)

```python
torch.autograd.function.FunctionCtx.mark_non_differentiable(*args)
```

### [paddle.autograd.PyLayerContext.mark_non_differentiable](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/autograd/PyLayerContext_cn.html#mark-non-differentiable-self-tensors)

```python
paddle.autograd.PyLayerContext.mark_non_differentiable(*tensors)
```

两者功能一致,仅参数名不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ----------------------------- |
| args | tensors | 需要标记不需要反向的 Tensor。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## [参数完全一致]torch.autograd.function.FunctionCtx.save_for_backward

### [torch.autograd.function.FunctionCtx.save_for_backward](https://pytorch.org/docs/stable/generated/torch.autograd.function.FunctionCtx.save_for_backward.html#torch.autograd.function.FunctionCtx.save_for_backward)

```python
torch.autograd.function.FunctionCtx.save_for_backward(*tensors)
```

### [paddle.autograd.PyLayerContext.save_for_backward](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/autograd/PyLayerContext_cn.html#save-for-backward-tensors)

```python
paddle.autograd.PyLayerContext.save_for_backward(*tensors)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | --------------------- |
| tensors | tensors | 需要被暂存的 Tensor。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## [参数完全一致]torch.autograd.function.FunctionCtx.set_materialize_grads

### [torch.autograd.function.FunctionCtx.set_materialize_grads](https://pytorch.org/docs/stable/generated/torch.autograd.function.FunctionCtx.set_materialize_grads.html#torch.autograd.function.FunctionCtx.set_materialize_grads)

```python
torch.autograd.function.FunctionCtx.set_materialize_grads(value)
```

### [paddle.autograd.PyLayerContext.set_materialize_grads](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/autograd/PyLayerContext_cn.html#set-materialize-grads-self-value)

```python
paddle.autograd.PyLayerContext.set_materialize_grads(value)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | -------------------------------------- |
| value | value | 是否要框架来初始化未初始化的反向梯度。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## [参数完全一致]torch.autograd.graph.saved_tensors_hooks

### [torch.autograd.graph.saved_tensors_hooks](https://pytorch.org/docs/stable/autograd.html?highlight=saved_tensors_hooks#torch.autograd.graph.saved_tensors_hooks)

```python
torch.autograd.graph.saved_tensors_hooks(pack_hook, unpack_hook)
```

### [paddle.autograd.saved_tensors_hooks](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/autograd/saved_tensors_hooks_cn.html)

```python
paddle.autograd.saved_tensors_hooks(pack_hook, unpack_hook)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ----------- | ------------ | ----------------------------------------------------------------------------------------- |
| pack_hook | pack_hook | 当某个算子的前向执行时,存在 Tensor 需要保留给反向计算梯度使用时, pack_hook 将会被调用。 |
| unpack_hook | unpack_hook | 当反向执行,需要用到前向保留的 Tensor 时, unpack_hook 会被调用。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.special.erfc

### [torch.special.erfc](https://pytorch.org/docs/stable/special.html#torch.special.erfc)

```python
torch.special.erfc(input, *, out=None)
```

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

### 转写示例

```python
# Pytorch 写法
y = torch.special.erfc(x)

# Paddle 写法
y = 1 - paddle.erf(x)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.special.exp2

### [torch.special.exp2](https://pytorch.org/docs/stable/special.html#torch.special.exp2)

```python
torch.special.exp2(input, *, out=None)
```

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

### 转写示例

```python
# Pytorch 写法
y = torch.special.exp2(x)

# Paddle 写法
y = 2 ** x
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [组合替代实现]torch.special.expit

### [torch.special.expit](https://pytorch.org/docs/stable/special.html#torch.special.expit)

```python
torch.special.expit(input, *, out=None)
```

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

### 转写示例

```python
# Pytorch 写法
y = torch.special.expit(x)

# Paddle 写法
y = 1 / (1 + 1 / paddle.exp(x))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## [torch 参数更多]torch.special.gammaln

### [torch.special.gammaln](https://pytorch.org/docs/stable/special.html#torch.special.gammaln)

```python
torch.special.gammaln(input, *, out=None)
```

### [paddle.lgamma](https://pytorch.org/docs/stable/special.html#torch.special.gammaln)

```python
paddle.lgamma(x, name=None)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ---------------------------------------------------- |
| input | x | 输入的 Tensor,仅参数名不一致。 |
| out | - | 表示输出的 Tensor , Paddle 无此参数,需要转写。 |

### 转写示例

#### out:指定输出

```python
# Pytorch 写法
torch.special.gammaln(x, out=y)

# Paddle 写法
paddle.assign(paddle.lgamma(x), y)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## [ 仅参数名不一致 ]torch.nn.utils.parametrizations.spectral_norm

### [torch.nn.utils.parametrizations.spectral_norm](https://pytorch.org/docs/stable/generated/torch.nn.utils.parametrizations.spectral_norm.html#torch.nn.utils.parametrizations.spectral_norm)

```python
torch.nn.utils.parametrizations.spectral_norm(module, name='weight', n_power_iterations=1, eps=1e-12, dim=None)
```

### [paddle.nn.utils.spectral_norm](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/nn/utils/spectral_norm_cn.html#spectral-norm)

```python
paddle.nn.utils.spectral_norm(layer, name='weight', n_power_iterations=1, eps=1e-12, dim=None)
```

PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------------ | ------------------ | ---------------------------------------- |
| module | layer | 要添加权重谱归一化的层,仅参数名不一致。 |
| name | name | 权重参数的名字。 |
| n_power_iterations | n_power_iterations | 将用于计算的 SpectralNorm 幂迭代次数。 |
| eps | eps | 用于保证计算中的数值稳定性。 |
| dim | dim | 重塑为矩阵之前应排列到第一个的维度索引。 |
Loading

0 comments on commit 5b05ce7

Please sign in to comment.