#!/usr/bin/env python3 """Manifest of NVFP4 per-expert tensors for the 2-tier hot sets. Fetches the HF repo's safetensors index + shard headers via range requests and emits (url, offset, length, out) entries for every tensor of every hot expert in the assignment, excluding layers whose NVFP4 bytes are already in the local checkpoint's compacted arrays (LOCAL_LAYERS). """ import json import os import struct import sys import urllib.request from concurrent.futures import ThreadPoolExecutor BASE = "https://huggingface.co/lukealonso/GLM-5.2-NVFP4/resolve/main" DL = "/tmp/glm52-hot-dl2" ASSIGN = "/data/glm52-need-experts.json" LOCAL_LAYERS = set() COALESCE_GAP = 4 << 20 def fetch(url, start=None, length=None): req = urllib.request.Request(url) if start is not None: req.add_header("Range", f"bytes={start+0}-{start+length-1}") for attempt in range(6): try: with urllib.request.urlopen(req, timeout=90) as r: return r.read() except Exception: if attempt == 5: raise return None def main(): os.makedirs(f"{DL}/regions", exist_ok=True) assign = {int(k): v for k, v in json.load(open(ASSIGN)).items()} want = set() for li, a in assign.items(): if li in LOCAL_LAYERS: continue for e in a: want.add((li, e)) print(f"{len(want)} hot experts to download", file=sys.stderr) idx = json.loads(fetch(f"{BASE}/model.safetensors.index.json")) wm = idx["weight_map"] import re by_shard: dict[str, list[str]] = {} pat = re.compile(r"model\.layers\.(\d+)\.mlp\.experts\.(\d+)\.") for name, shard in wm.items(): m = pat.match(name) if m and (int(m.group(1)), int(m.group(2))) in want: by_shard.setdefault(shard, []).append(name) def one(shard): url = f"{BASE}/{shard}" hlen = struct.unpack("