| """Strip machine-specific values from every `config.json` in the release. |
| |
| The fit pipeline records absolute paths to local AOT (ahead-of-time compiled) |
| PyTorch artifacts in `AOT_ARTIFACT_FILE`. Those paths only resolve on the |
| machine where the fit was produced, and the binary AOT artifacts themselves are |
| not part of the release. This script nulls those paths in place. All numeric |
| hyperparameters, seeds, and reproducibility fields are left untouched. |
| |
| The script is idempotent: keys that are already `None` (or absent) are skipped. |
| It supports `--dry-run` to preview affected files without writing. |
| |
| Usage: |
| python scripts/sanitize_configs.py # write in place |
| python scripts/sanitize_configs.py --dry-run # report only |
| python scripts/sanitize_configs.py --root /path/to/sf_release |
| """ |
|
|
| from __future__ import annotations |
|
|
| import argparse |
| import json |
| from pathlib import Path |
| from typing import Any |
|
|
| |
| PATH_KEYS_TO_NULL: tuple[str, ...] = ( |
| "AOT_ARTIFACT_FILE", |
| ) |
|
|
| |
| SANITIZED_MARKER_KEY = "_RELEASE_SANITIZED" |
|
|
|
|
| def sanitize_config(data: dict[str, Any]) -> tuple[dict[str, Any], list[str]]: |
| """Return (new_data, list_of_changed_keys). Does not modify input.""" |
| new = dict(data) |
| changed: list[str] = [] |
| for key in PATH_KEYS_TO_NULL: |
| if key in new and new[key] is not None: |
| new[key] = None |
| changed.append(key) |
| if changed and not new.get(SANITIZED_MARKER_KEY): |
| new[SANITIZED_MARKER_KEY] = True |
| return new, changed |
|
|
|
|
| def process_file(path: Path, dry_run: bool) -> tuple[bool, list[str]]: |
| raw = path.read_text() |
| try: |
| data = json.loads(raw) |
| except json.JSONDecodeError as exc: |
| print(f" SKIP {path}: invalid JSON ({exc})") |
| return False, [] |
| if not isinstance(data, dict): |
| print(f" SKIP {path}: not a JSON object") |
| return False, [] |
| new_data, changed = sanitize_config(data) |
| if not changed: |
| return False, [] |
| if not dry_run: |
| |
| path.write_text(json.dumps(new_data, indent=4) + "\n") |
| return True, changed |
|
|
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser(description=__doc__) |
| default_root = Path(__file__).resolve().parent.parent |
| parser.add_argument("--root", type=Path, default=default_root, |
| help="Release root containing config.json files anywhere underneath.") |
| parser.add_argument("--dry-run", action="store_true", |
| help="Report files that would be modified; do not write.") |
| args = parser.parse_args() |
|
|
| if not args.root.is_dir(): |
| raise SystemExit(f"Release root does not exist: {args.root}") |
|
|
| config_paths = sorted(args.root.rglob("config.json")) |
| print(f"Scanning {len(config_paths)} config.json files under {args.root}") |
| print(f"Keys to null: {PATH_KEYS_TO_NULL}") |
| if args.dry_run: |
| print("(dry run; no files will be written)") |
|
|
| changed_count = 0 |
| per_key: dict[str, int] = {k: 0 for k in PATH_KEYS_TO_NULL} |
| for path in config_paths: |
| was_changed, changed_keys = process_file(path, args.dry_run) |
| if was_changed: |
| changed_count += 1 |
| for k in changed_keys: |
| per_key[k] = per_key.get(k, 0) + 1 |
|
|
| print(f"Changed: {changed_count}/{len(config_paths)} configs") |
| for k, n in per_key.items(): |
| print(f" {k}: {n}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|