code wiki / (root) / nx_f32_qwen_capital_gate.nx

nx_f32_qwen_capital_gate.nx source

↩ module page · 119 lines · 4731 B

1// nx_f32_llm_live_gen_test.nx -- LIVE-FIRE TEXT GENERATION from real 2// Qwen2.5-0.5B. Loads model + vocab + cache + runs greedy 5-token 3// generation from a fixed prompt. Writes output bytes to 4// /tmp/nx_qwen_gen.txt for inspection. 5// 6// This is the substrate's "text in, text out" proof on real 7// production weights. 8 9import "nx_syscalls.nx" 10import "nx_tier.nx" 11import "nx_bpe.nx" 12import "nx_gguf.nx" 13import "nx_gguf_load.nx" 14import "nx_gguf_meta.nx" 15import "nx_f32.nx" 16import "nx_f32_kv_cache.nx" 17import "nx_f32_lazy_weight.nx" 18import "nx_f32_llama_block.nx" 19import "nx_f32_llama_block_v4.nx" 20import "nx_f32_llama_stack_v4.nx" 21import "nx_f32_llama_layer_lazy_load.nx" 22import "nx_f32_llm.nx" 23import "nx_f32_llm_v4.nx" 24import "nx_f32_llm_read_dims.nx" 25import "nx_f32_bpe_load.nx" 26import "nx_f32_llm_special_tokens.nx" 27import "nx_f32_sampler.nx" 28import "nx_f32_llm_run_v2.nx" 29import "nx_f32_llm_run_v3.nx" 30 31func main() -> i64 { 32 let path: *u8 = sys_mmap(64) 33 path[0]=0x2F as u8; path[1]=0x74 as u8; path[2]=0x6D as u8; path[3]=0x70 as u8 34 path[4]=0x2F as u8; path[5]=0x6E as u8; path[6]=0x78 as u8; path[7]=0x5F as u8 35 path[8]=0x72 as u8; path[9]=0x65 as u8; path[10]=0x61 as u8; path[11]=0x6C as u8 36 path[12]=0x5F as u8; path[13]=0x6D as u8; path[14]=0x6F as u8; path[15]=0x64 as u8 37 path[16]=0x65 as u8; path[17]=0x6C as u8; path[18]=0x2E as u8 38 path[19]=0x67 as u8; path[20]=0x67 as u8; path[21]=0x75 as u8; path[22]=0x66 as u8 39 path[23]=0 as u8 40 41 let len_out: *i64 = sys_mmap(8) as *i64 42 let buf: *u8 = sys_read_file(path, len_out) 43 if buf == (0 as *u8) { return 10 } 44 45 let hdr: *NxGgufHeader = sys_mmap(NX_GGUF_HDR_BYTES) as *NxGgufHeader 46 if nx_gguf_parse(buf, len_out[0], hdr) != NX_GGUF_OK { return 20 } 47 48 let model: *NxF32LlamaModel = nx_f32_llama_model_alloc() 49 let out_err: *i64 = sys_mmap(8) as *i64 50 if nx_f32_llm_read_dims_from_gguf(buf, len_out[0], hdr, model, out_err) != NX_FLD_OK { 51 return 30 52 } 53 if nx_f32_llm_load_weights_v4_from_gguf(buf, hdr, model, out_err) != NX_FLV4_OK { 54 return 40 55 } 56 57 // Bump intern caps for Qwen2.5: vocab=151,936; merges ~150K; 58 // tokens can be multi-byte UTF-8. 64MB bytes, 256K ids, 512K merges. 59 let vocab: *NxBpeVocab = nx_bpe_vocab_new(67108864, 262144, 524288) 60 let nt: *i64 = sys_mmap(8) as *i64 61 let nm: *i64 = sys_mmap(8) as *i64 62 let bpe_v: nx_int = nx_f32_bpe_load_from_gguf(buf, len_out[0], hdr, vocab, nt, nm, out_err) 63 if bpe_v != NX_FBL_OK { 64 return (150 + bpe_v) as i64 65 } 66 // CHECKPOINT 1: BPE loaded OK. nt[0] = n_tokens loaded. 67 if nt[0] < 1000 { return 51 } // expect >100K tokens 68 if nt[0] > 200000 { return 52 } // sanity ceiling 69 70 let eos: nx_int = nx_f32_llm_read_eos(buf, len_out[0], hdr) 71 72 let cache: *NxF32KVCache = nx_f32_kv_cache_alloc( 73 model.n_layers, model.n_kv_heads, 64, model.head_dim) 74 if cache == (0 as *NxF32KVCache) { return 53 } 75 76 let cfg: *NxF32SamplerCfg = nx_f32_sampler_cfg_alloc() 77 // Greedy (top_k = top_p = 0 -> argmax) for reproducibility. 78 79 let prompt: *u8 = "The capital of France is\x00" as *u8 // H2H control: strong-completion prompt (expect " Paris") 80 81 let out_bytes: *u8 = sys_mmap(256) 82 let max_new: nx_int = 1 83 84 let prng: *i64 = sys_mmap(8) as *i64 85 prng[0] = 1 86 87 // Qwen2.5-0.5B published config: 88 // rms_norm_eps = 1e-6 -> f32 0x358637BD 89 // rope_freq_base = 1000000 -> log(1e6) = 13.8155 -> f32 0x415D0EAB 90 // head_dim = 64 -> 1/sqrt(64) = 0.125 -> f32 0x3E000000 91 let eps: i64 = 0x358637BD // 1e-6 92 let attn_scale: i64 = 0x3E000000 // 0.125 93 let rope_base: i64 = 0x415D0EAB // log(1000000) 94 95 let n_emit: nx_int = nx_f32_llm_run_v3( 96 model, vocab, cache, prompt, 24, max_new, cfg, 97 eps, attn_scale, rope_base, 1, 98 prng, eos, out_bytes, 256) 99 if n_emit < 0 { 100 // Encode the negative as 60 + |n_emit| (capped at 9). 101 var ne: nx_int = 0 - n_emit 102 if ne > 9 { ne = 9 } 103 return (60 + ne) as i64 104 } 105 106 sys_write(1, "GEN[" as *u8, 4) 107 sys_write(1, out_bytes, n_emit as i64) 108 sys_write(1, "]\n" as *u8, 2) 109 // Write output to the scratchpad (a real /mnt/c mount -> sandbox-independent, readable from Windows) 110 let out_path: *u8 = "/mnt/c/Users/elder/AppData/Local/Temp/claude/C--Users-elder/10730ed9-0e7d-4843-ad8a-e2d4a8122cbd/scratchpad/genout.txt\x00" as *u8 111 let fd: i64 = sys_openat_wr(out_path, 0x1A4) 112 if fd < 0 { return 70 } 113 sys_write(fd, out_bytes, n_emit as i64) 114 sys_close(fd) 115 116 // Return n_emit so caller knows how many bytes were generated. 117 if n_emit > 250 { return 250 } 118 return n_emit as i64 119}