From 824e57b756da182ed1401a931aa768ad2072e0e0 Mon Sep 17 00:00:00 2001 From: Wenfei_Qi <1419344875@qq.com> Date: Mon, 4 May 2026 12:48:45 +0800 Subject: [PATCH 1/2] align torch.cuda.OutOfMemoryError --- paconvert/api_mapping.json | 6 ++++ tests/test_cuda_CudaError.py | 48 +++++++++++++++++++++++++++++ tests/test_cuda_OutOfMemoryError.py | 48 +++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 tests/test_cuda_CudaError.py create mode 100644 tests/test_cuda_OutOfMemoryError.py diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 2b4e144f8..e408a02a9 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -3762,6 +3762,9 @@ "torch.cuda.CharTensor": { "Matcher": "ChangePrefixMatcher" }, + "torch.cuda.CudaError": { + "Matcher": "ChangePrefixMatcher" + }, "torch.cuda.DoubleTensor": { "Matcher": "ChangePrefixMatcher" }, @@ -3780,6 +3783,9 @@ "torch.cuda.LongTensor": { "Matcher": "ChangePrefixMatcher" }, + "torch.cuda.OutOfMemoryError": { + "Matcher": "ChangePrefixMatcher" + }, "torch.cuda.ShortTensor": { "Matcher": "ChangePrefixMatcher" }, diff --git a/tests/test_cuda_CudaError.py b/tests/test_cuda_CudaError.py new file mode 100644 index 000000000..bda745090 --- /dev/null +++ b/tests/test_cuda_CudaError.py @@ -0,0 +1,48 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import textwrap + +from apibase import APIBase + +obj = APIBase("torch.cuda.CudaError") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + try: + torch.cuda.check_error(1) + except torch.cuda.CudaError as e: + result = str(e) + """ + ) + obj.run( + pytorch_code, + expect_paddle_code="import paddle\n\ntry:\n paddle.cuda.check_error(1)\nexcept paddle.cuda.CudaError as e:\n result = str(e)\n", + ) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = issubclass(torch.cuda.CudaError, RuntimeError) + """ + ) + obj.run( + pytorch_code, + expect_paddle_code="import paddle\n\nresult = issubclass(paddle.cuda.CudaError, RuntimeError)\n", + ) diff --git a/tests/test_cuda_OutOfMemoryError.py b/tests/test_cuda_OutOfMemoryError.py new file mode 100644 index 000000000..5d03e7856 --- /dev/null +++ b/tests/test_cuda_OutOfMemoryError.py @@ -0,0 +1,48 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import textwrap + +from apibase import APIBase + +obj = APIBase("torch.cuda.OutOfMemoryError") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + try: + torch.cuda.check_error(2) + except torch.cuda.OutOfMemoryError as e: + result = str(e) + """ + ) + obj.run( + pytorch_code, + expect_paddle_code="import paddle\n\ntry:\n paddle.cuda.check_error(2)\nexcept paddle.cuda.OutOfMemoryError as e:\n result = str(e)\n", + ) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = issubclass(torch.cuda.OutOfMemoryError, RuntimeError) + """ + ) + obj.run( + pytorch_code, + expect_paddle_code="import paddle\n\nresult = issubclass(paddle.cuda.OutOfMemoryError, RuntimeError)\n", + ) From ef8504218b55cc20b8871339aaa92517a418a0c5 Mon Sep 17 00:00:00 2001 From: Wenfei_Qi <1419344875@qq.com> Date: Fri, 8 May 2026 17:17:37 +0800 Subject: [PATCH 2/2] update tests --- tests/test_cuda_CudaError.py | 4 ++-- tests/test_cuda_OutOfMemoryError.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_cuda_CudaError.py b/tests/test_cuda_CudaError.py index bda745090..623b76fe8 100644 --- a/tests/test_cuda_CudaError.py +++ b/tests/test_cuda_CudaError.py @@ -24,14 +24,14 @@ def test_case_1(): """ import torch try: - torch.cuda.check_error(1) + raise torch.cuda.CudaError("test") except torch.cuda.CudaError as e: result = str(e) """ ) obj.run( pytorch_code, - expect_paddle_code="import paddle\n\ntry:\n paddle.cuda.check_error(1)\nexcept paddle.cuda.CudaError as e:\n result = str(e)\n", + expect_paddle_code="import paddle\n\ntry:\n raise paddle.cuda.CudaError(\"test\")\nexcept paddle.cuda.CudaError as e:\n result = str(e)\n", ) diff --git a/tests/test_cuda_OutOfMemoryError.py b/tests/test_cuda_OutOfMemoryError.py index 5d03e7856..91de1f0cd 100644 --- a/tests/test_cuda_OutOfMemoryError.py +++ b/tests/test_cuda_OutOfMemoryError.py @@ -24,14 +24,14 @@ def test_case_1(): """ import torch try: - torch.cuda.check_error(2) + raise torch.cuda.OutOfMemoryError("test") except torch.cuda.OutOfMemoryError as e: result = str(e) """ ) obj.run( pytorch_code, - expect_paddle_code="import paddle\n\ntry:\n paddle.cuda.check_error(2)\nexcept paddle.cuda.OutOfMemoryError as e:\n result = str(e)\n", + expect_paddle_code="import paddle\n\ntry:\n raise paddle.cuda.OutOfMemoryError(\"test\")\nexcept paddle.cuda.OutOfMemoryError as e:\n result = str(e)\n", )