Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/om/flint/test/gen.py # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / om / flint / test / gen.py` 238 lines ยท 8523 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """Flint manifest fixture generator. TEST REFEREE ONLY, never an implementation dependency. 3 4 Builds a full Flint-shaped checkpoint from config.json alone: every tensor name and shape is derived 5 here independently of manifest.om. Shards are sparse: an 8-byte little-endian header length, the 6 header JSON, then ftruncate to the data length, so 79.5 GB of apparent size costs about 4 MB of disk. 7 8 Usage: gen.py CONFIG OUT MUTANT 9 Writes OUT/config.json, OUT/index.json, OUT/shards/*.safetensors and, for MUTANT base, OUT/expected.tsv. 10 Every other MUTANT applies exactly one defect; check.sh holds the exit code each one must produce. 11 """ 12 import json 13 import os 14 import struct 15 import sys 16 17 cfg_path, out, mut = sys.argv[1:4] 18 raw = open(cfg_path, "rb").read() 19 c = json.loads(raw) 20 H = c["hidden_size"] 21 V = c["vocab_size"] 22 HD = c["head_dim"] 23 NH = c["num_attention_heads"] 24 NKV = c["num_key_value_heads"] 25 NE = c["num_experts"] 26 MI = c["moe_intermediate_size"] 27 SI = c["shared_expert_intermediate_size"] 28 DI = c["intermediate_size"] 29 NL = c["num_hidden_layers"] 30 DENSE = set(c["mlp_only_layers"]) 31 32 33 def tensors(): 34 t = [("model.embed_tokens.weight", [V, H]), ("model.norm.weight", [H]), ("lm_head.weight", [V, H])] 35 for L in range(NL): 36 p = "model.layers.%d." % L 37 t += [ 38 (p + "input_layernorm.weight", [H]), 39 (p + "post_attention_layernorm.weight", [H]), 40 (p + "self_attn.q_proj.weight", [NH * HD, H]), 41 (p + "self_attn.k_proj.weight", [NKV * HD, H]), 42 (p + "self_attn.v_proj.weight", [NKV * HD, H]), 43 (p + "self_attn.o_proj.weight", [H, NH * HD]), 44 (p + "self_attn.q_norm.weight", [HD]), 45 (p + "self_attn.k_norm.weight", [HD]), 46 (p + "self_attn.softmax_offset", [NH]), 47 ] 48 if L in DENSE: 49 t += [(p + "mlp.gate_proj.weight", [DI, H]), (p + "mlp.up_proj.weight", [DI, H]), 50 (p + "mlp.down_proj.weight", [H, DI])] 51 continue 52 t += [(p + "mlp.gate.weight", [NE, H]), 53 (p + "mlp.shared_expert.gate_proj.weight", [SI, H]), 54 (p + "mlp.shared_expert.up_proj.weight", [SI, H]), 55 (p + "mlp.shared_expert.down_proj.weight", [H, SI])] 56 for e in range(NE): 57 q = p + "mlp.experts.%d." % e 58 t += [(q + "gate_proj.weight", [MI, H]), (q + "up_proj.weight", [MI, H]), 59 (q + "down_proj.weight", [H, MI])] 60 return t 61 62 63 def numel(shape): 64 n = 1 65 for d in shape: 66 n *= d 67 return n 68 69 70 NS = 8 if mut == "shard-cap" else 15 71 shape_of = dict(tensors()) 72 names = sorted(shape_of) 73 sname = ["model-%05d-of-%05d.safetensors" % (i + 1, NS) for i in range(NS)] 74 # shard i: list of [name, dtype, shape, begin, end]; extras: unparsed text spliced into the header 75 shards = [] 76 for i in range(NS): 77 part = names[i * len(names) // NS:(i + 1) * len(names) // NS] 78 shards.append([[n, "BF16", list(shape_of[n]), 0, 0] for n in part]) 79 wmap = [[n, sname[i]] for i in range(NS) for n in (e[0] for e in shards[i])] 80 81 82 def pack(rows): 83 o = 0 84 for r in rows: 85 r[3] = o 86 o += 2 * numel(r[2]) 87 r[4] = o 88 return o 89 90 91 for rows in shards: 92 pack(rows) 93 total = sum(2 * numel(s) for s in shape_of.values()) 94 95 # ---- one defect per mutant ------------------------------------------------------------------- 96 s0 = shards[0] 97 cfg_text = raw 98 idx_tail = "" 99 extra_head = "" # text placed before the first entry of shard 0 100 extra_entry = "" # extra key inside the first entry of shard 0 101 dup_entry = False # repeat the first entry of shard 0 at the end of its header 102 size_delta = 0 # bytes added to shard 0's data length 103 hl_mode = "le" 104 GATE = "model.layers.2.mlp.shared_expert_gate.weight" 105 106 if mut == "cfg-json": 107 cfg_text = b"{" 108 elif mut == "cfg-field": 109 cfg_text = raw.replace(b'"num_experts": 256', b'"num_experts": 128') 110 elif mut == "cfg-unknown": 111 cfg_text = raw.replace(b'"vocab_size": 150144', b'"vocab_size": 150144,\n "foo": 1') 112 elif mut == "cfg-dup": 113 cfg_text = raw.replace(b'"vocab_size": 150144', b'"vocab_size": 150144,\n "vocab_size": 150144') 114 elif mut == "cfg-missing": 115 cfg_text = raw.replace(b',\n "vocab_size": 150144', b"") 116 elif mut == "cfg-softmax": 117 cfg_text = raw.replace(b'"router_score_function": "sigmoid"', b'"router_score_function": "softmax"') 118 elif mut == "cfg-window": 119 cfg_text = raw.replace(b'"sliding_window": 513', b'"sliding_window": 4096') 120 elif mut == "cfg-tie": 121 cfg_text = raw.replace(b'"tie_word_embeddings": false', b'"tie_word_embeddings": true') 122 elif mut == "idx-total": 123 total += 2 124 elif mut == "idx-missing": 125 del wmap[5] 126 elif mut == "idx-dup": 127 wmap.insert(6, list(wmap[5])) 128 elif mut == "idx-unknown": 129 wmap.append([GATE, sname[0]]) 130 elif mut == "idx-shard-name": 131 wmap[5][1] = "../" + sname[0] 132 elif mut == "idx-syntax": 133 idx_tail = "x" 134 elif mut == "idx-size": 135 idx_tail = " " * `4194304` 136 elif mut == "hdr-dtype": 137 s0[0][1] = "F16" 138 elif mut == "hdr-shape": 139 r = next(r for r in s0 if r[0].endswith("q_proj.weight")) 140 r[2] = [r[2][1], r[2][0]] 141 elif mut == "hdr-rank": 142 r = next(r for r in s0 if r[0].endswith("q_norm.weight")) 143 r[2] = r[2] + [1] 144 elif mut == "hdr-off-len": 145 s0[-1][4] += 2 146 size_delta = 2 147 elif mut == "hdr-off-overflow": 148 s0[-1][4] = 2 ** 64 149 elif mut == "hdr-overlap": 150 a, b = s0[0], s0[1] 151 assert a[4] - a[3] == b[4] - b[3] 152 b[3], b[4] = a[3], a[4] 153 elif mut == "hdr-gap": 154 s0[-1][3] += 2 155 s0[-1][4] += 2 156 size_delta = 2 157 elif mut == "hdr-trailing": 158 size_delta = 2 159 elif mut == "hdr-truncated": 160 size_delta = -2 161 elif mut == "hdr-dup": 162 dup_entry = True 163 elif mut == "hdr-mismatch": 164 moved = s0.pop() 165 shards[1].append(moved) 166 pack(shards[1]) 167 elif mut == "hdr-missing": 168 s0.pop() 169 elif mut == "hdr-unknown": 170 s0.append([GATE, "BF16", [1, H], 0, 0]) 171 pack(s0) 172 elif mut == "hdr-meta": 173 extra_head = '"__metadata__":{"format":"pt"},' 174 elif mut == "hdr-entry": 175 extra_entry = ',"extra":1' 176 elif mut not in ("base", "hdr-json", "hdr-len-be", "hdr-len-big", "shard-short", "shard-absent", 177 "shard-symlink", "shard-dir", "shard-cap"): 178 sys.exit("gen.py: unknown mutant " + mut) 179 if cfg_text == raw and mut.startswith("cfg-") and mut != "cfg-json": 180 sys.exit("gen.py: mutant " + mut + " did not change config.json") 181 182 # ---- write ------------------------------------------------------------------------------------- 183 os.makedirs(out + "/shards") 184 with open(out + "/config.json", "wb") as f: 185 f.write(cfg_text) 186 187 lines = ["{", ' "metadata": {', ' "total_size": %d' % total, " },", ' "weight_map": {'] 188 lines += [' "%s": "%s"%s' % (n, s, "," if k + 1 < len(wmap) else "") for k, (n, s) in enumerate(wmap)] 189 lines += [" }", "}"] 190 with open(out + "/index.json", "w") as f: 191 f.write("\n".join(lines) + "\n" + idx_tail) 192 193 194 def entry(r, more=""): 195 return '"%s":{"dtype":"%s","shape":[%s],"data_offsets":[%d,%d]%s}' % ( 196 r[0], r[1], ",".join(str(d) for d in r[2]), r[3], r[4], more) 197 198 199 for i, rows in enumerate(shards): 200 path = out + "/shards/" + sname[i] 201 ents = [entry(r, extra_entry if (i == 0 and k == 0) else "") for k, r in enumerate(rows)] 202 if i == 0 and dup_entry: 203 ents.append(entry(rows[0])) 204 head = "{" + (extra_head if i == 0 else "") + ",".join(ents) + "}" 205 if i == 0 and mut == "hdr-json": 206 head = "{" 207 head = head.encode() 208 head += b" " * (-len(head) % 8) 209 data = max((r[4] for r in rows if r[4] < 2 ** 63), default=0) 210 if i == 0: 211 data += size_delta 212 hl = len(head) 213 if i == 0 and mut == "hdr-len-big": 214 hl = 8 + len(head) + data 215 with open(path, "wb") as f: 216 if i == 0 and mut == "shard-short": 217 f.write(b"\x00" * 4) 218 continue 219 f.write(struct.pack(">Q" if (i == 0 and mut == "hdr-len-be") else "