code wiki / (root) / nx_nofloat_qwen_config_gate.nx

nx_nofloat_qwen_config_gate.nx source

↩ module page · 97 lines · 6168 B

1// nx_nofloat_qwen_config_gate.nx -- rung 2a: read the REAL Qwen2.5-0.5B config from GGUF metadata. 2// Schema-first before assembling the layer: the no-float forward must use the model's ACTUAL dims, not 3// assumed ones. Reads general.architecture + qwen2.{embedding_length, attention.head_count, 4// attention.head_count_kv, block_count, feed_forward_length, attention.layer_norm_rms_epsilon, rope.freq_base} 5// via the sovereign nx_gguf_meta walker, and asserts the known Qwen2.5-0.5B shape (so the rest of the arc is 6// grounded). No hw writes (Rule 26). expect_exit: 0 license_tier: ORIGINAL 7// PRECONDITION (2026-08-19): the model lives under the host's stage root (nx_stage_path: NX_STAGE env > 8// knowledge/stage.conf > the dev-box default). Absent -> SKIP (exit 3), never RED: "I could not look" is 9// not "it is broken". Teeth are per-tooth gv_check so a tooth that stops running lowers the denominator. 10import "nx_syscalls.nx" 11import "nx_tier.nx" 12import "nx_le.nx" 13import "nx_gguf.nx" 14import "nx_gguf_meta.nx" 15import "nx_gate_verdict.nx" 16import "nx_stage_path.nx" 17 18func cg_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 19func cg_num(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 20func cg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 21func cg_beq(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 } 22 23// find a metadata key + read its UINT32 value; returns -1 if the key is absent. 24func cg_u32(buf: *u8, flen: i64, hdr: *NxGgufHeader, key: *u8) -> i64 { 25 let voff: *i64 = sys_mmap(8) as *i64 26 let vty: *i64 = sys_mmap(8) as *i64 27 let r: nx_int = nx_gguf_meta_find(buf, flen, hdr, key, cg_slen(key), voff, vty) 28 if r != NX_GMETA_OK { return 0 - 1 } 29 return nx_gguf_meta_read_u32(buf, voff[0]) 30} 31 32func main() -> i64 { 33 cg_puts("Rung 2a: read the REAL Qwen2.5-0.5B config from GGUF metadata (schema-first, grounds the layer build)\n\n" as *u8) 34 let path: *u8 = sp_path("nx_real_model.gguf" as *u8, sys_mmap(SP_PATH_MAX)) 35 sp_skip_unless("NOFLOAT-QWEN-CONFIG-GATE" as *u8, path) 36 let len_out: *i64 = sys_mmap(8) as *i64 37 len_out[0]=0 38 let buf: *u8 = sys_read_file(path, len_out) 39 var got: i64 = 0 40 if buf != (0 as *u8) { if len_out[0] > 1000 { got = 1 } } 41 42 var arch_ok: i64 = 0 43 var n_embd: i64 = 0 44 var n_heads: i64 = 0 45 var n_kv: i64 = 0 46 var n_layers: i64 = 0 47 var ffn: i64 = 0 48 var head_dim: i64 = 0 49 var eps_q10: i64 = 0 50 var rope_q10: i64 = 0 51 if got == 1 { 52 let hdr: *NxGgufHeader = sys_mmap(NX_GGUF_HDR_BYTES) as *NxGgufHeader 53 if nx_gguf_parse(buf, len_out[0], hdr) == NX_GGUF_OK { 54 // architecture string == "qwen2" 55 let voff: *i64 = sys_mmap(8) as *i64 56 let vty: *i64 = sys_mmap(8) as *i64 57 let ka: *u8 = "general.architecture\x00" as *u8 58 if nx_gguf_meta_find(buf, len_out[0], hdr, ka, cg_slen(ka), voff, vty) == NX_GMETA_OK { 59 if vty[0] == NX_GGUF_TYPE_STRING { 60 let alen: i64 = nx_gguf_meta_read_string_len(buf, voff[0]) 61 let aptr: *u8 = nx_gguf_meta_read_string_ptr(buf, voff[0]) 62 if alen == 5 { if cg_beq(aptr, "qwen2" as *u8, 5) == 1 { arch_ok = 1 } } 63 } 64 } 65 n_embd = cg_u32(buf, len_out[0], hdr, "qwen2.embedding_length\x00" as *u8) 66 n_heads = cg_u32(buf, len_out[0], hdr, "qwen2.attention.head_count\x00" as *u8) 67 n_kv = cg_u32(buf, len_out[0], hdr, "qwen2.attention.head_count_kv\x00" as *u8) 68 n_layers = cg_u32(buf, len_out[0], hdr, "qwen2.block_count\x00" as *u8) 69 ffn = cg_u32(buf, len_out[0], hdr, "qwen2.feed_forward_length\x00" as *u8) 70 if n_heads > 0 { head_dim = n_embd / n_heads } 71 // eps + rope base (f32 -> Q10 for display) 72 let ke: *u8 = "qwen2.attention.layer_norm_rms_epsilon\x00" as *u8 73 if nx_gguf_meta_find(buf, len_out[0], hdr, ke, cg_slen(ke), voff, vty) == NX_GMETA_OK { eps_q10 = nx_gguf_meta_read_f32_q10(buf, voff[0]) } 74 let kr: *u8 = "qwen2.rope.freq_base\x00" as *u8 75 if nx_gguf_meta_find(buf, len_out[0], hdr, kr, cg_slen(kr), voff, vty) == NX_GMETA_OK { rope_q10 = nx_gguf_meta_read_f32_q10(buf, voff[0]) } 76 } 77 } 78 79 cg_puts(" arch=qwen2? "); cg_num(arch_ok); cg_puts(" n_embd="); cg_num(n_embd); cg_puts(" n_heads="); cg_num(n_heads); cg_puts(" n_kv_heads="); cg_num(n_kv); cg_puts(" head_dim="); cg_num(head_dim); cg_puts("\n"); 80 cg_puts(" n_layers="); cg_num(n_layers); cg_puts(" ffn_dim="); cg_num(ffn); cg_puts(" rms_eps(Q10)="); cg_num(eps_q10); cg_puts(" rope_freq_base(Q10)="); cg_num(rope_q10); cg_puts("\n\n"); 81 82 // PER-TOOTH on the base class (2026-08-19): declared == executed by construction; a tooth that 83 // silently stops running lowers the denominator instead of reading GREEN. 84 let ctr: *i64 = gv_ctr() 85 var t1: i64 = 0; if got == 1 { if arch_ok == 1 { t1 = 1 } } 86 gv_check("T1 real model parsed + architecture == qwen2" as *u8, t1, ctr) 87 var t2: i64 = 0; if n_embd == 896 { if n_heads == 14 { if n_kv == 2 { t2 = 1 } } } 88 gv_check("T2 attention dims = Qwen2.5-0.5B (n_embd=896, n_heads=14, n_kv=2)" as *u8, t2, ctr) 89 var t3: i64 = 0; if n_layers == 24 { if ffn == 4864 { t3 = 1 } } 90 gv_check("T3 depth/ffn = Qwen2.5-0.5B (n_layers=24, ffn_dim=4864)" as *u8, t3, ctr) 91 var t4: i64 = 0; if head_dim == 64 { if n_heads * head_dim == n_embd { if n_kv < n_heads { t4 = 1 } } } 92 gv_check("T4 config internally consistent (head_dim=64, n_heads*head_dim=n_embd, GQA n_kv<n_heads)" as *u8, t4, ctr) 93 94 let rc: i64 = gv_verdict("NOFLOAT-QWEN-CONFIG-GATE" as *u8, ctr, "real Qwen2.5-0.5B config read from GGUF metadata -- the layer build is now grounded in real dims" as *u8) 95 sys_exit(rc) 96 return rc 97}