[API Compatibility] Change compatibility apis to ChangePrefixMatcher, inject paddle.enable_compat(), fix test environment context pollution -part (#895)
change for enable_compat
add two left api
[API Compatibility] inject enable_compat(level=2) so prefix-converted calls hit the alias
The default enable_compat() (level=1) does not alias paddle., so prefix-converted calls (torch.X -> paddle.X) would run native and reject torch-style kwargs. level=2 turns on the paddle. alias; caller-aware dispatch keeps paddle internals native.
Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com
- [API Compatibility] refine enable_compat injection; activate compat reset
import_transformer.py:
- Inject paddle.enable_compat(level=2) via a transform() post-pass so it lands after the docstring, future imports and the import block (fixes a pre-existing SyntaxError where injected imports preceded
from __future__).- Gate on real torch imports (torch_packages), not paddle_package_list, so a torch-free file importing only os/einops/setuptools no longer gains a needless paddle import or the process-global compat switch.
tests:
- Rename confest.py -> conftest.py so the autouse compat-reset fixture is loaded by pytest (was dead due to the typo).
- Dedupe the disable logic into conftest.disable_paddle_compat(), imported by apibase and called before each torch reference exec.
Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com
update ChangePrefixMatcher for those can passed under enable_compat(level=2) now.
ChangePrefixMatcher for BatchNorm1d/2d/3d
[API Compatibility] fix softmin/allclose/sort matchers under enable_compat
Switch Tensor.allclose and Tensor.sort to ChangePrefixMatcher and keep softmin’s softmax call using torch arg names (input/dim), since compat paddle APIs now accept torch-style args and reject paddle-native ones. Update tests to inject enable_compat(level=2); fix torchvision model_apibase proxy leak around the torch reference forward.
fix code conversion mis-alignment
restore test script
update min-mode consistency baselines
Align expected diffs with compat injection and intentional ChangePrefixMatcher behavior.
Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com
- fix default-mode consistency: dedupe injected imports, update baselines
Deduplicate paddle_package_list in ImportTransformer.visit_Module: since imports are now batched into one record_scope call, its cross-call dedup no longer removes duplicates, producing two ‘import paddle’ lines when a file imports multiple torch-family packages (e.g. torchvision + datasets).
Regenerate default-mode goldens for the paddle.enable_compat(level=2) injection and the intentional compat-prefix removals (equal, BatchNorm1d/2d), matching the min-mode baseline update in f7e1c04.
Co-Authored-By: Claude Fable 5 noreply@anthropic.com
Simplify ImportTransformer.visit_Module to insert paddle imports and a single paddle.enable_compat(level=2) on top of the already-cleaned node, dropping redundant enable_compat/torch-package scans and adding log_info
paddle_package == paddle and add once
update after paddle changed
fix
improve
AvgPool1/2/3d, BatchNorm1/2/3d, MultiheadAttention, Tensor.split to ChangePrefixMathcer.
fix
add fix for split, to help distinguish from string split() and Tensor.split()
fix
delete same conversion param for nn.functional.softmin
enhance three Tensor api tests, enhance Categorical tests, and PReLU tests
still fix split typo
clean
split root cause
fix
add reason for check_value=False
fix backslash
use paddle.compat.split rather than paddle.tensor.split, since enable_compat(level=2) in use
move the logic into apibase and remove conftest.py; adjust other files that could affect running env
simplify logic
manage context, especially for paddle.nn.Softmin
address review feedback: isolate state-polluting tests and align softmin config
- reduce_scatter_tensor: use paddle.split instead of paddle.compat.split
- tests/apibase.py: restore the original test mechanism and run the state-polluting tests separately via the CI isolated list
- softmin: switch to the new enable_compat-based config
- tests/test_Tensor_type.py: compare the converted tensor directly when a dtype is given, without stripping the type-name namespace prefix
- delete comments
Co-authored-by: Claude Opus 4.8 (1M context) noreply@anthropic.com
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802047560号
代码自动转换工具
PaddlePaddle Code Convert Toolkits(**PaConvert Github**)
🤗 公告 🤗
✅ 本工具由 Paddle 官方维护与建设,高效自动化迁移 Pytorch 代码到 PaddlePaddle,欢迎使用
✅ 本工具基于 PyTorch 最新 release 与 Paddle develop API 映射表 实现,文档经过详细验证分析,欢迎查阅
✅ 具备强大的转换能力,当前支持高达 1600+个torch API、200个torchvision API 的一键转换,基本实现Pytorch全覆盖
✅ 通过 100+个Pytorch代码库(超25万行代码)测试,代码行数**平均转换率约为95+%**(剩余<5%需要您手动修改),转换速度极快(不低于2000+行/秒)
概述
本工具能自动将其它深度学习框架训练或推理的代码,转换为 PaddlePaddle 的代码,方便快速自动地 模型代码迁移。目前仅支持自动转换 Pytorch 代码,其它深度学习框架的支持后续新增中。
使用
-i指定输入目录,转换过程不会改动原目录,会将原目录中的文件一一转换到-o指定的输出目录中(如不指定,则默认为./paddle_project),转换时会尽量保持原代码的风格与结构不变。安装使用
通过终端命令行的方式使用:
转换示例
以下面一个简单的 Pytorch Demo 代码为例:
转换前
转换后
日志打印
在转换过程中,终端打印信息如下:
转换完成后,会打印 转换总结 ,包含 总 API 数、成功转换 API 数、不支持转换 API 数、转换率 。例如,上述代码里一共有 16 个 Pytorch API(含基于 Pytorch 的第三方库 API 例如 mmcv),其中 15 个被成功转换,仅 1 个不支持转换,因此转换率为
93.75%。对于成功转换的 API:代码风格会略有变化,会 补全 API 全名、补全参数关键字、移除注释 。因为代码在扫描识别的过程中,注释 无法识别,会被移除。
对于不支持转换的 API:将 补全为 Pytorch API 全名,同时在行前通过
>>>>>>的形式加以标记,用户需要对该 API 进行人工手动转换,然后删除>>>>>>标记,否则代码无法运行。经典案例
以下大语言模型代码库已经支持一键 100%转换率,欢迎学习与交流:
贡献代码
代码自动转换工具(PaConvert)为开源贡献形式,欢迎向我们贡献代码,详细开发步骤请参考 贡献代码教程。