├── tests ├── code_library │ ├── .gitignore │ ├── code_case │ │ ├── .gitignore │ │ ├── paddle_code │ │ │ ├── api_paddle_version.py │ │ │ ├── api_paddle_nn_BatchNorm3D.py │ │ │ ├── api_paddle_random_manual_seed.py │ │ │ ├── api_paddle_cuda_is_available.py │ │ │ ├── api_paddle_sigmoid.py │ │ │ ├── api_paddle_permute.py │ │ │ ├── api_paddle_equall.py │ │ │ ├── api_paddle_Tensor_normal_.py │ │ │ ├── api_remove_decorator.py │ │ │ ├── api_paddle_Tensor_uniform_.py │ │ │ ├── package_visit_name.py │ │ │ ├── non_paddle_class.py │ │ │ ├── import_analysis.py │ │ │ ├── api_paddle_Tensor2Int.py │ │ │ ├── api_paddle_Tensor2Long.py │ │ │ ├── api_paddle_Tensor2Float.py │ │ │ ├── api_paddle_Tensor_expand.py │ │ │ ├── simplified_name_call.py │ │ │ ├── class_method_static_call.py │ │ │ ├── api_paddle_nn_functional_interpolate.py │ │ │ ├── api_paddle_Tensor_new_tensor.py │ │ │ ├── api_paddle_randint.py │ │ │ ├── api_paddle_Tensor_new_empty.py │ │ │ ├── api_paddle_Tensor_new_full.py │ │ │ ├── api_paddle_nn_BatchNorm2D.py │ │ │ ├── attribute_setattr.py │ │ │ ├── api_paddle_nn_BatchNorm1D.py │ │ │ ├── api_paddle_Tensor_new_ones.py │ │ │ ├── api_paddle_Tensor_new_zeros.py │ │ │ ├── api_paddle_Generator.py │ │ │ ├── insert_pass_in_ast_if.py │ │ │ ├── api_paddle_Size.py │ │ │ ├── api_paddle_to_tensor.py │ │ │ ├── api_paddle_io_BatchSampler.py │ │ │ ├── attribute_visit_name.py │ │ │ ├── may_paddle_package_list.py │ │ │ ├── api_paddle_nn_InstanceNorm3D.py │ │ │ └── type_hinting.py │ │ ├── custom_op │ │ │ ├── paddle_code │ │ │ │ ├── function │ │ │ │ │ ├── __init__.py │ │ │ │ │ └── geometric_kernel_attn_func.py │ │ │ │ ├── src │ │ │ │ │ ├── version.cpp │ │ │ │ │ └── geometric_kernel_attn_cuda.h │ │ │ │ └── network.py │ │ │ └── torch_code │ │ │ │ ├── function │ │ │ │ └── __init__.py │ │ │ │ ├── src │ │ │ │ ├── version.cpp │ │ │ │ └── geometric_kernel_attn_cuda.h │ │ │ │ └── network.py │ │ └── torch_code │ │ │ ├── non_torch_class.py │ │ │ ├── package_visit_name.py │ │ │ ├── class_method_static_call.py │ │ │ ├── simplified_name_call.py │ │ │ ├── import_analysis.py │ │ │ ├── attribute_setattr.py │ │ │ ├── api_torch_version.py │ │ │ ├── insert_pass_in_ast_if.py │ │ │ ├── api_torch_nn_sigmoid.py │ │ │ ├── api_torch_cuda_is_available.py │ │ │ ├── api_torch_nn_BatchNorm3d.py │ │ │ ├── api_torch_random_manual_seed.py │ │ │ ├── api_torch_sigmoid.py │ │ │ ├── api_torch_equal.py │ │ │ ├── api_torch_permute.py │ │ │ ├── api_torch_jit_script.py │ │ │ ├── api_torch_Tensor_normal_.py │ │ │ ├── api_torch_Tensor_uniform_.py │ │ │ ├── api_torch_data_BatchSampler.py │ │ │ ├── api_torch_IntTensor.py │ │ │ ├── api_torch_LongTensor.py │ │ │ ├── api_torch_FloatTensor.py │ │ │ ├── api_torch_Tensor_expand.py │ │ │ ├── api_torch_nn_functional_interpolate.py │ │ │ ├── api_torch_Tensor_new_tensor.py │ │ │ ├── api_torch_nn_BatchNorm1d.py │ │ │ ├── api_torch_nn_BatchNorm2d.py │ │ │ ├── api_torch_randint.py │ │ │ ├── api_torch_Tensor_new_empty.py │ │ │ ├── api_torch_Tensor_new_full.py │ │ │ ├── api_torch_Tensor_new_ones.py │ │ │ ├── api_torch_Tensor_new_zeros.py │ │ │ ├── type_hinting.py │ │ │ └── may_torch_package_list.py │ └── model_case │ │ ├── torch_code │ │ └── exclude_convert.py │ │ └── __init__.py ├── requirements.txt ├── pytest.ini ├── torchvision_tests │ └── __init__.py ├── distributed │ ├── barrier.py │ ├── ReduceOp.py │ ├── monitored_barrier.py │ ├── nn_DataParallel.py │ ├── nn_parallel_DistributedDataParallel.py │ ├── get_rank.py │ ├── get_world_size.py │ ├── get_backend.py │ ├── all_gather_object.py │ ├── recv.py │ ├── send.py │ ├── reduce_scatter_tensor2.py │ ├── all_reduce.py │ ├── irecv.py │ ├── isend.py │ ├── reduce_scatter_tensor.py │ ├── all_to_all_single.py │ ├── batch_isend_irecv.py │ ├── broadcast_object_list.py │ ├── all_gather.py │ ├── reduce.py │ └── scatter_object_list.py ├── test_e.py ├── test_pi.py ├── test_inf.py ├── test_get_rng_state.py ├── test_newaxis.py ├── test_random_seed.py ├── test_nan.py ├── test_random_get_rng_state.py ├── test_seed.py ├── test_is_grad_enabled.py ├── test_Tensor_frexp.py ├── test_cuda_is_available.py ├── test_Tensor_erf_.py ├── test_Tensor_item.py ├── test_Tensor_tan_.py ├── test_Tensor_frac_.py ├── test_Tensor_t.py ├── test_Tensor_trunc_.py ├── test_get_num_threads.py ├── test_Tensor_digamma_.py ├── test_Tensor_is_cuda.py ├── test_Tensor_sigmoid_.py ├── test_Tensor_sin.py ├── test_Tensor_t_.py ├── test_Tensor_tan.py ├── test_backends_cuda_is_built.py ├── test_set_num_interop_threads.py ├── test_Tensor_arcsin_.py ├── test_Tensor_asinh.py ├── test_Tensor_sinh.py ├── test_Tensor_tanh.py ├── test_Tensor_trace.py ├── test_Tensor_trunc.py ├── test_initial_seed.py ├── test_multiprocessing_cpu_count.py ├── test_Tensor_grad.py ├── test_Tensor_tanh_.py ├── test_distributed_is_available.py ├── test_distributed_is_initialized.py ├── test_Tensor_conj.py ├── test_Tensor_cos_.py ├── test_Tensor_cosh_.py ├── test_Tensor_is_sparse.py ├── test_Tensor_sigmoid.py ├── test_Tensor_sin_.py ├── test_Tensor_sinh_.py ├── test_backends_cudnn_is_available.py ├── test_Tensor_is_leaf.py ├── test_get_num_interop_threads.py ├── test_Tensor_arccosh_.py ├── test_Tensor_arctan_.py ├── test_Tensor_arctanh_.py ├── test_Tensor_atanh.py ├── test_Tensor_tolist.py ├── test_distributed_is_nccl_available.py ├── test_random_initial_seed.py ├── test_Tensor_arctanh.py ├── test_Tensor_names.py ├── test_backends_cudnn_version.py ├── test_Tensor_conj_physical.py ├── test_Tensor_apply_.py ├── test_Tensor_col_indices.py ├── test_Tensor_crow_indices.py ├── test_Tensor_retain_grad.py ├── test_Tensor_dtype.py ├── test_Tensor_indices.py ├── test_Tensor_values.py ├── test_Tensor_deg2rad.py ├── test_hub_get_dir.py ├── test_onnx_enable_log.py ├── test_onnx_disable_log.py ├── test_Tensor_is_pinned.py ├── test_hub_set_dir.py ├── test_layout.py ├── test_get_deterministic_debug_mode.py ├── test_nn_Module_xpu.py ├── test_profiler_ProfilerAction.py ├── test_profiler_ProfilerActivity.py ├── test_Tensor_is_coalesced.py ├── test_Tensor_matrix_exp.py ├── test_distributed_rpc_WorkerInfo.py ├── test_Tensor_is_conj.py ├── test_cuda_empty_cache.py ├── test_cuda_ipc_collect.py ├── test_nn_Module_cpu.py ├── test_nn_Module_half.py ├── test_Tensor_dense_dim.py ├── test_Tensor_retains_grad.py ├── test_Tensor_sparse_resize_.py ├── test_Tensor_to_dense.py ├── test_cuda_device_count.py ├── test_nn_Module_float.py ├── test_Tensor_sparse_dim.py ├── test_nn_Module_double.py ├── test_nn_Module_modules.py ├── test_cuda_initial_seed.py ├── test_nn_Module_children.py ├── test_Tensor_sparse_resize_and_clear_.py ├── test_Tensor_pin_memory.py ├── test_to_dense.py ├── test_Tensor_coalesce.py ├── test_autograd_profiler_profile_self_cpu_time_total.py └── test_set_num_threads.py ├── requirements.txt ├── tools ├── validate_docs │ └── .gitignore ├── validate_unittest │ └── .gitignore └── docker │ └── Dockerfile ├── images └── comate_paconvert.jpeg ├── paconvert ├── api_wildcard_mapping.json ├── transformer │ └── __init__.py └── __init__.py ├── .github ├── PULL_REQUEST_TEMPLATE.md └── workflows_origin │ ├── tests.yml │ ├── lint.yml │ └── coverage.yml ├── .gitignore └── .flake8 /tests/code_library/.gitignore: -------------------------------------------------------------------------------- 1 | */__pycache__ 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | astor 2 | black==22.8.0 3 | isort==5.11.5 4 | -------------------------------------------------------------------------------- /tools/validate_docs/.gitignore: -------------------------------------------------------------------------------- 1 | docs_mappings.json 2 | *.log 3 | -------------------------------------------------------------------------------- /tests/code_library/code_case/.gitignore: -------------------------------------------------------------------------------- 1 | # consistency check output 2 | convert_paddle_code/ 3 | -------------------------------------------------------------------------------- /tests/requirements.txt: -------------------------------------------------------------------------------- 1 | pre-commit 2 | pytest 3 | pytest-cov 4 | regex 5 | coverage 6 | diff-cover 7 | pandas 8 | -------------------------------------------------------------------------------- /images/comate_paconvert.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaConvert/HEAD/images/comate_paconvert.jpeg -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_version.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print(paddle.__version__) 4 | -------------------------------------------------------------------------------- /tests/pytest.ini: -------------------------------------------------------------------------------- 1 | [pytest] 2 | ; print user output; clear warning 3 | addopts = -v -s -p no:warnings 4 | timeout = 300 5 | -------------------------------------------------------------------------------- /tools/validate_unittest/.gitignore: -------------------------------------------------------------------------------- 1 | validation_report.md 2 | validation.json 3 | 4 | missing_unittest_list.log 5 | 6 | __pycache__/ 7 | -------------------------------------------------------------------------------- /tests/code_library/model_case/torch_code/exclude_convert.py: -------------------------------------------------------------------------------- 1 | # An empty file, just test exclude convert 2 | print('test exclude convert') 3 | exit(1) 4 | -------------------------------------------------------------------------------- /tests/code_library/code_case/custom_op/paddle_code/function/__init__.py: -------------------------------------------------------------------------------- 1 | import GeometricKernelAttention 2 | from .geometric_kernel_attn_func import custom_op_module 3 | -------------------------------------------------------------------------------- /tests/code_library/code_case/custom_op/torch_code/function/__init__.py: -------------------------------------------------------------------------------- 1 | from .geometric_kernel_attn_func import GeometricKernelAttentionFunc, GeometricKernelAttentionFuncLoad 2 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_nn_BatchNorm3D.py: -------------------------------------------------------------------------------- 1 | print("#########################case1#########################") 2 | out_channels = 3 3 | nn.BatchNorm3d(out_channels) 4 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_random_manual_seed.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | paddle.manual_seed(23) 5 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_cuda_is_available.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | paddle.cuda.is_available() 5 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_sigmoid.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | t = paddle.randn(4) 5 | paddle.sigmoid(t) 6 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_permute.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | x = paddle.rand([2, 3, 4, 4]) 5 | x.permute(0, 2, 3, 1) 6 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_equall.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | r = paddle.compat.equal(paddle.tensor([1, 2]), paddle.tensor([1, 2])) 5 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_Tensor_normal_.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | a = paddle.tensor([1, 3, 4, 9, 0.5, 1.5]) 5 | a = a.normal_(0.2, 0.3) 6 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_remove_decorator.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | 5 | 6 | @paddle.jit.to_static 7 | def a(x: paddle.IntTensor): 8 | pass 9 | -------------------------------------------------------------------------------- /paconvert/api_wildcard_mapping.json: -------------------------------------------------------------------------------- 1 | { 2 | "einops.layers.torch.*": { 3 | "Matcher": "EinopsTorchMatcher" 4 | }, 5 | "transformers.*": { 6 | "Matcher": "TransformersGenericMatcher", 7 | "disable": true 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_Tensor_uniform_.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | a = paddle.tensor([1, 3, 4, 9, 0.5, 1.5]) 5 | c = paddle.tensor(a.uniform_(2, 6)) 6 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/package_visit_name.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | import paddleformers 3 | 4 | setattr(paddle, "nn", nn_mymodule) 5 | hasattr(paddle, "nn") 6 | hasattr(paddle, "__version__") 7 | hasattr(paddleformers, "__version__") 8 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | ### PR Docs 3 | 4 | 5 | ### PR APIs 6 | 7 | ```bash 8 | 9 | ``` 10 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/non_paddle_class.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | uids = df[["uid"]] 4 | history = pd.merge(uids, self.data_loader.train_his_df, on="uid", how="left") 5 | history = history.rename(columns={"iids": global_p.C_HISTORY}) 6 | self.args.device 7 | paddle.max 8 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/non_torch_class.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | uids = df[['uid']] 4 | history = pd.merge(uids, self.data_loader.train_his_df, on='uid', how='left') 5 | history = history.rename(columns={'iids': global_p.C_HISTORY}) 6 | self.args.device 7 | paddle.max 8 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/package_visit_name.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import flash_attn 3 | import transformers 4 | 5 | setattr(torch, 'nn', nn_mymodule) 6 | 7 | hasattr(torch, "nn") 8 | 9 | hasattr(flash_attn, "__version__") 10 | 11 | hasattr(transformers, "__version__") 12 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/import_analysis.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | from . import datasets 3 | from .datasets import x 4 | from yolov3.datasets import a 5 | from . import torchvision 6 | from .torchvision import x 7 | from yolov3.torchvision import a 8 | from baidubce.services.bos.bos_client import BosClient 9 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_Tensor2Int.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | 5 | 6 | def a(x: paddle.IntTensor): 7 | pass 8 | 9 | 10 | print("#########################case2#########################") 11 | a = paddle.IntTensor(2, 3, 6) 12 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_Tensor2Long.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | 5 | 6 | def a(x: paddle.LongTensor): 7 | pass 8 | 9 | 10 | print("#########################case2#########################") 11 | a = paddle.LongTensor(2, 3) 12 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_Tensor2Float.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | 5 | 6 | def a(x: paddle.FloatTensor): 7 | pass 8 | 9 | 10 | print("#########################case2#########################") 11 | a = paddle.FloatTensor(2, 3, 6) 12 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_Tensor_expand.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | x = paddle.tensor([[1], [2], [3]]) 5 | y = x.expand(3, 4) 6 | print("#########################case2#########################") 7 | x = paddle.tensor([[1], [2], [3]]) 8 | y = x.expand((3, 4)) 9 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/class_method_static_call.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | class PT_Optimizer(torch.optim.Optimizer): 4 | pass 5 | 6 | PT_Optimizer.load_state_dict(self, swa_state_dict) 7 | PT_Optimizer.load_state_dict(self, swa_state_dict, True) 8 | self.optimizer.load_state_dict(opt_state_dict) 9 | sgd.load_state_dict(opt_state_dict) 10 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/simplified_name_call.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | a = paddle.tensor([1]) 4 | b = paddle.tensor([2]) 5 | print("#########################case1#########################") 6 | func = paddle.add 7 | paddle.add(a, b) 8 | print("#########################case2#########################") 9 | func = paddle.matmul 10 | paddle.matmul(a, b) 11 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/class_method_static_call.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | 4 | class PT_Optimizer(paddle.optimizer.Optimizer): 5 | pass 6 | 7 | 8 | PT_Optimizer.load_state_dict(self, swa_state_dict) 9 | PT_Optimizer.load_state_dict(self, swa_state_dict, True) 10 | self.optimizer.load_state_dict(opt_state_dict) 11 | sgd.load_state_dict(opt_state_dict) 12 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_nn_functional_interpolate.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | a = paddle.nn.functional.interpolate(paddle.randn(1, 2, 20, 20), [24, 24]) 5 | print("#########################case2#########################") 6 | a = paddle.nn.functional.interpolate(paddle.rand(1, 2, 20, 20), scale_factor=0.6) 7 | -------------------------------------------------------------------------------- /tests/code_library/code_case/custom_op/paddle_code/src/version.cpp: -------------------------------------------------------------------------------- 1 | #include "geometric_kernel_attn.h" 2 | 3 | PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) 4 | { 5 | m.def("geometric_kernel_attn_cuda_forward", &geometric_kernel_attn_cuda_forward, "geometric_kernel_attn_cuda_forward"); 6 | m.def("geometric_kernel_attn_cuda_backward", &geometric_kernel_attn_cuda_backward, "geometric_kernel_attn_cuda_backward"); 7 | } 8 | -------------------------------------------------------------------------------- /tests/code_library/code_case/custom_op/torch_code/src/version.cpp: -------------------------------------------------------------------------------- 1 | #include "geometric_kernel_attn.h" 2 | 3 | PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) 4 | { 5 | m.def("geometric_kernel_attn_cuda_forward", &geometric_kernel_attn_cuda_forward, "geometric_kernel_attn_cuda_forward"); 6 | m.def("geometric_kernel_attn_cuda_backward", &geometric_kernel_attn_cuda_backward, "geometric_kernel_attn_cuda_backward"); 7 | } 8 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/simplified_name_call.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.add as TorchAdd 3 | import torch.matmul as TorchMatul 4 | 5 | a=torch.tensor([1]) 6 | b=torch.tensor([2]) 7 | print("#########################case1#########################") 8 | func = TorchAdd 9 | func(a,b) 10 | 11 | print("#########################case2#########################") 12 | func = TorchMatul 13 | func(a,b) 14 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_tensor.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | a = paddle.tensor([1, 2, 3]) 5 | b = paddle.to_tensor(data=[4, 5, 6], dtype=paddle.float64, stop_gradient=not True) 6 | print("#########################case2#########################") 7 | a = paddle.tensor([1, 2, 3], dtype=paddle.int64) 8 | b = paddle.to_tensor(data=[4, 5, 6], dtype=a.dtype) 9 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_randint.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | a = paddle.randint(low=2, high=5, shape=[3, 4]) 5 | print("#########################case2#########################") 6 | paddle.randint(low=0, high=10, shape=[2, 2]) 7 | print("#########################case3#########################") 8 | a, b = 2, 25 9 | a = paddle.randint(low=a, high=b, shape=[3, 4]) 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | convert.log 2 | build/ 3 | dist/ 4 | torch_project 5 | paddle_project/ 6 | paconvert.egg-info/ 7 | paconvert/__pycache__/ 8 | paconvert/transformer/__pycache__/ 9 | paconvert/temp_out/ 10 | tests/__pycache__/ 11 | tests/.pytest_cache 12 | tests/flash_attn_tests/__pycache__/ 13 | tests/test.py 14 | test_project 15 | .pytest_cache 16 | coverage.xml 17 | .coverage 18 | tools/coverage/__pycache__ 19 | temp.txt 20 | test_set 21 | tensor.pt 22 | __pycache__ 23 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_empty.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | a = paddle.tensor([1, 2, 3], dtype=paddle.int64) 5 | b = a.new_empty([3, 4], dtype=paddle.float64, requires_grad=True, pin_memory=True) 6 | print("#########################case2#########################") 7 | flag = False 8 | a = paddle.tensor([1, 2, 3], dtype=paddle.int64) 9 | b = a.new_empty((2, 3), requires_grad=flag) 10 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_full.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | a = paddle.tensor([1, 2, 3], dtype=paddle.int64) 5 | b = a.new_full([3, 4], 2.43, dtype=paddle.float64, requires_grad=True, pin_memory=True) 6 | print("#########################case2#########################") 7 | flag = False 8 | a = paddle.tensor([1, 2, 3], dtype=paddle.int64) 9 | b = a.new_full((2, 3), 4, requires_grad=flag) 10 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_nn_BatchNorm2D.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | bn = paddle.nn.BatchNorm2D(num_features=5) 5 | print("#########################case2#########################") 6 | bn = paddle.nn.BatchNorm2D(num_features=27) 7 | print("#########################case3#########################") 8 | paddle.nn.BatchNorm2D( 9 | num_features=10, epsilon=1e-05, weight_attr=False, bias_attr=False 10 | ) 11 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/attribute_setattr.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | 4 | def add_module(self, name, module): 5 | self.add_module(f"{name} - {len(self) + 1}", module) 6 | 7 | 8 | paddle.nn.Module.add_module = add_module 9 | paddle.nn.Module.add_module = add_module 10 | paddle.nn.Module.add_module = add_module 11 | network = paddle.nn.Sequential(paddle.nn.Conv2d(1, 20, 5), paddle.nn.Conv2d(20, 64, 5)) 12 | network.add_module("ReLU", paddle.nn.ReLU()) 13 | print(network) 14 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_nn_BatchNorm1D.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | paddle.nn.BatchNorm1D(num_features=3, epsilon=0.001, momentum=1 - 0.01) 5 | print("#########################case2#########################") 6 | bn = paddle.nn.BatchNorm1D(num_features=27) 7 | print("#########################case3#########################") 8 | paddle.nn.BatchNorm1D( 9 | num_features=10, epsilon=1e-05, weight_attr=False, bias_attr=False 10 | ) 11 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_ones.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | a = paddle.tensor([1, 2, 3], dtype=paddle.int64) 5 | b = a.new_ones([3, 4], dtype=paddle.float64, requires_grad=True, pin_memory=True) 6 | print("#########################case2#########################") 7 | a = paddle.tensor([1, 2, 3], dtype=paddle.int64) 8 | b = a.new_ones(3, 4, requires_grad=True) 9 | print("#########################case3#########################") 10 | b = a.new_ones([3, 4]) 11 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/import_analysis.py: -------------------------------------------------------------------------------- 1 | import datasets # noqa: F401 2 | from . import datasets # noqa: F401 3 | from .datasets import x # noqa: F401 4 | from datasets import x # noqa: F401 5 | from yolov3.datasets import a # noqa: F401 6 | 7 | import torchvision # noqa: F401 8 | from . import torchvision # noqa: F401 9 | from .torchvision import x # noqa: F401 10 | from torchvision import x # noqa: F401 11 | from yolov3.torchvision import a # noqa: F401 12 | from baidubce.services.bos.bos_client import BosClient # noqa: F401 13 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_Tensor_new_zeros.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | a = paddle.tensor([1, 2, 3], dtype=paddle.int64) 5 | b = a.new_zeros([3, 4], dtype=paddle.float64, requires_grad=True, pin_memory=True) 6 | print("#########################case2#########################") 7 | a = paddle.tensor([1, 2, 3], dtype=paddle.int64) 8 | b = a.new_zeros(3, 4, requires_grad=True) 9 | print("#########################case3#########################") 10 | b = a.new_zeros([3, 4]) 11 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/attribute_setattr.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch import nn 3 | from torch.nn import Module 4 | 5 | def add_module(self, name, module): 6 | self.add_module(f'{name} - {len(self) + 1}', module) 7 | 8 | torch.nn.Module.add_module = add_module 9 | nn.Module.add_module = add_module 10 | Module.add_module = add_module 11 | 12 | network = torch.nn.Sequential( 13 | torch.nn.Conv2d(1,20,5), 14 | torch.nn.Conv2d(20,64,5) 15 | ) 16 | 17 | network.add_module("ReLU", torch.nn.ReLU()) 18 | 19 | print(network) 20 | -------------------------------------------------------------------------------- /.github/workflows_origin/tests.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | Test: 7 | name: Test 8 | runs-on: ubuntu-20.04 9 | permissions: 10 | pull-requests: write 11 | contents: read 12 | id-token: write 13 | steps: 14 | - uses: actions/checkout@v3 15 | - uses: actions/setup-python@v1 16 | with: 17 | python-version: 3.8 18 | - name: Install dependencies 19 | run: | 20 | python -m pip install --upgrade pip 21 | make install 22 | - name: run the command 23 | run: make test 24 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_Generator.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | g_cpu = paddle.Generator() 5 | print("#########################case2#########################") 6 | g_cpu = paddle.Generator(device="cpu") 7 | print("#########################case3#########################") 8 | g_cpu = paddle.Generator("cpu") 9 | print("#########################case4#########################") 10 | g_cuda = paddle.Generator("cuda") 11 | print("#########################case5#########################") 12 | g_cuda = paddle.Generator(device="cuda") 13 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/insert_pass_in_ast_if.py: -------------------------------------------------------------------------------- 1 | from typing import TYPE_CHECKING 2 | 3 | import paddle 4 | 5 | if TYPE_CHECKING: 6 | pass 7 | import warnings 8 | if TYPE_CHECKING: 9 | import numpy 10 | try: 11 | pass 12 | except: 13 | flash_attn_qkvpacked_func = None 14 | if True: 15 | pass 16 | else: 17 | flash_attn_qkvpacked_func = None 18 | if TYPE_CHECKING: 19 | a = paddle.randn(10, 20) 20 | b = paddle.randn(10, 20) 21 | c = paddle.matmul(a, b) 22 | d = paddle.add(a, b) 23 | numpy.array([1, 2, 3]) 24 | else: 25 | warnings.warn("The above code is meaningless, in order to pass the code style CI") 26 | -------------------------------------------------------------------------------- /paconvert/transformer/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /tests/torchvision_tests/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_Size.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | print(paddle.Size([2, 8, 64, 64])) 5 | print("#########################case2#########################") 6 | assert paddle.randn(6, 5, 7).size() == paddle.Size([6, 5, 7]) 7 | print("#########################case3#########################") 8 | out = paddle.Size([6, 5, 7]) 9 | shape_nchw = paddle.Size([6, 5, 7]) 10 | assert out == paddle.Size(shape_nchw) 11 | print("#########################case4#########################") 12 | print(paddle.Size([1])) 13 | print("#########################case5#########################") 14 | shape = paddle.Size([1]) 15 | -------------------------------------------------------------------------------- /paconvert/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from paconvert import main 16 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_to_tensor.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | a = paddle.tensor( 5 | paddle.tensor([2, 3, 4]), 6 | dtype=paddle.float32, 7 | device=paddle.device("cuda"), 8 | requires_grad=True, 9 | pin_memory=True, 10 | ) 11 | print("#########################case2#########################") 12 | flag = True 13 | a = paddle.tensor( 14 | paddle.tensor([2, 3, 4]), 15 | dtype=paddle.float32, 16 | device=paddle.device("cuda"), 17 | requires_grad=flag, 18 | pin_memory=True, 19 | ) 20 | print("#########################case3#########################") 21 | a = paddle.tensor([2, 3, 4], device="cuda") 22 | -------------------------------------------------------------------------------- /tests/code_library/code_case/custom_op/paddle_code/src/geometric_kernel_attn_cuda.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | #include 4 | 5 | at::Tensor geometric_kernel_attn_cuda_forward( 6 | const at::Tensor &value, 7 | const at::Tensor &spatial_shapes, 8 | const at::Tensor &level_start_index, 9 | const at::Tensor &sampling_loc, 10 | const at::Tensor &attn_weight, 11 | const int im2col_step); 12 | 13 | std::vector geometric_kernel_attn_cuda_backward( 14 | const at::Tensor &value, 15 | const at::Tensor &spatial_shapes, 16 | const at::Tensor &level_start_index, 17 | const at::Tensor &sampling_loc, 18 | const at::Tensor &attn_weight, 19 | const at::Tensor &grad_output, 20 | const int im2col_step); 21 | -------------------------------------------------------------------------------- /tests/code_library/code_case/custom_op/torch_code/src/geometric_kernel_attn_cuda.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | #include 4 | 5 | at::Tensor geometric_kernel_attn_cuda_forward( 6 | const at::Tensor &value, 7 | const at::Tensor &spatial_shapes, 8 | const at::Tensor &level_start_index, 9 | const at::Tensor &sampling_loc, 10 | const at::Tensor &attn_weight, 11 | const int im2col_step); 12 | 13 | std::vector geometric_kernel_attn_cuda_backward( 14 | const at::Tensor &value, 15 | const at::Tensor &spatial_shapes, 16 | const at::Tensor &level_start_index, 17 | const at::Tensor &sampling_loc, 18 | const at::Tensor &attn_weight, 19 | const at::Tensor &grad_output, 20 | const int im2col_step); 21 | -------------------------------------------------------------------------------- /tools/docker/Dockerfile: -------------------------------------------------------------------------------- 1 | # A image for testing PaConvert 2 | FROM registry.baidubce.com/paddlepaddle/paddle:latest-dev-cuda11.8-cudnn8.6-trt8.5-gcc82 3 | 4 | RUN apt-get update && \ 5 | apt-get install -y net-tools 6 | 7 | RUN ln -sf `which python3.10` /usr/bin/python 8 | 9 | RUN ln -sf `which pip3.10` /usr/local/bin/pip 10 | 11 | RUN python -m pip install astor 12 | 13 | RUN python -m pip install -U torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 14 | 15 | RUN wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-keyring_1.1-1_all.deb && \ 16 | dpkg -i cuda-keyring_1.1-1_all.deb && \ 17 | rm cuda-keyring_1.1-1_all.deb 18 | 19 | RUN apt-get update && \ 20 | apt-get install -y \ 21 | cudnn9-cuda-11 22 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_version.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | # case 1 19 | print(torch.__version__) 20 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/insert_pass_in_ast_if.py: -------------------------------------------------------------------------------- 1 | from typing import TYPE_CHECKING 2 | import torch 3 | if TYPE_CHECKING: 4 | from torch import randn 5 | from torch import matmul 6 | import warnings 7 | 8 | if TYPE_CHECKING: 9 | import torch, numpy 10 | 11 | try: 12 | from flash_attn import flash_attn_qkvpacked_func 13 | except: 14 | flash_attn_qkvpacked_func = None 15 | 16 | 17 | if True: 18 | from flash_attn import flash_attn_qkvpacked_func 19 | else: 20 | flash_attn_qkvpacked_func = None 21 | 22 | 23 | if TYPE_CHECKING: 24 | a=randn(10, 20) 25 | b=randn(10, 20) 26 | c=matmul(a, b) 27 | d=torch.add(a,b) 28 | numpy.array([1,2,3]) 29 | else: 30 | warnings.warn("The above code is meaningless, in order to pass the code style CI") 31 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_nn_sigmoid.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch.nn as nn 17 | 18 | print("#########################case1#########################") 19 | sigmoid = nn.Sigmoid() 20 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_cuda_is_available.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | torch.cuda.is_available() 20 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_nn_BatchNorm3d.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | 17 | print("#########################case1#########################") 18 | out_channels = 3 19 | nn.BatchNorm3d(out_channels) 20 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_random_manual_seed.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | torch.random.manual_seed(23) 20 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_sigmoid.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | t = torch.randn(4) 20 | torch.sigmoid(t) 21 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_equal.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | r = torch.equal(torch.tensor([1, 2]), torch.tensor([1, 2])) 20 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_permute.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | x = torch.rand([2, 3, 4, 4]) 20 | x.permute(0, 2, 3, 1) 21 | -------------------------------------------------------------------------------- /tests/distributed/barrier.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | dist.barrier() 25 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_jit_script.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | 20 | @torch.jit.script 21 | def a(x: torch.IntTensor): 22 | pass 23 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_Tensor_normal_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | a = torch.tensor([1, 3, 4, 9, 0.5, 1.5]) 20 | a = a.normal_(0.2, 0.3) 21 | -------------------------------------------------------------------------------- /tests/distributed/ReduceOp.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | print(dist.ReduceOp) 25 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_Tensor_uniform_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | a = torch.tensor([1, 3, 4, 9, 0.5, 1.5]) 20 | c = torch.tensor(a.uniform_(2, 6)) 21 | -------------------------------------------------------------------------------- /tests/distributed/monitored_barrier.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="gloo") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | dist.monitored_barrier() 25 | -------------------------------------------------------------------------------- /.github/workflows_origin/lint.yml: -------------------------------------------------------------------------------- 1 | name: Lint 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | Lint: 7 | name: Lint 8 | runs-on: ubuntu-20.04 9 | steps: 10 | - uses: actions/checkout@v3 11 | - name: checkout develop 12 | run: | 13 | if ! git show-ref --quiet refs/heads/master; then \ 14 | echo "local master branch is missing, creating local develop branch that tracks remote master branch" 15 | git fetch origin master 16 | git branch master --track origin/master 17 | else 18 | echo "local master branch exist, skipping" 19 | fi 20 | - uses: actions/setup-python@v1 21 | with: 22 | python-version: 3.8 23 | - name: Install dependencies 24 | run: | 25 | python -m pip install --upgrade pip 26 | make install 27 | - name: run the command 28 | run: make lint 29 | -------------------------------------------------------------------------------- /.github/workflows_origin/coverage.yml: -------------------------------------------------------------------------------- 1 | name: Coverage 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | Coverage: 7 | name: Coverage 8 | runs-on: ubuntu-20.04 9 | steps: 10 | - uses: actions/checkout@v3 11 | - name: checkout develop 12 | run: | 13 | if ! git show-ref --quiet refs/heads/master; then \ 14 | echo "local master branch is missing, creating local develop branch that tracks remote master branch" 15 | git fetch origin master 16 | git branch master --track origin/master 17 | else 18 | echo "local master branch exist, skipping" 19 | fi 20 | - uses: actions/setup-python@v1 21 | with: 22 | python-version: 3.8 23 | - name: Install dependencies 24 | run: | 25 | python -m pip install --upgrade pip 26 | make install 27 | - name: run the command 28 | run: make coverage 29 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_data_BatchSampler.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch.utils.data import BatchSampler 3 | 4 | print("#########################case1#########################") 5 | o = list(BatchSampler(range(10), batch_size=3, drop_last=True)) 6 | print("#########################case2#########################") 7 | o = list(BatchSampler(range(10), batch_size=3, drop_last=False)) 8 | print("#########################case3#########################") 9 | batch_sampler_train = torch.utils.data.BatchSampler(range(10), 2, drop_last=True) 10 | print("#########################case4#########################") 11 | batch_size = 4 12 | batch_sampler_train = torch.utils.data.BatchSampler( 13 | range(10), batch_size, drop_last=False 14 | ) 15 | print("#########################case5#########################") 16 | batch_size = 4 17 | batch_sampler_train = torch.utils.data.BatchSampler( 18 | sampler=range(10), batch_size=batch_size, drop_last=False 19 | ) 20 | -------------------------------------------------------------------------------- /tests/distributed/nn_DataParallel.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | 16 | import torch 17 | import torch.distributed as dist 18 | 19 | dist.init_process_group(backend="nccl") 20 | rank = dist.get_rank() 21 | torch.cuda.set_device(rank) 22 | 23 | model = torch.nn.Linear(10, 10, bias=False) 24 | model = torch.nn.DataParallel(model) 25 | print(model) 26 | -------------------------------------------------------------------------------- /tests/test_e.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.e") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.e 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"]) 30 | -------------------------------------------------------------------------------- /tests/test_pi.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.pi") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.pi 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"]) 30 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_IntTensor.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | 20 | 21 | def a(x: torch.IntTensor): 22 | pass 23 | 24 | 25 | print("#########################case2#########################") 26 | a = torch.IntTensor(2, 3, 6) 27 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_LongTensor.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | 20 | 21 | def a(x: torch.LongTensor): 22 | pass 23 | 24 | 25 | print("#########################case2#########################") 26 | a = torch.LongTensor(2, 3) 27 | -------------------------------------------------------------------------------- /tests/test_inf.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.inf") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.inf 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"]) 30 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_io_BatchSampler.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | o = list(paddle.io.BatchSampler(sampler=range(10), batch_size=3, drop_last=True)) 5 | print("#########################case2#########################") 6 | o = list(paddle.io.BatchSampler(sampler=range(10), batch_size=3, drop_last=False)) 7 | print("#########################case3#########################") 8 | batch_sampler_train = paddle.io.BatchSampler( 9 | sampler=range(10), batch_size=2, drop_last=True 10 | ) 11 | print("#########################case4#########################") 12 | batch_size = 4 13 | batch_sampler_train = paddle.io.BatchSampler( 14 | sampler=range(10), batch_size=batch_size, drop_last=False 15 | ) 16 | print("#########################case5#########################") 17 | batch_size = 4 18 | batch_sampler_train = paddle.io.BatchSampler( 19 | sampler=range(10), batch_size=batch_size, drop_last=False 20 | ) 21 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_FloatTensor.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | 20 | 21 | def a(x: torch.FloatTensor): 22 | pass 23 | 24 | 25 | print("#########################case2#########################") 26 | a = torch.FloatTensor(2, 3, 6) 27 | -------------------------------------------------------------------------------- /tests/test_get_rng_state.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import textwrap 15 | 16 | from apibase import APIBase 17 | 18 | obj = APIBase("torch.get_rng_state") 19 | 20 | 21 | def test_case_1(): 22 | pytorch_code = textwrap.dedent( 23 | """ 24 | import torch 25 | torch.get_rng_state() 26 | """ 27 | ) 28 | obj.run(pytorch_code) 29 | -------------------------------------------------------------------------------- /tests/test_newaxis.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.newaxis") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.newaxis 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"]) 30 | -------------------------------------------------------------------------------- /tests/test_random_seed.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.random.seed") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.random.seed() 27 | """ 28 | ) 29 | obj.run(pytorch_code) 30 | -------------------------------------------------------------------------------- /tests/test_nan.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.nan") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.nan 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"], check_value=False) 30 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_Tensor_expand.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | x = torch.tensor([[1], [2], [3]]) 20 | y = x.expand(3, 4) 21 | print("#########################case2#########################") 22 | x = torch.tensor([[1], [2], [3]]) 23 | y = x.expand((3, 4)) 24 | -------------------------------------------------------------------------------- /tests/test_random_get_rng_state.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import textwrap 15 | 16 | from apibase import APIBase 17 | 18 | obj = APIBase("torch.random.get_rng_state") 19 | 20 | 21 | def test_case_1(): 22 | pytorch_code = textwrap.dedent( 23 | """ 24 | import torch 25 | torch.random.get_rng_state() 26 | """ 27 | ) 28 | obj.run(pytorch_code) 29 | -------------------------------------------------------------------------------- /tests/test_seed.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.seed") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.seed() 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"], check_value=False) 30 | -------------------------------------------------------------------------------- /tests/test_is_grad_enabled.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.is_grad_enabled") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result=torch.is_grad_enabled() 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"]) 30 | -------------------------------------------------------------------------------- /tests/distributed/nn_parallel_DistributedDataParallel.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | 16 | import torch 17 | import torch.distributed as dist 18 | 19 | dist.init_process_group(backend="nccl") 20 | rank = dist.get_rank() 21 | torch.cuda.set_device(rank) 22 | 23 | model = torch.nn.Linear(1, 1, bias=False).to("cuda") 24 | model = torch.nn.parallel.DistributedDataParallel(model) 25 | 26 | print(model) 27 | -------------------------------------------------------------------------------- /tests/test_Tensor_frexp.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.frexp") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | x = torch.arange(9.) 27 | a,b = x.frexp() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["a"]) 31 | -------------------------------------------------------------------------------- /tests/test_cuda_is_available.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.cuda.is_available") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.cuda.is_available() 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"]) 30 | -------------------------------------------------------------------------------- /tests/code_library/code_case/custom_op/torch_code/network.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from .function import GeometricKernelAttentionFunc, GeometricKernelAttentionFuncLoad 3 | 4 | class GeometryKernelAttention(torch.nn.Module): 5 | def forward(self, 6 | query, 7 | key=None, 8 | value=None, 9 | identity=None, 10 | query_pos=None, 11 | key_padding_mask=None, 12 | reference_points=None, 13 | spatial_shapes=None, 14 | level_start_index=None, 15 | **kwargs): 16 | output1 = GeometricKernelAttentionFunc.apply( 17 | value, spatial_shapes, level_start_index, sampling_locations.contiguous(), attention_weights, self.im2col_step 18 | ) 19 | output2 = GeometricKernelAttentionFuncLoad.apply( 20 | value, spatial_shapes, level_start_index, sampling_locations.contiguous(), attention_weights, self.im2col_step 21 | ) 22 | return output1, output2 23 | -------------------------------------------------------------------------------- /tests/distributed/get_rank.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | g = dist.new_group([0, 1]) 25 | result = dist.get_rank(g) 26 | 27 | if rank == 0: 28 | print(result) 29 | torch.save(result, os.environ["DUMP_FILE"]) 30 | -------------------------------------------------------------------------------- /tests/test_Tensor_erf_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.erf_") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | a = torch.tensor([0, -1., 10.]) 28 | a.erf_() 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["a"]) 32 | -------------------------------------------------------------------------------- /tests/test_Tensor_item.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.item") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.tensor([4]) 27 | result = a.item() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_tan_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.tan_") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | a.tan_() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["a"]) 31 | -------------------------------------------------------------------------------- /tests/code_library/code_case/custom_op/paddle_code/function/geometric_kernel_attn_func.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | from __future__ import absolute_import 3 | from __future__ import print_function 4 | from __future__ import division 5 | 6 | 7 | def get_sources(self): 8 | current_dir = os.path.dirname(os.path.abspath(__file__)) 9 | extensions_dir = os.path.join(current_dir, 'src') 10 | main_file = glob.glob(os.path.join(extensions_dir, '*.cpp')) 11 | source_cuda = glob.glob(os.path.join(extensions_dir, '*.cu')) 12 | sources = main_file 13 | sources += source_cuda 14 | sources = [os.path.join(extensions_dir, s) for s in sources] 15 | return extensions_dir, sources 16 | 17 | 18 | custom_op_module = paddle.utils.cpp_extension.load(name= 19 | 'GeometricKernelAttention', sources=get_sources()[1], extra_cxx_cflags= 20 | [], extra_cuda_cflags=['-DCUDA_HAS_FP16=1', 21 | '-D__CUDA_NO_HALF_OPERATORS__', '-D__CUDA_NO_HALF_CONVERSIONS__', 22 | '-D__CUDA_NO_HALF2_OPERATORS__'], extra_include_paths=get_sources()[0]) 23 | -------------------------------------------------------------------------------- /tests/test_Tensor_frac_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.frac_") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | a = torch.tensor([1, 2.5, -3.2]) 28 | a.frac_() 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["a"]) 32 | -------------------------------------------------------------------------------- /tests/test_Tensor_t.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.t") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result = a.t() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_trunc_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.trunc_") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | a.trunc_() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["a"]) 31 | -------------------------------------------------------------------------------- /tests/test_get_num_threads.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.get_num_threads") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.get_num_threads() 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"], check_value=False) 30 | -------------------------------------------------------------------------------- /tests/distributed/get_world_size.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | g = dist.new_group([0, 1]) 25 | result = dist.get_world_size(g) 26 | 27 | if rank == 0: 28 | print(result) 29 | torch.save(result, os.environ["DUMP_FILE"]) 30 | -------------------------------------------------------------------------------- /tests/test_Tensor_digamma_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.digamma_") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | a = torch.tensor([1, 0.5]) 28 | a.digamma_() 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["a"]) 32 | -------------------------------------------------------------------------------- /tests/test_Tensor_is_cuda.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.is_cuda") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | x = torch.zeros(5, 3).cpu() 27 | result = x.is_cuda 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_sigmoid_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.sigmoid_") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | a.sigmoid_() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["a"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_sin.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.sin") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result = a.sin() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_t_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.t_") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result = a.t_() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_tan.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.tan") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result = a.tan() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_backends_cuda_is_built.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.backends.cuda.is_built") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.backends.cuda.is_built() 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"]) 30 | -------------------------------------------------------------------------------- /tests/test_set_num_interop_threads.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.set_num_interop_threads") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.set_num_interop_threads(1) 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"]) 30 | -------------------------------------------------------------------------------- /tests/distributed/get_backend.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | g = dist.new_group([0, 1]) 25 | result = dist.get_backend(g) 26 | 27 | if rank == 0: 28 | print(result) 29 | torch.save(result.upper(), os.environ["DUMP_FILE"]) 30 | -------------------------------------------------------------------------------- /tests/test_Tensor_arcsin_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.arcsin_") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | a = torch.tensor([0.34, -0.56, 0.73]) 28 | a.arcsin_() 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["a"]) 32 | -------------------------------------------------------------------------------- /tests/test_Tensor_asinh.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.asinh") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result = a.asinh() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_sinh.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.sinh") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result = a.sinh() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_tanh.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.tanh") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result = a.tanh() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_trace.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.trace") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result = a.trace() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_trunc.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.trunc") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result = a.trunc() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_initial_seed.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.initial_seed") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | torch.manual_seed(100) 27 | result = torch.initial_seed() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_multiprocessing_cpu_count.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.multiprocessing.cpu_count") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.multiprocessing.cpu_count() 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"]) 30 | -------------------------------------------------------------------------------- /tests/test_Tensor_grad.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.grad") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | src = torch.tensor([1., 2., 3., 4., 5., 6.]) 27 | result = src.grad 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_tanh_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.tanh_") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result.tanh_() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_distributed_is_available.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.distributed.is_available") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | result = torch.distributed.is_available() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_distributed_is_initialized.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.distributed.is_initialized") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result=torch.distributed.is_initialized() 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"]) 30 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_nn_functional_interpolate.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | a = torch.nn.functional.interpolate(torch.randn(1, 2, 20, 20), [24, 24]) 20 | print("#########################case2#########################") 21 | a = torch.nn.functional.interpolate(torch.rand(1, 2, 20, 20), scale_factor=0.6) 22 | -------------------------------------------------------------------------------- /tests/test_Tensor_conj.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.conj") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | src = torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j]) 27 | result = src.conj() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_cos_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.cos_") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | a = torch.tensor([ 1.4309, 1.2706, -0.8562, 0.9796]) 28 | a.cos_() 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["a"]) 32 | -------------------------------------------------------------------------------- /tests/test_Tensor_cosh_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.cosh_") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | a = torch.tensor([1.4309, 1.2706, -0.8562, 0.9796]) 28 | a.cosh_() 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["a"]) 32 | -------------------------------------------------------------------------------- /tests/test_Tensor_is_sparse.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | import textwrap 15 | 16 | from apibase import APIBase 17 | 18 | obj = APIBase("torch.Tensor.is_sparse") 19 | 20 | 21 | def test_case_1(): 22 | pytorch_code = textwrap.dedent( 23 | """ 24 | import torch 25 | a = torch.tensor([[ 0.9254, -0.6213]]) 26 | result = a.is_sparse 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"]) 30 | -------------------------------------------------------------------------------- /tests/test_Tensor_sigmoid.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.sigmoid") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result = a.sigmoid() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_sin_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.sin_") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | a = torch.tensor([1.4309, 1.2706, -0.8562, 0.9796]) 28 | a.sin_() 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["a"]) 32 | -------------------------------------------------------------------------------- /tests/test_Tensor_sinh_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.sinh_") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | a = torch.tensor([1.4309, 1.2706, -0.8562, 0.9796]) 28 | a.sinh_() 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["a"]) 32 | -------------------------------------------------------------------------------- /tests/test_backends_cudnn_is_available.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.backends.cudnn.is_available") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.backends.cudnn.is_available() 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"]) 30 | -------------------------------------------------------------------------------- /tests/test_Tensor_is_leaf.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.is_cuda") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | src = torch.tensor([1., 2., 3., 4., 5., 6.]) 27 | result = src.is_leaf 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_get_num_interop_threads.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.get_num_interop_threads") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.get_num_interop_threads() 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"], check_value=False) 30 | -------------------------------------------------------------------------------- /tests/test_Tensor_arccosh_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.arccosh_") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | a = torch.tensor([1.3192, 1.9915, 1.9674, 1.7151]) 28 | a.arccosh_() 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["a"]) 32 | -------------------------------------------------------------------------------- /tests/test_Tensor_arctan_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.arctan_") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | a = torch.tensor([ 0.2341, 0.2539, -0.6256, -0.6448]) 28 | a.arctan_() 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["a"]) 32 | -------------------------------------------------------------------------------- /tests/test_Tensor_arctanh_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.arctanh_") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | a = torch.tensor([-0.9385, 0.2968, -0.8591, -0.1871]) 28 | a.arctanh_() 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["a"]) 32 | -------------------------------------------------------------------------------- /tests/test_Tensor_atanh.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.atanh") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result = a.atanh() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"], check_value=False) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_tolist.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.tolist") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result =torch.tensor(a.tolist()) 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_distributed_is_nccl_available.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.distributed.is_nccl_available") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | result = torch.distributed.is_nccl_available() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_random_initial_seed.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.random.initial_seed") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | torch.manual_seed(100) 27 | result = torch.random.initial_seed() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_arctanh.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.arctanh") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result = a.arctanh() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"], check_value=False) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_names.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.names") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | result = torch.tensor([1, 0.5]).names 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"], unsupport=True, reason="Paddle has no this API") 31 | -------------------------------------------------------------------------------- /tests/test_backends_cudnn_version.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.backends.cudnn.version") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.backends.cudnn.version() == torch.backends.cudnn.version() 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"]) 30 | -------------------------------------------------------------------------------- /.flake8: -------------------------------------------------------------------------------- 1 | [flake8] 2 | select = C,E,F,W 3 | exclude = 4 | paconvert/example_code.py, 5 | paconvert/legacy.py 6 | paconvert/__init__.py 7 | paconvert/transformer/basic_transformer.py 8 | tests/code_library/* 9 | ignore = 10 | # Whitespace before ‘,’, ‘;’, or ‘:’, it is not compatible with black 11 | E203, 12 | # Module level import not at top of file 13 | E402, 14 | # Line too long (82 > 79 characters) 15 | E501, 16 | # Do not compare types, use `isinstance()` 17 | E721, 18 | # Do not use bare except, specify exception instead 19 | E722, 20 | # Do not assign a lambda expression, use a def 21 | E731, 22 | # Do not use variables named ‘l’, ‘O’, or ‘I’ 23 | E741, 24 | # `name` may be undefined, or defined from star imports: `module` 25 | F405, 26 | # Local variable name is assigned to but never used 27 | F841, 28 | # Line break before binary operator, it is not compatible with black 29 | W503 30 | per-file-ignores = 31 | # These files need special signals. 32 | paconvert/base.py: W605 33 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_Tensor_new_tensor.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | a = torch.tensor([1, 2, 3]) 20 | b = a.new_tensor([4, 5, 6], dtype=torch.float64, requires_grad=True) 21 | print("#########################case2#########################") 22 | a = torch.tensor([1, 2, 3], dtype=torch.int64) 23 | b = a.new_tensor([4, 5, 6]) 24 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_nn_BatchNorm1d.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch.nn as nn 17 | 18 | print("#########################case1#########################") 19 | nn.BatchNorm1d(3, eps=1e-3, momentum=0.01) 20 | print("#########################case2#########################") 21 | bn = nn.BatchNorm1d(27) 22 | print("#########################case3#########################") 23 | nn.BatchNorm1d(10, eps=1e-5, affine=False) 24 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_nn_BatchNorm2d.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | import torch.nn as nn 18 | 19 | print("#########################case1#########################") 20 | bn = torch.nn.BatchNorm2d(5) 21 | print("#########################case2#########################") 22 | bn = nn.BatchNorm2d(27) 23 | print("#########################case3#########################") 24 | nn.BatchNorm2d(10, eps=1e-5, affine=False) 25 | -------------------------------------------------------------------------------- /tests/test_Tensor_conj_physical.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.conj_physical") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | src = torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j]) 27 | result = src.conj_physical() 28 | """ 29 | ) 30 | obj.run(pytorch_code, ["result"]) 31 | -------------------------------------------------------------------------------- /tests/test_Tensor_apply_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.apply_") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | x = torch.tensor([1.3192, 1.9915, 1.9674, 1.7151]) 28 | result = x.apply_(lambda x: x*2) 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["x", "result"]) 32 | -------------------------------------------------------------------------------- /tests/code_library/code_case/custom_op/paddle_code/network.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | import GeometricKernelAttention 3 | from .function import custom_op_module 4 | 5 | 6 | class GeometryKernelAttention(paddle.nn.Module): 7 | 8 | def forward(self, query, key=None, value=None, identity=None, query_pos 9 | =None, key_padding_mask=None, reference_points=None, spatial_shapes 10 | =None, level_start_index=None, **kwargs): 11 | """C++ Custom OP, only convert Python part, C++ part not support to convert, you must write C++/CUDA file manually""" 12 | >>> output1 = 13 | GeometricKernelAttention.custom_op_xxx(value, spatial_shapes, 14 | level_start_index, sampling_locations, attention_weights, self. 15 | im2col_step) 16 | """C++ Custom OP, only convert Python part, C++ part not support to convert, you must write C++/CUDA file manually""" 17 | >>> output2 = 18 | custom_op_module(value, spatial_shapes, level_start_index, 19 | sampling_locations, attention_weights, self.im2col_step) 20 | return output1, output2 21 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/attribute_visit_name.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | import paddleformers 3 | 4 | 5 | class A(paddle.nn.Module): 6 | def __init__(self, data: paddle.Tensor): 7 | pass 8 | 9 | 10 | def func1(data: paddle.Tensor): 11 | >>>>>> torch.utils.data(x) 12 | >>>>>> torch.utils.data.data_loader(x) 13 | data["id"] 14 | func(data) 15 | data = json.load(f) 16 | self.data = self.__make_dataset() 17 | 18 | 19 | def func2(data) -> paddle.Tensor: 20 | pass 21 | 22 | 23 | def func3(dtype=paddle.float32): 24 | pass 25 | 26 | 27 | isinstance(x, paddle.Tensor) 28 | setattr(paddle.Tensor, "add", add_func) 29 | hasattr(paddle.Tensor, "add") 30 | Union[paddleformers.transformers.model_outputs.BaseModelOutput, paddle.LongTensor] 31 | Optional[paddle.Tensor] = None 32 | my_add = paddle.add 33 | setattr(paddle.nn, "functional", my_functional_module) 34 | >>>>>>transformers.activations.ACT2FN["tanh"] 35 | >>>>>>transformers.modeling_utils.ALL_ATTENTION_FUNCTIONS["flash_attention_2"] 36 | >>>>>>transformers.modeling_rope_utils.ROPE_INIT_FUNCTIONS["linear"] 37 | -------------------------------------------------------------------------------- /tests/test_Tensor_col_indices.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.col_indices") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | a = a = torch.tensor([[1, 2, 3], [1, 2, 3]]).to_sparse_csr() 28 | result = a.col_indices() 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["result"]) 32 | -------------------------------------------------------------------------------- /tests/test_Tensor_crow_indices.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.crow_indices") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | a = a = torch.tensor([[1, 2, 3], [1, 2, 3]]).to_sparse_csr() 28 | result = a.crow_indices() 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["result"]) 32 | -------------------------------------------------------------------------------- /tests/test_Tensor_retain_grad.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.retain_grad") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result.requires_grad = True 28 | result.retain_grad() 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["result"]) 32 | -------------------------------------------------------------------------------- /tests/test_Tensor_dtype.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.dtype") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | src = torch.tensor([1., 2., 3., 4., 5., 6.]) 27 | print(src.dtype) 28 | result = torch.tensor([1,2,3], dtype=src.dtype ) 29 | """ 30 | ) 31 | obj.run(pytorch_code, ["result"]) 32 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_randint.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | a = torch.randint(2, 5, [3, 4], device=torch.device("cuda")) 20 | print("#########################case2#########################") 21 | torch.randint(10, [2, 2]) 22 | print("#########################case3#########################") 23 | a, b = 2, 25 24 | a = torch.randint(a, b, [3, 4], device=torch.device("cuda")) 25 | -------------------------------------------------------------------------------- /tests/distributed/all_gather_object.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | gather_objects = ["foo", 12] 25 | output = [None for _ in gather_objects] 26 | dist.all_gather_object(output, gather_objects[dist.get_rank()]) 27 | 28 | if rank == 0: 29 | print(output) 30 | torch.save(output, os.environ["DUMP_FILE"]) 31 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/may_paddle_package_list.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import einops 4 | import paddle 5 | import setuptools 6 | from numpy.random import randint 7 | 8 | print("#########################case1#########################") 9 | paddle.distributed.get_world_size() 10 | print("#########################case2#########################") 11 | paddle.distributed.get_rank() 12 | print("#########################case3#########################") 13 | os.environ.get("RANK", 1) 14 | print("#########################case4#########################") 15 | rand_x = randint(10, size=(5,)) 16 | print("#########################case5#########################") 17 | setuptools.setup() 18 | print("#########################case6#########################") 19 | setuptools.setup() 20 | print("#########################case7#########################") 21 | paddle.tensor([1]) 22 | print("#########################case7#########################") 23 | einops.layers.paddle.Rearrange("b (h w) -> b h w", h=16, w=16) 24 | einops.layers.paddle.Rearrange("b (h w) -> b h w", h=16, w=16) 25 | einops.layers.paddle.Rearrange("b (h w) -> b h w", h=16, w=16) 26 | -------------------------------------------------------------------------------- /tests/test_Tensor_indices.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.indices") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | i = [[0, 1, 1], [2, 0, 2]] 27 | v = [3, 4, 5] 28 | x = torch.sparse_coo_tensor(i, v, (2, 3)).coalesce() 29 | result = x.indices() 30 | """ 31 | ) 32 | obj.run(pytorch_code, ["result"]) 33 | -------------------------------------------------------------------------------- /tests/test_Tensor_values.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.values") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | i = [[0, 1, 1], [2, 0, 2]] 27 | v = [3, 4, 5] 28 | x = torch.sparse_coo_tensor(i, v, (2, 3)).coalesce() 29 | result = x.values() 30 | """ 31 | ) 32 | obj.run(pytorch_code, ["result"]) 33 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_Tensor_new_empty.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | a = torch.tensor([1, 2, 3], dtype=torch.int64) 20 | b = a.new_empty([3, 4], dtype=torch.float64, requires_grad=True, pin_memory=True) 21 | print("#########################case2#########################") 22 | flag = False 23 | a = torch.tensor([1, 2, 3], dtype=torch.int64) 24 | b = a.new_empty((2, 3), requires_grad=flag) 25 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_Tensor_new_full.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | a = torch.tensor([1, 2, 3], dtype=torch.int64) 20 | b = a.new_full([3, 4], 2.43, dtype=torch.float64, requires_grad=True, pin_memory=True) 21 | print("#########################case2#########################") 22 | flag = False 23 | a = torch.tensor([1, 2, 3], dtype=torch.int64) 24 | b = a.new_full((2, 3), 4, requires_grad=flag) 25 | -------------------------------------------------------------------------------- /tests/distributed/recv.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | if rank == 0: 25 | data = torch.tensor([7, 8, 9]).cuda() 26 | dist.send(data, dst=1) 27 | else: 28 | data = torch.tensor([1, 2, 3]).cuda() 29 | dist.recv(data, src=0) 30 | 31 | if rank == 0: 32 | print(data) 33 | torch.save(data, os.environ["DUMP_FILE"]) 34 | -------------------------------------------------------------------------------- /tests/distributed/send.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | if rank == 0: 25 | data = torch.tensor([7, 8, 9]).cuda() 26 | dist.send(data, dst=1) 27 | else: 28 | data = torch.tensor([1, 2, 3]).cuda() 29 | dist.recv(data, src=0) 30 | 31 | if rank == 0: 32 | print(data) 33 | torch.save(data, os.environ["DUMP_FILE"]) 34 | -------------------------------------------------------------------------------- /tests/test_Tensor_deg2rad.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.deg2rad") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | x = torch.tensor([[1.0, 1.0, 1.0], 27 | [2.0, 2.0, 2.0], 28 | [3.0, 3.0, 3.0]]) 29 | result = x.deg2rad() 30 | """ 31 | ) 32 | obj.run(pytorch_code, ["result"]) 33 | -------------------------------------------------------------------------------- /tests/test_hub_get_dir.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.hub.get_dir") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | result = torch.hub.get_dir() 28 | """ 29 | ) 30 | obj.run( 31 | pytorch_code, 32 | ["result"], 33 | unsupport=True, 34 | reason="paddle does not support this function temporarily", 35 | ) 36 | -------------------------------------------------------------------------------- /tests/test_onnx_enable_log.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.onnx.enable_log") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.onnx.enable_log() 27 | """ 28 | ) 29 | obj.run( 30 | pytorch_code, 31 | ["result"], 32 | unsupport=True, 33 | reason="paddle does not support this function temporarily", 34 | ) 35 | -------------------------------------------------------------------------------- /tests/distributed/reduce_scatter_tensor2.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | input = torch.arange(4, dtype=torch.int64).cuda() 25 | output = torch.zeros(2, dtype=torch.int64).cuda() 26 | dist.reduce_scatter_tensor(output, input, op=dist.ReduceOp.SUM) 27 | 28 | if rank == 0: 29 | print(output) 30 | torch.save(output, os.environ["DUMP_FILE"]) 31 | -------------------------------------------------------------------------------- /tests/test_onnx_disable_log.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.onnx.disable_log") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.onnx.disable_log() 27 | """ 28 | ) 29 | obj.run( 30 | pytorch_code, 31 | ["result"], 32 | unsupport=True, 33 | reason="paddle does not support this function temporarily", 34 | ) 35 | -------------------------------------------------------------------------------- /tests/test_Tensor_is_pinned.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.Tensor.is_pinned") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | if torch.cuda.is_available(): 28 | x = torch.randn(4,4).pin_memory() 29 | else: 30 | x = torch.randn(4,4) 31 | result = x.is_pinned() 32 | """ 33 | ) 34 | obj.run(pytorch_code, ["result"]) 35 | -------------------------------------------------------------------------------- /tests/test_hub_set_dir.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.hub.set_dir") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | result = torch.hub.set_dir('/tmp/temporary_dir') 28 | """ 29 | ) 30 | obj.run( 31 | pytorch_code, 32 | ["result"], 33 | unsupport=True, 34 | reason="paddle does not support this function temporarily", 35 | ) 36 | -------------------------------------------------------------------------------- /tests/distributed/all_reduce.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | if rank == 0: 25 | data = torch.tensor([[4, 5, 6], [4, 5, 6]]).cuda() 26 | else: 27 | data = torch.tensor([[1, 2, 3], [1, 2, 3]]).cuda() 28 | dist.all_reduce(data) 29 | # [[5, 7, 9], [5, 7, 9]] (2 GPUs) 30 | if rank == 0: 31 | print(data) 32 | torch.save(data, os.environ["DUMP_FILE"]) 33 | -------------------------------------------------------------------------------- /tests/distributed/irecv.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | if rank == 0: 25 | data = torch.tensor([7, 8, 9]).cuda() 26 | task = dist.isend(data, dst=1) 27 | else: 28 | data = torch.tensor([1, 2, 3]).cuda() 29 | task = dist.irecv(data, src=0) 30 | task.wait() 31 | 32 | if rank == 0: 33 | print(data) 34 | torch.save(data, os.environ["DUMP_FILE"]) 35 | -------------------------------------------------------------------------------- /tests/distributed/isend.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | if rank == 0: 25 | data = torch.tensor([7, 8, 9]).cuda() 26 | task = dist.isend(data, dst=1) 27 | else: 28 | data = torch.tensor([1, 2, 3]).cuda() 29 | task = dist.irecv(data, src=0) 30 | task.wait() 31 | 32 | if rank == 0: 33 | print(data) 34 | torch.save(data, os.environ["DUMP_FILE"]) 35 | -------------------------------------------------------------------------------- /tests/distributed/reduce_scatter_tensor.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | input = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32).cuda() 25 | output = torch.empty(1, 2, dtype=torch.float32).cuda() 26 | dist.reduce_scatter_tensor(output, input, op=dist.ReduceOp.SUM) 27 | 28 | if rank == 0: 29 | print(output) 30 | torch.save(output, os.environ["DUMP_FILE"]) 31 | -------------------------------------------------------------------------------- /tests/test_layout.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.layout") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.tensor([[4, 9], [23, 2]]) 27 | result = torch.layout(a) 28 | """ 29 | ) 30 | obj.run( 31 | pytorch_code, 32 | ["result"], 33 | unsupport=True, 34 | reason="paddle does not support this function temporarily", 35 | ) 36 | -------------------------------------------------------------------------------- /tests/test_get_deterministic_debug_mode.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.get_deterministic_debug_mode") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.get_deterministic_debug_mode() 27 | """ 28 | ) 29 | obj.run( 30 | pytorch_code, 31 | ["result"], 32 | unsupport=True, 33 | reason="paddle does not support this function temporarily", 34 | ) 35 | -------------------------------------------------------------------------------- /tests/test_nn_Module_xpu.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.nn.Module.xpu") 20 | 21 | 22 | def _test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | x = torch.tensor([1., 2., 3.]) 27 | module1 = torch.nn.Module() 28 | module1.register_buffer('buffer', x) 29 | module1.xpu() 30 | result = None 31 | """ 32 | ) 33 | obj.run( 34 | pytorch_code, 35 | ["result"], 36 | ) 37 | -------------------------------------------------------------------------------- /tests/test_profiler_ProfilerAction.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.profiler.ProfilerAction") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | result = torch.profiler.ProfilerAction.WARMUP 28 | """ 29 | ) 30 | obj.run( 31 | pytorch_code, 32 | ["result"], 33 | unsupport=True, 34 | reason="paddle does not support this function temporarily", 35 | ) 36 | -------------------------------------------------------------------------------- /tests/test_profiler_ProfilerActivity.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.profiler.ProfilerActivity") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | result = torch.profiler.ProfilerActivity.CUDA 28 | """ 29 | ) 30 | obj.run( 31 | pytorch_code, 32 | ["result"], 33 | unsupport=True, 34 | reason="paddle does not support this function temporarily", 35 | ) 36 | -------------------------------------------------------------------------------- /tests/test_Tensor_is_coalesced.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.is_coalesced") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | i = torch.tensor([[0, 1, 1], 27 | [2, 0, 2]]) 28 | v = torch.tensor([3, 4, 5]) 29 | x = torch.sparse_coo_tensor(i, v, [2, 4]) 30 | result = x.is_coalesced() 31 | """ 32 | ) 33 | obj.run(pytorch_code, ["result"]) 34 | -------------------------------------------------------------------------------- /tests/test_Tensor_matrix_exp.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.matrix_exp") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | x = torch.tensor([[0.02773777, 0.93004224, 0.06911496], 27 | [0.24831591, 0.45733623, 0.07717843], 28 | [0.48016702, 0.14235102, 0.42620817]]) 29 | result = x.matrix_exp() 30 | """ 31 | ) 32 | obj.run(pytorch_code, ["result"]) 33 | -------------------------------------------------------------------------------- /tests/test_distributed_rpc_WorkerInfo.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.distributed.rpc.WorkerInfo") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.distributed.rpc.WorkerInfo("test1", 1) 27 | """ 28 | ) 29 | obj.run( 30 | pytorch_code, 31 | ["result"], 32 | unsupport=True, 33 | reason="paddle does not support this function temporarily", 34 | ) 35 | -------------------------------------------------------------------------------- /tests/distributed/all_to_all_single.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | 25 | input = torch.tensor( 26 | [[rank * 4, rank * 4 + 1], [rank * 4 + 2, rank * 4 + 3]], device=f"cuda:{rank}" 27 | ) 28 | 29 | output = torch.empty_like(input) 30 | 31 | 32 | dist.all_to_all_single(output, input) 33 | 34 | if rank == 0: 35 | print(output) 36 | torch.save(output, os.environ["DUMP_FILE"]) 37 | -------------------------------------------------------------------------------- /tests/test_Tensor_is_conj.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.is_conj") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | src = torch.tensor([1., 2., 3., 4., 5., 6.]) 27 | result = src.is_conj() 28 | """ 29 | ) 30 | obj.run( 31 | pytorch_code, 32 | ["result"], 33 | unsupport=True, 34 | reason="paddle does not support this function temporarily", 35 | ) 36 | -------------------------------------------------------------------------------- /tests/test_cuda_empty_cache.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | import paddle 18 | import pytest 19 | from apibase import APIBase 20 | 21 | obj = APIBase("torch.cuda.empty_cache") 22 | 23 | 24 | @pytest.mark.skipif( 25 | condition=not paddle.device.is_compiled_with_cuda(), 26 | reason="can only run on paddle with CUDA", 27 | ) 28 | def test_case_1(): 29 | pytorch_code = textwrap.dedent( 30 | """ 31 | import torch 32 | result =torch.cuda.empty_cache() 33 | """ 34 | ) 35 | obj.run(pytorch_code, ["result"]) 36 | -------------------------------------------------------------------------------- /tests/test_cuda_ipc_collect.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | import paddle 18 | import pytest 19 | from apibase import APIBase 20 | 21 | obj = APIBase("torch.cuda.ipc_collect") 22 | 23 | 24 | @pytest.mark.skipif( 25 | condition=not paddle.device.is_compiled_with_cuda(), 26 | reason="can only run on paddle with CUDA", 27 | ) 28 | def test_case_1(): 29 | pytorch_code = textwrap.dedent( 30 | """ 31 | import torch 32 | result = torch.cuda.ipc_collect() 33 | """ 34 | ) 35 | obj.run(pytorch_code, ["result"]) 36 | -------------------------------------------------------------------------------- /tests/test_nn_Module_cpu.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.nn.Module.cpu") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | x = torch.tensor([1., 2., 3.]) 27 | module1 = torch.nn.Module() 28 | module1.register_buffer('buffer', x) 29 | module1.cpu() 30 | result = module1.buffer 31 | """ 32 | ) 33 | obj.run( 34 | pytorch_code, 35 | ["result"], 36 | ) 37 | -------------------------------------------------------------------------------- /tests/test_nn_Module_half.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.nn.Module.half") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | x = torch.tensor([1., 2., 3.]) 27 | module1 = torch.nn.Module() 28 | module1.register_buffer('buffer', x) 29 | module1.half() 30 | result = module1.buffer 31 | """ 32 | ) 33 | obj.run( 34 | pytorch_code, 35 | ["result"], 36 | ) 37 | -------------------------------------------------------------------------------- /tests/distributed/batch_isend_irecv.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | import torch.distributed as dist 18 | from torch.distributed import P2POp, batch_isend_irecv 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | tensor = torch.zeros(10).cuda() 25 | 26 | if rank == 0: 27 | op_list = [P2POp(dist.isend, tensor, 1)] 28 | elif rank == 1: 29 | op_list = [P2POp(dist.irecv, tensor, 0)] 30 | 31 | work_list = batch_isend_irecv(op_list) 32 | 33 | for work in work_list: 34 | work.wait() 35 | -------------------------------------------------------------------------------- /tests/test_Tensor_dense_dim.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.dense_dim") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | i = torch.tensor([[0, 1, 1], 27 | [2, 0, 2]]) 28 | v = torch.tensor([3, 4, 5], dtype=torch.float32) 29 | x = torch.sparse_coo_tensor(i, v, [2, 4]) 30 | result = x.dense_dim() 31 | """ 32 | ) 33 | obj.run(pytorch_code, ["result"]) 34 | -------------------------------------------------------------------------------- /tests/test_Tensor_retains_grad.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.retains_grad") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | y = torch.Tensor([[1.,2.], [3.,4.]]) 27 | result = y.retains_grad() 28 | """ 29 | ) 30 | obj.run( 31 | pytorch_code, 32 | ["result"], 33 | unsupport=True, 34 | reason="paddle does not support this function temporarily", 35 | ) 36 | -------------------------------------------------------------------------------- /tests/test_Tensor_sparse_resize_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.sparse_resize_") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.arange(4.) 27 | result = a.sparse_resize_((1, 1), 1, 1) 28 | """ 29 | ) 30 | obj.run( 31 | pytorch_code, 32 | ["result"], 33 | unsupport=True, 34 | reason="paddle does not support this function temporarily", 35 | ) 36 | -------------------------------------------------------------------------------- /tests/test_Tensor_to_dense.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.to_dense") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | i = torch.tensor([[0, 1, 1], 27 | [2, 0, 2]]) 28 | v = torch.tensor([3, 4, 5], dtype=torch.float32) 29 | x = torch.sparse_coo_tensor(i, v, [2, 4]) 30 | result = x.to_dense() 31 | """ 32 | ) 33 | obj.run(pytorch_code, ["result"]) 34 | -------------------------------------------------------------------------------- /tests/test_cuda_device_count.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | import paddle 18 | import pytest 19 | from apibase import APIBase 20 | 21 | obj = APIBase("torch.cuda.device_count") 22 | 23 | 24 | @pytest.mark.skipif( 25 | condition=not paddle.device.is_compiled_with_cuda(), 26 | reason="can only run on paddle with CUDA", 27 | ) 28 | def test_case_1(): 29 | pytorch_code = textwrap.dedent( 30 | """ 31 | import torch 32 | result = torch.cuda.device_count() 33 | """ 34 | ) 35 | obj.run(pytorch_code, ["result"]) 36 | -------------------------------------------------------------------------------- /tests/test_nn_Module_float.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.nn.Module.float") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | x = torch.tensor([1., 2., 3.]) 27 | module1 = torch.nn.Module() 28 | module1.register_buffer('buffer', x) 29 | module1.float() 30 | result = module1.buffer 31 | """ 32 | ) 33 | obj.run( 34 | pytorch_code, 35 | ["result"], 36 | ) 37 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_Tensor_new_ones.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | a = torch.tensor([1, 2, 3], dtype=torch.int64) 20 | b = a.new_ones([3, 4], dtype=torch.float64, requires_grad=True, pin_memory=True) 21 | print("#########################case2#########################") 22 | a = torch.tensor([1, 2, 3], dtype=torch.int64) 23 | b = a.new_ones(3, 4, requires_grad=True) 24 | print("#########################case3#########################") 25 | b = a.new_ones([3, 4]) 26 | -------------------------------------------------------------------------------- /tests/distributed/broadcast_object_list.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | if rank == 0: 25 | object_list = [{"foo": [1, 2, 3]}] 26 | else: 27 | object_list = [{"bar": [4, 5, 6]}] 28 | dist.broadcast_object_list(object_list, src=1) 29 | 30 | assert object_list == [{"bar": [4, 5, 6]}] 31 | 32 | if rank == 0: 33 | print(object_list) 34 | torch.save(object_list, os.environ["DUMP_FILE"]) 35 | -------------------------------------------------------------------------------- /tests/test_Tensor_sparse_dim.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.sparse_dim") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | i = torch.tensor([[0, 1, 1], 27 | [2, 0, 2]]) 28 | v = torch.tensor([3, 4, 5], dtype=torch.float32) 29 | x = torch.sparse_coo_tensor(i, v, [2, 4]) 30 | result = x.sparse_dim() 31 | """ 32 | ) 33 | obj.run(pytorch_code, ["result"]) 34 | -------------------------------------------------------------------------------- /tests/test_nn_Module_double.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.nn.Module.double") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | x = torch.tensor([1., 2., 3.]) 27 | module1 = torch.nn.Module() 28 | module1.register_buffer('buffer', x) 29 | module1.double() 30 | result = module1.buffer 31 | """ 32 | ) 33 | obj.run( 34 | pytorch_code, 35 | ["result"], 36 | ) 37 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/api_torch_Tensor_new_zeros.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import torch 17 | 18 | print("#########################case1#########################") 19 | a = torch.tensor([1, 2, 3], dtype=torch.int64) 20 | b = a.new_zeros([3, 4], dtype=torch.float64, requires_grad=True, pin_memory=True) 21 | print("#########################case2#########################") 22 | a = torch.tensor([1, 2, 3], dtype=torch.int64) 23 | b = a.new_zeros(3, 4, requires_grad=True) 24 | print("#########################case3#########################") 25 | b = a.new_zeros([3, 4]) 26 | -------------------------------------------------------------------------------- /tests/test_nn_Module_modules.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.nn.Module.modules") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | import torch.nn as nn 27 | l = nn.Linear(2, 2, bias=False) 28 | net = nn.Sequential(l, l) 29 | result = torch.Tensor([0,0]) 30 | for i,j in enumerate(net.modules()): 31 | result = j(result) 32 | """ 33 | ) 34 | obj.run(pytorch_code, ["result"]) 35 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/api_paddle_nn_InstanceNorm3D.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | print("#########################case1#########################") 4 | m = paddle.nn.InstanceNorm3D(num_features=100) 5 | input = paddle.randn(20, 100, 35, 45, 10) 6 | output = m(input) 7 | print("#########################case2#########################") 8 | m = paddle.nn.InstanceNorm3D(num_features=100, weight_attr=True, bias_attr=True) 9 | input = paddle.randn(20, 100, 35, 45, 10) 10 | output = m(input) 11 | print("#########################case3#########################") 12 | m = paddle.nn.InstanceNorm3D(num_features=100, weight_attr=False, bias_attr=False) 13 | input = paddle.randn(20, 100, 35, 45, 10) 14 | output = m(input) 15 | print("#########################case4#########################") 16 | m = paddle.nn.InstanceNorm3D( 17 | num_features=100, weight_attr=True, bias_attr=True, momentum=1 - 0.1 18 | ) 19 | input = paddle.randn(20, 100, 35, 45, 10) 20 | output = m(input) 21 | print("#########################case5#########################") 22 | m = paddle.nn.InstanceNorm3D( 23 | num_features=100, weight_attr=False, bias_attr=False, momentum=1 - 0.1 24 | ) 25 | input = paddle.randn(20, 100, 35, 45, 10) 26 | output = m(input) 27 | -------------------------------------------------------------------------------- /tests/distributed/all_gather.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | tensor_list = [torch.zeros(2, dtype=torch.int64).cuda() for _ in range(2)] 25 | data = torch.arange(2, dtype=torch.int64).cuda() + 1 + 2 * rank 26 | dist.all_gather(tensor_list, data) 27 | # [[[4, 5, 6], [4, 5, 6]], [[1, 2, 3], [1, 2, 3]]] (2 GPUs) 28 | if rank == 0: 29 | print(tensor_list) 30 | torch.save(tensor_list, os.environ["DUMP_FILE"]) 31 | -------------------------------------------------------------------------------- /tests/test_cuda_initial_seed.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | import paddle 18 | import pytest 19 | from apibase import APIBase 20 | 21 | obj = APIBase("torch.cuda.initial_seed") 22 | 23 | 24 | @pytest.mark.skipif( 25 | condition=not paddle.device.is_compiled_with_cuda(), 26 | reason="can only run on paddle with CUDA", 27 | ) 28 | def test_case_1(): 29 | pytorch_code = textwrap.dedent( 30 | """ 31 | import torch 32 | result = torch.cuda.initial_seed() 33 | """ 34 | ) 35 | obj.run(pytorch_code, ["result"], check_value=False) 36 | -------------------------------------------------------------------------------- /tests/test_nn_Module_children.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.nn.Module.children") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | import torch.nn as nn 27 | l = nn.Linear(2, 2, bias=False) 28 | net = nn.Sequential(l, l) 29 | result = torch.Tensor([0,0]) 30 | for i,j in enumerate(net.children()): 31 | result = j(result) 32 | """ 33 | ) 34 | obj.run(pytorch_code, ["result"]) 35 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/type_hinting.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from typing import Optional, Tuple, Union, List 3 | 4 | print('#########################case1#########################') 5 | Union[Tuple,torch.BoolTensor] 6 | print('#########################case2#########################') 7 | torch.BoolTensor([True,False]) 8 | print('#########################case3#########################') 9 | def fun(b: torch.BoolTensor): 10 | pass 11 | print('#########################case4#########################') 12 | torch.set_default_tensor_type(torch.cuda.HalfTensor) 13 | print('#########################case5#########################') 14 | def forward( 15 | hidden_states: Optional[Tuple[torch.FloatTensor]], 16 | rotary_pos_emb_list: Optional[List[List[torch.Tensor]]] = None, 17 | layer_past: Optional[Tuple[torch.Tensor]] = None, 18 | attention_mask: Optional[torch.FloatTensor] = None, 19 | head_mask: Optional[torch.FloatTensor] = None, 20 | encoder_hidden_states: Optional[torch.Tensor] = None, 21 | encoder_attention_mask: Optional[torch.FloatTensor] = None, 22 | output_attentions: Optional[bool] = False, 23 | use_cache: Optional[bool] = False, 24 | ): 25 | pass 26 | -------------------------------------------------------------------------------- /tests/test_Tensor_sparse_resize_and_clear_.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.sparse_resize_and_clear_") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | a = torch.arange(4.) 27 | result = a.sparse_resize_and_clear_((1, 1), 1, 1) 28 | """ 29 | ) 30 | obj.run( 31 | pytorch_code, 32 | ["result"], 33 | unsupport=True, 34 | reason="paddle does not support this function temporarily", 35 | ) 36 | -------------------------------------------------------------------------------- /tests/code_library/code_case/torch_code/may_torch_package_list.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from os import environ 3 | from numpy.random import randint 4 | from setuptools import setup 5 | import setuptools 6 | import einops 7 | import einops.layers.torch 8 | from einops.layers.torch import Rearrange 9 | import einops.layers.torch as einops_torch 10 | 11 | print("#########################case1#########################") 12 | environ.get("WORLD_SIZE",1) 13 | print("#########################case2#########################") 14 | os.environ.get('LOCAL_RANK',1) 15 | print("#########################case3#########################") 16 | os.environ.get('RANK',1) 17 | print("#########################case4#########################") 18 | rand_x=randint(10,size=(5,)) 19 | print('#########################case5#########################') 20 | setup() 21 | print('#########################case6#########################') 22 | setuptools.setup() 23 | print('#########################case7#########################') 24 | torch.tensor([1]) 25 | print('#########################case7#########################') 26 | Rearrange('b (h w) -> b h w', h=16, w=16) 27 | einops.layers.torch.Rearrange('b (h w) -> b h w', h=16, w=16) 28 | einops_torch.Rearrange('b (h w) -> b h w', h=16, w=16) 29 | -------------------------------------------------------------------------------- /tests/test_Tensor_pin_memory.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | import paddle 18 | import pytest 19 | from apibase import APIBase 20 | 21 | obj = APIBase("torch.Tensor.pin_memory") 22 | 23 | 24 | @pytest.mark.skipif( 25 | condition=not paddle.device.is_compiled_with_cuda(), 26 | reason="can only run on paddle with CUDA", 27 | ) 28 | def test_case_1(): 29 | pytorch_code = textwrap.dedent( 30 | """ 31 | import torch 32 | a = torch.Tensor([[1.,2.], [3.,4.]]) 33 | result = a.pin_memory() 34 | """ 35 | ) 36 | obj.run(pytorch_code, ["result"]) 37 | -------------------------------------------------------------------------------- /tests/test_to_dense.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.to_dense") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | i = torch.tensor([[0, 1, 1], 27 | [2, 0, 2]]) 28 | v = torch.tensor([3, 4, 5], dtype=torch.float32) 29 | result = torch.sparse_coo_tensor(i, v, [2, 4]) 30 | v = result.coalesce() 31 | result = result.to_dense() 32 | """ 33 | ) 34 | obj.run(pytorch_code, ["result"]) 35 | -------------------------------------------------------------------------------- /tests/code_library/code_case/paddle_code/type_hinting.py: -------------------------------------------------------------------------------- 1 | from typing import List, Optional, Tuple, Union 2 | 3 | import paddle 4 | 5 | print("#########################case1#########################") 6 | Union[Tuple, paddle.BoolTensor] 7 | print("#########################case2#########################") 8 | paddle.BoolTensor([True, False]) 9 | print("#########################case3#########################") 10 | 11 | 12 | def fun(b: paddle.BoolTensor): 13 | pass 14 | 15 | 16 | print("#########################case4#########################") 17 | paddle.set_default_dtype(d=paddle.cuda.HalfTensor) 18 | print("#########################case5#########################") 19 | 20 | 21 | def forward( 22 | hidden_states: Optional[Tuple[paddle.FloatTensor]], 23 | rotary_pos_emb_list: Optional[List[List[paddle.Tensor]]] = None, 24 | layer_past: Optional[Tuple[paddle.Tensor]] = None, 25 | attention_mask: Optional[paddle.FloatTensor] = None, 26 | head_mask: Optional[paddle.FloatTensor] = None, 27 | encoder_hidden_states: Optional[paddle.Tensor] = None, 28 | encoder_attention_mask: Optional[paddle.FloatTensor] = None, 29 | output_attentions: Optional[bool] = False, 30 | use_cache: Optional[bool] = False, 31 | ): 32 | pass 33 | -------------------------------------------------------------------------------- /tests/test_Tensor_coalesce.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.Tensor.coalesce") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | i = torch.tensor([[0, 1, 1], 27 | [2, 0, 2]]) 28 | v = torch.tensor([3, 4, 5], dtype=torch.float32) 29 | x = torch.sparse_coo_tensor(i, v, [2, 4]) 30 | result = x.coalesce() 31 | result = result.to_dense() 32 | """ 33 | ) 34 | obj.run(pytorch_code, ["result"]) 35 | -------------------------------------------------------------------------------- /tests/test_autograd_profiler_profile_self_cpu_time_total.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import textwrap 17 | 18 | from apibase import APIBase 19 | 20 | obj = APIBase("torch.autograd.profiler.profile.self_cpu_time_total") 21 | 22 | 23 | def test_case_1(): 24 | pytorch_code = textwrap.dedent( 25 | """ 26 | import torch 27 | result = torch.autograd.profiler.profile.self_cpu_time_total 28 | """ 29 | ) 30 | obj.run( 31 | pytorch_code, 32 | ["result"], 33 | unsupport=True, 34 | reason="paddle does not support this function temporarily", 35 | ) 36 | -------------------------------------------------------------------------------- /tests/code_library/model_case/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | import os 17 | 18 | MODEL_LIST = [] 19 | 20 | def add_model(torch_file): 21 | dir_name = os.path.dirname(__file__) 22 | torch_file = os.path.join(dir_name, "torch_code/", torch_file) 23 | global MODEL_LIST 24 | MODEL_LIST.append(torch_file) 25 | 26 | 27 | # this part is about model file list 28 | add_model("exclude_convert.py") 29 | # add_model("model_torch_mobilenet.py") 30 | add_model("model_torch_resnet.py") 31 | # add_model("model_torch_vggnet.py") 32 | # add_model("model_torch_xception.py") 33 | add_model("model_torch_lenet.py") 34 | -------------------------------------------------------------------------------- /tests/distributed/reduce.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | if rank == 0: 25 | data = torch.tensor([[4, 5, 6], [4, 5, 6]]).cuda() 26 | else: 27 | data = torch.tensor([[1, 2, 3], [1, 2, 3]]).cuda() 28 | dist.reduce(data, dst=0) 29 | 30 | # [[5, 7, 9], [5, 7, 9]] (2 GPUs, out for rank 0) 31 | # [[1, 2, 3], [1, 2, 3]] (2 GPUs, out for rank 1) 32 | 33 | if rank == 0: 34 | print(data) 35 | torch.save(data, os.environ["DUMP_FILE"]) 36 | -------------------------------------------------------------------------------- /tests/test_set_num_threads.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import textwrap 16 | 17 | from apibase import APIBase 18 | 19 | obj = APIBase("torch.set_num_threads") 20 | 21 | 22 | def test_case_1(): 23 | pytorch_code = textwrap.dedent( 24 | """ 25 | import torch 26 | result = torch.set_num_threads(1) 27 | """ 28 | ) 29 | obj.run(pytorch_code, ["result"]) 30 | 31 | 32 | def test_case_2(): 33 | pytorch_code = textwrap.dedent( 34 | """ 35 | import torch 36 | result = torch.set_num_threads(2) 37 | """ 38 | ) 39 | obj.run(pytorch_code, ["result"]) 40 | -------------------------------------------------------------------------------- /tests/distributed/scatter_object_list.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import os 16 | 17 | import torch 18 | import torch.distributed as dist 19 | 20 | dist.init_process_group(backend="nccl") 21 | rank = dist.get_rank() 22 | torch.cuda.set_device(rank) 23 | 24 | out_object_list = [None] 25 | if rank == 0: 26 | in_object_list = [{"foo": [1, 2, 3]}, {"foo": [4, 5, 6]}] 27 | else: 28 | in_object_list = [{"bar": [1, 2, 3]}, {"bar": [4, 5, 6]}] 29 | dist.scatter_object_list(out_object_list, in_object_list, src=1, group=None) 30 | 31 | if rank == 0: 32 | print(out_object_list) 33 | torch.save(out_object_list, os.environ["DUMP_FILE"]) 34 | --------------------------------------------------------------------------------