Download pulse/config.py from velvet-pine-22/PULSE-code: direct link, hf CLI and curl.
- Browser
- Download file 1.97 kB
-
https://huggingface.co/velvet-pine-22/PULSE-code/resolve/main/pulse/config.py
- Command line
-
hf download hf://velvet-pine-22/PULSE-code/pulse/config.py
-
curl -L -o config.py https://huggingface.co/velvet-pine-22/PULSE-code/resolve/main/pulse/config.py
1.97 kB
| """YAML run configurations. | |
| A config may inherit from another with ``_base_: relative/path.yaml`` (deep | |
| merge, child wins). Command-line overrides use ``--set key.sub=value`` where | |
| the value is parsed as YAML (``--set train.epochs=2 --set modalities=[emg]``). | |
| """ | |
| from __future__ import annotations | |
| import copy | |
| from pathlib import Path | |
| from typing import Any, Dict, Iterable, Optional | |
| import yaml | |
| def deep_merge(base: Dict[str, Any], child: Dict[str, Any]) -> Dict[str, Any]: | |
| out = copy.deepcopy(base) | |
| for k, v in child.items(): | |
| if isinstance(v, dict) and isinstance(out.get(k), dict): | |
| out[k] = deep_merge(out[k], v) | |
| else: | |
| out[k] = copy.deepcopy(v) | |
| return out | |
| def load_yaml(path, _seen: Optional[set] = None) -> Dict[str, Any]: | |
| p = Path(path).resolve() | |
| seen = set(_seen or ()) | |
| if p in seen: | |
| raise ValueError(f"config inheritance cycle at {p}") | |
| seen.add(p) | |
| with open(p, encoding="utf-8") as f: | |
| d = yaml.safe_load(f) or {} | |
| if not isinstance(d, dict): | |
| raise ValueError(f"{p}: top level must be a mapping") | |
| base = d.pop("_base_", None) | |
| if base: | |
| d = deep_merge(load_yaml(p.parent / base, seen), d) | |
| return d | |
| def apply_overrides(cfg: Dict[str, Any], overrides: Iterable[str]) -> Dict[str, Any]: | |
| cfg = copy.deepcopy(cfg) | |
| for item in overrides or (): | |
| if "=" not in item: | |
| raise ValueError(f"override must be key=value, got {item!r}") | |
| key, val = item.split("=", 1) | |
| node = cfg | |
| parts = key.strip().split(".") | |
| for part in parts[:-1]: | |
| node = node.setdefault(part, {}) | |
| if not isinstance(node, dict): | |
| raise ValueError(f"override {key!r}: {part!r} is not a mapping") | |
| node[parts[-1]] = yaml.safe_load(val) | |
| return cfg | |
| def load_config(path, overrides: Iterable[str] = ()) -> Dict[str, Any]: | |
| return apply_overrides(load_yaml(path), overrides) | |