| from __future__ import annotations | |
| import json | |
| import random | |
| from pathlib import Path | |
| from typing import Any | |
| import numpy as np | |
| import torch | |
| def set_seed(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) | |
| torch.backends.cudnn.benchmark = False | |
| torch.backends.cudnn.deterministic = True | |
| def write_json(path: str | Path, value: dict[str, Any]) -> None: | |
| path = Path(path) | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| with path.open("w", encoding="utf-8") as file: | |
| json.dump(value, file, ensure_ascii=False, indent=2) | |
| def read_json(path: str | Path) -> dict[str, Any]: | |
| with Path(path).open("r", encoding="utf-8") as file: | |
| return json.load(file) | |