Table of Contents
Shortcuts

# Source code for torch.cpu.amp.autocast_mode

    from typing import Any

    import torch

    __all__ = ["autocast"]

    class autocast(torch.amp.autocast_mode.autocast):
        r"""
        See :class:`torch.autocast`.
        ``torch.cpu.amp.autocast(args...)`` is equivalent to ``torch.autocast("cpu", args...)``
        """

        def __init__(
            self,
            enabled: bool = True,
            dtype: torch.dtype = torch.bfloat16,
            cache_enabled: bool = True,
        ):
            if torch._jit_internal.is_scripting():
                self._enabled = enabled
                self.device = "cpu"
                self.fast_dtype = dtype
                return
            super().__init__(
                "cpu", enabled=enabled, dtype=dtype, cache_enabled=cache_enabled
            )

        def __enter__(self):
            if torch._jit_internal.is_scripting():
                return self
            return super().__enter__()

        # TODO: discuss a unified TorchScript-friendly API for autocast
        def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any):  # type: ignore[override]
            if torch._jit_internal.is_scripting():
                return
            return super().__exit__(exc_type, exc_val, exc_tb)

        def __call__(self, func):
            if torch._jit_internal.is_scripting():
                return func
            return super().__call__(func)

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.