danym commited on
Commit
6fc73df
·
verified ·
1 Parent(s): 2a360d9

Upload mi300x_9b/rebuild_vllm_compat_final.py with huggingface_hub

Browse files
mi300x_9b/rebuild_vllm_compat_final.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Rebuild vllm_compat from the final step-596 checkpoint.
3
+
4
+ Strategy: clone the existing (working) step-150 vllm_compat dir (which has
5
+ correct config.json with rtpurbo block, tokenizer, probe, and real safetensors
6
+ copies of the base model), then swap in the new indexer weights and update
7
+ the index.json weight_map. Keeps config architecture as ForConditionalGeneration.
8
+ """
9
+ import json
10
+ import shutil
11
+ import sys
12
+ from pathlib import Path
13
+ import torch
14
+ from safetensors.torch import save_file
15
+
16
+ OLD = Path("/home/hotaisle/work/checkpoints/mi300x_9b_131k_step150/vllm_compat")
17
+ NEW = Path("/home/hotaisle/work/checkpoints/mi300x_9b_131k/vllm_compat")
18
+ SD = Path("/home/hotaisle/work/checkpoints/mi300x_9b_131k/state_dict.pt")
19
+ PROBE = Path("/home/hotaisle/work/probe_results/mi300x_9b_allretr.json")
20
+ IDX_FNAME = "model-rtpurbo-indexers-of-1.safetensors"
21
+
22
+ print(f"[rebuild] cloning {OLD} -> {NEW}", file=sys.stderr)
23
+ if NEW.exists():
24
+ shutil.rmtree(NEW)
25
+ shutil.copytree(OLD, NEW)
26
+ print(f"[rebuild] cloned (preserving config/tokenizer/base safetensors)", file=sys.stderr)
27
+
28
+ # 1. Load final state_dict, extract indexer tensors
29
+ print(f"[rebuild] loading {SD}", file=sys.stderr)
30
+ sd = torch.load(str(SD), map_location="cpu", weights_only=True)
31
+ idx_tensors = {}
32
+ for k, v in sd.items():
33
+ if torch.is_tensor(v) and ".indexers." in k:
34
+ idx_tensors[k] = v.contiguous()
35
+ print(f"[rebuild] {len(idx_tensors)} indexer tensors", file=sys.stderr)
36
+
37
+ # 2. Save new indexer safetensors (overwrite the old one)
38
+ idx_path = NEW / IDX_FNAME
39
+ save_file(idx_tensors, str(idx_path), metadata={"format": "pt"})
40
+ size_kb = idx_path.stat().st_size / 1024
41
+ print(f"[rebuild] wrote {IDX_FNAME} ({size_kb:.0f} KB)", file=sys.stderr)
42
+
43
+ # 3. Update weight_map in model.safetensors.index.json (point indexer keys
44
+ # at the indexer file — should already be there, but rewrite to be safe)
45
+ wm_path = NEW / "model.safetensors.index.json"
46
+ index_data = json.loads(wm_path.read_text())
47
+ for k in idx_tensors:
48
+ index_data["weight_map"][k] = IDX_FNAME
49
+ wm_path.write_text(json.dumps(index_data, indent=2))
50
+ print(f"[rebuild] updated model.safetensors.index.json ({len(idx_tensors)} indexer keys -> {IDX_FNAME})", file=sys.stderr)
51
+
52
+ # 4. Refresh probe (copy latest)
53
+ shutil.copy2(str(PROBE), str(NEW / "rtpurbo_probe.json"))
54
+ print(f"[rebuild] refreshed rtpurbo_probe.json", file=sys.stderr)
55
+
56
+ # 5. Verify architecture in config.json
57
+ cfg = json.loads((NEW / "config.json").read_text())
58
+ arch = cfg.get("architectures", [])
59
+ print(f"[rebuild] config architectures: {arch}", file=sys.stderr)
60
+ if "Qwen3_5RTPurboForConditionalGeneration" not in arch:
61
+ cfg["architectures"] = ["Qwen3_5RTPurboForConditionalGeneration"]
62
+ (NEW / "config.json").write_text(json.dumps(cfg, indent=2))
63
+ print(f"[rebuild] FIXED architecture -> ForConditionalGeneration", file=sys.stderr)
64
+ else:
65
+ print(f"[rebuild] architecture OK (ForConditionalGeneration)", file=sys.stderr)
66
+
67
+ # 6. Drop a marker file recording the build provenance
68
+ marker = {
69
+ "checkpoint_step": 596,
70
+ "source_state_dict": str(SD),
71
+ "cloned_from": str(OLD),
72
+ "final_mean_kl": 2.3574943174134333,
73
+ "n_indexer_tensors": len(idx_tensors),
74
+ "probe": str(PROBE),
75
+ }
76
+ (NEW / "build_provenance.json").write_text(json.dumps(marker, indent=2))
77
+ print(f"[rebuild] wrote build_provenance.json", file=sys.stderr)
78
+
79
+ print(f"\n[rebuild] DONE -> {NEW}", file=sys.stderr)
80
+ print(f"[rebuild] contents:", file=sys.stderr)
81
+ for f in sorted(NEW.iterdir()):
82
+ if f.is_symlink():
83
+ print(f" {f.name} -> {f.resolve()} ({f.stat().st_size/1e6:.1f} MB)", file=sys.stderr)
84
+ else:
85
+ print(f" {f.name} ({f.stat().st_size} bytes)", file=sys.stderr)