nx_f32_llm_live_load_only_test.nx source
↩ module page · 85 lines · 3272 B
1// nx_f32_llm_live_load_only_test.nx -- LIVE-FIRE structural load
2// proof. Loads the real Qwen2.5-0.5B Q4_K_M model + verifies every
3// top-level + per-layer pointer is non-null + samples a few weight
4// values to confirm dequant produced non-degenerate output.
5//
6// Skips the forward pass (covered by nx_f32_llm_live_load_test).
7// This is the fast structural binding proof: ~1-2 min in qemu.
8
9import "nx_syscalls.nx"
10import "nx_tier.nx"
11import "nx_gguf.nx"
12import "nx_gguf_load.nx"
13import "nx_f32.nx"
14import "nx_f32_kv_cache.nx"
15import "nx_f32_lazy_weight.nx"
16import "nx_f32_llama_block.nx"
17import "nx_f32_llama_block_v4.nx"
18import "nx_f32_llama_layer_lazy_load.nx"
19import "nx_f32_llm.nx"
20import "nx_f32_llm_v4.nx"
21import "nx_f32_llm_read_dims.nx"
22
23func main() -> i64 {
24 let path: *u8 = sys_mmap(64)
25 path[0]=0x2F as u8; path[1]=0x74 as u8; path[2]=0x6D as u8; path[3]=0x70 as u8
26 path[4]=0x2F as u8; path[5]=0x6E as u8; path[6]=0x78 as u8; path[7]=0x5F as u8
27 path[8]=0x72 as u8; path[9]=0x65 as u8; path[10]=0x61 as u8; path[11]=0x6C as u8
28 path[12]=0x5F as u8; path[13]=0x6D as u8; path[14]=0x6F as u8; path[15]=0x64 as u8
29 path[16]=0x65 as u8; path[17]=0x6C as u8; path[18]=0x2E as u8
30 path[19]=0x67 as u8; path[20]=0x67 as u8; path[21]=0x75 as u8; path[22]=0x66 as u8
31 path[23]=0 as u8
32
33 let len_out: *i64 = sys_mmap(8) as *i64
34 let buf: *u8 = sys_read_file(path, len_out)
35 if buf == (0 as *u8) { return 10 }
36
37 let hdr: *NxGgufHeader = sys_mmap(NX_GGUF_HDR_BYTES) as *NxGgufHeader
38 if nx_gguf_parse(buf, len_out[0], hdr) != NX_GGUF_OK { return 20 }
39
40 let model: *NxF32LlamaModel = nx_f32_llama_model_alloc()
41 let out_err: *i64 = sys_mmap(8) as *i64
42 if nx_f32_llm_read_dims_from_gguf(buf, len_out[0], hdr, model, out_err) != NX_FLD_OK {
43 return 30
44 }
45
46 if nx_f32_llm_load_weights_v4_from_gguf(buf, hdr, model, out_err) != NX_FLV4_OK {
47 return 40
48 }
49
50 // All top-level non-null.
51 if (model.embed_weights as i64) == 0 { return 50 }
52 if (model.gamma_out as i64) == 0 { return 51 }
53 if (model.lm_head as i64) == 0 { return 52 }
54 if (model.layers as i64) == 0 { return 53 }
55
56 // All 24 layers populated.
57 var L: nx_int = 0
58 while L < 24 {
59 let layer: *NxF32LlamaLayerLazy = (model.layers[L]) as *NxF32LlamaLayerLazy
60 if (layer as i64) == 0 { return 60 + L }
61 if (layer.gamma_attn as i64) == 0 { return 100 + L }
62 if (layer.W_q as i64) == 0 { return 200 + L }
63 if (layer.W_o as i64) == 0 { return 230 + L }
64 L = L + 1
65 }
66
67 // Sample weight values to confirm dequant produced non-zero results.
68 let layer0: *NxF32LlamaLayerLazy = (model.layers[0]) as *NxF32LlamaLayerLazy
69
70 // gamma_attn is always F32 (RMSNorm scale). Verify first value is non-zero.
71 if layer0.gamma_attn[0] == 0 { return 254 }
72
73 // embed_weights: sample a few token embeddings. Should be non-zero.
74 // Use small range to keep memory access bounded.
75 var nz_count: nx_int = 0
76 var i: nx_int = 0
77 while i < 100 {
78 if model.embed_weights[i] != 0 { nz_count = nz_count + 1 }
79 i = i + 1
80 }
81 // Expect at least 10% non-zero among first 100 values.
82 if nz_count < 10 { return 252 }
83
84 return 0
85}