diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 9484daa39..32199f0a8 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -487,18 +487,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.allclose": { - "Matcher": "AllcloseMatcher", - "paddle_api": "paddle.Tensor.allclose", - "min_input_args": 1, - "args_list": [ - "other", - "rtol", - "atol", - "equal_nan" - ], - "kwargs_change": { - "other": "y" - } + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.amax": { "Matcher": "ChangePrefixMatcher" @@ -1533,18 +1522,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.max": { - "Matcher": "TensorMaxMatcher", - "paddle_api": "paddle.Tensor.max", - "min_input_args": 0, - "args_list": [ - "dim", - "keepdim", - "other" - ], - "kwargs_change": { - "dim": "axis", - "other": "y" - } + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.maximum": { "Matcher": "ChangePrefixMatcher" @@ -1553,33 +1531,10 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.median": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.Tensor.median", - "min_input_args": 0, - "args_list": [ - "dim", - "keepdim" - ], - "kwargs_change": { - "dim": "axis" - }, - "unsupport_args": [ - "dim" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.min": { - "Matcher": "TensorMinMatcher", - "paddle_api": "paddle.Tensor.min", - "min_input_args": 0, - "args_list": [ - "dim", - "keepdim", - "other" - ], - "kwargs_change": { - "dim": "axis", - "other": "y" - } + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.minimum": { "Matcher": "ChangePrefixMatcher" @@ -1737,9 +1692,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.numel": { - "Matcher": "Func2Attribute", - "paddle_api": "paddle.Tensor.size", - "min_input_args": 0 + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.numpy": { "Matcher": "ChangePrefixMatcher" @@ -2008,20 +1961,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.sort": { - "Matcher": "SortMatcher", - "paddle_api": "paddle.Tensor.sort", - "min_input_args": 0, - "args_list": [ - "dim", - "descending", - "*", - "stable", - "dim", - "descending" - ], - "kwargs_change": { - "dim": "axis" - } + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.sparse_dim": { "Matcher": "ChangePrefixMatcher" @@ -2032,17 +1972,7 @@ "torch.Tensor.sparse_resize_": {}, "torch.Tensor.sparse_resize_and_clear_": {}, "torch.Tensor.split": { - "Matcher": "TensorSplitMatcher", - "paddle_api": "paddle.Tensor.split", - "min_input_args": 1, - "args_list": [ - "split_size", - "dim" - ], - "kwargs_change": { - "split_size": "num_or_sections", - "dim": "axis" - } + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.split_with_sizes": { "Matcher": "ChangePrefixMatcher" @@ -2250,14 +2180,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.type": { - "Matcher": "TensorTypeMatcher", - "paddle_api": "paddle.Tensor.astype", - "min_input_args": 0, - "args_list": [ - "dtype", - "non_blocking", - "**kwargs" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.type_as": { "Matcher": "ChangePrefixMatcher" @@ -2883,8 +2806,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.allclose": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.allclose" + "Matcher": "ChangePrefixMatcher" }, "torch.alpha_dropout": { "Matcher": "GenericMatcher", @@ -4213,8 +4135,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.distributions.Categorical": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.distributions.Categorical" + "Matcher": "ChangePrefixMatcher" }, "torch.distributions.Distribution": { "Matcher": "ChangePrefixMatcher" @@ -4288,8 +4209,7 @@ } }, "torch.distributions.categorical.Categorical": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.distributions.categorical.Categorical" + "Matcher": "ChangePrefixMatcher" }, "torch.distributions.cauchy.Cauchy": { "Matcher": "GenericMatcher", @@ -4860,8 +4780,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.equal": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.equal" + "Matcher": "ChangePrefixMatcher" }, "torch.erf": { "Matcher": "ChangePrefixMatcher" @@ -6040,8 +5959,7 @@ "min_input_args": 1 }, "torch.max": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.max" + "Matcher": "ChangePrefixMatcher" }, "torch.max_pool1d": { "Matcher": "ChangeAPIMatcher", @@ -6062,15 +5980,13 @@ "Matcher": "ChangePrefixMatcher" }, "torch.median": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.median" + "Matcher": "ChangePrefixMatcher" }, "torch.meshgrid": { "Matcher": "ChangePrefixMatcher" }, "torch.min": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.min" + "Matcher": "ChangePrefixMatcher" }, "torch.minimum": { "Matcher": "ChangePrefixMatcher" @@ -6147,8 +6063,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nanmedian": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nanmedian" + "Matcher": "ChangePrefixMatcher" }, "torch.nanquantile": { "Matcher": "ChangePrefixMatcher" @@ -6224,16 +6139,13 @@ ] }, "torch.nn.AvgPool1d": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.AvgPool1d" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.AvgPool2d": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.AvgPool2d" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.AvgPool3d": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.AvgPool3d" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.BCELoss": { "Matcher": "ChangePrefixMatcher" @@ -6242,16 +6154,13 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.BatchNorm1d": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.BatchNorm1d" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.BatchNorm2d": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.BatchNorm2d" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.BatchNorm3d": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.BatchNorm3d" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.Bilinear": { "Matcher": "GenericMatcher", @@ -6638,8 +6547,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.Linear": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.Linear" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.LocalResponseNorm": { "Matcher": "ChangePrefixMatcher" @@ -6838,26 +6746,13 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.MultiheadAttention": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.MultiheadAttention" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.NLLLoss": { "Matcher": "ChangePrefixMatcher" }, "torch.nn.PReLU": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.nn.PReLU", - "args_list": [ - "num_parameters", - "init", - "device", - "dtype" - ], - "kwargs_change": { - "device": "", - "dtype": "" - }, - "min_input_args": 0 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.PairwiseDistance": { "Matcher": "ChangePrefixMatcher" @@ -7018,14 +6913,13 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.Softmax": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.Softmax" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.Softmax2d": { "Matcher": "GenericMatcher", "paddle_api": "paddle.nn.Softmax", "paddle_default_kwargs": { - "axis": -3 + "dim": -3 }, "min_input_args": 0 }, @@ -7243,8 +7137,7 @@ } }, "torch.nn.Unfold": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.Unfold" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.Upsample": { "Matcher": "ChangePrefixMatcher" @@ -7661,8 +7554,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.linear": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.functional.linear" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.local_response_norm": { "Matcher": "GenericMatcher", @@ -7680,8 +7572,7 @@ } }, "torch.nn.functional.log_softmax": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.functional.log_softmax" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.logsigmoid": { "Matcher": "ChangePrefixMatcher" @@ -7777,8 +7668,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.pad": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.functional.pad" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.pairwise_distance": { "Matcher": "ChangePrefixMatcher" @@ -7849,8 +7739,7 @@ } }, "torch.nn.functional.scaled_dot_product_attention": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.functional.scaled_dot_product_attention" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.selu": { "Matcher": "GenericMatcher", @@ -7887,8 +7776,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.softmax": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.functional.softmax" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.softmin": { "Matcher": "FSoftMinMatcher", @@ -7900,10 +7788,7 @@ "dtype" ], "kwargs_change": { - "input": "x", - "dim": "axis", - "_stacklevel": "", - "dtype": "dtype" + "_stacklevel": "" }, "min_input_args": 1 }, @@ -7972,8 +7857,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.unfold": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.nn.functional.unfold" + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.upsample": { "Matcher": "GenericMatcher", @@ -9073,8 +8957,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.seed": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.seed" + "Matcher": "ChangePrefixMatcher" }, "torch.select": { "Matcher": "SelectMatcher", @@ -9390,8 +9273,7 @@ "min_input_args": 2 }, "torch.sort": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.sort" + "Matcher": "ChangePrefixMatcher" }, "torch.sparse.FloatTensor": { "Matcher": "GenericMatcher", @@ -9766,8 +9648,7 @@ }, "torch.special.zeta": {}, "torch.split": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.split" + "Matcher": "ChangePrefixMatcher" }, "torch.sqrt": { "Matcher": "ChangePrefixMatcher" @@ -10002,16 +9883,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.unique": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.compat.unique", - "min_input_args": 1, - "args_list": [ - "input", - "sorted", - "return_inverse", - "return_counts", - "dim" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.unique_consecutive": { "Matcher": "ChangePrefixMatcher" diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index d9baa116a..b4305628d 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -395,7 +395,11 @@ def get_paddle_class_nodes(self, func, args, kwargs): if self.transformer.mode == "min": self.paddle_api = astor.to_source(func).strip("\n") else: - self.parse_func(func) + func_str = astor.to_source(func).strip("\n") + paddle_api = self.get_paddle_api() + paddle_class = func_str.rsplit(".", 1)[0] + paddle_class_api = paddle_api.rsplit(".", 1)[0] + self.paddle_api = paddle_api.replace(paddle_class_api, paddle_class, 1) args = self.parse_args(args) kwargs = self.parse_kwargs(kwargs, allow_none=True) @@ -5799,7 +5803,9 @@ def reduce_scatter_tensor(output, input, op, group, async_op): if input.shape[0] == world_size: input_list = paddle.unstack(input, axis=0) else: - input_list = paddle.split(input, num_or_sections=world_size, axis=0) + input_list = paddle.tensor.split( + input, num_or_sections=world_size, axis=0 + ) paddle.distributed.reduce_scatter(output, input_list, op, group, async_op) """ ) diff --git a/paconvert/attribute_mapping.json b/paconvert/attribute_mapping.json index 5fc3f259b..b5122271a 100644 --- a/paconvert/attribute_mapping.json +++ b/paconvert/attribute_mapping.json @@ -33,8 +33,7 @@ "torch.Tensor.is_meta": {}, "torch.Tensor.is_quantized": {}, "torch.Tensor.is_sparse": { - "Matcher": "Attribute2Func", - "paddle_api": "paddle.Tensor.is_sparse" + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.is_sparse_csr": { "Matcher": "Attribute2Func", diff --git a/paconvert/transformer/import_transformer.py b/paconvert/transformer/import_transformer.py index 99a20840b..a66865138 100644 --- a/paconvert/transformer/import_transformer.py +++ b/paconvert/transformer/import_transformer.py @@ -505,7 +505,6 @@ def visit_Module(self, node): 'import torch_package' has been removed already, add 'import paddle_package' """ super(ImportTransformer, self).generic_visit(node) - line_NO = 1 paddle_package_list = [] for torch_package in self.imports_map[self.file]["torch_packages"]: paddle_package_list.append( @@ -515,13 +514,29 @@ def visit_Module(self, node): for may_torch_package in self.imports_map[self.file]["may_torch_packages"]: paddle_package_list.append(may_torch_package) - for paddle_package in paddle_package_list: + import_code = "" + enable_compat_code = "" + for paddle_package in dict.fromkeys(paddle_package_list): + import_code += f"import {paddle_package}\n" + if paddle_package == "paddle": + enable_compat_code = "paddle.enable_compat(level=2)\n" + + import_code += enable_compat_code + + if import_code: log_info( self.logger, - f"add 'import {paddle_package}' in line {line_NO}", + f"add '{import_code.strip()}' ", self.file_name, ) + import_end = 1 if ast.get_docstring(node, clean=False) else 0 + while ( + import_end < len(node.body) + and isinstance(node.body[import_end], ast.ImportFrom) + and node.body[import_end].module == "__future__" + ): + import_end += 1 self.record_scope( - (self.root, "body", 0), ast.parse(f"import {paddle_package}").body + (self.root, "body", import_end), ast.parse(import_code).body ) - line_NO += 1 + return node diff --git a/scripts/ci/run_cpu_unittest.sh b/scripts/ci/run_cpu_unittest.sh index 5ec27003f..6d12cfa69 100644 --- a/scripts/ci/run_cpu_unittest.sh +++ b/scripts/ci/run_cpu_unittest.sh @@ -38,6 +38,10 @@ echo '************************************************************************** echo "Checking code cpu unit test by pytest ..." set +e +ISOLATED_TESTS=( + tests/test_set_num_interop_threads.py +) + PYTEST_IGNORE=( --ignore=tests/test_backends_cuda_is_built.py --ignore=tests/test_cuda_is_bf16_supported.py @@ -49,8 +53,16 @@ PYTEST_IGNORE=( --ignore=tests/test_hub_load.py --ignore=tests/test_hub_load_state_dict_from_url.py ) +for test_file in "${ISOLATED_TESTS[@]}"; do + PYTEST_IGNORE+=(--ignore="${test_file}") +done + +python -m pytest -v -s -p no:warnings "${ISOLATED_TESTS[@]}" \ + --reruns=3 2>&1 | tee pytest.log +isolated_errors=${PIPESTATUS[0]} -python -m pytest -v -s -p no:warnings "${PYTEST_IGNORE[@]}" --reruns=3 ./tests 2>&1 | tee pytest.log +python -m pytest -v -s -p no:warnings "${PYTEST_IGNORE[@]}" \ + --reruns=3 ./tests 2>&1 | tee -a pytest.log check_errors=${PIPESTATUS[0]} if [ ${check_errors} -ne 0 ]; then echo "Rerun CPU unit test" @@ -58,6 +70,10 @@ if [ ${check_errors} -ne 0 ]; then check_errors=${PIPESTATUS[0]} fi +if [ ${isolated_errors} -ne 0 ]; then + check_errors=${isolated_errors} +fi + echo '******************************************************************************' if [ ${check_errors} -ne 0 ]; then echo "Your PR code CPU unit test check FAILED" diff --git a/scripts/ci/run_gpu_unittest.sh b/scripts/ci/run_gpu_unittest.sh index f0a7c4c96..789fc7607 100644 --- a/scripts/ci/run_gpu_unittest.sh +++ b/scripts/ci/run_gpu_unittest.sh @@ -39,6 +39,13 @@ echo '************************************************************************** echo "Checking code gpu unit test by pytest ..." set +e +ISOLATED_TESTS=( + tests/test_cuda_stream.py + tests/test_cuda_CUDAGraph.py + tests/test_cuda_set_stream.py + tests/test_set_num_interop_threads.py +) + PYTEST_IGNORE=( --ignore=tests/test_hub_download_url_to_file.py --ignore=tests/test_hub_help.py @@ -46,33 +53,26 @@ PYTEST_IGNORE=( --ignore=tests/test_hub_load.py --ignore=tests/test_hub_load_state_dict_from_url.py ) +for test_file in "${ISOLATED_TESTS[@]}"; do + PYTEST_IGNORE+=(--ignore="${test_file}") +done -# Run test_cuda_stream.py separately and FIRST (GPU state is clean), -# as it can segfault when run after other GPU tests (Paddle FullKernel issue). -# Running in isolation prevents the segfault from killing the entire test batch. -python -m pytest -v -s -p no:warnings tests/test_cuda_stream.py 2>&1 | tee pytest.log -stream_exit=${PIPESTATUS[0]} +python -m pytest -v -s -p no:warnings "${ISOLATED_TESTS[@]}" \ + -n 1 --reruns=3 2>&1 | tee pytest.log +isolated_errors=${PIPESTATUS[0]} -# Run test_cuda_CUDAGraph.py in its own process as well: CUDA graph -# capture/replay leaves Paddle GPU state that can natively crash later -# AMP tests (e.g. test_cuda_amp_GradScaler) in the same pytest worker. -python -m pytest -v -s -p no:warnings tests/test_cuda_CUDAGraph.py 2>&1 | tee -a pytest.log -cudagraph_exit=${PIPESTATUS[0]} - -python -m pytest -v -s -p no:warnings "${PYTEST_IGNORE[@]}" --ignore=tests/test_cuda_stream.py --ignore=tests/test_cuda_CUDAGraph.py -n 1 --reruns=3 ./tests 2>&1 | tee -a pytest.log +python -m pytest -v -s -p no:warnings "${PYTEST_IGNORE[@]}" \ + -n 1 --reruns=3 ./tests 2>&1 | tee -a pytest.log check_errors=${PIPESTATUS[0]} if [ ${check_errors} -ne 0 ]; then echo "Rerun GPU unit test" - python -m pytest -v -s -p no:warnings "${PYTEST_IGNORE[@]}" --ignore=tests/test_cuda_stream.py --ignore=tests/test_cuda_CUDAGraph.py -n 1 --lf ./tests 2>&1 | tee -a pytest.log + python -m pytest -v -s -p no:warnings "${PYTEST_IGNORE[@]}" \ + -n 1 --lf ./tests 2>&1 | tee -a pytest.log check_errors=${PIPESTATUS[0]} fi -# Propagate isolated test failures if any -if [ ${stream_exit} -ne 0 ]; then - check_errors=${stream_exit} -fi -if [ ${cudagraph_exit} -ne 0 ]; then - check_errors=${cudagraph_exit} +if [ ${isolated_errors} -ne 0 ]; then + check_errors=${isolated_errors} fi echo '******************************************************************************' diff --git a/tests/apibase.py b/tests/apibase.py index a882e18d9..a2ad73eea 100644 --- a/tests/apibase.py +++ b/tests/apibase.py @@ -16,6 +16,7 @@ import os import re import sys +from contextlib import nullcontext import numpy as np @@ -24,6 +25,13 @@ from paconvert.converter import Converter +def _pytorch_exec_guard(): + paddle = sys.modules.get("paddle") + if paddle is None: + return nullcontext() + return paddle.use_compat_guard(enable=False) + + class APIBase(object): def __init__(self, pytorch_api) -> None: """ @@ -87,51 +95,59 @@ def run( elif compared_tensor_names: pytorch_ns = {} try: - exec(pytorch_code, pytorch_ns) + with _pytorch_exec_guard(): + exec(pytorch_code, pytorch_ns) except Exception as e: raise RuntimeError(f"Failed to execute pytorch code:\n{e}") pytorch_result = [pytorch_ns[name] for name in compared_tensor_names] pytorch_ns.clear() - paddle_ns = {} - try: - exec(paddle_code, paddle_ns) - except Exception as e: - raise RuntimeError(f"Failed to execute paddle code:\n{e}") - paddle_result = [paddle_ns[name] for name in compared_tensor_names] - paddle_ns.clear() + import paddle - for i in range(len(compared_tensor_names)): + paddle_ns = {} + with paddle.use_compat_guard(enable=False): try: - self.compare( - self.pytorch_api, - pytorch_result[i], - paddle_result[i], - check_value, - check_shape, - check_dtype, - check_stop_gradient, - rtol, - atol, - ) + exec(paddle_code, paddle_ns) except Exception as e: - raise AssertionError(f"Unable to align results: {e}") + raise RuntimeError(f"Failed to execute paddle code:\n{e}") + paddle_result = [paddle_ns[name] for name in compared_tensor_names] + paddle_ns.clear() + + for i in range(len(compared_tensor_names)): + try: + self.compare( + self.pytorch_api, + pytorch_result[i], + paddle_result[i], + check_value, + check_shape, + check_dtype, + check_stop_gradient, + rtol, + atol, + ) + except Exception as e: + raise AssertionError(f"Unable to align results: {e}") else: pytorch_ns = {} try: - exec(pytorch_code, pytorch_ns) + with _pytorch_exec_guard(): + exec(pytorch_code, pytorch_ns) except Exception as e: raise RuntimeError(f"Failed to execute pytorch code:\n{e}") finally: pytorch_ns.clear() + import paddle + paddle_ns = {} - try: - exec(paddle_code, paddle_ns) - except Exception as e: - raise RuntimeError(f"Failed to execute paddle code:\n{e}") - finally: - paddle_ns.clear() + with paddle.use_compat_guard(enable=False): + try: + exec(paddle_code, paddle_ns) + except Exception as e: + raise RuntimeError(f"Failed to execute paddle code:\n{e}") + finally: + paddle_ns.clear() def compare( self, diff --git a/tests/code_library/code_case/minmode_code_case/diff/combine.txt b/tests/code_library/code_case/minmode_code_case/diff/combine.txt index 04f5f6316..859cc5e7a 100644 --- a/tests/code_library/code_case/minmode_code_case/diff/combine.txt +++ b/tests/code_library/code_case/minmode_code_case/diff/combine.txt @@ -1,81 +1,127 @@ ---- tests/code_library/code_case/minmode_code_case/torch_code/combine.py -+++ tests/code_library/code_case/minmode_code_case/paddle_code/combine.py -@@ -1,3 +1,4 @@ +--- /workspace/paconvert/tests/code_library/code_case/minmode_code_case/torch_code/combine.py ++++ /workspace/paconvert/tests/code_library/code_case/minmode_code_case/convert_paddle_code/combine.py +@@ -1,3 +1,5 @@ +import paddle + ++paddle.enable_compat(level=2) + import torch + import torch.nn as nn + from torch.autograd import Function -@@ -190,7 +191,7 @@ - result = F.grid_sample(input=x, grid=grid, mode='bilinear', padding_mode= - 'border', align_corners=True) - print('#########################case27#########################') --l = nn.Linear(2, 2) -+l = paddle.compat.nn.Linear(2, 2) - net = nn.Sequential(OrderedDict([('wfs', l), ('wfs1', l)])) - memo = set() - z = net.named_modules(memo=memo, prefix='wfs', remove_duplicate=False) -@@ -461,19 +462,19 @@ + +@@ -461,15 +463,15 @@ result = F.softplus(x) + print('#########################case75#########################') + out = torch.empty([6], dtype=torch.int32) + -result = torch.randperm(6, out=out, dtype=torch.int32, layout=torch.strided, + - device=torch.device('cpu'), pin_memory=False, requires_grad=False) + +result = torch.randperm(6, out=out, dtype=torch.int32, device=torch.device( + + 'cpu'), pin_memory=False, requires_grad=False) + print('#########################case76#########################') + torch.cuda.manual_seed(123) + -result = torch.cuda.initial_seed() + +result = paddle.get_cuda_rng_state()[0].current_seed() + print('#########################case77#########################') + x = torch.tensor([1.0, 2.0, 3.0]) + result = x.new_empty(size=(2, 3), dtype=torch.float64, device='cpu', + - requires_grad=True, layout=torch.strided, pin_memory=False) + + requires_grad=True, pin_memory=False) + print('#########################case78#########################') + x = torch.tensor([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0], [3.0, 3.0, 3.0]], + requires_grad=True) --linear = torch.nn.Linear(3, 4, bias=False) -+linear = paddle.compat.nn.Linear(3, 4, bias=False) - linear.weight.data.fill_(0.1) - y = linear(x) - y.detach_() -@@ -516,7 +517,7 @@ + +@@ -516,7 +518,7 @@ results = x.as_strided(size=(2, 2), stride=(2, 2), storage_offset=0) + print('#########################case86#########################') + torch.cuda.manual_seed_all(123) + -result = torch.cuda.initial_seed() + +result = paddle.get_cuda_rng_state()[0].current_seed() + print('#########################case87#########################') + result = torch.as_tensor([1, 2, 3]) + print('#########################case88#########################') -@@ -527,8 +528,8 @@ + +@@ -527,8 +529,8 @@ print('#########################case89#########################') + a = 3 + out = torch.tensor([2.0, 3.0], dtype=torch.float64) + -result = torch.rand(size=(a, a), generator=None, out=out, dtype=torch. + - float64, device=torch.device('cpu'), requires_grad=True, pin_memory=False) + +result = torch.rand(size=(a, a), out=out, dtype=torch.float64, device=torch + + .device('cpu'), requires_grad=True, pin_memory=False) + print('#########################case90#########################') + x = torch.tensor([[-1.0813, -0.8619, 0.7105], [0.0935, 0.138, 2.2112], [- + 0.3409, -0.9828, 0.0289]]) -@@ -542,8 +543,8 @@ + +@@ -542,8 +544,8 @@ print('#########################case92#########################') + a = 3 + out = torch.tensor([2.0, 3.0], dtype=torch.float64) + -result = torch.randn(size=(a, a), generator=None, out=out, dtype=torch. + - float64, device=torch.device('cpu'), requires_grad=True, pin_memory=False) + +result = torch.randn(size=(a, a), out=out, dtype=torch.float64, device= + + torch.device('cpu'), requires_grad=True, pin_memory=False) + print('#########################case93#########################') + a = 3 + out = torch.tensor([2.0, 3.0], dtype=torch.float64) -@@ -555,7 +556,7 @@ + +@@ -555,7 +557,7 @@ result = x.cross(other=y, dim=1) + print('#########################case95#########################') + result = torch.full_like(torch.empty(2, 3), 6, dtype=torch.float64, device= + - 'cpu', requires_grad=True, memory_format=torch.preserve_format) + + 'cpu', requires_grad=True) + print('#########################case96#########################') + result = torch.ones_like(torch.empty(2, 3), dtype=torch.float64, + requires_grad=True, device=None) diff --git a/tests/code_library/code_case/minmode_code_case/diff/keep_same.txt b/tests/code_library/code_case/minmode_code_case/diff/keep_same.txt index f35dd6d3d..f445da44f 100644 --- a/tests/code_library/code_case/minmode_code_case/diff/keep_same.txt +++ b/tests/code_library/code_case/minmode_code_case/diff/keep_same.txt @@ -1,8 +1,10 @@ ---- tests/code_library/code_case/minmode_code_case/torch_code/keep_same.py -+++ tests/code_library/code_case/minmode_code_case/paddle_code/keep_same.py -@@ -1,3 +1,4 @@ +--- /workspace/paconvert/tests/code_library/code_case/minmode_code_case/torch_code/keep_same.py ++++ /workspace/paconvert/tests/code_library/code_case/minmode_code_case/convert_paddle_code/keep_same.py +@@ -1,3 +1,5 @@ +import paddle ++paddle.enable_compat(level=2) + import torch import copy diff --git a/tests/code_library/code_case/paddle_code/api_paddle_Generator.py b/tests/code_library/code_case/paddle_code/api_paddle_Generator.py index d88f12db0..fca12b0a1 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_Generator.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_Generator.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") g_cpu = paddle.Generator() print("#########################case2#########################") diff --git a/tests/code_library/code_case/paddle_code/api_paddle_Size.py b/tests/code_library/code_case/paddle_code/api_paddle_Size.py index 138e8a43d..bb3223f25 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_Size.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_Size.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") print(paddle.Size([2, 8, 64, 64])) print("#########################case2#########################") diff --git a/tests/code_library/code_case/paddle_code/api_paddle_Tensor2Float.py b/tests/code_library/code_case/paddle_code/api_paddle_Tensor2Float.py index f89a88122..13c4be3da 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_Tensor2Float.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_Tensor2Float.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") diff --git a/tests/code_library/code_case/paddle_code/api_paddle_Tensor2Int.py b/tests/code_library/code_case/paddle_code/api_paddle_Tensor2Int.py index 3a0c2ade8..878784c31 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_Tensor2Int.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_Tensor2Int.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") diff --git a/tests/code_library/code_case/paddle_code/api_paddle_Tensor2Long.py b/tests/code_library/code_case/paddle_code/api_paddle_Tensor2Long.py index 7610dc9e2..7bb44c307 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_Tensor2Long.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_Tensor2Long.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") diff --git a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_expand.py b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_expand.py index c4e748f43..1994e001e 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_expand.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_expand.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") x = paddle.tensor([[1], [2], [3]]) y = x.expand(3, 4) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_empty.py b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_empty.py index 49d9e3991..d97caa2e7 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_empty.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_empty.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") a = paddle.tensor([1, 2, 3], dtype=paddle.int64) b = a.new_empty([3, 4], dtype=paddle.float64, requires_grad=True, pin_memory=True) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_full.py b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_full.py index aae3ea427..35ee65dcf 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_full.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_full.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") a = paddle.tensor([1, 2, 3], dtype=paddle.int64) b = a.new_full([3, 4], 2.43, dtype=paddle.float64, requires_grad=True, pin_memory=True) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_ones.py b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_ones.py index a447b4468..4f010f6d0 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_ones.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_ones.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") a = paddle.tensor([1, 2, 3], dtype=paddle.int64) b = a.new_ones([3, 4], dtype=paddle.float64, requires_grad=True, pin_memory=True) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_tensor.py b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_tensor.py index 90c212d12..c70e047da 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_tensor.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_tensor.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") a = paddle.tensor([1, 2, 3]) b = a.new_tensor([4, 5, 6], dtype=paddle.float64, requires_grad=True) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_zeros.py b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_zeros.py index 973c7afec..01de2fcd2 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_zeros.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_zeros.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") a = paddle.tensor([1, 2, 3], dtype=paddle.int64) b = a.new_zeros([3, 4], dtype=paddle.float64, requires_grad=True, pin_memory=True) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_normal_.py b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_normal_.py index 3b7802e57..277c5253b 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_normal_.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_normal_.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") a = paddle.tensor([1, 3, 4, 9, 0.5, 1.5]) a = a.normal_(0.2, 0.3) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_to.py b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_to.py index b227c47a4..7abf8752c 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_to.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_to.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") cpu = paddle.device("cpu") a = paddle.randn(2, 3) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_uniform_.py b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_uniform_.py index 830049a4f..5153352c1 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_Tensor_uniform_.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_Tensor_uniform_.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") a = paddle.tensor([1, 3, 4, 9, 0.5, 1.5]) c = paddle.tensor(a.uniform_(2, 6)) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_cuda_is_available.py b/tests/code_library/code_case/paddle_code/api_paddle_cuda_is_available.py index 87c4d2569..dcd6681c8 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_cuda_is_available.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_cuda_is_available.py @@ -1,4 +1,5 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") paddle.cuda.is_available() diff --git a/tests/code_library/code_case/paddle_code/api_paddle_equall.py b/tests/code_library/code_case/paddle_code/api_paddle_equall.py index d23bc1966..4863b8a75 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_equall.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_equall.py @@ -1,4 +1,5 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") -r = paddle.compat.equal(paddle.tensor([1, 2]), paddle.tensor([1, 2])) +r = paddle.equal(paddle.tensor([1, 2]), paddle.tensor([1, 2])) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_index_copy_.py b/tests/code_library/code_case/paddle_code/api_paddle_index_copy_.py index 75ecc6dc7..d26953e95 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_index_copy_.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_index_copy_.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") x = paddle.zeros(5, 3) t = paddle.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=paddle.float) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_io_BatchSampler.py b/tests/code_library/code_case/paddle_code/api_paddle_io_BatchSampler.py index 5367719d9..0c97954db 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_io_BatchSampler.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_io_BatchSampler.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") o = list(paddle.utils.data.BatchSampler(range(10), batch_size=3, drop_last=True)) print("#########################case2#########################") diff --git a/tests/code_library/code_case/paddle_code/api_paddle_nn_BCEWithLogitsLoss.py b/tests/code_library/code_case/paddle_code/api_paddle_nn_BCEWithLogitsLoss.py index 8b1cbffc5..fb2e5e375 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_nn_BCEWithLogitsLoss.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_nn_BCEWithLogitsLoss.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") loss = paddle.nn.BCEWithLogitsLoss(reduction="none") input = paddle.tensor([1.0, 0.7, 0.2], requires_grad=True) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_nn_BatchNorm1D.py b/tests/code_library/code_case/paddle_code/api_paddle_nn_BatchNorm1D.py index 5d29e9551..82a582183 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_nn_BatchNorm1D.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_nn_BatchNorm1D.py @@ -1,8 +1,9 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") -paddle.compat.nn.BatchNorm1d(3, eps=0.001, momentum=0.01) +paddle.nn.BatchNorm1d(3, eps=0.001, momentum=0.01) print("#########################case2#########################") -bn = paddle.compat.nn.BatchNorm1d(27) +bn = paddle.nn.BatchNorm1d(27) print("#########################case3#########################") -paddle.compat.nn.BatchNorm1d(10, eps=1e-05, affine=False) +paddle.nn.BatchNorm1d(10, eps=1e-05, affine=False) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_nn_BatchNorm2D.py b/tests/code_library/code_case/paddle_code/api_paddle_nn_BatchNorm2D.py index e5b22eae8..fa50e4089 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_nn_BatchNorm2D.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_nn_BatchNorm2D.py @@ -1,8 +1,9 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") -bn = paddle.compat.nn.BatchNorm2d(5) +bn = paddle.nn.BatchNorm2d(5) print("#########################case2#########################") -bn = paddle.compat.nn.BatchNorm2d(27) +bn = paddle.nn.BatchNorm2d(27) print("#########################case3#########################") -paddle.compat.nn.BatchNorm2d(10, eps=1e-05, affine=False) +paddle.nn.BatchNorm2d(10, eps=1e-05, affine=False) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_nn_InstanceNorm3D.py b/tests/code_library/code_case/paddle_code/api_paddle_nn_InstanceNorm3D.py index 2a583c9df..be1645f4d 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_nn_InstanceNorm3D.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_nn_InstanceNorm3D.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") m = paddle.nn.InstanceNorm3D(num_features=100) input = paddle.randn(20, 100, 35, 45, 10) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_nn_functional_interpolate.py b/tests/code_library/code_case/paddle_code/api_paddle_nn_functional_interpolate.py index 3ea8841b3..a8fd2004b 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_nn_functional_interpolate.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_nn_functional_interpolate.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") a = paddle.nn.functional.interpolate(paddle.randn(1, 2, 20, 20), [24, 24]) print("#########################case2#########################") diff --git a/tests/code_library/code_case/paddle_code/api_paddle_permute.py b/tests/code_library/code_case/paddle_code/api_paddle_permute.py index 4f29c79ab..0be2c69b9 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_permute.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_permute.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") x = paddle.rand([2, 3, 4, 4]) x.permute(0, 2, 3, 1) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_randint.py b/tests/code_library/code_case/paddle_code/api_paddle_randint.py index df404ab02..c37c56d0b 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_randint.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_randint.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") a = paddle.randint(2, 5, [3, 4], device=paddle.device("cuda")) print("#########################case2#########################") diff --git a/tests/code_library/code_case/paddle_code/api_paddle_random_manual_seed.py b/tests/code_library/code_case/paddle_code/api_paddle_random_manual_seed.py index 36dd99e87..e340982c2 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_random_manual_seed.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_random_manual_seed.py @@ -1,4 +1,5 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") paddle.manual_seed(23) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_sigmoid.py b/tests/code_library/code_case/paddle_code/api_paddle_sigmoid.py index aeec7f65f..f40c50d5c 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_sigmoid.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_sigmoid.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") t = paddle.randn(4) paddle.sigmoid(t) diff --git a/tests/code_library/code_case/paddle_code/api_paddle_to_tensor.py b/tests/code_library/code_case/paddle_code/api_paddle_to_tensor.py index a0a508514..aa9038911 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_to_tensor.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_to_tensor.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") a = paddle.tensor( paddle.tensor([2, 3, 4]), diff --git a/tests/code_library/code_case/paddle_code/api_paddle_version.py b/tests/code_library/code_case/paddle_code/api_paddle_version.py index 967b882e5..3bac91fd6 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_version.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_version.py @@ -1,3 +1,4 @@ import paddle +paddle.enable_compat(level=2) print(paddle.__version__) diff --git a/tests/code_library/code_case/paddle_code/api_remove_decorator.py b/tests/code_library/code_case/paddle_code/api_remove_decorator.py index 9fe54061b..26c7ce5d6 100644 --- a/tests/code_library/code_case/paddle_code/api_remove_decorator.py +++ b/tests/code_library/code_case/paddle_code/api_remove_decorator.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") diff --git a/tests/code_library/code_case/paddle_code/attribute_paddle_Tensor_requires_grad.py b/tests/code_library/code_case/paddle_code/attribute_paddle_Tensor_requires_grad.py index 10351335a..02842cd43 100644 --- a/tests/code_library/code_case/paddle_code/attribute_paddle_Tensor_requires_grad.py +++ b/tests/code_library/code_case/paddle_code/attribute_paddle_Tensor_requires_grad.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") data = paddle.tensor([23.0, 32.0, 43.0]) if not data.requires_grad: diff --git a/tests/code_library/code_case/paddle_code/attribute_setattr.py b/tests/code_library/code_case/paddle_code/attribute_setattr.py index 381ae1dba..31c3eca3b 100644 --- a/tests/code_library/code_case/paddle_code/attribute_setattr.py +++ b/tests/code_library/code_case/paddle_code/attribute_setattr.py @@ -1,5 +1,7 @@ import paddle +paddle.enable_compat(level=2) + def add_module(self, name, module): self.add_module(f"{name} - {len(self) + 1}", module) diff --git a/tests/code_library/code_case/paddle_code/attribute_visit_name.py b/tests/code_library/code_case/paddle_code/attribute_visit_name.py index f1b9c1f39..fca18304e 100644 --- a/tests/code_library/code_case/paddle_code/attribute_visit_name.py +++ b/tests/code_library/code_case/paddle_code/attribute_visit_name.py @@ -1,6 +1,8 @@ import paddle import paddleformers +paddle.enable_compat(level=2) + class A(paddle.nn.Module): def __init__(self, data: paddle.Tensor): diff --git a/tests/code_library/code_case/paddle_code/class_method_static_call.py b/tests/code_library/code_case/paddle_code/class_method_static_call.py index fa7974725..590eeb366 100644 --- a/tests/code_library/code_case/paddle_code/class_method_static_call.py +++ b/tests/code_library/code_case/paddle_code/class_method_static_call.py @@ -1,5 +1,7 @@ import paddle +paddle.enable_compat(level=2) + class PT_Optimizer(paddle.optim.Optimizer): pass diff --git a/tests/code_library/code_case/paddle_code/import_analysis.py b/tests/code_library/code_case/paddle_code/import_analysis.py index 78f0b5776..fc3afd837 100644 --- a/tests/code_library/code_case/paddle_code/import_analysis.py +++ b/tests/code_library/code_case/paddle_code/import_analysis.py @@ -1,4 +1,5 @@ import paddle +paddle.enable_compat(level=2) from . import datasets from .datasets import x from yolov3.datasets import a diff --git a/tests/code_library/code_case/paddle_code/insert_pass_in_ast_if.py b/tests/code_library/code_case/paddle_code/insert_pass_in_ast_if.py index 40ed1dca0..a28d887a0 100644 --- a/tests/code_library/code_case/paddle_code/insert_pass_in_ast_if.py +++ b/tests/code_library/code_case/paddle_code/insert_pass_in_ast_if.py @@ -1,7 +1,8 @@ -from typing import TYPE_CHECKING - import paddle +paddle.enable_compat(level=2) +from typing import TYPE_CHECKING + if TYPE_CHECKING: pass import warnings diff --git a/tests/code_library/code_case/paddle_code/mark_unsupport.py b/tests/code_library/code_case/paddle_code/mark_unsupport.py index 40407f27f..b6174f2da 100644 --- a/tests/code_library/code_case/paddle_code/mark_unsupport.py +++ b/tests/code_library/code_case/paddle_code/mark_unsupport.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) paddle.add _LOCAL_PROCESS_GROUP = None _MISSING_LOCAL_PG_ERROR = "Local process group is not yet created! Please use detectron2's `launch()` to start processes and initialize pytorch process group. If you need to start processes in other ways, please call comm.create_local_process_group(num_workers_per_machine) after calling torch.distributed.init_process_group()." diff --git a/tests/code_library/code_case/paddle_code/may_paddle_package_list.py b/tests/code_library/code_case/paddle_code/may_paddle_package_list.py index 1ae4ca251..e21827267 100644 --- a/tests/code_library/code_case/paddle_code/may_paddle_package_list.py +++ b/tests/code_library/code_case/paddle_code/may_paddle_package_list.py @@ -3,6 +3,8 @@ import einops import paddle import setuptools + +paddle.enable_compat(level=2) from numpy.random import randint print("#########################case1#########################") diff --git a/tests/code_library/code_case/paddle_code/package_visit_name.py b/tests/code_library/code_case/paddle_code/package_visit_name.py index e8bf8505a..8f73b17ac 100644 --- a/tests/code_library/code_case/paddle_code/package_visit_name.py +++ b/tests/code_library/code_case/paddle_code/package_visit_name.py @@ -1,6 +1,7 @@ import paddle import paddleformers +paddle.enable_compat(level=2) setattr(paddle, "nn", nn_mymodule) hasattr(paddle, "nn") hasattr(paddle, "__version__") diff --git a/tests/code_library/code_case/paddle_code/paddle_llama.py b/tests/code_library/code_case/paddle_code/paddle_llama.py index 20b1adf47..0c18e6b25 100644 --- a/tests/code_library/code_case/paddle_code/paddle_llama.py +++ b/tests/code_library/code_case/paddle_code/paddle_llama.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) print("#########################case1#########################") model_parallel_size_0 = ( paddle.distributed.fleet.base.topology._HYBRID_PARALLEL_GROUP._mp_degree diff --git a/tests/code_library/code_case/paddle_code/paddlenlp_Qwen.py b/tests/code_library/code_case/paddle_code/paddlenlp_Qwen.py index 8abecc2c3..6ad21c77b 100644 --- a/tests/code_library/code_case/paddle_code/paddlenlp_Qwen.py +++ b/tests/code_library/code_case/paddle_code/paddlenlp_Qwen.py @@ -72,6 +72,7 @@ def _rotate_half(x): ############################## 相关utils函数,如上 ############################## +paddle.enable_compat(level=2) print("#########################case1#########################") diff --git a/tests/code_library/code_case/paddle_code/simplified_name_call.py b/tests/code_library/code_case/paddle_code/simplified_name_call.py index 88814d982..e6746bdc5 100644 --- a/tests/code_library/code_case/paddle_code/simplified_name_call.py +++ b/tests/code_library/code_case/paddle_code/simplified_name_call.py @@ -1,5 +1,6 @@ import paddle +paddle.enable_compat(level=2) a = paddle.tensor([1]) b = paddle.tensor([2]) print("#########################case1#########################") diff --git a/tests/code_library/code_case/paddle_code/type_hinting.py b/tests/code_library/code_case/paddle_code/type_hinting.py index e3738d091..8a8df530f 100644 --- a/tests/code_library/code_case/paddle_code/type_hinting.py +++ b/tests/code_library/code_case/paddle_code/type_hinting.py @@ -1,7 +1,8 @@ -from typing import List, Optional, Tuple, Union - import paddle +paddle.enable_compat(level=2) +from typing import List, Optional, Tuple, Union + print("#########################case1#########################") Union[Tuple, paddle.BoolTensor] print("#########################case2#########################") diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 000000000..4842d9ffc --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,256 @@ +# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Global-state isolation for the test suite. + +Each test execs both a torch reference and the converted paddle code in the SAME +worker process (see ``tests/apibase.py``). Process-global switches such as +``torch.set_default_device('cuda')``, ``set_default_dtype`` or the grad-enabled +flag therefore LEAK into every later test in that process, causing failures that +only appear in the full suite (and vanish on a ``--lf`` rerun that no longer +schedules the polluting test first). + +The autouse fixture below snapshots that global state before each test and +restores it afterwards, so a test that forgets to reset (e.g. +``torch.set_default_device('cuda:0')`` with no matching reset) can no longer +corrupt its neighbours. + +The logic is lazy and best-effort: it only touches a framework that is already in +``sys.modules``, so it never forces an import of torch/paddle (which would change +import ordering) for tests that don't use them. +""" + +import os +import sys + +import pytest + + +def _snapshot_environ(): + """Record the process environment so env-var writes can be reverted. + + Converted code maps some torch APIs to environment variables, e.g. + ``torch.set_num_threads(n)`` -> ``os.environ['CPU_NUM'] = str(n)``. Left set, + that leaks into ``torch.get_num_threads`` -> ``os.getenv('CPU_NUM', 1)`` in a + later test, which then returns a ``str`` instead of the default ``int``. + """ + return dict(os.environ) + + +def _restore_environ(snap): + for key in list(os.environ.keys()): + if key not in snap: + try: + del os.environ[key] + except Exception: + pass + for key, value in snap.items(): + if os.environ.get(key) != value: + try: + os.environ[key] = value + except Exception: + pass + + +def _snapshot_torch(): + torch = sys.modules.get("torch") + if torch is None: + return None + context_holder = torch._GLOBAL_DEVICE_CONTEXT + if getattr(context_holder, "device_context", False) is None: + del context_holder.device_context + snap = {} + try: + snap["dtype"] = torch.get_default_dtype() + except Exception: + pass + try: + snap["device"] = torch.get_default_device() + snap["has_device_context"] = hasattr( + torch._GLOBAL_DEVICE_CONTEXT, "device_context" + ) + except Exception: + pass + try: + snap["grad"] = torch.is_grad_enabled() + except Exception: + pass + return snap + + +def _restore_torch(snap): + torch = sys.modules.get("torch") + if torch is None: + return + dtype = torch.float32 + device = "cpu" + grad = True + if snap: + dtype = snap.get("dtype", dtype) + device = snap.get("device", device) + grad = snap.get("grad", grad) + try: + torch.set_default_dtype(dtype) + except Exception: + pass + try: + context_holder = torch._GLOBAL_DEVICE_CONTEXT + current_context = getattr(context_holder, "device_context", None) + if current_context is not None: + current_context.__exit__(None, None, None) + if hasattr(context_holder, "device_context"): + del context_holder.device_context + if snap and snap.get("has_device_context"): + torch.set_default_device(device) + except Exception: + pass + try: + torch.set_grad_enabled(grad) + except Exception: + pass + + +def _snapshot_paddle(): + paddle = sys.modules.get("paddle") + if paddle is None: + return None + snap = {} + try: + snap["dtype"] = paddle.get_default_dtype() + except Exception: + pass + try: + snap["device"] = paddle.device.get_device() + except Exception: + pass + try: + snap["grad"] = paddle.is_grad_enabled() + except Exception: + pass + return snap + + +def _restore_paddle(snap): + paddle = sys.modules.get("paddle") + if paddle is None or not snap: + return + if "dtype" in snap: + try: + paddle.set_default_dtype(snap["dtype"]) + except Exception: + pass + if "device" in snap: + try: + paddle.device.set_device(snap["device"]) + except Exception: + pass + if "grad" in snap: + try: + paddle.set_grad_enabled(snap["grad"]) + except Exception: + pass + + +_SENTINEL = object() + + +def _patch_targets(): + """Global paddle objects that converted code monkeypatches via ``setattr``. + + Converted paddle code frequently does e.g. + ``setattr(paddle.Tensor, "add", _add)`` or + ``setattr(paddle.nn.LogSoftmax, "forward", _log_softmax_forward)`` where the + helper is defined in the exec namespace. Because the patch lands on the + *global* class it leaks into every later test; and because ``apibase`` clears + the exec namespace afterwards, the helper's ``__globals__`` is emptied, so a + later call raises ``NameError: name 'paddle' is not defined``. + + We return ``paddle.Tensor`` plus every class exposed on ``paddle.nn`` so the + fixture can snapshot their attributes and revert any test-induced change. + """ + paddle = sys.modules.get("paddle") + if paddle is None: + return [] + targets = [] + tensor = getattr(paddle, "Tensor", None) + if tensor is not None: + targets.append(tensor) + nn = getattr(paddle, "nn", None) + if nn is not None: + for name in dir(nn): + try: + obj = getattr(nn, name) + except Exception: + continue + if isinstance(obj, type): + targets.append(obj) + return targets + + +def _snapshot_patches(): + """Record current attribute values of monkeypatchable paddle targets.""" + snap = [] + for target in _patch_targets(): + try: + attrs = dict(vars(target)) + except TypeError: + continue + snap.append((target, attrs)) + return snap + + +def _restore_patches(snap): + """Revert only the attributes a test added or replaced on paddle targets.""" + for target, attrs in snap: + try: + current = dict(vars(target)) + except TypeError: + continue + # Restore attributes whose object identity changed during the test. + for name, original in attrs.items(): + if current.get(name, _SENTINEL) is not original: + try: + setattr(target, name, original) + except (AttributeError, TypeError): + pass + # Delete attributes that the test added and that were not present before. + for name in current: + if name not in attrs: + try: + delattr(target, name) + except (AttributeError, TypeError): + pass + + +@pytest.fixture(autouse=True) +def _reset_global_state(): + """Snapshot and restore torch/paddle process-global state around each test. + + Covers the classes of cross-test pollution that only surface in the full + suite (and vanish on ``--lf``): (1) torch/paddle default device, (2) default + dtype and grad mode, (3) global monkeypatches converted code applies to + ``paddle.Tensor`` / ``paddle.nn.*`` classes, and (4) environment variables + such as ``CPU_NUM`` written by converted ``set_num_threads`` code. + """ + torch_snap = _snapshot_torch() + paddle_snap = _snapshot_paddle() + patch_snap = _snapshot_patches() + environ_snap = _snapshot_environ() + try: + yield + finally: + _restore_patches(patch_snap) + _restore_torch(torch_snap) + _restore_paddle(paddle_snap) + _restore_environ(environ_snap) diff --git a/tests/test_Tensor_is_sparse.py b/tests/test_Tensor_is_sparse.py index 6a6ca780c..d16795b36 100644 --- a/tests/test_Tensor_is_sparse.py +++ b/tests/test_Tensor_is_sparse.py @@ -27,3 +27,67 @@ def test_case_1(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + indices = torch.tensor([[0], [1]]) + values = torch.tensor([1.0]) + a = torch.sparse_coo_tensor(indices, values, [2, 2]) + result = a.is_sparse + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + crows = torch.tensor([0, 1, 1]) + cols = torch.tensor([0]) + values = torch.tensor([1.0]) + a = torch.sparse_csr_tensor(crows, cols, values, [2, 2]) + result = a.is_sparse + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.empty([0, 3], dtype=torch.float64) + result = a.is_sparse + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.arange(24, dtype=torch.int32).reshape(2, 3, 4) + result = a.is_sparse + result_type = type(result).__name__ + """ + ) + obj.run(pytorch_code, ["result", "result_type"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + indices = torch.tensor([[0, 1], [1, 2]]) + values = torch.tensor([1, 2], dtype=torch.int64) + a = torch.sparse_coo_tensor(indices, values, [2, 3]) + result = a.is_sparse + result_type = type(result).__name__ + """ + ) + obj.run(pytorch_code, ["result", "result_type"]) diff --git a/tests/test_Tensor_median.py b/tests/test_Tensor_median.py index 7d42a404f..e2097229e 100644 --- a/tests/test_Tensor_median.py +++ b/tests/test_Tensor_median.py @@ -38,12 +38,7 @@ def test_case_2(): result = input.median(1) """ ) - obj.run( - pytorch_code, - ["result"], - unsupport=True, - reason="paddle does not return index when dim is specified", - ) + obj.run(pytorch_code, ["result"]) def test_case_3(): @@ -54,12 +49,7 @@ def test_case_3(): result = input.median(1, keepdim=True) """ ) - obj.run( - pytorch_code, - ["result"], - unsupport=True, - reason="paddle does not return index when dim is specified", - ) + obj.run(pytorch_code, ["result"]) def test_case_4(): @@ -70,12 +60,7 @@ def test_case_4(): result = input.median(dim=1, keepdim=True) """ ) - obj.run( - pytorch_code, - ["result"], - unsupport=True, - reason="paddle does not return index when dim is specified", - ) + obj.run(pytorch_code, ["result"]) def test_case_5(): @@ -86,9 +71,26 @@ def test_case_5(): result = input.median(0) """ ) - obj.run( - pytorch_code, - ["result"], - unsupport=True, - reason="paddle does not return index when dim is specified", + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([1.0, 2.0, 3.0, 4.0]) + result = input.median() + """ ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1.0, 2.0, 3.0, 4.0], [8.0, 7.0, 6.0, 5.0]]) + result = input.median(dim=1) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_numel.py b/tests/test_Tensor_numel.py index 907176ce6..1940e3a4d 100644 --- a/tests/test_Tensor_numel.py +++ b/tests/test_Tensor_numel.py @@ -49,3 +49,73 @@ def test_case_3(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.empty(0, 3).numel() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1.0, 2.0, 3.0], dtype=torch.float64) + count = a.numel() + result = count * 3 + 1 + is_python_int = type(count) is int + """ + ) + obj.run(pytorch_code, ["result", "is_python_int"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.arange(24, dtype=torch.int32).reshape(2, 3, 4) + result = a.numel() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor(7, dtype=torch.int32) + result = a.numel() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + indices = torch.tensor([[0, 1], [1, 2]]) + values = torch.tensor([1.0, 2.0]) + a = torch.sparse_coo_tensor(indices, values, [2, 3]) + result = a.numel() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.ones(2, 3) + args = () + result = a.numel(*args) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_rename.py b/tests/test_Tensor_rename.py index 4083b1ac4..37243533f 100644 --- a/tests/test_Tensor_rename.py +++ b/tests/test_Tensor_rename.py @@ -29,7 +29,13 @@ def test_case_1(): ) obj.run( pytorch_code, - expect_paddle_code='import paddle\n\nx = paddle.tensor([1, 2, 3])\nx.rename(columns={"iids": iids})\n', + expect_paddle_code=""" +import paddle + +paddle.enable_compat(level=2) +x = paddle.tensor([1, 2, 3]) +x.rename(columns={"iids": iids}) + """, ) diff --git a/tests/test_Tensor_split.py b/tests/test_Tensor_split.py index 5be698bea..5741e3e02 100644 --- a/tests/test_Tensor_split.py +++ b/tests/test_Tensor_split.py @@ -100,6 +100,7 @@ def test_case_7(): """ import paddle + paddle.enable_compat(level=2) a = paddle.tensor([1, 2, 3]) str1 = "1,2,3" str1.split(",") diff --git a/tests/test_Tensor_type.py b/tests/test_Tensor_type.py index 14df0a0ef..3fc35e591 100644 --- a/tests/test_Tensor_type.py +++ b/tests/test_Tensor_type.py @@ -60,12 +60,7 @@ def test_case_4(): result = a.type() """ ) - obj.run( - pytorch_code, - ["result"], - check_value=False, - reason="Pytorch return 'torch.FloatTensor' while Paddle rerurn 'paddle.float32' ", - ) + obj.run(pytorch_code, ["result"]) def test_case_5(): @@ -77,3 +72,144 @@ def test_case_5(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.ones(2, 3) + result = a.type("torch.DoubleTensor") + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.ones(2, 3, device="cpu") + result = a.type(dtype=None, non_blocking=False) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.ones(2, 3) + args = (torch.float64, True) + result = a.type(*args) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.ones(2, 3) + kwargs = {"dtype": torch.float64, "non_blocking": True} + result = a.type(**kwargs) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.ones(2, 3) + result = a.type(torch.DoubleTensor) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_11(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.ones(2, 3, device="cpu") + kwargs = {"async": True} + result = a.type(**kwargs) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_12(): + pytorch_code = textwrap.dedent( + """ + import torch + indices = torch.tensor([[0], [1]]) + values = torch.tensor([1.0]) + a = torch.sparse_coo_tensor( + indices, values, [2, 2], device="cpu" + ) + result = a.type() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_13(): + pytorch_code = textwrap.dedent( + """ + import torch + result = [ + torch.ones(1, dtype=torch.bool, device="cpu").type(), + torch.ones(1, dtype=torch.int32, device="cpu").type(), + torch.ones(1, dtype=torch.float64, device="cpu").type(), + torch.ones(1, dtype=torch.bfloat16, device="cpu").type(), + torch.ones(1, dtype=torch.complex64, device="cpu").type(), + ] + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_14(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1.0, 2.0, 3.0], requires_grad=True) + y = a.type(torch.float64) + y.sum().backward() + a_grad = a.grad + """ + ) + obj.run(pytorch_code, ["y", "a_grad"], check_stop_gradient=False) + + +def test_case_15(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.ones(2, 3, device="cpu") + result = ( + a.type(torch.float32) is a, + a.type("torch.FloatTensor") is a, + ) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_16(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.ones(2, 3) + result = [ + a.type("torch.Float8_e4m3fnTensor").type(), + a.type("torch.Float8_e5m2Tensor").type(), + ] + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_add_start_docstrings_to_model_forward.py b/tests/test_add_start_docstrings_to_model_forward.py index 509109fe9..aac30e84d 100644 --- a/tests/test_add_start_docstrings_to_model_forward.py +++ b/tests/test_add_start_docstrings_to_model_forward.py @@ -36,6 +36,8 @@ def forward(self, input_ids): import paddle import paddleformers + paddle.enable_compat(level=2) + class LlamaForCausalLM(paddle.nn.Module): @paddleformers.trainer.utils.add_start_docstrings_to_model_forward("test docstring") diff --git a/tests/test_distributions_categorical_Categorical.py b/tests/test_distributions_categorical_Categorical.py index 54f3e31f9..67bed9e27 100644 --- a/tests/test_distributions_categorical_Categorical.py +++ b/tests/test_distributions_categorical_Categorical.py @@ -141,3 +141,219 @@ def test_case_11(): """ ) obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_12(): + """All positional, mixed, and variadic constructor arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + logits = torch.tensor([[0.2, 0.3, 0.5], [1.0, -1.0, 0.5]]) + result1 = torch.distributions.categorical.Categorical(None, logits, False) + result2 = torch.distributions.categorical.Categorical(None, logits=logits, validate_args=False) + args = (None, logits, False) + result3 = torch.distributions.categorical.Categorical(*args) + """ + ) + obj.run(pytorch_code, ["result1", "result2", "result3"]) + + +def test_case_13(): + """Distribution properties and probability methods with batched input""" + pytorch_code = textwrap.dedent( + """ + import torch + probs = torch.tensor([[0.1, 0.3, 0.6], [0.5, 0.2, 0.3]]) + dist = torch.distributions.categorical.Categorical(probs=probs) + value = torch.tensor([2, 0]) + log_prob = dist.log_prob(value) + entropy = dist.entropy() + perplexity = dist.perplexity() + mode = dist.mode + mean_is_nan = torch.isnan(dist.mean) + variance_is_nan = torch.isnan(dist.variance) + param_shape = dist.param_shape + batch_shape = dist.batch_shape + event_shape = dist.event_shape + has_enumerate_support = dist.has_enumerate_support + has_rsample = dist.has_rsample + """ + ) + obj.run( + pytorch_code, + [ + "log_prob", + "entropy", + "perplexity", + "mode", + "mean_is_nan", + "variance_is_nan", + "param_shape", + "batch_shape", + "event_shape", + "has_enumerate_support", + "has_rsample", + ], + ) + + +def test_case_14(): + """Zero probability keeps finite logits and entropy semantics""" + pytorch_code = textwrap.dedent( + """ + import torch + dist = torch.distributions.categorical.Categorical( + probs=torch.tensor([0.0, 0.25, 0.75]) + ) + logits = dist.logits + entropy = dist.entropy() + log_prob = dist.log_prob(torch.tensor([0, 2])) + """ + ) + obj.run(pytorch_code, ["logits", "entropy", "log_prob"]) + + +def test_case_15(): + """Support checks and support enumeration""" + pytorch_code = textwrap.dedent( + """ + import torch + probs = torch.tensor([[0.2, 0.3, 0.5], [0.4, 0.4, 0.2]]) + dist = torch.distributions.categorical.Categorical(probs=probs) + support_check = dist.support.check(torch.tensor([0, 2, -1, 3])) + support_expanded = dist.enumerate_support(expand=True) + support_unexpanded = dist.enumerate_support(expand=False) + """ + ) + obj.run( + pytorch_code, + ["support_check", "support_expanded", "support_unexpanded"], + ) + + +def test_case_16(): + """Expand preserves normalized parameters and distribution shapes""" + pytorch_code = textwrap.dedent( + """ + import torch + logits = torch.tensor([[0.2, 0.3, 0.5], [1.0, -1.0, 0.5]]) + dist = torch.distributions.categorical.Categorical(logits=logits) + result = dist.expand((4, 2)) + expanded_log_prob = result.log_prob(torch.tensor([[0, 1], [1, 2], [2, 0], [0, 2]])) + """ + ) + obj.run(pytorch_code, ["result", "expanded_log_prob"]) + + +def test_case_17(): + """Sample shape, dtype, and support""" + pytorch_code = textwrap.dedent( + """ + import torch + dist = torch.distributions.categorical.Categorical( + probs=torch.tensor([[0.2, 0.8], [0.6, 0.4]]) + ) + samples = dist.sample((2, 3)) + sample_shape = samples.shape + sample_dtype_is_int64 = samples.dtype == torch.int64 + samples_in_support = dist.support.check(samples).all() + """ + ) + obj.run( + pytorch_code, + ["sample_shape", "sample_dtype_is_int64", "samples_in_support"], + ) + + +def test_case_18(): + """Gradient through normalized logits, log_prob, and entropy""" + pytorch_code = textwrap.dedent( + """ + import torch + logits = torch.tensor([0.2, -0.1, 0.7], requires_grad=True) + dist = torch.distributions.categorical.Categorical(logits=logits) + loss = dist.log_prob(torch.tensor(2)) + dist.entropy() + loss.backward() + logits_grad = logits.grad + """ + ) + obj.run( + pytorch_code, + ["loss", "logits_grad"], + check_stop_gradient=False, + rtol=1e-6, + atol=1e-6, + ) + + +def test_case_19(): + """Constructor and sample validation errors""" + pytorch_code = textwrap.dedent( + """ + import torch + errors = [] + try: + torch.distributions.categorical.Categorical() + except ValueError: + errors.append("missing") + try: + torch.distributions.categorical.Categorical( + probs=torch.tensor([0.2, 0.8]), + logits=torch.tensor([0.2, 0.8]), + ) + except ValueError: + errors.append("both") + try: + torch.distributions.categorical.Categorical(torch.tensor(1.0)) + except ValueError: + errors.append("scalar") + dist = torch.distributions.categorical.Categorical( + probs=torch.tensor([0.2, 0.3, 0.5]), + validate_args=True, + ) + try: + dist.log_prob(torch.tensor([3])) + except ValueError: + errors.append("out_of_support") + try: + dist.log_prob(torch.tensor([1.5])) + except ValueError: + errors.append("non_integer") + try: + torch.distributions.categorical.Categorical( + probs=torch.tensor([-0.1, 1.1]), + validate_args=True, + ) + except ValueError: + errors.append("invalid_probs") + try: + torch.distributions.categorical.Categorical( + logits=torch.tensor([float("nan"), 0.0]), + validate_args=True, + ) + except ValueError: + errors.append("invalid_logits") + try: + torch.distributions.categorical.Categorical(torch.empty((0,))) + except ValueError: + errors.append("empty") + """ + ) + obj.run(pytorch_code, ["errors"]) + + +def test_case_20(): + """Integer probabilities and empty categories with validation disabled""" + pytorch_code = textwrap.dedent( + """ + import torch + integer_result = torch.distributions.categorical.Categorical( + probs=torch.tensor([1, 2, 3]) + ) + empty_result = torch.distributions.categorical.Categorical( + probs=torch.empty(0), + validate_args=False, + ) + """ + ) + obj.run(pytorch_code, ["integer_result", "empty_result"]) diff --git a/tests/test_jit_ignore.py b/tests/test_jit_ignore.py index e607a5844..39629feae 100644 --- a/tests/test_jit_ignore.py +++ b/tests/test_jit_ignore.py @@ -43,6 +43,8 @@ def helper_function(self, x): """ import paddle + paddle.enable_compat(level=2) + class MyModule(paddle.nn.Module): def forward(self, x): diff --git a/tests/test_jit_load.py b/tests/test_jit_load.py index ce977a45e..10b22c52c 100644 --- a/tests/test_jit_load.py +++ b/tests/test_jit_load.py @@ -31,6 +31,7 @@ def test_case_1(): """ import paddle + paddle.enable_compat(level=2) result = paddle.jit.load(path="model.pt") """ ) @@ -48,6 +49,7 @@ def test_case_2(): """ import paddle + paddle.enable_compat(level=2) result = paddle.jit.load(path="model.pt") """ ) diff --git a/tests/test_jit_save.py b/tests/test_jit_save.py index f0e2b4f3d..b18a18131 100644 --- a/tests/test_jit_save.py +++ b/tests/test_jit_save.py @@ -45,6 +45,8 @@ def forward(self, x): """ import paddle + paddle.enable_compat(level=2) + class MyModule(paddle.nn.Module): def __init__(self): @@ -89,6 +91,8 @@ def forward(self, x): """ import paddle + paddle.enable_compat(level=2) + class MyModule(paddle.nn.Module): def __init__(self): @@ -134,6 +138,8 @@ def forward(self, x): """ import paddle + paddle.enable_compat(level=2) + class MyModule(paddle.nn.Module): def __init__(self): @@ -180,6 +186,8 @@ def forward(self, x): """ import paddle + paddle.enable_compat(level=2) + class MyModule(paddle.nn.Module): def __init__(self): diff --git a/tests/test_jit_script.py b/tests/test_jit_script.py index 26ceaa33d..601ed577f 100644 --- a/tests/test_jit_script.py +++ b/tests/test_jit_script.py @@ -34,6 +34,8 @@ def foo(x, scale, shift): """ import paddle + paddle.enable_compat(level=2) + @paddle.jit.to_static def foo(x, scale, shift): @@ -66,6 +68,8 @@ def add(x, y): """ import paddle + paddle.enable_compat(level=2) + def add(x, y): return x + y @@ -99,6 +103,8 @@ def add(x, y): """ import paddle + paddle.enable_compat(level=2) + def add(x, y): return x + y diff --git a/tests/test_nn_PReLU.py b/tests/test_nn_PReLU.py index d7cd012ee..f177af65d 100644 --- a/tests/test_nn_PReLU.py +++ b/tests/test_nn_PReLU.py @@ -292,3 +292,68 @@ def test_case_18(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_19(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + x = torch.tensor( + [[[-1.5, 0.5, 2.0], [-0.25, -2.0, 1.0]]], + dtype=torch.float32, + ) + model1 = nn.PReLU(1, 0.3, "cpu") + model2 = nn.PReLU(1, 0.3, "cpu") + result1 = model1(x) + result2 = model2(x) + """ + ) + obj.run(pytorch_code, ["result1", "result2"]) + + +def test_case_20(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + x = torch.tensor( + [[-2.0, -0.5, 0.75, 1.5]], + dtype=torch.float32, + ) + args = (1, 0.45, "cpu") + model = nn.PReLU(*args) + result = model(x) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_21(): + import importlib + + import paddle + import torch + + class GuardCheckingAPIBase(APIBase): + def compare(self, *args, **kwargs): + result = paddle.sort(paddle.to_tensor([2.0, 1.0]), dim=0) + assert hasattr(result, "values") + return super().compare(*args, **kwargs) + + guard_obj = GuardCheckingAPIBase("torch.nn.PReLU") + native_prelu = paddle.nn.PReLU + native_torch = torch + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[-2.0, -0.5, 0.75, 1.5]]) + result = torch.nn.PReLU(1, 0.45)(x) + """ + ) + + guard_obj.run(pytorch_code, ["result"]) + guard_obj.run(pytorch_code, ["result"]) + + assert paddle.nn.PReLU is native_prelu + assert importlib.import_module("torch") is native_torch diff --git a/tests/test_onnx_export.py b/tests/test_onnx_export.py index a3271fb35..1adec9c7d 100644 --- a/tests/test_onnx_export.py +++ b/tests/test_onnx_export.py @@ -105,12 +105,14 @@ def onnx_export(model,f): ############################## 相关utils函数,如上 ############################## + paddle.enable_compat(level=2) + class SimpleModel(paddle.nn.Module): def __init__(self): super(SimpleModel, self).__init__() - self.fc1 = paddle.compat.nn.Linear(3, 3) - self.fc2 = paddle.compat.nn.Linear(3, 1) + self.fc1 = paddle.nn.Linear(3, 3) + self.fc2 = paddle.nn.Linear(3, 1) def forward(self, x): x = paddle.relu(self.fc1(x)) diff --git a/tests/test_optim_lr_scheduler_LRScheduler.py b/tests/test_optim_lr_scheduler_LRScheduler.py index 3d47b0c1d..c9d6d1ea5 100644 --- a/tests/test_optim_lr_scheduler_LRScheduler.py +++ b/tests/test_optim_lr_scheduler_LRScheduler.py @@ -36,6 +36,7 @@ def get_lr(self): loss = parameter.sum() loss.backward() optimizer.step() + scheduler.step() result = parameter result_epoch = scheduler.last_epoch result_is_base = isinstance( diff --git a/tests/test_utils_model_zoo_load_url.py b/tests/test_utils_model_zoo_load_url.py index a23726057..46b1afdb8 100644 --- a/tests/test_utils_model_zoo_load_url.py +++ b/tests/test_utils_model_zoo_load_url.py @@ -30,6 +30,7 @@ def test_case_1(): """ import paddle + paddle.enable_compat(level=2) result = paddle.hub.load_state_dict_from_url(url="http://example.com/model.pth") """ ) @@ -47,6 +48,7 @@ def test_case_2(): """ import paddle + paddle.enable_compat(level=2) result = paddle.hub.load_state_dict_from_url( url="http://example.com/model.pth", model_dir="/tmp" ) @@ -66,6 +68,7 @@ def test_case_3(): """ import paddle + paddle.enable_compat(level=2) result = paddle.hub.load_state_dict_from_url( url="http://example.com/model.pth", model_dir="/tmp", map_location="cpu" ) diff --git a/tests/torchvision_tests/model_apibase.py b/tests/torchvision_tests/model_apibase.py index c3ee9f397..fda30eed1 100644 --- a/tests/torchvision_tests/model_apibase.py +++ b/tests/torchvision_tests/model_apibase.py @@ -44,7 +44,8 @@ def compare( else: simple_input = np.random.rand(1, 3, 224, 224).astype(np.float32) - pytorch_output = pytorch_result(torch.tensor(simple_input)) + with paddle.use_compat_guard(enable=False): + pytorch_output = pytorch_result(torch.tensor(simple_input)) paddle_output = paddle_result(paddle.to_tensor(simple_input)) if isinstance(pytorch_output, torch.Tensor): diff --git a/tests/torchvision_tests/test_Cifar10.py b/tests/torchvision_tests/test_Cifar10.py index 3aba0f1c6..3f886cc21 100644 --- a/tests/torchvision_tests/test_Cifar10.py +++ b/tests/torchvision_tests/test_Cifar10.py @@ -38,6 +38,7 @@ def test_case_1(): import paddle + paddle.enable_compat(level=2) train_dataset = paddle.vision.datasets.Cifar10( transform=paddle.vision.transforms.Normalize(mean=(0.5,), std=(0.5,)), download=True, @@ -67,6 +68,7 @@ def test_case_2(): import paddle + paddle.enable_compat(level=2) train_dataset = paddle.vision.datasets.Cifar10( transform=paddle.vision.transforms.Normalize(mean=(0.5,), std=(0.5,)), data_file=os.path.join("./data", "cifar-10-python.tar.gz"), @@ -99,6 +101,7 @@ def test_case_3(): import paddle + paddle.enable_compat(level=2) train_dataset = paddle.vision.datasets.Cifar10( download=True, transform=paddle.vision.transforms.Normalize(mean=(0.5,), std=(0.5,)), @@ -126,6 +129,7 @@ def test_case_4(): import paddle + paddle.enable_compat(level=2) train_dataset = paddle.vision.datasets.Cifar10( data_file=os.path.join("./data", "cifar-10-python.tar.gz") ) @@ -151,6 +155,7 @@ def test_case_5(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.Cifar10( data_file=os.path.join(root_path, "cifar-10-python.tar.gz"), mode="train" @@ -177,6 +182,7 @@ def test_case_6(): import paddle + paddle.enable_compat(level=2) train = True train_dataset = paddle.vision.datasets.Cifar10( data_file=os.path.join("./data", "cifar-10-python.tar.gz"), diff --git a/tests/torchvision_tests/test_Cifar100.py b/tests/torchvision_tests/test_Cifar100.py index a83b41f00..9a0c0851a 100644 --- a/tests/torchvision_tests/test_Cifar100.py +++ b/tests/torchvision_tests/test_Cifar100.py @@ -38,6 +38,7 @@ def test_case_1(): import paddle + paddle.enable_compat(level=2) train_dataset = paddle.vision.datasets.Cifar100( transform=paddle.vision.transforms.Normalize(mean=(0.5,), std=(0.5,)), download=True, @@ -67,6 +68,7 @@ def test_case_2(): import paddle + paddle.enable_compat(level=2) train_dataset = paddle.vision.datasets.Cifar100( transform=paddle.vision.transforms.Normalize(mean=(0.5,), std=(0.5,)), data_file=os.path.join("./data", "cifar-100-python.tar.gz"), @@ -99,6 +101,7 @@ def test_case_3(): import paddle + paddle.enable_compat(level=2) train_dataset = paddle.vision.datasets.Cifar100( download=True, transform=paddle.vision.transforms.Normalize(mean=(0.5,), std=(0.5,)), @@ -126,6 +129,7 @@ def test_case_4(): import paddle + paddle.enable_compat(level=2) train_dataset = paddle.vision.datasets.Cifar100( data_file=os.path.join("./data", "cifar-100-python.tar.gz") ) @@ -151,6 +155,7 @@ def test_case_5(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.Cifar100( data_file=os.path.join(root_path, "cifar-100-python.tar.gz"), mode="train" @@ -177,6 +182,7 @@ def test_case_6(): import paddle + paddle.enable_compat(level=2) train = True train_dataset = paddle.vision.datasets.Cifar100( data_file=os.path.join("./data", "cifar-100-python.tar.gz"), diff --git a/tests/torchvision_tests/test_DeformConv2d.py b/tests/torchvision_tests/test_DeformConv2d.py index e0cccefb3..b39f632b4 100644 --- a/tests/torchvision_tests/test_DeformConv2d.py +++ b/tests/torchvision_tests/test_DeformConv2d.py @@ -39,6 +39,7 @@ def test_case_1(): """ import paddle + paddle.enable_compat(level=2) deform_conv = paddle.vision.ops.DeformConv2D( in_channels=3, out_channels=4, @@ -65,6 +66,7 @@ def test_case_2(): """ import paddle + paddle.enable_compat(level=2) deform_conv = paddle.vision.ops.DeformConv2D( in_channels=3, out_channels=4, @@ -99,6 +101,7 @@ def test_case_3(): """ import paddle + paddle.enable_compat(level=2) deform_conv = paddle.vision.ops.DeformConv2D( kernel_size=3, in_channels=3, @@ -125,6 +128,7 @@ def test_case_4(): """ import paddle + paddle.enable_compat(level=2) deform_conv = paddle.vision.ops.DeformConv2D( in_channels=3, out_channels=64, kernel_size=3 ) @@ -153,6 +157,7 @@ def test_case_5(): """ import paddle + paddle.enable_compat(level=2) deform_conv = paddle.vision.ops.DeformConv2D( groups=1, dilation=1, diff --git a/tests/torchvision_tests/test_FashionMNIST.py b/tests/torchvision_tests/test_FashionMNIST.py index e3e8676f1..8b4a14a77 100644 --- a/tests/torchvision_tests/test_FashionMNIST.py +++ b/tests/torchvision_tests/test_FashionMNIST.py @@ -33,6 +33,7 @@ def test_case_1(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.FashionMNIST( transform=None, @@ -63,6 +64,7 @@ def test_case_2(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.FashionMNIST( transform=None, @@ -93,6 +95,7 @@ def test_case_3(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.FashionMNIST( download=False, @@ -122,6 +125,7 @@ def test_case_4(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.FashionMNIST( mode="train", @@ -150,6 +154,7 @@ def test_case_5(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.FashionMNIST( mode="train", @@ -179,6 +184,7 @@ def test_case_6(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train = True train_dataset = paddle.vision.datasets.FashionMNIST( @@ -214,6 +220,7 @@ def test_case_7(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train = True download = False diff --git a/tests/torchvision_tests/test_Flowers102.py b/tests/torchvision_tests/test_Flowers102.py index 370da747f..107775fd2 100644 --- a/tests/torchvision_tests/test_Flowers102.py +++ b/tests/torchvision_tests/test_Flowers102.py @@ -33,6 +33,7 @@ def test_case_1(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.Flowers( transform=None, @@ -65,6 +66,7 @@ def test_case_2(): import paddle + paddle.enable_compat(level=2) root_path = "./data" split = "train" train_dataset = paddle.vision.datasets.Flowers( @@ -95,6 +97,7 @@ def test_case_3(): import paddle + paddle.enable_compat(level=2) train_dataset = paddle.vision.datasets.Flowers( download=False, mode="train", @@ -124,6 +127,7 @@ def test_case_4(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.Flowers( mode="train", @@ -153,6 +157,7 @@ def test_case_5(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.Flowers( mode="test", @@ -183,6 +188,7 @@ def test_case_6(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train = True train_dataset = paddle.vision.datasets.Flowers( @@ -215,6 +221,7 @@ def test_case_7(): import paddle + paddle.enable_compat(level=2) root_path = "./data" split = "val" download = False diff --git a/tests/torchvision_tests/test_ImageFolder.py b/tests/torchvision_tests/test_ImageFolder.py index c8eec7ba7..dd31fdad2 100644 --- a/tests/torchvision_tests/test_ImageFolder.py +++ b/tests/torchvision_tests/test_ImageFolder.py @@ -30,10 +30,11 @@ def test_case_1(): ) paddle_code = textwrap.dedent( """ - from pathlib import Path - import paddle + paddle.enable_compat(level=2) + from pathlib import Path + fake_data_dir = "./text_data" image_folder = paddle.vision.datasets.ImageFolder(root=Path(fake_data_dir)) """ @@ -59,9 +60,11 @@ def test_case_2(): ) paddle_code = textwrap.dedent( """ - import cv2 import paddle + paddle.enable_compat(level=2) + import cv2 + fake_data_dir = "./text_data" image_folder = paddle.vision.datasets.ImageFolder( root=fake_data_dir, loader=lambda x: cv2.imread(x), transform=transform @@ -91,9 +94,11 @@ def test_case_3(): ) paddle_code = textwrap.dedent( """ - import cv2 import paddle + paddle.enable_compat(level=2) + import cv2 + fake_data_dir = "./text_data" image_folder = paddle.vision.datasets.ImageFolder( root=fake_data_dir, @@ -126,9 +131,11 @@ def test_case_4(): ) paddle_code = textwrap.dedent( """ - import cv2 import paddle + paddle.enable_compat(level=2) + import cv2 + fake_data_dir = "./text_data" image_folder = paddle.vision.datasets.ImageFolder( root=fake_data_dir, @@ -161,9 +168,11 @@ def test_case_5(): ) paddle_code = textwrap.dedent( """ - import cv2 import paddle + paddle.enable_compat(level=2) + import cv2 + fake_data_dir = "./text_data" image_folder = paddle.vision.datasets.ImageFolder( loader=lambda x: cv2.imread(x), @@ -193,6 +202,7 @@ def test_case_6(): """ import paddle + paddle.enable_compat(level=2) fake_data_dir = "./text_data" image_folder = paddle.vision.datasets.ImageFolder(root=fake_data_dir) """ diff --git a/tests/torchvision_tests/test_MNIST.py b/tests/torchvision_tests/test_MNIST.py index 1516130cf..cefc2335c 100644 --- a/tests/torchvision_tests/test_MNIST.py +++ b/tests/torchvision_tests/test_MNIST.py @@ -33,6 +33,7 @@ def test_case_1(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.MNIST( transform=None, @@ -63,6 +64,7 @@ def test_case_2(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.MNIST( transform=None, @@ -93,6 +95,7 @@ def test_case_3(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.MNIST( download=False, @@ -122,6 +125,7 @@ def test_case_4(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.MNIST( mode="train", @@ -150,6 +154,7 @@ def test_case_5(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train_dataset = paddle.vision.datasets.MNIST( mode="train", @@ -179,6 +184,7 @@ def test_case_6(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train = True train_dataset = paddle.vision.datasets.MNIST( @@ -214,6 +220,7 @@ def test_case_7(): import paddle + paddle.enable_compat(level=2) root_path = "./data" train = True download = False diff --git a/tests/torchvision_tests/test_RoIAlign.py b/tests/torchvision_tests/test_RoIAlign.py index 65499d3ab..181323408 100644 --- a/tests/torchvision_tests/test_RoIAlign.py +++ b/tests/torchvision_tests/test_RoIAlign.py @@ -31,6 +31,7 @@ def test_case_1(): """ import paddle + paddle.enable_compat(level=2) roi_align = paddle.vision.ops.RoIAlign(output_size=(7, 7), spatial_scale=1.0) """ ) @@ -49,6 +50,7 @@ def test_case_2(): """ import paddle + paddle.enable_compat(level=2) roi_align = paddle.vision.ops.RoIAlign(output_size=(7, 7), spatial_scale=1.0) """ ) @@ -67,6 +69,7 @@ def test_case_3(): """ import paddle + paddle.enable_compat(level=2) roi_align = paddle.vision.ops.RoIAlign(spatial_scale=1.0, output_size=(7, 7)) """ ) @@ -85,6 +88,7 @@ def test_case_4(): """ import paddle + paddle.enable_compat(level=2) roi_align = paddle.vision.ops.RoIAlign(output_size=7, spatial_scale=1.0) """ ) @@ -103,6 +107,7 @@ def test_case_5(): """ import paddle + paddle.enable_compat(level=2) roi_align = paddle.vision.ops.RoIAlign(output_size=(7, 7), spatial_scale=2.0) """ ) diff --git a/tests/torchvision_tests/test_RoIPool.py b/tests/torchvision_tests/test_RoIPool.py index 6be8dfccf..4a2df6f9c 100644 --- a/tests/torchvision_tests/test_RoIPool.py +++ b/tests/torchvision_tests/test_RoIPool.py @@ -31,6 +31,7 @@ def test_case_1(): """ import paddle + paddle.enable_compat(level=2) roi_pool = paddle.vision.ops.RoIPool(output_size=(7, 7), spatial_scale=1.0) """ ) @@ -49,6 +50,7 @@ def test_case_2(): """ import paddle + paddle.enable_compat(level=2) roi_pool = paddle.vision.ops.RoIPool((7, 7), 1.0) """ ) @@ -67,6 +69,7 @@ def test_case_3(): """ import paddle + paddle.enable_compat(level=2) roi_pool = paddle.vision.ops.RoIPool(spatial_scale=1.0, output_size=(7, 7)) """ ) @@ -85,6 +88,7 @@ def test_case_4(): """ import paddle + paddle.enable_compat(level=2) roi_pool = paddle.vision.ops.RoIPool(output_size=7, spatial_scale=1.0) """ ) @@ -103,6 +107,7 @@ def test_case_5(): """ import paddle + paddle.enable_compat(level=2) roi_pool = paddle.vision.ops.RoIPool(output_size=(7, 7), spatial_scale=2.0) """ ) diff --git a/tests/torchvision_tests/test_VOCDetection.py b/tests/torchvision_tests/test_VOCDetection.py index 9a36a9ed9..7c639f181 100644 --- a/tests/torchvision_tests/test_VOCDetection.py +++ b/tests/torchvision_tests/test_VOCDetection.py @@ -58,6 +58,7 @@ def VOCDetection(*args, **kwargs): ############################## 相关utils函数,如上 ############################## + paddle.enable_compat(level=2) image_folder = VOCDetection(root="./text_data") """ ) @@ -105,6 +106,7 @@ def VOCDetection(*args, **kwargs): ############################## 相关utils函数,如上 ############################## + paddle.enable_compat(level=2) image_folder = VOCDetection( root="./text_data", image_set="train", download=False, transform=None ) @@ -154,6 +156,7 @@ def VOCDetection(*args, **kwargs): ############################## 相关utils函数,如上 ############################## + paddle.enable_compat(level=2) image_folder = VOCDetection( image_set="train", download=False, root="./text_data", transform=None ) diff --git a/tests/torchvision_tests/test_enums.py b/tests/torchvision_tests/test_enums.py index 9cef10070..0383b34f9 100644 --- a/tests/torchvision_tests/test_enums.py +++ b/tests/torchvision_tests/test_enums.py @@ -116,6 +116,7 @@ def test_case(): f""" import paddle + paddle.enable_compat(level=2) mode = "{paddle_value}" """ ) diff --git a/tests/torchvision_tests/test_googlenet.py b/tests/torchvision_tests/test_googlenet.py index 6f7880602..969f72ab7 100644 --- a/tests/torchvision_tests/test_googlenet.py +++ b/tests/torchvision_tests/test_googlenet.py @@ -90,6 +90,7 @@ def test_case_7(): """ import paddle + paddle.enable_compat(level=2) googlenet = paddle.vision.models.googlenet(pretrained=False) """ )