from __future__ import annotations import random from pathlib import Path from typing import Any import numpy as np import torch import yaml def load_config(path: str | Path) -> dict[str, Any]: with open(path, encoding="utf-8") as f: return yaml.safe_load(f) def ensure_dir(path: str | Path) -> Path: p = Path(path) p.mkdir(parents=True, exist_ok=True) return p def seed_everything(seed: int) -> None: random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed_all(seed) def select_device(name: str | None = None) -> torch.device: if name and name != "auto": return torch.device(name) return torch.device("cuda" if torch.cuda.is_available() else "cpu")