Buckets:
| import json | |
| import re | |
| from dataclasses import dataclass | |
| from pathlib import Path | |
| from typing import Any | |
| class RuntimeConfig: | |
| device: str | |
| num_threads: int | |
| seed: int | |
| use_cuda: bool | |
| use_amp: bool | |
| def load_runtime_config(path: Path) -> RuntimeConfig: | |
| data: Any = json.loads(path.read_text(encoding="utf-8")) | |
| if not isinstance(data, dict): | |
| raise ValueError("runtime config must be a JSON object") | |
| required_fields = {"device", "num_threads", "seed", "use_cuda", "use_amp"} | |
| if set(data) != required_fields: | |
| raise ValueError("runtime config must contain exactly the required fields") | |
| device = data["device"] | |
| if not isinstance(device, str) or (device != "cpu" and re.fullmatch(r"cuda(?::[0-9]+)?", device) is None): | |
| raise ValueError("device must be cpu or cuda[:index]") | |
| num_threads = data["num_threads"] | |
| if ( | |
| isinstance(num_threads, bool) | |
| or not isinstance(num_threads, int) | |
| or not 1 <= num_threads <= 8 | |
| ): | |
| raise ValueError("num_threads must be an integer between 1 and 8") | |
| seed = data["seed"] | |
| if isinstance(seed, bool) or not isinstance(seed, int) or seed < 0: | |
| raise ValueError("seed must be a non-negative integer") | |
| use_cuda = data["use_cuda"] | |
| if not isinstance(use_cuda, bool) or use_cuda != device.startswith("cuda"): | |
| raise ValueError("use_cuda must match the configured device") | |
| use_amp = data["use_amp"] | |
| if not isinstance(use_amp, bool): | |
| raise ValueError("use_amp must be boolean") | |
| if use_amp and device == "cpu": | |
| raise ValueError("automatic mixed precision requires CUDA") | |
| return RuntimeConfig( | |
| device=device, | |
| num_threads=num_threads, | |
| seed=seed, | |
| use_cuda=use_cuda, | |
| use_amp=use_amp, | |
| ) | |
Xet Storage Details
- Size:
- 1.9 kB
- Xet hash:
- 861e9a1438d184ccb5b78de9c87f2a241857ec69409b1e59b083cab060f48259
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.