|
| 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 unittest |
| 16 | + |
| 17 | +import paddle |
| 18 | + |
| 19 | + |
| 20 | +@unittest.skipIf(paddle.device.get_device() == "cpu", "Skip AMP test on CPU") |
| 21 | +class TestAutocast(unittest.TestCase): |
| 22 | + def setUp(self) -> None: |
| 23 | + paddle.disable_static() |
| 24 | + self.device_list = [None, paddle.device.get_device()] |
| 25 | + self.default_dtype = "float16" |
| 26 | + |
| 27 | + def do_test(self, device, expected_type): |
| 28 | + self.assertTrue(paddle.get_autocast_dtype(device) == expected_type) |
| 29 | + self.assertTrue(paddle.get_autocast_gpu_dtype() == expected_type) |
| 30 | + self.assertTrue(paddle.amp.get_autocast_dtype(device) == expected_type) |
| 31 | + self.assertTrue(paddle.amp.get_autocast_gpu_dtype() == expected_type) |
| 32 | + self.assertTrue(paddle.amp.get_autocast_cpu_dtype() == expected_type) |
| 33 | + self.assertTrue( |
| 34 | + paddle.amp.get_autocast_cpu_dtype(device) == expected_type |
| 35 | + ) |
| 36 | + |
| 37 | + def test_amp_default(self): |
| 38 | + for device in self.device_list: |
| 39 | + self.do_test(device, self.default_dtype) |
| 40 | + |
| 41 | + def test_amp_autocast_fp16(self): |
| 42 | + for device in self.device_list: |
| 43 | + with paddle.amp.auto_cast(True, dtype="float16"): |
| 44 | + self.do_test(device, "float16") |
| 45 | + self.do_test(device, self.default_dtype) |
| 46 | + |
| 47 | + def test_amp_autocast_bf16(self): |
| 48 | + for device in self.device_list: |
| 49 | + with paddle.amp.auto_cast(True, dtype="bfloat16"): |
| 50 | + self.do_test(device, "bfloat16") |
| 51 | + self.do_test(device, self.default_dtype) |
| 52 | + |
| 53 | + def test_amp_autocast_false_bf16(self): |
| 54 | + for device in self.device_list: |
| 55 | + with paddle.amp.auto_cast(True, dtype="bfloat16"): |
| 56 | + self.do_test(device, "bfloat16") |
| 57 | + self.do_test(device, self.default_dtype) |
| 58 | + |
| 59 | + def test_amp_nested_context(self): |
| 60 | + for device in self.device_list: |
| 61 | + with paddle.amp.auto_cast(True, dtype="bfloat16"): |
| 62 | + self.do_test(device, "bfloat16") |
| 63 | + with paddle.amp.auto_cast(True, dtype="float16"): |
| 64 | + self.do_test(device, "float16") |
| 65 | + self.do_test(device, "bfloat16") |
| 66 | + self.do_test(device, self.default_dtype) |
| 67 | + |
| 68 | + |
| 69 | +class TestAutocastStatic(TestAutocast): |
| 70 | + def setUp(self) -> None: |
| 71 | + paddle.enable_static() |
| 72 | + self.device_list = [None, paddle.device.get_device()] |
| 73 | + self.default_dtype = "float16" |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + unittest.main() |
0 commit comments