[API Compatibility] Change compatibility apis to ChangePrefixMatcher, inject paddle.enable_compat() -part#895
Open
Manfredss wants to merge 12 commits into
Open
[API Compatibility] Change compatibility apis to ChangePrefixMatcher, inject paddle.enable_compat() -part#895Manfredss wants to merge 12 commits into
Manfredss wants to merge 12 commits into
Conversation
…to patch_compat
… 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>
…to patch_compat
…to patch_compat
…eset 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>
…into patch_compat
…ompat 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.
zhwesky2010
reviewed
Jul 10, 2026
| # enable_compat is injected in transform() (needs all imports in the body). | ||
| # Gate on real torch imports, not paddle_package_list: the latter also holds | ||
| # MAY_TORCH packages (os/einops/setuptools) that need no compat switch. | ||
| if self.imports_map[self.file]["torch_packages"]: |
Collaborator
There was a problem hiding this comment.
简化下代码,看有无更简单的写法,是否只需要修改visit_Module就可以?
Collaborator
|
PR冲突了 |
zhwesky2010
reviewed
Jul 13, 2026
Collaborator
|
注意代码风格简化并贴合已有架构来写,不要让AI随意发挥:
|
zhwesky2010
reviewed
Jul 13, 2026
| return True | ||
| return False | ||
|
|
||
| def _inject_enable_compat(self): |
Collaborator
There was a problem hiding this comment.
这个使用record_scope不能插入吗?插入这个不需要从0开始重写吧
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Docs
For the 23
torch.*APIs that are aligned throughpaddle.compat.*, switch theconverter to the "prefix-only + enable_compat" strategy instead of mapping each to
paddle.compat.Xexplicitly:api_mapping.json— change those 23 entries fromChangeAPIMatcher → paddle.compat.Xto{"Matcher": "ChangePrefixMatcher"}, sotorch.X(...)converts topaddle.X(...)with arguments untouched(e.g.
torch.sort(a, dim=-1)→paddle.sort(a, dim=-1)).transformer/import_transformer.py— injectimport paddle+paddle.enable_compat()right after the imports of any converted file, so theprefix-only
paddle.Xcalls resolve to the torch-alignedpaddle.compat.Ximplementations at runtime. (Included explicitly + deduped so it is valid even
when torch was imported as a submodule alias, e.g.
import torch.nn as nn.)tests/apibase.py—paddle.enable_compat()flips global state (animport torch→ Paddle proxy +paddle.*aliases). Disable it before eachPyTorch reference run (
_ensure_paddle_compat_disabled()), so the referencealways executes against real PyTorch, including across multiple
run()calls inone test. (No-op until Paddle is first imported, so it does not change torch/paddle
import ordering.)
PR APIs
related Paddle PR:
Co-authored by Claude.