#!/usr/bin/env python3 """patch-yarn.py on synthetic fixtures: both config shapes (text_config nested like the targets, root-level like the DFlash2 draft), key preservation, idempotence, backup creation, exact-revision selection, and the native-mode restore path (--restore / --check). No network.""" import json import os import shutil import subprocess import sys import tempfile REPO_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SCRIPT = os.path.join(REPO_DIR, "patch-yarn.py") def make_fixture(base: str, repo: str, sha: str, nested: bool) -> str: d = f"{base}/hub/models--{repo.replace('/', '--')}/snapshots/{sha}" os.makedirs(d, exist_ok=True) inner = {"max_position_embeddings": 262144, "rope_parameters": {"rope_theta": 10000000, "partial_rotary_factor": 0.25}} cfg = {"architectures": ["X"], "text_config": inner} if nested else dict(inner) with open(f"{d}/config.json", "w") as f: json.dump(cfg, f) return f"{d}/config.json" def run(base: str, repo: str, rev: str) -> str: r = subprocess.run([sys.executable, SCRIPT, base, repo, rev], capture_output=True, text=True) assert r.returncode == 0, r.stdout + r.stderr return r.stdout def run_mode(mode: str, base: str, repo: str, rev: str): return subprocess.run([sys.executable, SCRIPT, mode, base, repo, rev], capture_output=True, text=True) def main() -> None: base = tempfile.mkdtemp(prefix="yarn-fixture-") try: sha = "a" * 40 for repo, nested in (("org/target", True), ("org/draft", False)): path = make_fixture(base, repo, sha, nested) out1 = run(base, repo, sha) assert "applied to" in out1, out1 out2 = run(base, repo, sha) assert "already applied" in out2, out2 c = json.load(open(path)) tc = c.get("text_config", c) assert tc["max_position_embeddings"] == 1010000 rp = tc["rope_parameters"] assert rp["rope_type"] == "yarn" and rp["factor"] == 4.0 assert rp["original_max_position_embeddings"] == 262144 assert rp["rope_theta"] == 10000000, "existing rope keys must be preserved" assert rp["partial_rotary_factor"] == 0.25 if nested: assert c["architectures"] == ["X"], "non-rope root keys must be preserved" backup = json.load(open(path + ".pre-yarn")) btc = backup.get("text_config", backup) assert btc["max_position_embeddings"] == 262144, "backup must be the original" # Revision selection: with two snapshots, the requested sha wins even # when another snapshot is newer. sha2 = "b" * 40 p2 = make_fixture(base, "org/target", sha2, nested=True) os.utime(p2, None) # newest mtime out = run(base, "org/target", sha) assert "already applied" in out and sha in out, out c2 = json.load(open(p2)) assert c2["text_config"]["max_position_embeddings"] == 262144, \ "the other snapshot must be untouched" # --restore brings back the original and consumes the backup: a box # coming home from 1m serves the pristine config again. Both shapes. for repo, nested in (("org/target", True), ("org/draft", False)): path = (f"{base}/hub/models--{repo.replace('/', '--')}" f"/snapshots/{sha}/config.json") r = run_mode("--restore", base, repo, sha) assert r.returncode == 0, r.stdout + r.stderr assert "restored" in r.stdout, r.stdout c = json.load(open(path)) tc = c.get("text_config", c) assert tc["max_position_embeddings"] == 262144 assert tc["rope_parameters"] == {"rope_theta": 10000000, "partial_rotary_factor": 0.25}, \ "restore must bring back the exact original rope block" assert not os.path.exists(path + ".pre-yarn"), \ "the backup is consumed by the restore" # Restoring twice is a no-op, and checking a clean tree is silent. digest_clean = open(path, "rb").read() r = run_mode("--restore", base, repo, sha) assert r.returncode == 0 and "not patched" in r.stdout, \ r.stdout + r.stderr assert open(path, "rb").read() == digest_clean, \ "a no-op restore must not touch the file" r = run_mode("--check", base, repo, sha) assert r.returncode == 0, r.stdout + r.stderr # --check never mutates: a patched tree reports 2, and the file is # byte-identical afterwards. path = (f"{base}/hub/models--org--target/snapshots/{sha}/config.json") run(base, "org/target", sha) digest_before = open(path, "rb").read() r = run_mode("--check", base, "org/target", sha) assert r.returncode == 2, f"{r.returncode} {r.stdout} {r.stderr}" assert "restorable" in r.stdout, r.stdout assert open(path, "rb").read() == digest_before, "--check must not mutate" # Patched with no backup (hand-patched cache, or a pre-backup era # install): restore and check refuse with a re-download fix-it. os.remove(path + ".pre-yarn") r = run_mode("--restore", base, "org/target", sha) assert r.returncode == 3, f"{r.returncode} {r.stdout} {r.stderr}" assert "re-download" in r.stderr, r.stderr r = run_mode("--check", base, "org/target", sha) assert r.returncode == 3, f"{r.returncode} {r.stdout} {r.stderr}" # --restore honors the exact revision like the patch does. p2 = make_fixture(base, "org/pick", "c" * 40, nested=True) run(base, "org/pick", "c" * 40) make_fixture(base, "org/pick", "d" * 40, nested=True) r = run_mode("--restore", base, "org/pick", "c" * 40) assert r.returncode == 0 and ("c" * 40)[:12] in r.stdout, r.stdout assert json.load(open(p2))["text_config"]["max_position_embeddings"] == 262144 print("test_patch_yarn: OK") finally: shutil.rmtree(base, ignore_errors=True) if __name__ == "__main__": main()