| """ | |
| src/utils.py | |
| Small shared helpers. Nothing domain-specific lives here — just things that | |
| would otherwise be copy-pasted across train.py / evaluate.py. | |
| """ | |
| import random | |
| import numpy as np | |
| import torch | |
| def set_seed(seed: int) -> None: | |
| """Make training reproducible across runs with the same seed.""" | |
| random.seed(seed) | |
| np.random.seed(seed) | |
| torch.manual_seed(seed) | |
| if torch.cuda.is_available(): | |
| torch.cuda.manual_seed_all(seed) | |
| # Slight performance hit but needed for strict reproducibility | |
| torch.backends.cudnn.deterministic = True | |
| torch.backends.cudnn.benchmark = False | |
| def get_device() -> torch.device: | |
| """Return CUDA if available, else CPU.""" | |
| return torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| def load_yaml(path: str) -> dict: | |
| """Load a YAML config file into a plain dict.""" | |
| import yaml | |
| with open(path, "r") as f: | |
| return yaml.safe_load(f) | |