Skip to content

Commit 8031e24

Browse files
refactor: Use with in Accelerator.autocast()instead of __enter__() and __exit__() for more elegant style. (#3767)
* refactor: Use ` with` in `Accelerator.autocast()`instead of `__enter__()` and `__exit__()`for more elegant style. * Apply style fixes --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 3db9fb6 commit 8031e24

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

src/accelerate/accelerator.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import os
2222
import re
2323
import shutil
24-
import sys
2524
import warnings
2625
from collections import OrderedDict
2726
from contextlib import contextmanager
@@ -4069,10 +4068,8 @@ def autocast(self, autocast_handler: AutocastKwargs = None):
40694068
if autocast_handler is None:
40704069
autocast_handler = self.autocast_handler
40714070
autocast_context = get_mixed_precision_context_manager(self.native_amp, autocast_handler)
4072-
autocast_context.__enter__()
4073-
# TODO: should the `yield` be in a try/finally block?
4074-
yield
4075-
autocast_context.__exit__(*sys.exc_info())
4071+
with autocast_context:
4072+
yield
40764073

40774074
@contextmanager
40784075
def profile(self, profile_handler: ProfileKwargs | None = None):

0 commit comments

Comments
 (0)