File size: 2,430 Bytes
bbd7f1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io, json, os, zipfile, resource
import numpy as np, torch
from safetensors import safe_open
from torch.distributed.checkpoint import FileSystemReader
CKPT="/projects/u6uv/ziyan/models/megatron/Qwen3.8-27B_torch_dist/release"
HF="/projects/u6uv/ziyan/models/Qwen/Qwen3.8-27B"
idx=json.load(open(os.path.join(HF,"model.safetensors.index.json")))["weight_map"]
def rssG(): return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1e6

class Slice(io.RawIOBase):
    def __init__(s,p,o,l): s.f=open(p,'rb'); s.o=o; s.l=l; s.p=0
    def readable(s): return True
    def seekable(s): return True
    def seek(s,off,wh=0):
        s.p = off if wh==0 else (s.p+off if wh==1 else s.l+off); return s.p
    def tell(s): return s.p
    def read(s,n=-1):
        if n is None or n<0: n=s.l-s.p
        n=min(n,s.l-s.p); s.f.seek(s.o+s.p); b=s.f.read(n); s.p+=len(b); return b
    def readinto(s,b):
        d=s.read(len(b)); b[:len(d)]=d; return len(d)

md=FileSystemReader(CKPT).read_metadata()
targets={"embedding.word_embeddings.weight":"model.language_model.embed_tokens.weight",
         "output_layer.weight":"lm_head.weight"}
si={}
for k,v in md.storage_data.items():
    if getattr(k,"fqn",None) in targets: si[k.fqn]=v

for mn,hn in targets.items():
    info=si[mn]
    zf=zipfile.ZipFile(io.BufferedReader(Slice(os.path.join(CKPT,info.relative_path),info.offset,info.length)))
    names=[n for n in zf.namelist() if "/data/" in n or n.endswith("data/0")]
    dataname=[n for n in zf.namelist() if n.split("/")[-1].isdigit()]
    print(mn, "zip entries:", [(n, zf.getinfo(n).file_size) for n in zf.namelist()][:6], flush=True)
    dn=max(dataname, key=lambda n: zf.getinfo(n).file_size)
    total=zf.getinfo(dn).file_size
    rows, cols = 248320, 5120
    assert total==rows*cols*2, (total, rows*cols*2)
    stream=zf.open(dn)
    with safe_open(os.path.join(HF, idx[hn]), framework="pt", device="cpu") as g:
        sl=g.get_slice(hn)
        nb=0; done=0; CH=4096
        while done<rows:
            n=min(CH, rows-done)
            raw=stream.read(n*cols*2)
            a=torch.from_numpy(np.frombuffer(raw, dtype=np.int16).copy()).reshape(n,cols)
            ref=sl[done:done+n].contiguous().view(torch.int16)
            nb+=int((a!=ref).sum())
            done+=n; del raw,a,ref
    print(f"  {mn} -> {hn}: in_hf={hn in idx} rows={rows} nbitdiff={nb} maxRSS_GB={rssG():.2f}", flush=True)
    stream.close(); zf.close()