nx_zimage_layout.nx source
↩ module page · 71 lines · 3126 B
1// nx_zimage_layout.nx -- dump the REAL Z-Image/Qwen GGUF tensor layout (offsets/dims/types), sovereignly.
2//
3// sd-server -> Nishi migration: to run a REAL transformer op we must know where blk.0's weights live in the
4// file and their shapes. This parses the real `Z-Image_Qwen_3_4b-Q6_K.gguf` metadata (bounded prefix) and
5// writes, for token_embd + blk.0 attention/ffn tensors, their data offset (hdr.data_off + ti.offset), dim_0,
6// dim_1, and ggml_type -- so the next organ sizes the read + matmul correctly. Metadata only; no materialize.
7// license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_tier.nx"
10import "nx_le.nx"
11import "nx_strconv.nx"
12import "nx_tensor.nx"
13import "nx_gguf.nx"
14import "nx_gguf_load.nx"
15import "nx_gguf_meta.nx"
16import "nx_placement.nx"
17import "nx_gguf_load_lazy.nx"
18
19func zl_emit(fd: i64, key: *u8, key_len: i64, value: i64) -> i64 {
20 let line: *u8 = sys_mmap(96)
21 var lo: i64 = 0
22 var ki: i64 = 0
23 while ki < key_len { line[lo] = key[ki]; lo = lo + 1; ki = ki + 1 }
24 line[lo] = 0x3D; lo = lo + 1
25 let dec: *u8 = sys_mmap(32)
26 let n_dec: i64 = nx_strconv_format_i64(value, dec)
27 var k: i64 = 0
28 while k < n_dec { line[lo] = dec[k]; lo = lo + 1; k = k + 1 }
29 line[lo] = 0x0A; lo = lo + 1
30 return sys_write(fd, line, lo)
31}
32
33// emit "<label>_off/_d0/_d1/_ty" for a tensor by name; returns idx or -1
34func zl_dump(fd: i64, hdr: *NxGgufHeader, name: *u8, name_len: i64,
35 lo_off: *u8, lo0: *u8, lo1: *u8, loty: *u8, lolen: i64) -> i64 {
36 let idx: nx_int = nx_gguf_find_tensor(hdr, name, name_len)
37 if idx < 0 { return 0 - 1 }
38 let ti: *NxGgufTensorInfo = nx_gguf_tensor_at(hdr, idx)
39 zl_emit(fd, lo_off, lolen, hdr.data_off + ti.offset)
40 zl_emit(fd, lo0, lolen, ti.dim_0)
41 zl_emit(fd, lo1, lolen, ti.dim_1)
42 zl_emit(fd, loty, lolen, ti.ggml_type)
43 return idx
44}
45
46func main() -> i64 {
47 let path: *u8 = "/mnt/c/Users/elder/elder-ai-platform/models/unified/text_encoder/Z-Image_Qwen_3_4b-Q6_K.gguf" as *u8
48 let fd: i64 = sys_openat_rd(path)
49 if fd < 0 { return 30 }
50 let CAP: i64 = 201326592
51 let buf: *u8 = sys_mmap(CAP)
52 var total: i64 = 0
53 var go: i64 = 1
54 while go == 1 {
55 let r: i64 = sys_read(fd, ((buf as i64) + total) as *u8, CAP - total)
56 if r <= 0 { go = 0 } else { total = total + r; if total >= CAP { go = 0 } }
57 }
58 sys_close(fd)
59 if total < 1000 { return 31 }
60 let hdr: *NxGgufHeader = sys_mmap(NX_GGUF_HDR_BYTES) as *NxGgufHeader
61 if nx_gguf_parse(buf, total, hdr) != NX_GGUF_OK { return 40 }
62
63 let ofd: i64 = sys_openat_wr("/tmp/zimg_layout.txt" as *u8, 0x1a4)
64 if ofd < 0 { return 50 }
65 zl_emit(ofd, "data_off" as *u8, 8, hdr.data_off)
66 zl_dump(ofd, hdr, "token_embd.weight" as *u8, 17, "emb_off" as *u8, "emb_d0" as *u8, "emb_d1" as *u8, "emb_ty" as *u8, 7)
67 zl_dump(ofd, hdr, "blk.0.attn_q.weight" as *u8, 19, "q0_off" as *u8, "q0_d0" as *u8, "q0_d1" as *u8, "q0_ty" as *u8, 6)
68 zl_dump(ofd, hdr, "blk.0.attn_norm.weight" as *u8, 22, "an0_off" as *u8, "an0_d0" as *u8, "an0_d1" as *u8, "an0_ty" as *u8, 7)
69 sys_close(ofd)
70 return 0
71}