From 277f37e9c17e3c3073f6d37eb071dade7f1f3b4f Mon Sep 17 00:00:00 2001 From: fanxiangyu Date: Mon, 3 Nov 2025 17:03:56 +0800 Subject: [PATCH 1/4] autocast --- python/paddle/amp/auto_cast.py | 3 +- python/paddle/device/__init__.py | 53 ++++++++++++++++++++++++++++++-- test/amp/test_amp_api.py | 6 ++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/python/paddle/amp/auto_cast.py b/python/paddle/amp/auto_cast.py index cb3b647e38387e..0b1f6fd39cac6e 100644 --- a/python/paddle/amp/auto_cast.py +++ b/python/paddle/amp/auto_cast.py @@ -1064,7 +1064,7 @@ def amp_decorate( def autocast( - enabled=True, dtype=paddle.float16, cache_enabled=True + device_type, dtype=paddle.float16, enabled=True, cache_enabled=True ) -> AbstractContextManager: """ Create a context which enables auto-mixed-precision(AMP) of operators executed in dynamic graph mode. @@ -1075,6 +1075,7 @@ def autocast( imperative mode. Args: + device_type(str, optional): Device type.But because the paddle does not distinguish between devices, this parameter does not work enable(bool, optional): Enable auto-mixed-precision or not. Default is True. dtype(str, optional): Whether to use 'float16' or 'bfloat16'. Default is 'float16'. cache_enabled(bool, optional): whether to enable cache or not. Default is True. But this parameter is not used diff --git a/python/paddle/device/__init__.py b/python/paddle/device/__init__.py index b562e121d9f336..eee27b600aa326 100644 --- a/python/paddle/device/__init__.py +++ b/python/paddle/device/__init__.py @@ -54,6 +54,7 @@ if TYPE_CHECKING: from types import TracebackType + from contextlib import AbstractContextManager from paddle import IPUPlace as _IPUPlace, XPUPlace as _XPUPlace from paddle._typing.device_like import PlaceLike @@ -1788,13 +1789,61 @@ def manual_seed_all(seed: int) -> None: class _AutocastMode: - autocast = staticmethod(_autocast) + @staticmethod + def autocast( + enabled=True, dtype=paddle.float16, cache_enabled=True + ) -> AbstractContextManager: + """ + Create a context which enables auto-mixed-precision(AMP) of operators executed in dynamic graph mode. + If enabled, the input data type (float32, float16 or bfloat16) of each operator is decided + by autocast algorithm for better performance. + + Commonly, it is used together with `GradScaler` and `decorator` to achieve Auto-Mixed-Precision in + imperative mode. + + Args: + device_type(str, optional): Device type. But because the paddle does not distinguish between devices, this parameter does not work. + enable(bool, optional): Enable auto-mixed-precision or not. Default is True. + dtype(str, optional): Whether to use 'float16' or 'bfloat16'. Default is 'float16'. + cache_enabled(bool, optional): whether to enable cache or not. Default is True. But this parameter is not used + + Note: + paddle.cuda.amp. + + Examples: + + .. code-block:: python + + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + + >>> conv2d = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) + >>> data = paddle.rand([10, 3, 32, 32]) + + >>> with paddle.device.amp.auto_cast(): + ... conv = conv2d(data) + ... print(conv.dtype) + >>> # doctest: +SKIP("This has diff in xdoctest env") + paddle.float16 + >>> # doctest: -SKIP + + >>> with paddle.device.amp.auto_cast(enable=False): + ... conv = conv2d(data) + ... print(conv.dtype) + >>> # doctest: +SKIP("This has diff in xdoctest env") + paddle.float32 + >>> # doctest: -SKIP + + """ + return _autocast(device_type = 'cuda', enabled=enabled, dtype=dtype) + + class amp: """Namespace for amp marker operations.""" - autocast = staticmethod(_autocast) + autocast = staticmethod(_AutocastMode.autocast) autocast_mode = _AutocastMode() diff --git a/test/amp/test_amp_api.py b/test/amp/test_amp_api.py index 3f657f99037709..08fe296ceb87b3 100644 --- a/test/amp/test_amp_api.py +++ b/test/amp/test_amp_api.py @@ -117,6 +117,9 @@ def _run_autocast_test(self, ctx): def test_amp_autocast(self): self._run_autocast_test(paddle.amp.autocast()) + + def test_amp_autocast(self): + self._run_autocast_test(paddle.amp.autocast(device_type = 'cuda', enabled=True, dtype=paddle.float16, cache_enabled=True)) def test_cuda_amp_autocast(self): self._run_autocast_test(paddle.cuda.amp.autocast()) @@ -436,3 +439,6 @@ def test_pir_op_called_as_expected(self): if __name__ == '__main__': unittest.main() + +import torch +torch.amp.autocast \ No newline at end of file From cb7a2980fd8d50875f8f703f381079d82db3d181 Mon Sep 17 00:00:00 2001 From: fanxiangyu Date: Mon, 3 Nov 2025 20:05:04 +0800 Subject: [PATCH 2/4] rm mess code --- test/amp/test_amp_api.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/amp/test_amp_api.py b/test/amp/test_amp_api.py index 08fe296ceb87b3..ad8fe89f9d6ce7 100644 --- a/test/amp/test_amp_api.py +++ b/test/amp/test_amp_api.py @@ -439,6 +439,4 @@ def test_pir_op_called_as_expected(self): if __name__ == '__main__': unittest.main() - -import torch -torch.amp.autocast \ No newline at end of file + \ No newline at end of file From f78ee0e7c8e7513eb0a23f8130d43daa3a7f095c Mon Sep 17 00:00:00 2001 From: fanxiangyu Date: Mon, 3 Nov 2025 20:09:42 +0800 Subject: [PATCH 3/4] precommit --- python/paddle/device/__init__.py | 6 ++---- test/amp/test_amp_api.py | 14 ++++++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/python/paddle/device/__init__.py b/python/paddle/device/__init__.py index eee27b600aa326..96f6e8c2c01854 100644 --- a/python/paddle/device/__init__.py +++ b/python/paddle/device/__init__.py @@ -53,8 +53,8 @@ ) if TYPE_CHECKING: - from types import TracebackType from contextlib import AbstractContextManager + from types import TracebackType from paddle import IPUPlace as _IPUPlace, XPUPlace as _XPUPlace from paddle._typing.device_like import PlaceLike @@ -1835,9 +1835,7 @@ def autocast( >>> # doctest: -SKIP """ - return _autocast(device_type = 'cuda', enabled=enabled, dtype=dtype) - - + return _autocast(device_type='cuda', enabled=enabled, dtype=dtype) class amp: diff --git a/test/amp/test_amp_api.py b/test/amp/test_amp_api.py index ad8fe89f9d6ce7..dce27194e0dbd4 100644 --- a/test/amp/test_amp_api.py +++ b/test/amp/test_amp_api.py @@ -117,9 +117,16 @@ def _run_autocast_test(self, ctx): def test_amp_autocast(self): self._run_autocast_test(paddle.amp.autocast()) - - def test_amp_autocast(self): - self._run_autocast_test(paddle.amp.autocast(device_type = 'cuda', enabled=True, dtype=paddle.float16, cache_enabled=True)) + + def test_amp_autocast2(self): + self._run_autocast_test( + paddle.amp.autocast( + device_type='cuda', + enabled=True, + dtype=paddle.float16, + cache_enabled=True, + ) + ) def test_cuda_amp_autocast(self): self._run_autocast_test(paddle.cuda.amp.autocast()) @@ -439,4 +446,3 @@ def test_pir_op_called_as_expected(self): if __name__ == '__main__': unittest.main() - \ No newline at end of file From 37e71a52d3f2541435302a44eea7b3007ef6e308 Mon Sep 17 00:00:00 2001 From: fanxiangyu Date: Mon, 3 Nov 2025 20:15:20 +0800 Subject: [PATCH 4/4] code repair --- python/paddle/amp/auto_cast.py | 5 ++++- test/amp/test_amp_api.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/python/paddle/amp/auto_cast.py b/python/paddle/amp/auto_cast.py index 0b1f6fd39cac6e..8740ee348cf649 100644 --- a/python/paddle/amp/auto_cast.py +++ b/python/paddle/amp/auto_cast.py @@ -1064,7 +1064,10 @@ def amp_decorate( def autocast( - device_type, dtype=paddle.float16, enabled=True, cache_enabled=True + device_type: str | None, + dtype: _DTypeLiteral = 'float16', + enabled: bool = True, + cache_enabled: bool = True, ) -> AbstractContextManager: """ Create a context which enables auto-mixed-precision(AMP) of operators executed in dynamic graph mode. diff --git a/test/amp/test_amp_api.py b/test/amp/test_amp_api.py index dce27194e0dbd4..f405633b28886d 100644 --- a/test/amp/test_amp_api.py +++ b/test/amp/test_amp_api.py @@ -116,7 +116,7 @@ def _run_autocast_test(self, ctx): self.assertEqual(out3.dtype, paddle.float32) def test_amp_autocast(self): - self._run_autocast_test(paddle.amp.autocast()) + self._run_autocast_test(paddle.amp.autocast(device_type='cuda')) def test_amp_autocast2(self): self._run_autocast_test(