nx_zimage_real_proj.nx source
↩ module page · 105 lines · 4508 B
1// nx_zimage_real_proj.nx -- a REAL Qwen transformer projection on REAL weights, sovereignly.
2//
3// sd-server -> Nishi migration: the first genuine transformer-forward computation on the actual model.
4// Dequantizes token-0's real embedding (token_embd row, 2560 Q6_K) AND the first 4 output-neuron rows of
5// blk.0.attn_q.weight (the real Q-projection matrix, in=2560->out=4096, Q6_K, at file offset ~335MB), then
6// computes the attention Q projection Q[o] = sum_i embed[i] * Wq[o][i] with our own f32 mul/add. Real Qwen
7// weights -> our sovereign matmul -> real Q values. This is the same Qwen that Z-Image uses to encode image
8// prompts AND (in the elder fork) to chat -> the shared-LLM reuse, now flowing through OUR code.
9//
10// Read is a bounded 384MB prefix (covers metadata + token_embd + the start of blk.0); we only dequant the
11// slices we need (embed row + 4 Wq rows), not the full multi-GB tensors.
12// license_tier: ORIGINAL
13import "nx_syscalls.nx"
14import "nx_tier.nx"
15import "nx_le.nx"
16import "nx_strconv.nx"
17import "nx_tensor.nx"
18import "nx_gguf.nx"
19import "nx_gguf_load.nx"
20import "nx_gguf_meta.nx"
21import "nx_placement.nx"
22import "nx_gguf_load_lazy.nx"
23import "nx_q6_k_to_f32.nx"
24import "nx_f32.nx"
25import "nx_f32_cvt.nx"
26
27func rp_emit(fd: i64, key: *u8, key_len: i64, value: i64) -> i64 {
28 let line: *u8 = sys_mmap(64)
29 var lo: i64 = 0
30 var ki: i64 = 0
31 while ki < key_len { line[lo] = key[ki]; lo = lo + 1; ki = ki + 1 }
32 line[lo] = 0x3D; lo = lo + 1
33 let dec: *u8 = sys_mmap(32)
34 let nd: i64 = nx_strconv_format_i64(value, dec)
35 var k: i64 = 0
36 while k < nd { line[lo] = dec[k]; lo = lo + 1; k = k + 1 }
37 line[lo] = 0x0A; lo = lo + 1
38 return sys_write(fd, line, lo)
39}
40
41func main() -> i64 {
42 let path: *u8 = "/mnt/c/Users/elder/elder-ai-platform/models/unified/text_encoder/Z-Image_Qwen_3_4b-Q6_K.gguf" as *u8
43 let fd: i64 = sys_openat_rd(path)
44 if fd < 0 { return 30 }
45 let CAP: i64 = 402653184 // 384 MB prefix (covers blk.0.attn_q at ~335MB)
46 let buf: *u8 = sys_mmap(CAP)
47 var total: i64 = 0
48 var go: i64 = 1
49 while go == 1 {
50 let r: i64 = sys_read(fd, ((buf as i64) + total) as *u8, CAP - total)
51 if r <= 0 { go = 0 } else { total = total + r; if total >= CAP { go = 0 } }
52 }
53 sys_close(fd)
54 if total < 100000000 { return 31 } // must have read the bulk (>=100MB)
55
56 let hdr: *NxGgufHeader = sys_mmap(NX_GGUF_HDR_BYTES) as *NxGgufHeader
57 if nx_gguf_parse(buf, total, hdr) != NX_GGUF_OK { return 40 }
58
59 let ei: nx_int = nx_gguf_find_tensor(hdr, "token_embd.weight" as *u8, 17)
60 if ei < 0 { return 60 }
61 let eti: *NxGgufTensorInfo = nx_gguf_tensor_at(hdr, ei)
62 let HID: i64 = eti.dim_0 // 2560
63 let emb_off: i64 = hdr.data_off + eti.offset
64
65 let qi: nx_int = nx_gguf_find_tensor(hdr, "blk.0.attn_q.weight" as *u8, 19)
66 if qi < 0 { return 61 }
67 let qti: *NxGgufTensorInfo = nx_gguf_tensor_at(hdr, qi)
68 if qti.dim_0 != HID { return 62 } // in-dim must match the embedding dim
69 let q_off: i64 = hdr.data_off + qti.offset
70 if q_off + 262144 > total { return 63 } // the Wq rows we need must be inside the prefix
71
72 // dequant token-0's embedding (HID values) and the first NROWS output rows of Wq (NROWS*HID values)
73 let NROWS: i64 = 4
74 let embed: *i64 = sys_mmap(HID * 8) as *i64
75 nx_q6_k_to_f32(buf, emb_off, HID, embed)
76 let wq: *i64 = sys_mmap(NROWS * HID * 8) as *i64
77 nx_q6_k_to_f32(buf, q_off, NROWS * HID, wq)
78
79 // Q[o] = sum_i embed[i] * Wq[o][i] (real attention Q projection, our sovereign f32 matmul)
80 let Q: *i64 = sys_mmap(NROWS * 8) as *i64
81 var o: i64 = 0
82 while o < NROWS {
83 var acc: i64 = 0
84 var i: i64 = 0
85 while i < HID {
86 acc = nx_f32_add(acc, nx_f32_mul(embed[i], wq[o * HID + i]))
87 i = i + 1
88 }
89 if (acc & 0x7F800000) == 0x7F800000 { return 80 } // Inf/NaN in a Q output
90 Q[o] = acc
91 o = o + 1
92 }
93
94 let ofd: i64 = sys_openat_wr("/tmp/zimg_proj.txt" as *u8, 0x1a4)
95 if ofd >= 0 {
96 rp_emit(ofd, "HID" as *u8, 3, HID)
97 rp_emit(ofd, "q_out" as *u8, 5, qti.dim_1)
98 rp_emit(ofd, "Q0_bits" as *u8, 7, Q[0])
99 rp_emit(ofd, "Q1_bits" as *u8, 7, Q[1])
100 rp_emit(ofd, "Q2_bits" as *u8, 7, Q[2])
101 rp_emit(ofd, "Q3_bits" as *u8, 7, Q[3])
102 sys_close(ofd)
103 }
104 return 0
105}