Skip to content
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

映射文档 No.50, 80 #5933

Merged
merged 3 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,34 @@
## [torch 参数更多]torch.cuda.get_rng_state
co63oc marked this conversation as resolved.
Show resolved Hide resolved

### [torch.cuda.get_rng_state](https://pytorch.org/docs/1.13/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/api/paddle/get_cuda_rng_state_cn.html)
co63oc marked this conversation as resolved.
Show resolved Hide resolved

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

其中 PyTorch 相比 Paddle 支持更多其他参数,具体如下:
co63oc marked this conversation as resolved.
Show resolved Hide resolved

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | -------------- | -------------------------------------------------------------------------------------------------- |
| device | - | 返回随机数生成器状态的设备,Paddle 无此参数,暂无转写方式。 |
| Tensor | GeneratorState | 返回参数类型不一致, Pytorch 返回 torch.ByteTensor,Paddle 返回 GeneratorState 列表,需要进行转写。 |
co63oc marked this conversation as resolved.
Show resolved Hide resolved

### 转写示例

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

```python
# PyTorch 写法
x = torch.cuda.get_rng_state(device='cuda')
co63oc marked this conversation as resolved.
Show resolved Hide resolved

# Paddle 写法
x = paddle.get_cuda_rng_state()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## [参数不一致]torch.cuda.get_rng_state_all

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

```python
torch.cuda.get_rng_state_all()
```

### [paddle.get_cuda_rng_state](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/get_cuda_rng_state_cn.html)

```python
paddle.get_cuda_rng_state()
co63oc marked this conversation as resolved.
Show resolved Hide resolved
```

其中 Pytorch 与 Paddle 的返回参数类型不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | -------------- | ---------------------------------------------------------------------------------------------- |
| Tensor | GeneratorState | 返回参数类型不一致, Pytorch 返回 torch.ByteTensor,Paddle 返回 GeneratorState 对象,需要进行转写。 |
co63oc marked this conversation as resolved.
Show resolved Hide resolved

### 转写示例

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

```python
# PyTorch 写法
x = torch.cuda.get_rng_state_all()
co63oc marked this conversation as resolved.
Show resolved Hide resolved

# Paddle 写法
x = paddle.get_cuda_rng_state()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## [torch 参数更多]torch.cuda.set_rng_state

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

```python
torch.cuda.set_rng_state(new_state, device='cuda')
```

### [paddle.set_cuda_rng_state](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/set_cuda_rng_state_cn.html)

```python
paddle.set_cuda_rng_state(state_list)
co63oc marked this conversation as resolved.
Show resolved Hide resolved
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| --------- | ------------ | ------------------------------------------------------------------------------------------------ |
| new_state | - | 类型为 torch.ByteTensor,表示设备需要的状态,Paddle 无此参数,需要进行转写。 |
| device | - | 指定随机数生成器状态的设备,Paddle 无此参数,暂无转写方式。 |
| - | state_list | 类型为 GeneratorState 列表,需要设置的随机数生成器状态信息列表,PyTorch 无此参数,需要进行转写。 |
co63oc marked this conversation as resolved.
Show resolved Hide resolved

### 转写示例

#### 参数类型不同

```python
# PyTorch 写法
x = torch.cuda.get_rng_state(device='cuda')
co63oc marked this conversation as resolved.
Show resolved Hide resolved
torch.cuda.set_rng_state(x, device='cuda')

# Paddle 写法
x = paddle.get_cuda_rng_state()
paddle.set_cuda_rng_state(x)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## [参数不一致]torch.cuda.set_rng_state_all

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

```python
torch.cuda.set_rng_state_all(new_states)
```

### [paddle.set_cuda_rng_state](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/set_cuda_rng_state_cn.html)

```python
paddle.set_cuda_rng_state(state_list)
co63oc marked this conversation as resolved.
Show resolved Hide resolved
```

其中 Pytorch 与 Paddle 的参数类型不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ---------- | ------------ | --------------------------------------------------------------------------------------------------------------- |
| new_states | state_list | 表示每个设备需要的状态,PyTorch 类型为 torch.ByteTensor 列表,Paddle 类型为 GeneratorState 列表,需要进行转写。 |

### 转写示例

#### 参数类型不同

```python
# PyTorch 写法
x = torch.cuda.get_rng_state_all()
torch.cuda.set_rng_state_all(x)

# Paddle 写法
x = paddle.get_cuda_rng_state()
paddle.set_cuda_rng_state(x)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## [torch 参数更多]torch.distributions.transforms.ComposeTransform

### [torch.distributions.transforms.ComposeTransform](https://pytorch.org/docs/1.13/distributions.html#torch.distributions.transforms.ComposeTransform)

```python
torch.distributions.transforms.ComposeTransform(parts, cache_size=0)
```

### [paddle.distribution.ChainTransform](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/distribution/ChainTransform_cn.html)

```python
paddle.distribution.ChainTransform(transforms)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ---------- | ------------ | ------------------------------------------------ |
| parts | transforms | 输入的变换序列,仅参数名不一致。 |
| cache_size | - | 表示 cache 大小,Paddle 无此参数,暂无转写方式。 |
co63oc marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## [torch 参数更多]torch.nn.Hardswish

### [torch.nn.Hardswish](https://pytorch.org/docs/1.13/generated/torch.nn.Hardswish.html#torch.nn.Hardswish)

```python
torch.nn.Hardswish(inplace=False)
```

### [paddle.nn.Hardswish](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/Hardswish_cn.html)

```python
paddle.nn.Hardswish(name=None)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ----------------------------------------------------------------------------------------------------------- |
| inplace | - | 在不更改变量的内存地址的情况下,直接修改变量的值,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## [torch 参数更多]torch.nn.Hardtanh

### [torch.nn.Hardtanh](https://pytorch.org/docs/1.13/generated/torch.nn.Hardtanh.html#torch.nn.Hardtanh)

```python
torch.nn.Hardtanh(min_val=- 1.0, max_val=1.0, inplace=False)
```

### [paddle.nn.Hardtanh](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/Hardtanh_cn.html)

```python
paddle.nn.Hardtanh(min=- 1.0, max=1.0, name=None)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ----------------------------------------------------------------------------------------------------------- |
| min_val | min | Hardtanh 激活计算公式中的 min 值,仅参数名不一致。 |
| max_val | max | Hardtanh 激活计算公式中的 max 值,仅参数名不一致。 |
| inplace | - | 在不更改变量的内存地址的情况下,直接修改变量的值,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## [torch 参数更多]torch.jit.load

### [torch.jit.load](https://pytorch.org/docs/1.13/generated/torch.jit.load.html#torch.jit.load)

```python
torch.jit.load(f, map_location=None, _extra_files=None)
```

### [paddle.jit.load](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/jit/load_cn.html)

```python
paddle.jit.load(path, **configs)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------------ |
| f | - | 文件操作对象,Paddle 无此参数,暂无转写方式。 |
| map_location | - | 存储位置,Paddle 无此参数,暂无转写方式。 |
| \_extra_files | - | 额外加载的文件,Paddle 无此参数,暂无转写方式。 |
| - | path | 载入模型的路径前缀,PyTorch 无此参数,暂无转写方式。 |
| - | configs | 其他用于兼容的载入配置选项,PyTorch 无此参数,暂无转写方式。 |
co63oc marked this conversation as resolved.
Show resolved Hide resolved

### 转写示例

#### 参数用法不同

```python
# PyTorch 写法:
torch.jit.load('scriptmodule.pt')

# Paddle 写法:
paddle.jit.load('example_model/linear')
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
## [torch 参数更多]torch.onnx.export

### [torch.onnx.export](https://pytorch.org/docs/1.13/onnx.html#torch.onnx.export)

```python
torch.onnx.export(model, args, f, export_params=True, verbose=False, training=<TrainingMode.EVAL: 0>, input_names=None, output_names=None, operator_export_type=<OperatorExportTypes.ONNX: 0>, opset_version=None, do_constant_folding=True, dynamic_axes=None, keep_initializers_as_inputs=None, custom_opsets=None, export_modules_as_functions=False)
```

### [paddle.onnx.export](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/onnx/export_cn.html)

```python
paddle.onnx.export(layer, path, input_spec=None, opset_version=9, **configs)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| --------------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| model | layer | 导出的模型,PyTorch 类型为 torch.nn.Module, torch.jit.ScriptModule 或 torch.jit.ScriptFunction,Paddle 为 Layer 对象,需要进行转写。 |
| args | - | 模型参数,Paddle 无此参数,暂无转写方式。 |
| f | path | PyTorch 为存储模型路径,Paddle 为存储模型的路径前缀,需要进行转写。 |
| export_params | - | 是否导出参数,Paddle 无此参数,暂无转写方式。 |
| verbose | - | 是否输出详细信息,Paddle 无此参数,暂无转写方式。 |
| training | - | 训练模式,Paddle 无此参数,暂无转写方式。 |
| input_names | - | 输入节点名称列表,Paddle 无此参数,暂无转写方式。 |
| output_names | - | 输出节点名称列表,Paddle 无此参数,暂无转写方式。 |
| operator_export_type | - | 操作导出类型,Paddle 无此参数,暂无转写方式。 |
| opset_version | opset_version | opset 版本。 |
| do_constant_folding | - | 是否进行 constant-folding 优化,Paddle 无此参数,暂无转写方式。 |
| dynamic_axes | - | 是否动态维度,Paddle 无此参数,暂无转写方式。 |
| keep_initializers_as_inputs | - | 是否增加初始化器到输入,Paddle 无此参数,暂无转写方式。 |
| custom_opsets | - | 自定义 opset,Paddle 无此参数,暂无转写方式。 |
| export_modules_as_functions | - | 是否导出模型为 functions,Paddle 无此参数,暂无转写方式。 |
| - | input_spec | 描述存储模型 forward 方法的输入,PyTorch 无此参数,Paddle 保持默认即可。 |
| - | configs | 其他用于兼容的存储配置选项,PyTorch 无此参数,Paddle 保持默认即可。 |
co63oc marked this conversation as resolved.
Show resolved Hide resolved

### 转写示例

#### 参数类型不同

```python
# PyTorch 写法
torch.onnx.export(
model,
(
x,
{y: z},
{}
),
"test.onnx.pb"
)

# Paddle 写法
model = Logic()
x = paddle.to_tensor([1])
y = paddle.to_tensor([2])
# Static and run model.
paddle.jit.to_static(model)
out = model(x, y, z=True)
paddle.onnx.export(model, 'pruned', input_spec=[x], output_spec=[out])
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
## [torch 参数更多]torch.profiler.profile

### [torch.profiler.profile](https://pytorch.org/docs/1.13/profiler.html#torch.profiler.profile)

```python
torch.profiler.profile(*, activities=None, schedule=None, on_trace_ready=None, record_shapes=False, profile_memory=False, with_stack=False, with_flops=False, with_modules=False, experimental_config=None, use_cuda=None)
```

### [paddle.profiler.Profiler](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/profiler/Profiler_cn.html)

```python
paddle.profiler.Profiler(*, targets=None, scheduler=None, on_trace_ready=None, record_shapes=False, profile_memory=False, timer_only=False)
```

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

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------------- | -------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| activities | targets | 指定性能分析所要分析的设备,PyTorch 为 torch.profiler.ProfilerActivity 类型,Paddle 为 paddle.profiler.ProfilerTarget 类型。 |
| schedule | scheduler | 如果是 Callable 对象,代表是性能分析器状态的调度器,仅参数名不一致。 |
| on_trace_ready | on_trace_ready | 处理性能分析器的回调函数。 |
| record_shapes | record_shapes | 如果设置为 True, 则会开启收集框架算子输入张量的 shape。 |
| profile_memory | profile_memory | 如果设置为 True, 则会开启收集显存分析的数据。 |
| with_stack | - | 记录 source 信息,Paddle 无此参数,暂无转写方式。 |
| with_flops | - | 使用公式来估计浮点计算,Paddle 无此参数,暂无转写方式。 |
| with_modules | - | 记录模块层次,Paddle 无此参数,暂无转写方式。 |
| experimental_config | - | 实验性特征配置,Paddle 无此参数,暂无转写方式。 |
| use_cuda | - | 已废弃,Paddle 无此参数,暂无转写方式。 |
| - | timer_only | 如果设置为 True,将只统计模型的数据读取和每一个迭代所消耗的时间,而不进行性能分析,PyTorch 无此参数,Paddle 保持默认即可。 |

### 转写示例

#### 参数类型不同

```python
# PyTorch 写法:
with torch.profiler.profile(
activities=[
torch.profiler.ProfilerActivity.CPU,
torch.profiler.ProfilerActivity.CUDA,
]
) as p:
code_to_profile()
print(p.key_averages().table(
co63oc marked this conversation as resolved.
Show resolved Hide resolved
sort_by="self_cuda_time_total", row_limit=-1))

# Paddle 写法:
import paddle.profiler as profiler
with profiler.Profiler(
targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU],
scheduler = (2, 5),
on_trace_ready = profiler.export_chrome_tracing('./log')) as p:
for iter in range(10):
#train()
p.step()
```
Loading