PaddlePaddle/PaConvert
Python
Captured source
source ↗PaddlePaddle/PaConvert
Description: PaddlePaddle Code Convert Toolkit. 『飞桨』深度学习代码转换工具
Language: Python
License: Apache-2.0
Stars: 125
Forks: 97
Open issues: 27
Created: 2023-03-02T13:10:22Z
Pushed: 2026-04-30T09:37:34Z
Default branch: master
Fork: no
Archived: no
README:
代码自动转换工具
PaddlePaddle Code Convert Toolkits([PaConvert Github](https://github.com/PaddlePaddle/PaConvert))
🤗 公告 🤗
- ✅ 本工具由 Paddle 官方维护与建设,高效自动化迁移 Pytorch 代码到 PaddlePaddle,欢迎使用
- ✅ 本工具基于 PyTorch 最新 release 与 Paddle develop API 映射表 实现,文档经过详细验证分析,欢迎查阅
- ✅ 具备强大的转换能力,当前支持高达 1600+个torch API、200个torchvision API 的一键转换,基本实现Pytorch全覆盖
- ✅ 通过 100+个Pytorch代码库(超25万行代码)测试,代码行数平均转换率约为95+%(剩余>>>>> self._conv = mmcv.cnn.ConvModule(4, 6, (3, 3))
self._pool = paddle.nn.MaxPool2D(kernel_size=2, stride=1) self._fc1 = paddle.compat.nn.Linear(6 * 25 * 25, 120) self._fc2 = paddle.compat.nn.Linear(120, out_features=84) self._fc3 = paddle.compat.nn.Linear(in_features=84, out_features=num_classes)
def forward(self, x): x = self._conv(x) x = self._pool(x) x = self._fc1(paddle.flatten(x=x, start_axis=1)) x = self._fc2(x) x = self._fc3(x) y = paddle.add(x=x, y=paddle.to_tensor(x)) return y
net = MyNet() sgd = paddle.optimizer.SGD( parameters=net.parameters(), learning_rate=0.01, weight_decay=0.0 ) tmp_lr = paddle.optimizer.lr.MultiStepDecay( milestones=[2, 4, 6], gamma=0.8, learning_rate=sgd.get_lr() ) sgd.set_lr_scheduler(tmp_lr) lr = tmp_lr for i in range(10): x = paddle.rand(shape=[8, 4, 28, 28]) out = net(x).sum() sgd.clear_gradients(set_to_zero=False) out.backward() sgd.step()
#### 日志打印 在转换过程中,终端打印信息如下:
=========================================== PyTorch to Paddle Convert Start ------>: =========================================== Start convert file: /workspace/PaConvert/test.py --> /workspace/PaConvert/paddle_project/test.py [test.py:1] remove 'import torch' [test.py:2] remove 'import torch.nn as nn' [test.py:3] remove 'import torch.optim as optim' [test.py:4] remove 'import torch.nn.functional as F' [test.py:5] remove 'from torch.nn import Linear' [test.py:6] remove 'import mmcv' [test.py] add 'import paddle' in line 1 [test.py:1] [Success] Convert torch.nn.Module to Paddle [test.py:13] [Not Support] convert mmcv.cnn.ConvModule to Paddle is not supported currently [test.py:14] [Success] Convert torch.nn.MaxPool2d to Paddle [test.py:16] [Success] Convert torch.nn.Linear to Paddle [test.py:17] [Success] Convert torch.nn.Linear to Paddle [test.py:18] [Success] Convert torch.nn.Linear to Paddle [test.py:24] [Success] Convert torch.flatten to Paddle [test.py:27] [Success] Convert torch.add to Paddle [test.py:31] [Success] Convert Class Method: torch.nn.Module.parameters to Paddle [test.py:31] [Success] Convert torch.optim.SGD to Paddle [test.py:32] [Success] Convert torch.optim.lr_scheduler.MultiStepLR to Paddle [test.py:35] [Success] Convert torch.rand to Paddle [test.py:36] [Success] Convert Class Method: torch.Tensor.sum to Paddle [test.py:38] [Success] Convert Class Method: torch.nn.Module.zero_grad to Paddle [test.py:39] [Success] Convert Class Method: torch.Tensor.backward to Paddle [test.py:40] [Success] Convert Class Method: torch.optim.Optimizer.step to Paddle, just remain the same Finish convert /workspace/PaConvert/test.py --> /workspace/PaConvert/paddle_project/test.py
=========================================== Convert Summary =========================================== There are 16 Pytorch APIs in this Project: 15 Pytorch APIs have been converted to Paddle successfully! 1 Pytorch APIs are not supported to convert to Paddle currently! Convert Rate is: 93.75%
For these 1 Pytorch APIs that currently do not support to convert, which have been marked by >>> before the line, please refer to [https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/guides/model_convert/convert_from_pytorch/pytorch_api_mapping_cn.html] and convert it by yourself manually. In addition, these APIs will be supported in future.
Thank you to use Paddle Code Convert Tool. You can make any suggestions to us by submitting issues to [https://github.com/PaddlePaddle/PaConvert].
**************************************************************** ______ _____ _ | ___ \ / ____| | | | |_/ /_ _| | ___ _ ____ _____ _ __| |_ | __/ _ | | / _ \| \_ \ \ / / _ \ \__| __| | | | (_| | |___| (_) | | | \ V / __/ | | |_ \_| \__,_|\_____\___/|_| |_|\_/ \___|_| \__|
***********************************************************
转换完成后,会打印 **转换总结** ,包含 **总 API 数、成功转换 API 数、不支持转换 API 数、转换率** 。例如,上述代码里一共有 16 个 Pytorch API(含基于 Pytorch 的第三方库 API 例如 mmcv),其中 15 个被成功转换,仅 1 个不支持转换,因此转换率为 `93.75%` 。 - **对于成功转换的 API**:代码风格会略有变化,会 **补全 API 全名、补全参数关键字、移除注释** 。因为代码在扫描识别的过程中,**注释** 无法识别,会被移除。 - **对于不支持转换的 API**:将 **补全为 Pytorch API 全名**,同时在行前通过 `>>>>>>` 的形式加以标记,用户需要对该 API 进行人工手动转换,然后删除 `>>>>>>` 标记,否则代码无法运行。 ## 经典案例 以下大语言模型代码库已经支持一键 100%转换率,欢迎学习与交流: | 模型名 | Pytorch 代码库地址 | 支持类型 | 详细介绍文档 | | ----------------------------------------------------------| ------------------------------ | ----------- | ---------- | | Llama大模型 | https://github.com/meta-llama/llama.git | 推理 | [Llama](./docs/TypicalCase_Llama.md)| | Qwen大模型 | https://huggingface.co/Qwen/Qwen-7B-Chat | 推理 | [Qwen](./docs/TypicalCase_Qwen.md) | ## 贡献代码 代码自动转换工具([PaConvert](https://github.com/PaddlePaddle/PaConvert))为开源贡献形式,欢迎向我们贡献代码,详细开发步骤请参考 [贡献代码教程](./docs/CONTRIBUTING.md)。