#!/usr/bin/env python3 """Generate download manifests (url, byte range, output path) for the hybrid build. - NVFP4 repo: parse each shard's safetensors header remotely; select all non-routed-expert tensors plus routed experts of plan.nvfp4_layers; coalesce adjacent ranges. - GGUF repos: use the tensor maps produced by gguf_remote.py to select blk.N.ffn_{gate,up,down}_exps.weight for the planned layers. """ import json import re import struct import sys import urllib.request from concurrent.futures import ThreadPoolExecutor ROOT = "/home/coder/git/glm52" DL = "/tmp/glm52-dl" plan = json.load(open(f"{ROOT}/hybrid_plan.json")) NVFP4_LAYERS = set(plan["nvfp4_layers"]) GGUF_LAYERS = {**{l: "ud_q3" for l in plan["ud_q3_layers"]}, **{l: "ud_q2" for l in plan["ud_q2_layers"]}} 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}-{start+length-1}") for attempt in range(5): try: with urllib.request.urlopen(req, timeout=60) as r: return r.read() except Exception as e: if attempt == 4: raise return None def needed_nvfp4(name): m = re.match(r"model\.layers\.(\d+)\.mlp\.experts\.", name) if m: return int(m.group(1)) in NVFP4_LAYERS return True def st_header(url): n = struct.unpack("