nx_qwen_native_gate.nx source
↩ module page · 105 lines · 4798 B
1// nx_qwen_native_gate.nx -- NATIVE WINDOWS " Paris" forward.
2// Loads the real Qwen2.5-0.5B from a Windows path via sys_read_file (native
3// CreateFile/ReadFile bridge), runs the ST greedy forward (NX_Q8_ST_MAC_MAX
4// forced huge -> no pool/futex/clone), prints GEN[...] via sys_write. Compile
5// with the FULL compiler (nx_compile_x86.elf), wrap with nx_pe_natbw, run native.
6// license_tier: ORIGINAL expect_exit: n_emit
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"
25import "nx_f32_sampler.nx"
26import "nx_f32_llm_run_v2.nx"
27import "nx_f32_llm_run_v3.nx"
28import "nx_thread_pool.nx"
29
30const NX_GATE_NTOK: i64 = 24 // tokens to emit (use 2 vs 34 to load-subtract per-tok decode)
31const NX_GATE_NATIVE: i64 = 1 // 0=all serial ; 1=matmul->pteam-12, block->inline (PROVEN 1.8x, 2026-07-13; BEST) ; 2=block->capped-pool (REJECTED: two-team contention, 13-25s vs 12s, 2026-07-14)
32
33func _put_num(v: i64) -> i64 {
34 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
35 let digits: *u8 = sys_mmap(32)
36 var n: i64 = v
37 var d: i64 = 0
38 while n > 0 { digits[d] = (48 + (n - (n / 10) * 10)) as u8; n = n / 10; d = d + 1 }
39 let out: *u8 = sys_mmap(32)
40 var i: i64 = 0
41 while i < d { out[i] = digits[d - 1 - i]; i = i + 1 }
42 sys_write(1, out, d)
43 return 0
44}
45
46func main() -> i64 {
47 // mode 0: everything serial. mode 1: matmul->pteam-12 (g_pool_native), block
48 // ops inline-serial (g_pool_serial) so they don't touch the channel pool.
49 // mode 2: matmul->pteam-12 AND block ops->capped native channel pool (thread
50 // the ~24%-of-token block ops too, testing if that closes the gap to WSL2).
51 if NX_GATE_NATIVE == 0 { nx_pool_set_serial(1) }
52 else if NX_GATE_NATIVE == 1 { nx_pool_set_serial(1); nx_pool_set_native(1) }
53 else { nx_pool_set_native(1) }
54 let path: *u8 = "C:/Users/elder/nishi-core/nxc2/_scratch/model.gguf\x00" as *u8
55 let len_out: *i64 = sys_mmap(8) as *i64
56 let buf: *u8 = sys_read_file(path, len_out)
57 if buf == (0 as *u8) { sys_write(1, "MODEL-NULL\n" as *u8, 11); return 10 }
58 if len_out[0] < 1000 { sys_write(1, "MODEL-SHORT\n" as *u8, 12); return 11 }
59
60 let hdr: *NxGgufHeader = sys_mmap(NX_GGUF_HDR_BYTES) as *NxGgufHeader
61 if nx_gguf_parse(buf, len_out[0], hdr) != NX_GGUF_OK { return 20 }
62 let model: *NxF32LlamaModel = nx_f32_llama_model_alloc()
63 let out_err: *i64 = sys_mmap(8) as *i64
64 if nx_f32_llm_read_dims_from_gguf(buf, len_out[0], hdr, model, out_err) != NX_FLD_OK { return 30 }
65 if nx_f32_llm_load_weights_v4_from_gguf(buf, hdr, model, out_err) != NX_FLV4_OK { return 40 }
66 let vocab: *NxBpeVocab = nx_bpe_vocab_new(67108864, 262144, 524288)
67 let nt: *i64 = sys_mmap(8) as *i64
68 let nm: *i64 = sys_mmap(8) as *i64
69 let bpe_v: nx_int = nx_f32_bpe_load_from_gguf(buf, len_out[0], hdr, vocab, nt, nm, out_err)
70 if bpe_v != NX_FBL_OK { return (150 + bpe_v) as i64 }
71 let eos: nx_int = nx_f32_llm_read_eos(buf, len_out[0], hdr)
72 let cache: *NxF32KVCache = nx_f32_kv_cache_alloc(model.n_layers, model.n_kv_heads, 64, model.head_dim)
73 if cache == (0 as *NxF32KVCache) { return 53 }
74 let cfg: *NxF32SamplerCfg = nx_f32_sampler_cfg_alloc()
75
76 let prompt: *u8 = "The capital of France is\x00" as *u8
77 let out_bytes: *u8 = sys_mmap(1024)
78 let prng: *i64 = sys_mmap(8) as *i64
79 prng[0] = 1
80 let eps: i64 = 0x358637BD
81 let attn_scale: i64 = 0x3E000000
82 let rope_base: i64 = 0x415D0EAB
83
84 sys_write(1, "LOADED, generating...\n" as *u8, 22)
85 // internal timing: sys_now_us is Linux clock_gettime in WSL2 and the
86 // emitter-patched QueryPerformanceCounter/10 thunk on native -> RUN_US is
87 // load-immune (excludes the ~9-15s model load) on BOTH targets.
88 let t0: i64 = sys_now_us()
89 let n_emit: nx_int = nx_f32_llm_run_v3(
90 model, vocab, cache, prompt, 24, NX_GATE_NTOK, cfg,
91 eps, attn_scale, rope_base, 1,
92 prng, eos, out_bytes, 512)
93 let t1: i64 = sys_now_us()
94 sys_write(1, "RUN_US=" as *u8, 7); _put_num(t1 - t0); sys_write(1, "\n" as *u8, 1)
95 if n_emit < 0 {
96 var ne: nx_int = 0 - n_emit
97 if ne > 9 { ne = 9 }
98 return (60 + ne) as i64
99 }
100 sys_write(1, "GEN[" as *u8, 4)
101 sys_write(1, out_bytes, n_emit as i64)
102 sys_write(1, "]\n" as *u8, 2)
103 if n_emit > 250 { return 250 }
104 return n_emit as i64
105}