code wiki / (root) / nx_zimage_weights.nx

nx_zimage_weights.nx source

↩ module page · 183 lines · 8291 B

1// nx_zimage_weights.nx -- the LAYER-WEIGHT RESOLVER: (layer L, slot k) -> where that tensor lives. 2// 3// sd-server -> Nishi migration, the binding brick between the GGUF and the encoder. nx_zimage_layout 4// DUMPS blk.0's offsets to a text file for a human to read; that is a diagnostic, not an API. The 5// full-scale assembly needs to ask, 36 x 11 times, "where is layer L's slot k?" -- so that question 6// becomes a function here instead of being inlined into whichever organ needs it next. 7// 8// SLOT ORDER IS THE ENCODER'S ARGUMENT ORDER, deliberately, so a binder can fill the per-layer 9// pointer arrays of nx_f32_qwen_encoder_layers by looping slots with no mapping table: 10// 0 attn_norm 1 attn_q 2 attn_k 3 attn_v 4 attn_output 11// 5 ffn_norm 6 ffn_gate 7 ffn_up 8 ffn_down 12// 9 attn_q_norm 10 attn_k_norm <- Qwen3 QK-norm, see below 13// 14// ***FINDING (2026-07-30, debt 1785449705): slots 9/10 EXIST IN ALL 36 LAYERS, and the sovereign 15// nx_f32_qwen_layer DOES NOT CONSUME THEM.*** Their existence was not guessed -- it was FORCED by 16// arithmetic: 36 x 9 = 324 but tensor_count = 398, and 36*11 + token_embd + output_norm = 398 EXACTLY. 17// Running real weights through the 9-slot encoder therefore yields finite, plausible, WRONG numbers 18// with nothing erroring. LAW: tensor_count is a checksum on your architecture understanding. 19// 20// FAIL-CLOSED: an unknown slot or a missing tensor returns -1 and writes NOTHING to the box. It never 21// invents an offset -- a fabricated offset would read arbitrary bytes out of a 6.5GB file and the 22// resulting garbage would look like a numerical bug thirty layers later. 23// 24// METADATA ONLY: resolves offsets/dims/types from the parsed header. No tensor is materialized, so 25// this is cheap enough to call for every layer at startup. 26// license_tier: ORIGINAL expect_exit: 0 27import "nx_syscalls.nx" 28import "nx_tier.nx" 29import "nx_le.nx" 30import "nx_strconv.nx" 31import "nx_tensor.nx" 32import "nx_gguf.nx" 33import "nx_gguf_load.nx" 34import "nx_gguf_meta.nx" 35import "nx_placement.nx" 36import "nx_gguf_load_lazy.nx" 37const ZW_MAGIC_201326592: i64 = 201326592 38const ZW_MAGIC_1024: i64 = 1024 39 40const ZW_NSLOTS: i64 = 11 41 42// build "blk.<n>.<suffix>" into out (NUL-terminated); returns the name length. 43func zw_blk_name(out: *u8, n: i64, suffix: *u8, suffix_len: i64) -> i64 { 44 var o: i64 = 0 45 out[o] = 0x62 as u8; o = o + 1 // 'b' 46 out[o] = 0x6C as u8; o = o + 1 // 'l' 47 out[o] = 0x6B as u8; o = o + 1 // 'k' 48 out[o] = 0x2E as u8; o = o + 1 // '.' 49 let dec: *u8 = sys_mmap(32) 50 let nd: i64 = nx_strconv_format_i64(n, dec) 51 var i: i64 = 0 52 while i < nd { out[o] = dec[i]; o = o + 1; i = i + 1 } 53 out[o] = 0x2E as u8; o = o + 1 // '.' 54 var j: i64 = 0 55 while j < suffix_len { out[o] = suffix[j]; o = o + 1; j = j + 1 } 56 out[o] = 0 as u8 57 return o 58} 59 60// slot -> tensor-name suffix. Returns length, and writes the suffix pointer through `sp`. 61// Kept as one function so the slot vocabulary has exactly ONE definition site. 62func zw_slot_suffix(slot: i64, sp: *i64) -> i64 { 63 if slot == 0 { sp[0] = ("attn_norm.weight" as *u8) as i64; return 16 } 64 if slot == 1 { sp[0] = ("attn_q.weight" as *u8) as i64; return 13 } 65 if slot == 2 { sp[0] = ("attn_k.weight" as *u8) as i64; return 13 } 66 if slot == 3 { sp[0] = ("attn_v.weight" as *u8) as i64; return 13 } 67 if slot == 4 { sp[0] = ("attn_output.weight" as *u8) as i64; return 18 } 68 if slot == 5 { sp[0] = ("ffn_norm.weight" as *u8) as i64; return 15 } 69 if slot == 6 { sp[0] = ("ffn_gate.weight" as *u8) as i64; return 15 } 70 if slot == 7 { sp[0] = ("ffn_up.weight" as *u8) as i64; return 13 } 71 if slot == 8 { sp[0] = ("ffn_down.weight" as *u8) as i64; return 15 } 72 if slot == 9 { sp[0] = ("attn_q_norm.weight" as *u8) as i64; return 18 } 73 if slot == 10 { sp[0] = ("attn_k_norm.weight" as *u8) as i64; return 18 } 74 sp[0] = 0 75 return 0 76} 77 78// Resolve (layer, slot) -> box[0]=absolute file offset, box[1]=dim_0, box[2]=dim_1, box[3]=ggml_type. 79// Returns the tensor index, or -1 if the slot is unknown or the tensor is absent (box untouched). 80func zw_resolve(hdr: *NxGgufHeader, layer: i64, slot: i64, box: *i64) -> i64 { 81 if layer < 0 { return 0 - 1 } 82 if slot < 0 { return 0 - 1 } 83 if slot >= ZW_NSLOTS { return 0 - 1 } 84 let sp: *i64 = sys_mmap(16) as *i64 85 let slen: i64 = zw_slot_suffix(slot, sp) 86 if slen == 0 { return 0 - 1 } 87 let nmbuf: *u8 = sys_mmap(160) 88 let nl: i64 = zw_blk_name(nmbuf, layer, sp[0] as *u8, slen) 89 let idx: nx_int = nx_gguf_find_tensor(hdr, nmbuf, nl as nx_int) 90 if idx < 0 { return 0 - 1 } 91 let ti: *NxGgufTensorInfo = nx_gguf_tensor_at(hdr, idx) 92 box[0] = hdr.data_off + ti.offset 93 box[1] = ti.dim_0 94 box[2] = ti.dim_1 95 box[3] = ti.ggml_type 96 return idx as i64 97} 98 99// ===== Self-test (inline gate) ==================================== 100// Runs against the REAL production GGUF, metadata only. 101// T1 every layer 0..n_layers-1 resolves all 11 slots -> else 60+slot 102// T2 NON-VACUITY: layer n_layers (out of range) MUST MISS -> else 70 103// Without T2 a resolver that fabricated an offset for any name would pass T1. 104// T3 NON-VACUITY: an out-of-range SLOT must MISS -> else 71 105// T4 every resolved offset is inside the file and non-zero -> else 72 106func main() -> i64 { 107 let path: *u8 = "/mnt/c/Users/elder/elder-ai-platform/models/unified/text_encoder/Z-Image_Qwen_3_4b-Q6_K.gguf" as *u8 108 let fd: i64 = sys_openat_rd(path) 109 if fd < 0 { return 30 } 110 let CAP: i64 = ZW_MAGIC_201326592 111 let buf: *u8 = sys_mmap(CAP) 112 var total: i64 = 0 113 var go: i64 = 1 114 while go == 1 { 115 let r: i64 = sys_read(fd, ((buf as i64) + total) as *u8, CAP - total) 116 if r <= 0 { go = 0 } else { total = total + r; if total >= CAP { go = 0 } } 117 } 118 sys_close(fd) 119 if total < 1000 { return 31 } 120 121 let hdr: *NxGgufHeader = sys_mmap(NX_GGUF_HDR_BYTES) as *NxGgufHeader 122 let vp: nx_int = nx_gguf_parse(buf, total, hdr) 123 if vp != NX_GGUF_OK { return 40 + vp } 124 125 // derive n_layers by probing (same discipline as nx_zimage_gguf_arch: ask the file, never divide) 126 let box: *i64 = sys_mmap(64) as *i64 127 var n_layers: i64 = 0 128 var probing: i64 = 1 129 while probing == 1 { 130 if zw_resolve(hdr, n_layers, 0, box) < 0 { probing = 0 } 131 else { 132 n_layers = n_layers + 1 133 if n_layers >= ZW_MAGIC_1024 { probing = 0 } 134 } 135 } 136 if n_layers <= 0 { return 41 } 137 138 // T1 + T4: every layer, every slot resolves to a sane offset 139 var L: i64 = 0 140 while L < n_layers { 141 var slot: i64 = 0 142 while slot < ZW_NSLOTS { 143 if zw_resolve(hdr, L, slot, box) < 0 { return 60 + slot } 144 if box[0] <= 0 { return 72 } 145 if box[1] <= 0 { return 72 } 146 slot = slot + 1 147 } 148 L = L + 1 149 } 150 151 // T2: one past the last layer must MISS (the resolver can refuse) 152 if zw_resolve(hdr, n_layers, 0, box) >= 0 { return 70 } 153 // T3: an out-of-range slot must MISS 154 if zw_resolve(hdr, 0, ZW_NSLOTS, box) >= 0 { return 71 } 155 156 let ofd: i64 = sys_openat_wr("/tmp/zimg_weights.txt" as *u8, 0x1a4) 157 if ofd >= 0 { 158 let line: *u8 = sys_mmap(256) 159 var lo: i64 = 0 160 lo = 0 161 let dec: *u8 = sys_mmap(32) 162 var k: i64 = 0 163 let tag: *u8 = "n_layers_resolved=" as *u8 164 while tag[k] != (0 as u8) { line[lo] = tag[k]; lo = lo + 1; k = k + 1 } 165 let nd: i64 = nx_strconv_format_i64(n_layers, dec) 166 k = 0 167 while k < nd { line[lo] = dec[k]; lo = lo + 1; k = k + 1 } 168 line[lo] = 0x0A as u8; lo = lo + 1 169 sys_write(ofd, line, lo) 170 171 lo = 0 172 k = 0 173 let tag2: *u8 = "slots_per_layer=" as *u8 174 while tag2[k] != (0 as u8) { line[lo] = tag2[k]; lo = lo + 1; k = k + 1 } 175 let nd2: i64 = nx_strconv_format_i64(ZW_NSLOTS, dec) 176 k = 0 177 while k < nd2 { line[lo] = dec[k]; lo = lo + 1; k = k + 1 } 178 line[lo] = 0x0A as u8; lo = lo + 1 179 sys_write(ofd, line, lo) 180 sys_close(ofd) 181 } 182 return 0 183}