Repository navigation
Expand file tree
/
Copy pathdownload_h3.py
More file actions
83 lines (70 loc) · 3.61 KB
/
Copy pathdownload_h3.py
File metadata and controls
83 lines (70 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Download and verify the pinned public MiniMax-H3 checkpoint components."""
import argparse
import hashlib
import json
from pathlib import Path
MANIFEST = Path(__file__).resolve().parent / "configs" / "h3_checkpoint.json"
def sha256(path):
digest = hashlib.sha256()
with path.open("rb") as stream:
for chunk in iter(lambda: stream.read(8 * 1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def localize_index(source, output, spec):
if sha256(source) != spec["modular_index_sha256"]:
raise ValueError("The upstream modular index differs from the pinned snapshot.")
config = json.loads(source.read_text())
for entry in config.values():
if isinstance(entry, list) and len(entry) == 3 and isinstance(entry[2], dict):
if entry[2]["pretrained_model_name_or_path"] != spec["repo"]:
raise ValueError("Unexpected external component in the model index.")
entry[2]["pretrained_model_name_or_path"] = str(output.resolve())
target = output / "modular_model_index.json"
target.write_text(json.dumps(config, indent=2, sort_keys=True) + "\n")
def verify_index(output, spec):
config = json.loads((output / "modular_model_index.json").read_text())
for entry in config.values():
if isinstance(entry, list) and len(entry) == 3 and isinstance(entry[2], dict):
path = entry[2]["pretrained_model_name_or_path"]
if Path(path).resolve() != output.resolve():
raise ValueError("A model component is not bound to this local checkpoint.")
entry[2]["pretrained_model_name_or_path"] = spec["repo"]
digest = hashlib.sha256(json.dumps(config, sort_keys=True, separators=(",", ":")).encode()).hexdigest()
if digest != spec["normalized_index_sha256"]:
raise ValueError("The localized model index changes more than component paths.")
def verify_files(output, spec):
verify_index(output, spec)
for name, expected in spec["files_sha256"].items():
if sha256(output / name) != expected:
raise ValueError(f"Checkpoint file differs from the pinned manifest: {name}")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output", type=Path, default=Path("models/MiniMax-H3-ref2va"))
parser.add_argument("--verify", action="store_true", help="Hash all downloaded files; reads about 210 GB")
parser.add_argument("--verify-only", action="store_true", help="Check an existing local checkpoint, without downloads")
args = parser.parse_args()
output = args.output.expanduser().resolve()
spec = json.loads(MANIFEST.read_text())
if args.verify_only:
verify_files(output, spec)
print("All checkpoint hashes match the pinned manifest.")
return
from huggingface_hub import hf_hub_download, snapshot_download
output.mkdir(parents=True, exist_ok=True)
snapshot_download(
spec["repo"], revision=spec["revision"], local_dir=output, token=False,
allow_patterns=[*spec["files_sha256"], "LICENSE"],
)
# Read the immutable cached upstream index, never an already localized copy.
source = Path(hf_hub_download(spec["repo"], "modular_model_index.json",
revision=spec["revision"], token=False))
localize_index(source, output, spec)
verify_index(output, spec)
if args.verify:
verify_files(output, spec)
print("All checkpoint hashes match the pinned manifest.")
else:
print("Pinned checkpoint downloaded; use --verify-only for full content hashing.")
print(output)
if __name__ == "__main__":
main()