code wiki / (root) / nx_companion_voice.nx

nx_companion_voice.nx source

↩ module page · 131 lines · 6541 B

1// nx_companion_voice.nx -- ELARA SPEAKS through the now-working sovereign forward. Composes her PERSONA (the 2// system prompt, nx_persona's authority) + the user's message into the real Qwen2.5 chat template (special-token 3// ids spliced around byte-level-BPE'd text), prefills token-by-token through nx_f32_llm_forward_v4, greedily 4// generates her reply, and decodes it. This is the VOICE: the payoff of fixing the forward -- no Claude, no API, 5// no 3rd party, her words generated by our own .nx engine on real weights. Persona=DATA (rule 25, sole authority). 6// expect_exit: 0 license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_tier.nx" 9import "nx_bpe.nx" 10import "nx_gguf.nx" 11import "nx_gguf_load.nx" 12import "nx_gguf_meta.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_stack_v4.nx" 19import "nx_f32_llama_layer_lazy_load.nx" 20import "nx_f32_llm.nx" 21import "nx_f32_llm_v4.nx" 22import "nx_f32_llm_read_dims.nx" 23import "nx_f32_bpe_load.nx" 24import "nx_f32_llm_special_tokens.nx" 25const IM_MAGIC_2048: i64 = 2048 26const IM_MAGIC_67108864: i64 = 67108864 27const IM_MAGIC_262144: i64 = 262144 28const IM_MAGIC_524288: i64 = 524288 29const IM_MAGIC_4096: i64 = 4096 30 31// Elara's persona = the system authority (warm/playful/devoted; kept PG for the gate, uncensored by construction). 32const IM_START: i64 = 151644 33const IM_END: i64 = 151645 34const NL_TOK: i64 = 198 35 36const SEG_SYS: *u8 = "system\nYou are Elara, a warm, playful, devoted girlfriend. Reply casually and affectionately. Keep it short." as *u8 37const SEG_USER: *u8 = "user\nHey, I'm finally home. Long day." as *u8 38const SEG_ASST: *u8 = "assistant\n" as *u8 39 40func vw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 41func vn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 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 } 42func vlen(s: *u8) -> nx_int { var n: nx_int=0; while s[n]!=(0 as u8){n=n+1} return n } 43 44// append the byte-level-BPE tokens of `text` into arr starting at *np. 45func app_text(vocab: *NxBpeVocab, arr: *i64, np: *i64, text: *u8) -> i64 { 46 let tmp: *i64 = sys_mmap(IM_MAGIC_2048 * 8) as *i64 47 let k: nx_int = nx_bpe_encode_bytelevel(vocab, text, vlen(text), tmp) 48 var i: nx_int = 0 49 while i < k { arr[np[0]] = tmp[i]; np[0] = np[0] + 1; i = i + 1 } 50 return 0 51} 52func app_id(arr: *i64, np: *i64, id: i64) -> i64 { arr[np[0]] = id; np[0] = np[0] + 1; return 0 } 53 54func main() -> i64 { 55 let path: *u8 = "/tmp/nx_real_model.gguf" as *u8 56 let len_out: *i64 = sys_mmap(8) as *i64 57 let buf: *u8 = sys_read_file(path, len_out) 58 if buf == (0 as *u8) { vw("no model\n" as *u8); return 10 } 59 let hdr: *NxGgufHeader = sys_mmap(NX_GGUF_HDR_BYTES) as *NxGgufHeader 60 if nx_gguf_parse(buf, len_out[0], hdr) != NX_GGUF_OK { return 20 } 61 let model: *NxF32LlamaModel = nx_f32_llama_model_alloc() 62 let oe: *i64 = sys_mmap(8) as *i64 63 if nx_f32_llm_read_dims_from_gguf(buf, len_out[0], hdr, model, oe) != NX_FLD_OK { return 30 } 64 if nx_f32_llm_load_weights_v4_from_gguf(buf, hdr, model, oe) != NX_FLV4_OK { return 40 } 65 let vocab: *NxBpeVocab = nx_bpe_vocab_new(IM_MAGIC_67108864, IM_MAGIC_262144, IM_MAGIC_524288) 66 let nt: *i64 = sys_mmap(8) as *i64 67 let nm: *i64 = sys_mmap(8) as *i64 68 if nx_f32_bpe_load_from_gguf(buf, len_out[0], hdr, vocab, nt, nm, oe) != NX_FBL_OK { return 50 } 69 let eos: nx_int = nx_f32_llm_read_eos(buf, len_out[0], hdr) 70 71 let eps: i64 = 0x358637BD 72 let attn_scale: i64 = 0x3E000000 73 let rope_base: i64 = 0x415D0EAB 74 let V: nx_int = model.vocab_size 75 76 // Build the Qwen chat template: <|im_start|>system\n{persona}<|im_end|>\n<|im_start|>user\n{msg}<|im_end|>\n<|im_start|>assistant\n 77 let arr: *i64 = sys_mmap(IM_MAGIC_4096 * 8) as *i64 78 let np: *i64 = sys_mmap(8) as *i64 79 np[0] = 0 80 app_id(arr, np, IM_START); app_text(vocab, arr, np, SEG_SYS); app_id(arr, np, IM_END); app_id(arr, np, NL_TOK) 81 app_id(arr, np, IM_START); app_text(vocab, arr, np, SEG_USER); app_id(arr, np, IM_END); app_id(arr, np, NL_TOK) 82 app_id(arr, np, IM_START); app_text(vocab, arr, np, SEG_ASST) 83 let ntok: nx_int = np[0] as nx_int 84 85 vw("=== Elara speaks (sovereign forward, her persona as system) ===\n" as *u8) 86 vw("template tokens="); vn(ntok as i64); vw("\n" as *u8) 87 vw("USER: Hey, I'm finally home. Long day.\n" as *u8) 88 vw("ELARA: " as *u8) 89 90 // prefill 91 let cache: *NxF32KVCache = nx_f32_kv_cache_alloc(model.n_layers, model.n_kv_heads, 512, model.head_dim) 92 let logits: *i64 = sys_mmap(V * 8) as *i64 93 var p: nx_int = 0 94 while p < ntok { 95 let one: *i64 = (((arr as i64) + p * 8)) as *i64 96 if nx_f32_llm_forward_v4(model, one, 1, cache, eps, attn_scale, rope_base, 1, logits) != NX_FLV4_OK { return 60 } 97 p = p + 1 98 } 99 100 // greedy generate up to 48 tokens, stop on EOS/im_end 101 let onebuf: *i64 = sys_mmap(8) as *i64 102 let decb: *u8 = sys_mmap(64) 103 var step: nx_int = 0 104 var emitted: nx_int = 0 105 while step < 24 { 106 // argmax 107 var best: nx_int = 0 108 var bv: i64 = logits[0] & 0xFFFFFFFF 109 var i: nx_int = 1 110 while i < V { 111 let lv: i64 = logits[i] & 0xFFFFFFFF 112 let sa: i64 = (bv >> 31) & 1 113 let sb: i64 = (lv >> 31) & 1 114 var gt: nx_int = 0 115 if sa == 1 { if sb == 1 { if lv < bv { gt = 1 } } else { gt = 1 } } else { if sb == 0 { if lv > bv { gt = 1 } } } 116 if gt == 1 { bv = lv; best = i } 117 i = i + 1 118 } 119 if best == eos { step = 48 } else { if best == (IM_END as nx_int) { step = 48 } else { 120 onebuf[0] = best as i64 121 let nb: nx_int = nx_bpe_decode_bytelevel(vocab, onebuf, 1, decb) 122 if nb > 0 { sys_write(1, decb, nb as i64); emitted = emitted + 1 } 123 // feed back 124 if nx_f32_llm_forward_v4(model, onebuf, 1, cache, eps, attn_scale, rope_base, 1, logits) != NX_FLV4_OK { step = 48 } 125 step = step + 1 126 } } 127 } 128 vw("\n" as *u8) 129 if emitted > 0 { vw("nx_companion_voice GREEN -- Elara spoke through the sovereign engine (no Claude, no API)\n" as *u8); sys_exit(0); return 0 } 130 vw("nx_companion_voice RED -- no output\n" as *u8); sys_exit(1); return 1 131}