Download torch_speedkit/attention.py from officialcyber88/torch-tensorcore-speedkit: direct link, hf CLI and curl.
- Browser
- Download file 1.93 kB
-
https://huggingface.co/officialcyber88/torch-tensorcore-speedkit/resolve/main/torch_speedkit/attention.py
- Command line
-
hf download hf://officialcyber88/torch-tensorcore-speedkit/torch_speedkit/attention.py
-
curl -L -o attention.py https://huggingface.co/officialcyber88/torch-tensorcore-speedkit/resolve/main/torch_speedkit/attention.py
1.93 kB
| from __future__ import annotations | |
| import contextlib | |
| import torch | |
| def sdpa_backend_context( | |
| enable_flash: bool, | |
| enable_mem_efficient: bool, | |
| enable_math: bool, | |
| enable_cudnn: bool, | |
| ): | |
| """ | |
| Controls which SDPA backends PyTorch is allowed to use. | |
| These flags are beta in PyTorch and may change. | |
| """ | |
| if not torch.cuda.is_available(): | |
| yield | |
| return | |
| # Save current | |
| prev = {} | |
| if hasattr(torch.backends.cuda, "flash_sdp_enabled"): | |
| prev["flash"] = torch.backends.cuda.flash_sdp_enabled() | |
| if hasattr(torch.backends.cuda, "mem_efficient_sdp_enabled"): | |
| prev["mem"] = torch.backends.cuda.mem_efficient_sdp_enabled() | |
| if hasattr(torch.backends.cuda, "math_sdp_enabled"): | |
| prev["math"] = torch.backends.cuda.math_sdp_enabled() | |
| if hasattr(torch.backends.cuda, "cudnn_sdp_enabled"): | |
| prev["cudnn"] = torch.backends.cuda.cudnn_sdp_enabled() | |
| try: | |
| if hasattr(torch.backends.cuda, "enable_flash_sdp"): | |
| torch.backends.cuda.enable_flash_sdp(enable_flash) | |
| if hasattr(torch.backends.cuda, "enable_mem_efficient_sdp"): | |
| torch.backends.cuda.enable_mem_efficient_sdp(enable_mem_efficient) | |
| if hasattr(torch.backends.cuda, "enable_math_sdp"): | |
| torch.backends.cuda.enable_math_sdp(enable_math) | |
| if hasattr(torch.backends.cuda, "enable_cudnn_sdp"): | |
| torch.backends.cuda.enable_cudnn_sdp(enable_cudnn) | |
| yield | |
| finally: | |
| if "flash" in prev: | |
| torch.backends.cuda.enable_flash_sdp(prev["flash"]) | |
| if "mem" in prev: | |
| torch.backends.cuda.enable_mem_efficient_sdp(prev["mem"]) | |
| if "math" in prev: | |
| torch.backends.cuda.enable_math_sdp(prev["math"]) | |
| if "cudnn" in prev: | |
| torch.backends.cuda.enable_cudnn_sdp(prev["cudnn"]) | |