Download torch_speedkit/utils.py from officialcyber88/torch-tensorcore-speedkit: direct link, hf CLI and curl.
- Browser
- Download file 909 Bytes
-
https://huggingface.co/officialcyber88/torch-tensorcore-speedkit/resolve/main/torch_speedkit/utils.py
- Command line
-
hf download hf://officialcyber88/torch-tensorcore-speedkit/torch_speedkit/utils.py
-
curl -L -o utils.py https://huggingface.co/officialcyber88/torch-tensorcore-speedkit/resolve/main/torch_speedkit/utils.py
909 Bytes
| from __future__ import annotations | |
| import os | |
| import random | |
| from typing import Optional | |
| import numpy as np | |
| import torch | |
| def set_seed(seed: int, deterministic: bool) -> None: | |
| random.seed(seed) | |
| np.random.seed(seed) | |
| torch.manual_seed(seed) | |
| if torch.cuda.is_available(): | |
| torch.cuda.manual_seed_all(seed) | |
| if deterministic: | |
| # Determinism can reduce performance. | |
| torch.use_deterministic_algorithms(True) | |
| torch.backends.cudnn.deterministic = True | |
| torch.backends.cudnn.benchmark = False | |
| def maybe_channels_last(x: torch.Tensor, enabled: bool) -> torch.Tensor: | |
| if not enabled: | |
| return x | |
| if x.dim() == 4: | |
| return x.contiguous(memory_format=torch.channels_last) | |
| return x | |
| def env_flag(name: str) -> Optional[str]: | |
| v = os.environ.get(name) | |
| return v if v is not None and v != "" else None | |