File size: 9,535 Bytes
fdc6474
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python3
"""Re-tier a two-tier checkpoint per a REAP assignment (phase 1.5).

Per hybrid layer of TARGET:
  hyb_kind      <- REAP assignment (0=hot NVFP4, 2=cold AQLM)
  nvfp4_*       <- retained-hot experts sliced from TARGET's own arrays;
                   promoted experts from the NVFP4 teacher regions
                   (/tmp/glm52-hot-dl2 + /tmp/glm52-hot-dl fallback)
  w13_*/w2c_*   <- sliced from /data/glm52-aqlm-conv15 (covers the union
                   of all budgets' cold sets)
  w2m_*         <- kept (empty)
Non-expert tensors verbatim. Writes TARGET-reap; caller gates + swaps.

Usage: build_checkpoint_v8.py TARGET ASSIGNMENT_JSON
"""
import json
import os
import re
import shutil
import sys

import torch
from safetensors import safe_open
from safetensors.torch import save_file

TARGET = sys.argv[1].rstrip("/")
ASSIGN = sys.argv[2]
PARTS = "/data/glm52-aqlm-conv15"
DLS = ["/tmp/glm52-hot-dl2", "/tmp/glm52-hot-dl"]
DST = TARGET + "-reap"
SHARD_BYTES = 4 << 30
N_EXP, INTER, HIDDEN = 256, 2048, 6144

assignment = {int(k): v for k, v in json.load(open(ASSIGN)).items()}

DT = {"U8": torch.uint8, "F8_E4M3": torch.uint8, "BF16": torch.bfloat16,
      "F32": torch.float32}


class Regions:
    def __init__(self):
        self.sources = []
        for dl in DLS:
            if not os.path.exists(f"{dl}/headers.json"):
                continue
            headers = json.load(open(f"{dl}/headers.json"))
            wm = {n: s for s, h in headers.items()
                  for n in h["header"] if n != "__metadata__"}
            regions = {}
            for shard in headers:
                d = f"{dl}/regions/{shard}"
                regs = []
                if os.path.isdir(d):
                    for f in os.listdir(d):
                        p = os.path.join(d, f)
                        regs.append((int(f[:-4]), p, os.path.getsize(p)))
                regions[shard] = sorted(regs)
            self.sources.append((headers, wm, regions))

    def get(self, name):
        # ranged regions first (header presence != bytes present, so only
        # a byte-coverage hit counts) ...
        for headers, wm, regions in self.sources:
            if name not in wm:
                continue
            shard = wm[name]
            info = headers[shard]["header"][name]
            b, e = info["data_offsets"]
            for rb, path, sz in regions[shard]:
                if rb <= b and e <= rb + sz:
                    with open(path, "rb") as fh:
                        fh.seek(b - rb)
                        buf = fh.read(e - b)
                    return torch.frombuffer(
                        bytearray(buf), dtype=DT[info["dtype"]]
                    ).reshape(info["shape"])
        # ... then the old layer-wise checkpoint (whole-NVFP4 layers).
        if not hasattr(self, "_old_wm"):
            oidx = json.load(open(
                "/data/glm52-old-layerwise/model.safetensors.index.json"))
            self._old_wm = oidx["weight_map"]
            self._old_open = {}
        if name in self._old_wm:
            sh = self._old_wm[name]
            if sh not in self._old_open:
                self._old_open[sh] = safe_open(
                    f"/data/glm52-old-layerwise/{sh}", framework="pt")
            return self._old_open[sh].get_tensor(name)
        raise KeyError(name)


idx = json.load(open(f"{TARGET}/model.safetensors.index.json"))
wm = idx["weight_map"]
opened = {}


def get(name):
    s = wm[name]
    if s not in opened:
        opened[s] = safe_open(f"{TARGET}/{s}", framework="pt")
    return opened[s].get_tensor(name)


class Writer:
    def __init__(self):
        os.makedirs(DST, exist_ok=True)
        self.cur, self.cur_bytes, self.n, self.total = {}, 0, 0, 0
        self.weight_map, self.files = {}, []

    def add(self, name, t):
        nb = t.numel() * t.element_size()
        if self.cur_bytes + nb > SHARD_BYTES and self.cur:
            self.flush()
        self.cur[name] = t
        self.cur_bytes += nb
        self.total += nb

    def flush(self):
        if not self.cur:
            return
        self.n += 1
        f = f"model-{self.n:05d}.safetensors"
        save_file(self.cur, f"{DST}/{f}")
        for k in self.cur:
            self.weight_map[k] = f
        self.files.append(f)
        self.cur, self.cur_bytes = {}, 0

    def finalize(self):
        self.flush()
        out = {}
        for i, f in enumerate(self.files, 1):
            new = f"model-{i:05d}-of-{self.n:05d}.safetensors"
            os.rename(f"{DST}/{f}", f"{DST}/{new}")
            for k, v in self.weight_map.items():
                if v == f:
                    out[k] = new
        json.dump({"metadata": {"total_size": self.total},
                   "weight_map": out},
                  open(f"{DST}/model.safetensors.index.json", "w"), indent=0)
        print(f"index: {len(out)} tensors, {self.total/1e9:.1f} GB")


def layer_tensors(li, rr):
    p = f"model.layers.{li}.mlp.experts"
    hot = sorted(assignment[li]["hot"])
    cold = sorted(assignment[li]["cold"])
    assert len(hot) + len(cold) == N_EXP

    kind_old = get(f"{p}.hyb_kind")
    old_hot = (kind_old == 0).nonzero().flatten().tolist()
    pos = {e: j for j, e in enumerate(old_hot)}
    olds = {n: get(f"{p}.{n}") for n in
            ("nvfp4_w13_packed", "nvfp4_w13_bscale", "nvfp4_w13_scale2",
             "nvfp4_w2_packed", "nvfp4_w2_bscale", "nvfp4_w2_scale2")}

    na = len(hot)
    w13p = torch.empty(na, 2*INTER, HIDDEN//2, dtype=torch.uint8)
    w13b = torch.empty(na, 2*INTER, HIDDEN//16, dtype=torch.uint8)
    w13s = torch.empty(na, 2, dtype=torch.float32)
    w2p = torch.empty(na, HIDDEN, INTER//2, dtype=torch.uint8)
    w2b = torch.empty(na, HIDDEN, INTER//16, dtype=torch.uint8)
    w2s = torch.empty(na, 1, dtype=torch.float32)
    n_promoted = 0
    for j, e in enumerate(hot):
        if e in pos:
            k = pos[e]
            w13p[j] = olds["nvfp4_w13_packed"][k]
            w13b[j] = olds["nvfp4_w13_bscale"][k]
            w13s[j] = olds["nvfp4_w13_scale2"][k]
            w2p[j] = olds["nvfp4_w2_packed"][k]
            w2b[j] = olds["nvfp4_w2_bscale"][k]
            w2s[j] = olds["nvfp4_w2_scale2"][k]
        else:
            n_promoted += 1
            ep = f"model.layers.{li}.mlp.experts.{e}"
            w13p[j, :INTER] = rr.get(f"{ep}.gate_proj.weight")
            w13p[j, INTER:] = rr.get(f"{ep}.up_proj.weight")
            w2p[j] = rr.get(f"{ep}.down_proj.weight")
            w13b[j, :INTER] = rr.get(
                f"{ep}.gate_proj.weight_scale").view(torch.uint8)
            w13b[j, INTER:] = rr.get(
                f"{ep}.up_proj.weight_scale").view(torch.uint8)
            w2b[j] = rr.get(f"{ep}.down_proj.weight_scale").view(torch.uint8)
            w13s[j, 0] = rr.get(f"{ep}.gate_proj.weight_scale_2").float()
            w13s[j, 1] = rr.get(f"{ep}.up_proj.weight_scale_2").float()
            w2s[j, 0] = rr.get(f"{ep}.down_proj.weight_scale_2").float()

    part = torch.load(f"{PARTS}/layer_{li}.pt", map_location="cpu",
                      weights_only=True)
    ppos = {int(e): j for j, e in enumerate(part["expert_ids"].tolist())}
    missing = [e for e in cold if e not in ppos]
    assert not missing, f"L{li}: conv15 missing {missing[:5]}"
    sel = torch.tensor([ppos[e] for e in cold], dtype=torch.long)

    kind = torch.full((N_EXP,), 2, dtype=torch.int8)
    for e in hot:
        kind[e] = 0
    print(f"L{li}: hot={na} ({n_promoted} promoted) cold={len(cold)}",
          flush=True)
    return {
        "hyb_kind": kind,
        "w13_codes": part["w13_codes"][sel].contiguous(),
        "w13_codebooks": part["w13_codebooks"].clone(),
        "w13_scales": part["w13_scales"][sel].contiguous(),
        "w2c_codes": part["w2c_codes"][sel].contiguous(),
        "w2c_codebooks": part["w2c_codebooks"].clone(),
        "w2c_scales": part["w2c_scales"][sel].contiguous(),
        "nvfp4_w13_packed": w13p, "nvfp4_w13_bscale": w13b,
        "nvfp4_w13_scale2": w13s, "nvfp4_w2_packed": w2p,
        "nvfp4_w2_bscale": w2b, "nvfp4_w2_scale2": w2s,
    }, {"n_nvfp4": na, "n_base": 0, "n_cold": len(cold)}


REPL = ("hyb_kind", "w13_codes", "w13_codebooks", "w13_scales",
        "w2c_codes", "w2c_codebooks", "w2c_scales", "nvfp4_w13_packed",
        "nvfp4_w13_bscale", "nvfp4_w13_scale2", "nvfp4_w2_packed",
        "nvfp4_w2_bscale", "nvfp4_w2_scale2")
pat = re.compile(r"model\.layers\.(\d+)\.mlp\.experts\.("
                 + "|".join(REPL) + r")$")

rr = Regions()
w = Writer()
cache_li, cache = None, None
books = {}
for shard in sorted(set(wm.values())):
    with safe_open(f"{TARGET}/{shard}", framework="pt") as f:
        for name in f.keys():
            m = pat.match(name)
            if m and int(m.group(1)) in assignment:
                li = int(m.group(1))
                if cache_li != li:
                    cache, b = layer_tensors(li, rr)
                    books[str(li)] = b
                    cache_li = li
                w.add(name, cache[m.group(2)])
            else:
                w.add(name, f.get_tensor(name))
w.finalize()

cfg = json.load(open(f"{TARGET}/config.json"))
cfg["quantization_config"]["aqlm_layer_books"] = books
json.dump(cfg, open(f"{DST}/config.json", "w"), indent=2)
for f in os.listdir(TARGET):
    if (f.endswith(".json") and f not in ("config.json", "model.safetensors.index.json")
            or f.endswith((".txt", ".jinja", ".py", ".md", ".sh"))):
        shutil.copy2(f"{TARGET}/{f}", f"{DST}/{f}")
print("DONE:", DST)