Download gmnet/code/journal_exp/tests/test_e4_alignment_deploy.py from YFanwang/Backup: direct link, hf CLI and curl.
- Browser
- Download file 8.62 kB
-
https://huggingface.co/datasets/YFanwang/Backup/resolve/main/gmnet/code/journal_exp/tests/test_e4_alignment_deploy.py
- Command line
-
hf download hf://datasets/YFanwang/Backup/gmnet/code/journal_exp/tests/test_e4_alignment_deploy.py
-
curl -L -o test_e4_alignment_deploy.py https://huggingface.co/datasets/YFanwang/Backup/resolve/main/gmnet/code/journal_exp/tests/test_e4_alignment_deploy.py
8.62 kB
| """Guards for the matched E4 alignment ImageNet launch batch.""" | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| from typing import Any | |
| import yaml | |
| from scripts import generate_e4_alignment_deploy as e4 | |
| ROOT = Path(__file__).resolve().parents[1] | |
| BASE_TEMPLATE = Path("/nfs/ywang29/LongLive/deploy/jul11_vgp/j11_vgp01_base.yaml") | |
| OLD_MANIFEST = ROOT / "configs/imagenet_v2_code_manifest.json" | |
| COMPLETION_SEAL = Path( | |
| "/nfs/ywang29/GmNet/depoly/single_seed_followup_20260715/completion_seal.json" | |
| ) | |
| INVARIANT_KEYS = (*e4.RESOURCE_KEYS, *e4.PROJECT_KEYS) | |
| OLD_MANIFEST_FILE_SHA256 = ( | |
| "eb2150671596a108282a80705ec5c2020b0d2a8b22248c75dc1aa4f2f750f08a" | |
| ) | |
| OLD_CODE_SHA256 = ( | |
| "4e1c0a36f2cf2e7dfb0df8f5dcfe589f3d6a2837a71d2bdda37a66109ea75b7e" | |
| ) | |
| COMPLETION_SEAL_SHA256 = ( | |
| "e819c59c786d8aab63678d506712117760af3aa7e22569383dc1b5efd4cafc94" | |
| ) | |
| def _different_paths(left: Any, right: Any, prefix: str = "") -> set[str]: | |
| if isinstance(left, dict) and isinstance(right, dict): | |
| different: set[str] = set() | |
| for key in set(left) | set(right): | |
| path = f"{prefix}.{key}" if prefix else str(key) | |
| if key not in left or key not in right: | |
| different.add(path) | |
| else: | |
| different.update(_different_paths(left[key], right[key], path)) | |
| return different | |
| return {prefix} if left != right else set() | |
| def test_protocol_is_exactly_two_matched_single_seed_tasks() -> None: | |
| protocol = e4.load_protocol() | |
| tasks = e4.build_tasks(protocol) | |
| assert tuple(task.task_id for task in tasks) == e4.TASK_IDS | |
| assert tuple(task.gate_intervention for task in tasks) == ( | |
| "stop_gradient", | |
| "channel_derangement", | |
| ) | |
| assert {task.seed for task in tasks} == {0} | |
| assert {task.gate_intervention_seed for task in tasks} == {41041} | |
| assert {task.status for task in tasks} == {"ready_after_smoke"} | |
| assert protocol["policy"]["seed_replication_in_scope"] is False | |
| assert protocol["policy"]["gpus_per_job"] == 8 | |
| assert protocol["policy"]["epochs"] == 300 | |
| assert protocol["policy"]["launchjob_submitted_by_generator"] is False | |
| assert protocol["relationship_to_prior_work"].startswith("Post-hoc") | |
| def test_resolved_configs_have_only_the_registered_paper_baseline_delta() -> None: | |
| protocol = e4.load_protocol() | |
| baseline = e4.load_resolved_config(e4.BASELINE_CONFIG_PATH) | |
| for task in e4.build_tasks(protocol): | |
| resolved = e4.load_resolved_config(ROOT / task.config_path) | |
| assert _different_paths(baseline, resolved) == { | |
| "experiment_id", | |
| "protocol_id", | |
| "model.gate_intervention", | |
| "model.gate_intervention_seed", | |
| } | |
| assert resolved["model"]["gate_intervention"] == task.gate_intervention | |
| assert resolved["model"]["gate_intervention_seed"] == 41041 | |
| assert resolved["model"]["gate_type"] == "relu6_self" | |
| assert resolved["train"]["epochs"] == 300 | |
| e4.validate_task_config(protocol, task, resolved) | |
| def test_wrapper_enforces_e4_namespace_output_identity_and_full_run_lock() -> None: | |
| wrapper = (ROOT / "scripts/run_e4_alignment.sh").read_text(encoding="utf-8") | |
| assert '[[ "${RUN_NAME}" != e4a_* ]]' in wrapper | |
| assert ( | |
| 'EXPECTED_OUTPUT_DIR="/nfs/ywang29/GmNet/runs/e4_alignment/${RUN_NAME}"' | |
| in wrapper | |
| ) | |
| assert '[[ "${OUTPUT_DIR}" != "${EXPECTED_OUTPUT_DIR}" ]]' in wrapper | |
| assert 'LOCK_PATH="${OUTPUT_DIR}/.e4_alignment.lock"' in wrapper | |
| assert "flock -n 9" in wrapper | |
| assert "exit 73" in wrapper | |
| assert 'bash scripts/init_run.sh "$@"' in wrapper | |
| def test_generated_yamls_preserve_template_and_guard_local_staging() -> None: | |
| protocol = e4.load_protocol() | |
| tasks = e4.build_tasks(protocol) | |
| base = yaml.safe_load(BASE_TEMPLATE.read_text(encoding="utf-8")) | |
| expected_verify = "scripts/generate_e4_alignment_deploy.py --verify-task" | |
| outputs: set[str] = set() | |
| job_names: set[str] = set() | |
| for task in tasks: | |
| assert task.deploy_path.is_file(), task.deploy_path | |
| document = yaml.safe_load(task.deploy_path.read_text(encoding="utf-8")) | |
| for key in INVARIANT_KEYS: | |
| assert document[key] == base[key] | |
| pre_run = document["script"]["pre_run_event"] | |
| command = document["script"]["command"] | |
| setup_at = pre_run.index("setup_env.sh") | |
| verify_first_at = pre_run.index(expected_verify) | |
| stage_at = pre_run.index("stage_imagenet.sh full") | |
| verify_second_at = pre_run.index(expected_verify, verify_first_at + 1) | |
| assert setup_at < verify_first_at < stage_at < verify_second_at | |
| assert pre_run.count(expected_verify) == 2 | |
| assert "KEEP_ARCHIVE=0 bash ./scripts/stage_imagenet.sh full" in pre_run | |
| assert command.count(expected_verify) == 1 | |
| assert f"--verify-task {task.task_id} --require-approval" in pre_run | |
| assert f"--verify-task {task.task_id} --require-approval" in command | |
| assert "DATA_ROOT=/tmp/gmnet_data/imagenet-1k" in command | |
| assert "DATA_ROOT=/s3-code" not in command | |
| assert "NPROC_PER_NODE=8" in command | |
| assert "RESUME=auto" in command | |
| assert "POST_EVAL=1" in command | |
| assert f"CODE_MANIFEST_PATH={e4.CODE_MANIFEST_RELATIVE_PATH}" in command | |
| assert "CODE_MANIFEST_PATH=configs/imagenet_v2_code_manifest.json" not in command | |
| assert "bash scripts/run_e4_alignment.sh" in command | |
| assert document["script"]["jobs"] == [{"name": task.job_name}] | |
| outputs.add(str(task.output_dir)) | |
| job_names.add(task.job_name) | |
| assert len(outputs) == len(tasks) | |
| assert len(job_names) == len(tasks) | |
| def test_completed_approvals_bind_frozen_code_manifest_and_smoke_evidence() -> None: | |
| protocol = e4.load_protocol() | |
| tasks = e4.build_tasks(protocol) | |
| manifest = e4.read_code_manifest() | |
| code_sha256 = str(manifest["code_sha256"]) | |
| manifest_sha256 = e4.file_sha256( | |
| e4.JOURNAL_ROOT / e4.CODE_MANIFEST_RELATIVE_PATH | |
| ) | |
| smoke, smoke_sha256 = e4.verify_smoke_evidence( | |
| protocol, tasks, code_sha256, manifest_sha256 | |
| ) | |
| assert e4.CODE_MANIFEST_RELATIVE_PATH != "configs/imagenet_v2_code_manifest.json" | |
| assert smoke["status"] == "passed" | |
| assert smoke["world_size"] == 8 | |
| assert smoke["strict_resume"] is True | |
| for task in tasks: | |
| assert task.approval_path.is_file(), task.approval_path | |
| marker = json.loads(task.approval_path.read_text(encoding="utf-8")) | |
| assert marker == e4.approval_payload( | |
| protocol, | |
| task, | |
| code_sha256, | |
| manifest_sha256, | |
| smoke, | |
| smoke_sha256, | |
| ) | |
| assert marker["approval_basis"] == "passed_8gpu_strict_resume_smoke" | |
| assert marker["code_sha256"] == code_sha256 | |
| assert marker["code_manifest_sha256"] == manifest_sha256 | |
| assert marker["smoke_evidence_sha256"] == smoke_sha256 | |
| e4.verify_approval( | |
| protocol, | |
| task, | |
| code_sha256, | |
| manifest_sha256, | |
| smoke, | |
| smoke_sha256, | |
| ) | |
| def test_old_manifest_and_completed_followup_seal_remain_immutable() -> None: | |
| assert e4.file_sha256(OLD_MANIFEST) == OLD_MANIFEST_FILE_SHA256 | |
| old_manifest = json.loads(OLD_MANIFEST.read_text(encoding="utf-8")) | |
| assert old_manifest["code_sha256"] == OLD_CODE_SHA256 | |
| assert e4.file_sha256(COMPLETION_SEAL) == COMPLETION_SEAL_SHA256 | |
| seal = json.loads(COMPLETION_SEAL.read_text(encoding="utf-8")) | |
| historical = seal["historical_code_manifest"] | |
| assert seal["status"] == "completed_4_of_4" | |
| assert historical["path"] == str(OLD_MANIFEST) | |
| assert historical["file_sha256"] == OLD_MANIFEST_FILE_SHA256 | |
| assert historical["code_sha256"] == OLD_CODE_SHA256 | |
| assert historical["provenance_policy"] == "historical_immutable_do_not_regenerate" | |
| def test_generated_batch_manifest_is_complete_and_does_not_claim_submission() -> None: | |
| path = e4.BATCH_ROOT / "batch_manifest.json" | |
| assert path.is_file(), path | |
| manifest = json.loads(path.read_text(encoding="utf-8")) | |
| assert manifest["summary"] == { | |
| "task_count": 2, | |
| "approved_count": 2, | |
| "seed_count": 1, | |
| } | |
| assert [task["task_id"] for task in manifest["tasks"]] == list(e4.TASK_IDS) | |
| assert manifest["code_manifest_path"] == e4.CODE_MANIFEST_RELATIVE_PATH | |
| assert manifest["smoke_evidence_path"] == str(e4.SMOKE_EVIDENCE_PATH) | |
| assert manifest["policy"]["launchjob_submitted_by_generator"] is False | |