nx_quad_real_emit_test.nx source
↩ module page · 205 lines · 8424 B
1// nx_quad_real_emit_test.nx -- 4-modality REAL workload with wall-clock.
2//
3// Where nx_quad_companion_compose used the v1 synthetic 4-vocab LLM
4// adapter, THIS smoke runs the production-shape 4-modality scene:
5//
6// NARRATOR v2 LLM (REAL Llama-class forward) priority 80
7// NPC v2 LLM (REAL Llama-class forward) priority 60
8// AUDIO_QC nx_audio_scene_grade (5-axis Q14 verdict) priority 50
9// GAME nx_tictactoe PERFECT vs PERFECT priority 70
10//
11// All four share one nx_session. Wall-clock measured around the whole
12// concurrent workload. TSV row + elapsed_us emitted to stdout.
13//
14// HONEST SCOPE: cooperative interleaving under qemu-riscv64, NOT
15// physical-parallel kernel execution. But the substrate-honest claim
16// PROVABLE today: one session hosts four heterogeneous modality actors
17// each invoking a DIFFERENT other-agent primitive (transformer + audio
18// + game) without state cross-contamination.
19
20import "nx_syscalls.nx"
21import "nx_tier.nx"
22import "nx_clock.nx"
23import "nx_actor.nx"
24import "nx_message.nx"
25import "nx_stream_multiplexer.nx"
26import "nx_session.nx"
27import "nx_bench_companion.nx"
28import "nx_bench_companion_timed.nx"
29import "nx_bench_companion_tsv.nx"
30import "nx_gguf_fixture_tiny.nx"
31import "nx_actor_role_llm_v2.nx"
32import "nx_actor_role_audio_grader.nx"
33import "nx_actor_role_game_logic.nx"
34
35func _emit_dec_i64(fd: i64, n: i64) -> i64 {
36 let scratch: *u8 = sys_mmap(32)
37 var v: i64 = n
38 if v < 0 { v = 0 - v }
39 var k: i64 = 0
40 if v == 0 { scratch[0] = 0x30 as u8; k = 1 }
41 while v > 0 {
42 scratch[k] = (0x30 + (v - (v / 10) * 10)) as u8
43 v = v / 10
44 k = k + 1
45 }
46 let rev: *u8 = sys_mmap(32)
47 var j: i64 = 0
48 while j < k { rev[j] = scratch[k - 1 - j]; j = j + 1 }
49 rev[k] = 0x0A as u8
50 sys_write(fd, rev, k + 1)
51 return 0
52}
53
54func main() -> i64 {
55 let now: nx_size = 1000000
56 let N_LLM_ITERS: nx_int = 3
57
58 // ===== LLM fixtures (independent seeds for NARRATOR + NPC) =====
59 let fixA: *NxGgufFixtureBundle = nx_gft_build_tiny_llama(0xAA)
60 let fixB: *NxGgufFixtureBundle = nx_gft_build_tiny_llama(0xBB)
61 if nx_gft_is_built(fixA) != 1 { return 1 }
62 if nx_gft_is_built(fixB) != 1 { return 2 }
63
64 // ===== Audio scene fixture =====
65 let bins_buf: *u8 = sys_mmap(64)
66 let bins: *i64 = bins_buf as *i64
67 var b: nx_int = 0
68 while b < 8 { bins[b] = 5000 + ((b * 700) as i64); b = b + 1 }
69 let rms_buf: *u8 = sys_mmap(128)
70 let rms: *i64 = rms_buf as *i64
71 var f: nx_int = 0
72 while f < 16 { rms[f] = 2400 + ((f * 200) as i64); f = f + 1 }
73
74 // ===== Shared session =====
75 let s: *NxSession = nx_session_new_default(now)
76 if nx_session_is_ready(s) != 1 { return 3 }
77 let NARR: nx_int = 9001
78 let NPC: nx_int = 9002
79 let AUD: nx_int = 9003
80 let GAME: nx_int = 9004
81 let LIS: nx_int = 9099
82 nx_session_spawn_actor(s, NARR, 1, 80, 0, now)
83 nx_session_spawn_actor(s, NPC, 1, 60, 0, now)
84 nx_session_spawn_actor(s, AUD, 4, 50, 0, now)
85 nx_session_spawn_actor(s, GAME, 5, 70, 0, now)
86 nx_session_spawn_actor(s, LIS, 5, 30, 0, now)
87 nx_session_subscribe(s, LIS, NX_MS_KIND_LLM_TOKEN)
88 nx_session_subscribe(s, LIS, NX_MS_KIND_AUDIO_FRAME)
89 nx_session_subscribe(s, LIS, NX_MS_KIND_GAME_ACTION)
90
91 // ===== Actor contexts =====
92 let prompt_n: *u8 = sys_mmap(1); prompt_n[0] = 0x61
93 let prompt_p: *u8 = sys_mmap(1); prompt_p[0] = 0x63
94 let ctxN: *NxLlmV2ActorCtx = nx_lv_actor_new(
95 fixA.spec, fixA.gguf_buf, fixA.hdr, fixA.bpe, prompt_n, 1,
96 1024, 4, fixA.prng_state, 10000, 724)
97 let ctxP: *NxLlmV2ActorCtx = nx_lv_actor_new(
98 fixB.spec, fixB.gguf_buf, fixB.hdr, fixB.bpe, prompt_p, 1,
99 1024, 4, fixB.prng_state, 10000, 724)
100 let ctxA: *NxAudioGraderCtx = nx_ag_actor_new(bins, 8, rms, 16, 4)
101 let ctxG: *NxGameLogicCtx = nx_gl_actor_new(NX_TTT_X, NX_TTT_AI_PERFECT, NX_TTT_AI_PERFECT, 19)
102
103 // ===== Wall-clock start =====
104 let base: *NxBenchReport = nx_bc_report_new()
105 let timed: *NxBenchTimedReport = nx_bctm_new(base)
106 timed.wallclock_start_ns = nx_clock_monotonic_ns()
107
108 // ===== Drive workload =====
109 // NARR + NPC: 3 autoregressive iterations each (REAL transformer)
110 // AUD: 3 phases (one-shot grading)
111 // GAME: 9 plies (PERFECT vs PERFECT to DRAW)
112 var iter: nx_int = 0
113 var tick: nx_size = now + 100
114 while iter < N_LLM_ITERS {
115 nx_lv_actor_step(ctxN, s.scheduler, s.bus, NARR, tick); tick = tick + 50
116 nx_lv_actor_step(ctxN, s.scheduler, s.bus, NARR, tick); tick = tick + 50
117 nx_lv_actor_step(ctxN, s.scheduler, s.bus, NARR, tick); tick = tick + 50
118 let tokN: nx_int = ctxN.runner_result
119
120 nx_lv_actor_step(ctxP, s.scheduler, s.bus, NPC, tick); tick = tick + 50
121 nx_lv_actor_step(ctxP, s.scheduler, s.bus, NPC, tick); tick = tick + 50
122 nx_lv_actor_step(ctxP, s.scheduler, s.bus, NPC, tick); tick = tick + 50
123 let tokP: nx_int = ctxP.runner_result
124
125 if iter < (N_LLM_ITERS - 1) {
126 prompt_n[0] = nx_gft_vocab_byte(fixA, tokN) as u8
127 prompt_p[0] = nx_gft_vocab_byte(fixB, tokP) as u8
128 nx_lv_actor_reset_for_next_token(ctxN, s.scheduler, NARR, prompt_n, 1)
129 nx_lv_actor_reset_for_next_token(ctxP, s.scheduler, NPC, prompt_p, 1)
130 }
131 iter = iter + 1
132 }
133
134 // Audio grader: 3 cooperative phases
135 nx_ag_actor_step(ctxA, s.scheduler, s.bus, AUD, tick); tick = tick + 50
136 nx_ag_actor_step(ctxA, s.scheduler, s.bus, AUD, tick); tick = tick + 50
137 nx_ag_actor_step(ctxA, s.scheduler, s.bus, AUD, tick); tick = tick + 50
138
139 // Game: drive until COMPLETED (PERFECT vs PERFECT terminates in 9 plies)
140 var game_v: nx_int = NX_GL_V_STEPPED
141 var game_n: nx_int = 0
142 while game_v == NX_GL_V_STEPPED {
143 game_v = nx_gl_actor_step(ctxG, s.scheduler, s.bus, GAME, tick)
144 tick = tick + 50
145 game_n = game_n + 1
146 if game_n > 12 { return 4 }
147 }
148
149 nx_bctm_capture_finish(timed, s, 11)
150
151 // ===== Emit TSV header + data row =====
152 let buf: *u8 = sys_mmap(512)
153 let n_hdr: nx_int = nx_bc_emit_tsv_header(buf, 512)
154 if n_hdr <= 0 { return 5 }
155 sys_write(1, buf, n_hdr as i64)
156
157 // Label: "quad_real_v2_3iter" (18 bytes)
158 let label: *u8 = sys_mmap(18)
159 label[0]=0x71 as u8; label[1]=0x75 as u8; label[2]=0x61 as u8; label[3]=0x64 as u8
160 label[4]=0x5F as u8; label[5]=0x72 as u8; label[6]=0x65 as u8; label[7]=0x61 as u8
161 label[8]=0x6C as u8; label[9]=0x5F as u8; label[10]=0x76 as u8; label[11]=0x32 as u8
162 label[12]=0x5F as u8; label[13]=0x33 as u8; label[14]=0x69 as u8; label[15]=0x74 as u8
163 label[16]=0x65 as u8; label[17]=0x72 as u8
164
165 let n_row: nx_int = nx_bc_emit_tsv_row(buf, 512, base, label, 18)
166 if n_row <= 0 { return 6 }
167 sys_write(1, buf, n_row as i64)
168
169 // ===== wallclock_elapsed_us line =====
170 let elab: *u8 = sys_mmap(21)
171 elab[0]=0x77 as u8; elab[1]=0x61 as u8; elab[2]=0x6C as u8; elab[3]=0x6C as u8
172 elab[4]=0x63 as u8; elab[5]=0x6C as u8; elab[6]=0x6F as u8; elab[7]=0x63 as u8
173 elab[8]=0x6B as u8; elab[9]=0x5F as u8; elab[10]=0x65 as u8; elab[11]=0x6C as u8
174 elab[12]=0x61 as u8; elab[13]=0x70 as u8; elab[14]=0x73 as u8; elab[15]=0x65 as u8
175 elab[16]=0x64 as u8; elab[17]=0x5F as u8; elab[18]=0x75 as u8; elab[19]=0x73 as u8
176 elab[20]=0x09 as u8
177 sys_write(1, elab, 21)
178 _emit_dec_i64(1, timed.wallclock_elapsed_us)
179
180 // ===== Substrate invariants under 4-modality load =====
181 //
182 // NARR: 9 steps (3 iter * 3 phases)
183 // NPC: 9 steps
184 // AUD: 3 steps
185 // GAME: 9 steps (one per ply for tic-tac-toe to draw)
186 // Total: 30 actor steps
187 if base.total_actor_steps != 30 { return 100 }
188 // NARR + NPC + AUD + GAME = 4 completed; LIS still READY
189 if base.actors_completed != 4 { return 101 }
190 if base.actors_failed != 0 { return 102 }
191 if base.n_actors != 5 { return 103 }
192
193 // Listener: 3 + 3 + 1 + 9 = 16 messages (LLM_TOKEN*6 + AUDIO_FRAME*1 + GAME_ACTION*9)
194 if nx_ms_pending(s.bus, LIS) != 16 { return 104 }
195
196 // Audio grader score positive (healthy synthetic scene)
197 if nx_ag_actor_score_q14(ctxA) <= 0 { return 105 }
198
199 // Game outcome: PERFECT vs PERFECT must DRAW
200 if nx_gl_actor_outcome(ctxG) != NX_TTT_DRAW { return 106 }
201
202 if timed.wallclock_elapsed_us < 0 { return 107 }
203
204 return 0
205}