From 4e64f6f9c7e0dbbd08e81d8f69b6aefb95d1c23b Mon Sep 17 00:00:00 2001 From: nyx-c-language Date: Sun, 24 Aug 2025 21:36:42 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E6=A8=A1=E5=9D=97=20CUDA?= =?UTF-8?q?Platform=E3=80=81CPUPlatform=20=E5=8D=95=E6=B5=8B=E8=A1=A5?= =?UTF-8?q?=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/platforms/test_platforms.py | 86 +++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 tests/platforms/test_platforms.py diff --git a/tests/platforms/test_platforms.py b/tests/platforms/test_platforms.py new file mode 100644 index 00000000000..1d450b9f92b --- /dev/null +++ b/tests/platforms/test_platforms.py @@ -0,0 +1,86 @@ +""" +# Copyright (c) 2025 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 unittest +from unittest.mock import patch + +from fastdeploy.platforms.base import _Backend +from fastdeploy.platforms.cpu import CPUPlatform +from fastdeploy.platforms.cuda import CUDAPlatform + + +class TestCPUPlatform(unittest.TestCase): + def setUp(self): + self.platform = CPUPlatform() + + @patch("paddle.device.get_device", return_value="cpu") + def test_is_cpu_and_available(self, mock_get_device): + """ + Check hardware type (CPU) and availability + """ + self.assertTrue(self.platform.is_cpu()) + self.assertTrue(self.platform.available()) + + def test_attention_backend(self): + """CPUPlatform attention_backend should return empty string""" + self.assertEqual(self.platform.get_attention_backend_cls(None), "") + + +class TestCUDAPlatform(unittest.TestCase): + def setUp(self): + self.platform = CUDAPlatform() + + @patch("paddle.is_compiled_with_cuda", return_value=True) + @patch("paddle.device.get_device", return_value="cuda") + @patch("paddle.static.cuda_places", return_value=[0]) + def test_is_cuda(self, mock_get_device, mock_is_cuda, mock_cuda_places): + """ + Check hardware type (CUDA) and availability + """ + self.assertTrue(self.platform.is_cuda()) + self.assertTrue(self.platform.available()) + + def test_attention_backend_valid(self): + """ + CUDAPlatform should return correct backend class name for valid backends + """ + self.assertIn( + "PaddleNativeAttnBackend", + self.platform.get_attention_backend_cls(_Backend.NATIVE_ATTN), + ) + self.assertIn( + "AppendAttentionBackend", + self.platform.get_attention_backend_cls(_Backend.APPEND_ATTN), + ) + self.assertIn( + "MLAAttentionBackend", + self.platform.get_attention_backend_cls(_Backend.MLA_ATTN), + ) + self.assertIn( + "FlashAttentionBackend", + self.platform.get_attention_backend_cls(_Backend.FLASH_ATTN), + ) + + def test_attention_backend_invalid(self): + """ + CUDAPlatform should raise ValueError for invalid backend + """ + with self.assertRaises(ValueError): + self.platform.get_attention_backend_cls("INVALID_BACKEND") + + +if __name__ == "__main__": + unittest.main() From c1cac6f0ea57727b257e8985140f3fdfca73aea4 Mon Sep 17 00:00:00 2001 From: nyx-c-language Date: Sun, 24 Aug 2025 21:41:36 +0800 Subject: [PATCH 2/3] update the "is_cuda" to "is_cuda_and_available" --- tests/platforms/test_platforms.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/platforms/test_platforms.py b/tests/platforms/test_platforms.py index 1d450b9f92b..1b2a757f0d9 100644 --- a/tests/platforms/test_platforms.py +++ b/tests/platforms/test_platforms.py @@ -46,7 +46,9 @@ def setUp(self): @patch("paddle.is_compiled_with_cuda", return_value=True) @patch("paddle.device.get_device", return_value="cuda") @patch("paddle.static.cuda_places", return_value=[0]) - def test_is_cuda(self, mock_get_device, mock_is_cuda, mock_cuda_places): + def test_is_cuda_and_available( + self, mock_get_device, mock_is_cuda, mock_cuda_places + ): """ Check hardware type (CUDA) and availability """ From 9bf170a9d88f6ed361edfc5bdfc2299f8069c461 Mon Sep 17 00:00:00 2001 From: nyx-c-language Date: Tue, 26 Aug 2025 17:49:12 +0800 Subject: [PATCH 3/3] fix pre-commit --- tests/platforms/test_platforms.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/platforms/test_platforms.py b/tests/platforms/test_platforms.py index 1b2a757f0d9..70481af7f54 100644 --- a/tests/platforms/test_platforms.py +++ b/tests/platforms/test_platforms.py @@ -46,9 +46,7 @@ def setUp(self): @patch("paddle.is_compiled_with_cuda", return_value=True) @patch("paddle.device.get_device", return_value="cuda") @patch("paddle.static.cuda_places", return_value=[0]) - def test_is_cuda_and_available( - self, mock_get_device, mock_is_cuda, mock_cuda_places - ): + def test_is_cuda_and_available(self, mock_get_device, mock_is_cuda, mock_cuda_places): """ Check hardware type (CUDA) and availability """