Download gmnet/spectral_audit/scripts/generate_deploy.py from YFanwang/Backup: direct link, hf CLI and curl.
- Browser
- Download file 9.69 kB
-
https://huggingface.co/datasets/YFanwang/Backup/resolve/main/gmnet/spectral_audit/scripts/generate_deploy.py
- Command line
-
hf download hf://datasets/YFanwang/Backup/gmnet/spectral_audit/scripts/generate_deploy.py
-
curl -L -o generate_deploy.py https://huggingface.co/datasets/YFanwang/Backup/resolve/main/gmnet/spectral_audit/scripts/generate_deploy.py
9.69 kB
| #!/usr/bin/env python3 | |
| """Generate, but never submit, the formal E4 spectral-audit launch YAML.""" | |
| from __future__ import annotations | |
| import argparse | |
| import copy | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| from typing import Any | |
| import yaml | |
| AUDIT_ROOT = Path("/nfs/ywang29/GmNet/e4_spectral_audit") | |
| JOURNAL_ROOT = Path("/nfs/ywang29/GmNet/journal_exp") | |
| TEMPLATE = Path("/nfs/ywang29/LongLive/deploy/jul11_vgp/j11_vgp01_base.yaml") | |
| EXPECTED_TEMPLATE_SHA256 = "1c84a6a1219bbd6a68c73aa673912d8154065f020083581089b5b615fd1459a9" | |
| EXPECTED_IMAGE = "763104351884.dkr.ecr.us-west-2.amazonaws.com/pytorch-training:2.9.0-gpu-py312-cu130-ubuntu22.04-ec2" | |
| EXPECTED_INPUT_MANIFEST_SHA256 = "8b32da7c33f6b578dd2b5359d727afe313462be0c0040212a2640755ad2fadf7" | |
| DEFAULT_RUNNER = AUDIT_ROOT / "code" / "run_e4_imagenet_spectral_audit.py" | |
| DEFAULT_OUTPUT = AUDIT_ROOT / "results" / "matched_5arm_seed0_20260718" | |
| DEPLOY_ROOT = Path("/nfs/ywang29/GmNet/depoly/e4_imagenet_spectral_20260718") | |
| DEPLOY_YAML = DEPLOY_ROOT / "e4_matched_spectral_5arms_seed0.yaml" | |
| DEPLOY_MANIFEST = DEPLOY_ROOT / "deploy_manifest.json" | |
| def sha256(path: Path) -> str: | |
| digest = hashlib.sha256() | |
| with path.open("rb") as handle: | |
| for chunk in iter(lambda: handle.read(8 * 1024 * 1024), b""): | |
| digest.update(chunk) | |
| return digest.hexdigest() | |
| def require(condition: bool, message: str) -> None: | |
| if not condition: | |
| raise RuntimeError(message) | |
| def render(runner: Path, output_root: Path) -> tuple[str, str]: | |
| input_manifest_path = AUDIT_ROOT / "input_manifest.json" | |
| require( | |
| sha256(input_manifest_path) == EXPECTED_INPUT_MANIFEST_SHA256, | |
| "frozen input manifest changed", | |
| ) | |
| input_manifest = json.loads(input_manifest_path.read_text(encoding="utf-8")) | |
| subset_registration = input_manifest["spectral_subset"] | |
| frozen_subset = Path(subset_registration["artifact_path"]) | |
| required_files = { | |
| "input_manifest": input_manifest_path, | |
| "frozen_subset": frozen_subset, | |
| "parallel_wrapper": AUDIT_ROOT / "scripts" / "run_parallel_5arms.sh", | |
| "input_verifier": AUDIT_ROOT / "scripts" / "verify_inputs.py", | |
| "cross_task_aggregator": AUDIT_ROOT / "scripts" / "aggregate_and_validate.py", | |
| "static_checker": AUDIT_ROOT / "scripts" / "static_check.py", | |
| "generator": Path(__file__).resolve(), | |
| "protocol": AUDIT_ROOT / "protocol" / "IMAGENET_MATCHED_SPECTRAL_AUDIT_PROTOCOL.md", | |
| "setup_env": JOURNAL_ROOT / "scripts" / "setup_env.sh", | |
| "stage_imagenet": JOURNAL_ROOT / "scripts" / "stage_imagenet.sh", | |
| "longlive_init_run": Path("/nfs/ywang29/LongLive/scripts/init_run.sh"), | |
| "alignment_snapshot_manifest": AUDIT_ROOT / "source_snapshots" / "alignment_4a62" / "source_manifest.json", | |
| "alignment_snapshot_gmnet": AUDIT_ROOT / "source_snapshots" / "alignment_4a62" / "gmnet" / "models" / "gmnet.py", | |
| "alignment_snapshot_init": AUDIT_ROOT / "source_snapshots" / "alignment_4a62" / "gmnet" / "models" / "__init__.py", | |
| "source_equivalence_test": AUDIT_ROOT / "scripts" / "test_alignment_source_equivalence.py", | |
| "alignment_snapshot_reconstructor": AUDIT_ROOT / "scripts" / "reconstruct_alignment_snapshot.py", | |
| } | |
| for label, path in required_files.items(): | |
| require(path.is_file(), f"missing {label}: {path}") | |
| require( | |
| sha256(frozen_subset) == subset_registration["sha256"], | |
| "frozen subset manifest hash mismatch", | |
| ) | |
| require(runner.is_file(), f"runner is not frozen at the remote path: {runner}") | |
| require(str(runner).startswith(f"{AUDIT_ROOT}/code/"), "formal runner must be under the audit code directory") | |
| require(str(output_root).startswith(f"{AUDIT_ROOT}/results/"), "formal output must be under the audit results directory") | |
| require(sha256(TEMPLATE) == EXPECTED_TEMPLATE_SHA256, "LongLive launch template changed") | |
| runner_sha = sha256(runner) | |
| aggregator_sha = sha256(required_files["cross_task_aggregator"]) | |
| template = yaml.safe_load(TEMPLATE.read_text(encoding="utf-8")) | |
| require(template["docker_image"] == EXPECTED_IMAGE, "template no longer uses the required PyTorch 2.9 image") | |
| require(template["gpu_num"] == "8", "template no longer requests eight GPUs") | |
| deploy: dict[str, Any] = copy.deepcopy(template) | |
| verify_base = ( | |
| f"/tmp/gmnet_venv/bin/python {AUDIT_ROOT}/scripts/verify_inputs.py " | |
| f"--manifest {AUDIT_ROOT}/input_manifest.json " | |
| f"--runner {runner} --runner-sha256 {runner_sha} --runtime" | |
| ) | |
| deploy["script"] = { | |
| "pre_run_event": ( | |
| f"cd {JOURNAL_ROOT} && " | |
| f"chmod +x ./scripts/setup_env.sh ./scripts/stage_imagenet.sh " | |
| f"{AUDIT_ROOT}/scripts/*.sh && " | |
| "INSTALL_DEV=0 VENV_DIR=/tmp/gmnet_venv bash ./scripts/setup_env.sh && " | |
| "/tmp/gmnet_venv/bin/python ./scripts/generate_e4_mechanism_followup_deploy.py --check && " | |
| f"/tmp/gmnet_venv/bin/python {AUDIT_ROOT}/scripts/generate_deploy.py --check && " | |
| f"/tmp/gmnet_venv/bin/python {AUDIT_ROOT}/scripts/static_check.py --require-generated && " | |
| f"/tmp/gmnet_venv/bin/python {AUDIT_ROOT}/scripts/aggregate_and_validate.py --self-test && " | |
| f"/tmp/gmnet_venv/bin/python {AUDIT_ROOT}/scripts/reconstruct_alignment_snapshot.py --check && " | |
| f"/tmp/gmnet_venv/bin/python {AUDIT_ROOT}/scripts/test_alignment_source_equivalence.py " | |
| "--output /tmp/gmnet_alignment_equivalence_torch29.json && " | |
| f"{verify_base} --equivalence /tmp/gmnet_alignment_equivalence_torch29.json && " | |
| "KEEP_ARCHIVE=0 VENV_DIR=/tmp/gmnet_venv bash ./scripts/stage_imagenet.sh full && " | |
| f"{verify_base} --data-root /tmp/gmnet_data/imagenet-1k " | |
| "--equivalence /tmp/gmnet_alignment_equivalence_torch29.json" | |
| ), | |
| "command": ( | |
| f"cd {AUDIT_ROOT} && " | |
| f"AUDIT_ROOT={AUDIT_ROOT} " | |
| f"MANIFEST_PATH={AUDIT_ROOT}/input_manifest.json " | |
| f"SPECTRAL_RUNNER={runner} " | |
| f"EXPECTED_RUNNER_SHA256={runner_sha} " | |
| f"EXPECTED_AGGREGATOR_SHA256={aggregator_sha} " | |
| "DATA_ROOT=/tmp/gmnet_data/imagenet-1k " | |
| f"OUTPUT_ROOT={output_root} " | |
| "VENV_DIR=/tmp/gmnet_venv " | |
| f"bash {AUDIT_ROOT}/scripts/run_parallel_5arms.sh" | |
| ), | |
| "jobs": [{"name": "gmnet-e4-spectral-5arm-seed0"}], | |
| } | |
| yaml_text = ( | |
| "# Generated by e4_spectral_audit/scripts/generate_deploy.py; do not edit.\n" | |
| + yaml.safe_dump(deploy, sort_keys=False, width=100000) | |
| ) | |
| manifest = { | |
| "schema_version": 1, | |
| "status": "ready_not_submitted", | |
| "protocol_id": "gmnet-e4-imagenet-matched-spectral-v1", | |
| "protocol_path": str(required_files["protocol"]), | |
| "protocol_sha256": sha256(required_files["protocol"]), | |
| "launchjob_submitted_by_generator": False, | |
| "deploy_yaml": str(DEPLOY_YAML), | |
| "job_name": "gmnet-e4-spectral-5arm-seed0", | |
| "runner": str(runner), | |
| "runner_sha256": runner_sha, | |
| "aggregator": str(required_files["cross_task_aggregator"]), | |
| "aggregator_sha256": aggregator_sha, | |
| "output_root": str(output_root), | |
| "docker_image": EXPECTED_IMAGE, | |
| "resource_contract": { | |
| "gpu_type": template["gpu_type"], | |
| "gpu_num": template["gpu_num"], | |
| "gpu_memory": template["gpu_memory"], | |
| "cpu_num": template["cpu_num"], | |
| "memory": template["memory"], | |
| "canonical_arms": 5, | |
| "parallel_tasks": 8, | |
| "gpu_assignment": { | |
| "B": 0, | |
| "D": 1, | |
| "S": 2, | |
| "C": 3, | |
| "SC": 4, | |
| "D_order1": 5, | |
| "D_order2": 6, | |
| "B_order1": 7, | |
| }, | |
| }, | |
| "audit_contract": { | |
| "batch_size": 256, | |
| "workers_per_arm": 8, | |
| "max_eval_samples": 50000, | |
| "spectral_samples": 4096, | |
| "lowpass_cutoffs": [0.125, 0.25, 0.5, 0.75], | |
| "precision": "float32", | |
| }, | |
| "source_template": str(TEMPLATE), | |
| "source_template_sha256": EXPECTED_TEMPLATE_SHA256, | |
| "artifacts_sha256": { | |
| **{label: sha256(path) for label, path in required_files.items()}, | |
| "runner": runner_sha, | |
| }, | |
| } | |
| json_text = json.dumps(manifest, indent=2, sort_keys=True) + "\n" | |
| return yaml_text, json_text | |
| def main() -> None: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--runner", type=Path, default=DEFAULT_RUNNER) | |
| parser.add_argument("--output-root", type=Path, default=DEFAULT_OUTPUT) | |
| parser.add_argument("--check", action="store_true") | |
| args = parser.parse_args() | |
| yaml_text, manifest_text = render(args.runner.resolve(), args.output_root.resolve()) | |
| if args.check: | |
| require(DEPLOY_YAML.is_file(), f"missing generated YAML: {DEPLOY_YAML}") | |
| require(DEPLOY_MANIFEST.is_file(), f"missing deploy manifest: {DEPLOY_MANIFEST}") | |
| require(DEPLOY_YAML.read_text(encoding="utf-8") == yaml_text, "deploy YAML is stale") | |
| require(DEPLOY_MANIFEST.read_text(encoding="utf-8") == manifest_text, "deploy manifest is stale") | |
| print(f"Deployment package verified: {DEPLOY_YAML}") | |
| return | |
| DEPLOY_YAML.parent.mkdir(parents=True, exist_ok=True) | |
| DEPLOY_YAML.write_text(yaml_text, encoding="utf-8") | |
| DEPLOY_MANIFEST.write_text(manifest_text, encoding="utf-8") | |
| print(f"Generated without submission: {DEPLOY_YAML}") | |
| if __name__ == "__main__": | |
| main() | |