code wiki / (root) / nx_llm_france_probe.nx

nx_llm_france_probe.nx source

↩ module page · 75 lines · 3736 B

1// nx_llm_france_probe.nx -- DECISIVE correctness probe (post QKV-bias fix). Feeds the REAL factual prompt 2// "The capital of France is" and greedy-generates. A CORRECT sovereign forward continues " Paris" (or coherent 3// English); a still-broken forward emits garbage. Settles whether the bias fix completed the forward or a 4// residual remains. Mirrors the cohere harness but with the real prompt (not the repetition label). expect_exit: 0 5import "nx_syscalls.nx" 6import "nx_tier.nx" 7import "nx_bpe.nx" 8import "nx_gguf.nx" 9import "nx_gguf_load.nx" 10import "nx_gguf_meta.nx" 11import "nx_f32.nx" 12import "nx_f32_kv_cache.nx" 13import "nx_f32_lazy_weight.nx" 14import "nx_f32_llama_block.nx" 15import "nx_f32_llama_block_v4.nx" 16import "nx_f32_llama_stack_v4.nx" 17import "nx_f32_llama_layer_lazy_load.nx" 18import "nx_f32_llm.nx" 19import "nx_f32_llm_v4.nx" 20import "nx_f32_llm_read_dims.nx" 21import "nx_f32_bpe_load.nx" 22import "nx_f32_llm_special_tokens.nx" 23import "nx_f32_sampler.nx" 24import "nx_f32_llm_run_v2.nx" 25import "nx_f32_llm_run_v3.nx" 26 27func pr_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 28func pr_num(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; 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{bb[i]=t[k-1-i];i=i+1} sys_write(1,bb,k); return 0 } 29 30func main() -> i64 { 31 let path: *u8 = "/tmp/nx_real_model.gguf" as *u8 32 let len_out: *i64 = sys_mmap(8) as *i64 33 let buf: *u8 = sys_read_file(path, len_out) 34 if buf == (0 as *u8) { return 10 } 35 let hdr: *NxGgufHeader = sys_mmap(NX_GGUF_HDR_BYTES) as *NxGgufHeader 36 if nx_gguf_parse(buf, len_out[0], hdr) != NX_GGUF_OK { return 20 } 37 let model: *NxF32LlamaModel = nx_f32_llama_model_alloc() 38 let out_err: *i64 = sys_mmap(8) as *i64 39 if nx_f32_llm_read_dims_from_gguf(buf, len_out[0], hdr, model, out_err) != NX_FLD_OK { return 30 } 40 if nx_f32_llm_load_weights_v4_from_gguf(buf, hdr, model, out_err) != NX_FLV4_OK { return 40 } 41 let vocab: *NxBpeVocab = nx_bpe_vocab_new(67108864, 262144, 524288) 42 let nt: *i64 = sys_mmap(8) as *i64 43 let nm: *i64 = sys_mmap(8) as *i64 44 if nx_f32_bpe_load_from_gguf(buf, len_out[0], hdr, vocab, nt, nm, out_err) != NX_FBL_OK { return 50 } 45 let eos: nx_int = nx_f32_llm_read_eos(buf, len_out[0], hdr) 46 let cache: *NxF32KVCache = nx_f32_kv_cache_alloc(model.n_layers, model.n_kv_heads, 64, model.head_dim) 47 let cfg: *NxF32SamplerCfg = nx_f32_sampler_cfg_alloc() 48 49 let prompt: *u8 = "The capital of France is" as *u8 50 let plen: nx_int = 24 51 let out_bytes: *u8 = sys_mmap(512) 52 let prng: *i64 = sys_mmap(8) as *i64 53 prng[0] = 1 54 let eps: i64 = 0x358637BD 55 let attn_scale: i64 = 0x3E000000 56 let rope_base: i64 = 0x415D0EAB 57 58 // show tokens + round-trip (sanity) 59 let dtoks: *i64 = sys_mmap(128 * 8) as *i64 60 let dn: nx_int = nx_bpe_encode_bytelevel(vocab, prompt, plen, dtoks) 61 pr_puts("prompt toks (n=" as *u8); pr_num(dn as i64); pr_puts("):" as *u8) 62 var di: nx_int = 0 63 while di < dn { pr_puts(" " as *u8); pr_num(dtoks[di]); di = di + 1 } 64 pr_puts("\n" as *u8) 65 66 let n_emit: nx_int = nx_f32_llm_run_v3(model, vocab, cache, prompt, plen, 6, cfg, 67 eps, attn_scale, rope_base, 1, prng, eos, out_bytes, 512) 68 if n_emit < 0 { pr_puts("run_v3 err=" as *u8); pr_num((0-n_emit) as i64); pr_puts("\n" as *u8); return 60 } 69 70 sys_write(1, "PROMPT: The capital of France is\nCOMPLETION:" as *u8, 44) 71 sys_write(1, out_bytes, n_emit as i64) 72 sys_write(1, "\n" as *u8, 1) 73 pr_puts("(a CORRECT forward continues ' Paris' / coherent English; garbage = residual bug remains)\n" as *u8) 74 return 0 75}