coderofpears commited on
Commit
a3d4e75
·
verified ·
1 Parent(s): ebab781

Upload scripts/verify_stage4_checkpoint.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. scripts/verify_stage4_checkpoint.py +161 -0
scripts/verify_stage4_checkpoint.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Independently verify a Pear1.5 stage checkpoint and its completion receipt.
2
+
3
+ Checks are purely mechanical: deterministic READY marker, manifest fields, payload
4
+ SHA-256/size, reloaded step/token counters, data cursor, model/implementation
5
+ identity, lineage hash and campaign arithmetic.
6
+
7
+ This script does **not** run a benchmark and does **not** produce or claim an
8
+ official Artificial Analysis score. A benchmark is a separate, explicitly labeled
9
+ record.
10
+ """
11
+ import argparse
12
+ import hashlib
13
+ import json
14
+ import re
15
+ from pathlib import Path
16
+
17
+ FORMAT = "pear15.cloud_checkpoint.v1"
18
+ DIRECTORY = re.compile(r"step_([0-9]{12})_([0-9a-f]{32})\Z")
19
+ FIELDS = {"format", "file", "sha256", "bytes", "step", "created_ns", "id"}
20
+ DIGEST = re.compile(r"[0-9a-f]{64}\Z")
21
+
22
+
23
+ def sha256_file(path, chunk=1 << 20):
24
+ digest = hashlib.sha256()
25
+ with Path(path).open("rb") as stream:
26
+ while True:
27
+ block = stream.read(chunk)
28
+ if not block:
29
+ return digest.hexdigest()
30
+ digest.update(block)
31
+
32
+
33
+ def canonical_hash(value):
34
+ return hashlib.sha256(
35
+ json.dumps(value, sort_keys=True, separators=(",", ":")).encode()
36
+ ).hexdigest()
37
+
38
+
39
+ def main():
40
+ parser = argparse.ArgumentParser(description=__doc__)
41
+ parser.add_argument("--dir", type=Path, required=True, help="local checkpoint directory")
42
+ parser.add_argument("--receipt", type=Path, required=True, help="stage completion receipt")
43
+ parser.add_argument("--parent-sha", required=True)
44
+ parser.add_argument("--parent-step", type=int, required=True)
45
+ parser.add_argument("--parent-tokens", type=int, required=True)
46
+ parser.add_argument("--data-fp", required=True)
47
+ parser.add_argument("--runner-sha", required=True)
48
+ parser.add_argument("--implementation-fp", required=True)
49
+ parser.add_argument("--tokens-per-step", type=int, default=16384)
50
+ parser.add_argument("--target", type=int, default=50_000_000_000)
51
+ parser.add_argument("--out", type=Path)
52
+ args = parser.parse_args()
53
+
54
+ checks = {}
55
+ errors = []
56
+
57
+ def check(name, condition, detail=None):
58
+ ok = bool(condition)
59
+ checks[name] = ok if detail is None else {"ok": ok, "detail": detail}
60
+ if not ok:
61
+ errors.append(name)
62
+
63
+ directory = args.dir
64
+ match = DIRECTORY.fullmatch(directory.name)
65
+ check("directory_name", match is not None, directory.name)
66
+
67
+ manifest_raw = (directory / "manifest.json").read_bytes()
68
+ ready = (directory / "READY").read_bytes()
69
+ expected_ready = (FORMAT + "\n" + hashlib.sha256(manifest_raw).hexdigest() + "\n").encode("ascii")
70
+ check("ready_marker", ready == expected_ready, {"bytes": len(ready)})
71
+
72
+ manifest = json.loads(manifest_raw)
73
+ check("manifest_fields", isinstance(manifest, dict) and set(manifest) == FIELDS)
74
+ check("manifest_format", manifest.get("format") == FORMAT and manifest.get("file") == "checkpoint.pt")
75
+ if match:
76
+ check("manifest_step", manifest.get("step") == int(match.group(1)))
77
+ check("manifest_id", manifest.get("id") == match.group(2))
78
+ check("manifest_bytes", type(manifest.get("bytes")) is int and manifest["bytes"] > 0)
79
+ check("manifest_sha_format", isinstance(manifest.get("sha256"), str) and DIGEST.fullmatch(manifest["sha256"]) is not None)
80
+ check("manifest_created_ns", type(manifest.get("created_ns")) is int and manifest["created_ns"] >= 0)
81
+
82
+ payload_path = directory / "checkpoint.pt"
83
+ size = payload_path.stat().st_size
84
+ check("payload_size", size == manifest.get("bytes"), {"actual": size, "manifest": manifest.get("bytes")})
85
+ actual_sha = sha256_file(payload_path)
86
+ check("payload_sha256", actual_sha == manifest.get("sha256"), {"actual": actual_sha})
87
+
88
+ import torch
89
+
90
+ payload = torch.load(payload_path, map_location="cpu", weights_only=True)
91
+ check("payload_type", isinstance(payload, dict))
92
+ check("payload_step", payload.get("step") == manifest.get("step"), {"payload": payload.get("step")})
93
+ token_count = payload.get("token_count")
94
+ check("token_count_integer", type(token_count) is int and token_count >= 0, token_count)
95
+ if type(token_count) is int:
96
+ check("token_count_arithmetic", token_count == manifest["step"] * args.tokens_per_step,
97
+ {"token_count": token_count, "step": manifest["step"], "per_step": args.tokens_per_step})
98
+ cursor = payload.get("data_cursor") or {}
99
+ check("data_cursor_offset", cursor.get("offset_tokens") == token_count, cursor)
100
+ check("data_cursor_epoch", cursor.get("epoch") == 0, cursor)
101
+
102
+ identity = payload.get("identity") or {}
103
+ check("implementation_fingerprint", identity.get("implementation_fingerprint") == args.implementation_fp)
104
+ check("dataset_fingerprint", identity.get("dataset_fingerprint") == args.data_fp)
105
+ check("model_fingerprint_present", isinstance(identity.get("model_fingerprint"), str) and DIGEST.fullmatch(identity["model_fingerprint"]) is not None)
106
+ config = payload.get("model_config") or {}
107
+ check("model_config", config.get("model_type") == "llama" and config.get("vocab_size") == 49152
108
+ and config.get("hidden_size") == 576 and config.get("num_hidden_layers") == 30
109
+ and config.get("num_attention_heads") == 9 and config.get("num_key_value_heads") == 3,
110
+ {k: config.get(k) for k in ("model_type", "vocab_size", "hidden_size", "num_hidden_layers", "num_attention_heads", "num_key_value_heads")})
111
+
112
+ receipt = json.loads(Path(args.receipt).read_text())
113
+ check("receipt_format", receipt.get("format") == "pear15-stage-session-completion-v1")
114
+ lineage = receipt.get("lineage") or {}
115
+ check("receipt_lineage_hash", receipt.get("lineage_sha256") == canonical_hash(lineage))
116
+ check("lineage_parent_sha", lineage.get("parent_checkpoint_sha256") == args.parent_sha)
117
+ check("lineage_parent_step", lineage.get("parent_step") == args.parent_step)
118
+ check("lineage_parent_tokens", lineage.get("parent_campaign_token_count") == args.parent_tokens)
119
+ check("lineage_dataset", lineage.get("dataset_fingerprint") == args.data_fp)
120
+ check("lineage_runner", lineage.get("initializer_source_sha256") == args.runner_sha)
121
+ check("lineage_target", lineage.get("target_campaign_exposures") == args.target)
122
+
123
+ claimed = (receipt.get("checkpoint") or {}).get("manifest") or {}
124
+ check("receipt_manifest_sha", claimed.get("sha256") == manifest["sha256"])
125
+ check("receipt_manifest_bytes", claimed.get("bytes") == manifest["bytes"])
126
+ check("receipt_manifest_step", claimed.get("step") == manifest["step"])
127
+
128
+ summary = receipt.get("summary") or {}
129
+ check("receipt_summary_step", summary.get("step") == payload.get("step"))
130
+ check("receipt_summary_tokens", summary.get("token_count") == token_count)
131
+ check("receipt_session_completed", receipt.get("training_session_completed") is True)
132
+ campaign = args.parent_tokens + token_count if type(token_count) is int else None
133
+ check("campaign_counter", receipt.get("campaign_token_count") == campaign,
134
+ {"receipt": receipt.get("campaign_token_count"), "computed": campaign})
135
+ check("remaining_counter", receipt.get("remaining_campaign_tokens") == max(0, args.target - campaign)
136
+ if campaign is not None else False)
137
+
138
+ result = {
139
+ "format": "pear15-stage-checkpoint-verification-v1",
140
+ "verified": not errors,
141
+ "errors": errors,
142
+ "checkpoint_dir": str(directory),
143
+ "step": payload.get("step"),
144
+ "token_count": token_count,
145
+ "campaign_token_count": campaign,
146
+ "remaining_campaign_tokens": max(0, args.target - campaign) if campaign is not None else None,
147
+ "manifest_sha256": manifest.get("sha256"),
148
+ "payload_sha256": actual_sha,
149
+ "lineage_sha256": receipt.get("lineage_sha256"),
150
+ "checks": checks,
151
+ "note": "Mechanical checkpoint/lineage verification only. No benchmark and no official Artificial Analysis score is implied.",
152
+ }
153
+ text = json.dumps(result, indent=2, sort_keys=True)
154
+ print(text)
155
+ if args.out:
156
+ args.out.write_text(text + "\n", encoding="utf-8")
157
+ raise SystemExit(0 if result["verified"] else 1)
158
+
159
+
160
+ if __name__ == "__main__":
161
+ main()