code wiki / (root) / nx_bench_stress_emit_test.nx

nx_bench_stress_emit_test.nx source

↩ module page · 141 lines · 5374 B

1// nx_bench_stress_emit_test.nx -- runs the stress workload AND emits 2// the substrate-honest TSV row to stdout so operators see numbers. 3// 4// This is the FIRST end-to-end "hard real" bench in the substrate: 5// 1. Build real synthetic GGUF + BPE fixture 6// 2. Build session + spawn LLM actor + listener 7// 3. Drive 10 autoregressive iterations of REAL transformer forward 8// 4. Capture wall-clock + substrate counters 9// 5. Emit TSV header + data row to fd=1 (stdout) 10// 11// Output appears in the smoke driver's log so the human can READ the 12// actual numbers, not just see "PASS". 13// 14// HONEST SCOPE: wall-clock is qemu-emulated -- meaningful for 15// substrate-internal comparison + as the call-pattern that physical- 16// hardware bench rigs will use unchanged. 17 18import "nx_syscalls.nx" 19import "nx_tier.nx" 20import "nx_clock.nx" 21import "nx_actor.nx" 22import "nx_message.nx" 23import "nx_session.nx" 24import "nx_bench_companion.nx" 25import "nx_bench_companion_timed.nx" 26import "nx_bench_companion_tsv.nx" 27import "nx_gguf_fixture_tiny.nx" 28import "nx_actor_role_llm_v2.nx" 29 30func main() -> i64 { 31 let now: nx_size = 1000000 32 let N_ITERS: nx_int = 10 33 34 // ===== Substrate setup ===== 35 let fix: *NxGgufFixtureBundle = nx_gft_build_tiny_llama(0xfeed) 36 if nx_gft_is_built(fix) != 1 { return 1 } 37 let s: *NxSession = nx_session_new_default(now) 38 if nx_session_is_ready(s) != 1 { return 2 } 39 let LLM: nx_int = 8001 40 let LIS: nx_int = 8099 41 nx_session_spawn_actor(s, LLM, 1, 80, 0, now) 42 nx_session_spawn_actor(s, LIS, 5, 50, 0, now) 43 nx_session_subscribe(s, LIS, NX_MS_KIND_LLM_TOKEN) 44 45 let prompt: *u8 = sys_mmap(1) 46 prompt[0] = 0x61 47 let ctx: *NxLlmV2ActorCtx = nx_lv_actor_new( 48 fix.spec, fix.gguf_buf, fix.hdr, fix.bpe, 49 prompt, 1, 50 1024, 4, fix.prng_state, 10000, 724) 51 if (ctx as i64) == 0 { return 3 } 52 53 // ===== Wall-clock + workload ===== 54 let base: *NxBenchReport = nx_bc_report_new() 55 let timed: *NxBenchTimedReport = nx_bctm_new(base) 56 timed.wallclock_start_ns = nx_clock_monotonic_ns() 57 58 var iter: nx_int = 0 59 var tick: nx_size = now + 100 60 while iter < N_ITERS { 61 nx_lv_actor_step(ctx, s.scheduler, s.bus, LLM, tick) 62 tick = tick + 50 63 nx_lv_actor_step(ctx, s.scheduler, s.bus, LLM, tick) 64 if ctx.runner_result < 0 { return 10 + iter } 65 tick = tick + 50 66 nx_lv_actor_step(ctx, s.scheduler, s.bus, LLM, tick) 67 if ctx.current_phase != NX_LV_PHASE_DONE { return 20 + iter } 68 tick = tick + 50 69 if iter < (N_ITERS - 1) { 70 let new_byte: nx_int = nx_gft_vocab_byte(fix, ctx.runner_result) 71 prompt[0] = new_byte as u8 72 nx_lv_actor_reset_for_next_token(ctx, s.scheduler, LLM, prompt, 1) 73 } 74 iter = iter + 1 75 } 76 77 nx_bctm_capture_finish(timed, s, 11) 78 79 // ===== Emit TSV header + data row to stdout ===== 80 let buf: *u8 = sys_mmap(512) 81 82 let n_hdr: nx_int = nx_bc_emit_tsv_header(buf, 512) 83 if n_hdr <= 0 { return 4 } 84 sys_write(1, buf, n_hdr as i64) 85 86 // Label: "stress_v2_10iter" (16 bytes) 87 let label: *u8 = sys_mmap(16) 88 label[0]=0x73 as u8; label[1]=0x74 as u8; label[2]=0x72 as u8; label[3]=0x65 as u8 89 label[4]=0x73 as u8; label[5]=0x73 as u8; label[6]=0x5F as u8; label[7]=0x76 as u8 90 label[8]=0x32 as u8; label[9]=0x5F as u8; label[10]=0x31 as u8; label[11]=0x30 as u8 91 label[12]=0x69 as u8; label[13]=0x74 as u8; label[14]=0x65 as u8; label[15]=0x72 as u8 92 93 let n_row: nx_int = nx_bc_emit_tsv_row(buf, 512, base, label, 16) 94 if n_row <= 0 { return 5 } 95 sys_write(1, buf, n_row as i64) 96 97 // ===== Also emit elapsed_us as a separate stdout line ===== 98 // Format: "wallclock_elapsed_us\t<NUMBER>\n" 99 let elapsed_label: *u8 = sys_mmap(21) 100 elapsed_label[0]=0x77 as u8; elapsed_label[1]=0x61 as u8; elapsed_label[2]=0x6C as u8 101 elapsed_label[3]=0x6C as u8; elapsed_label[4]=0x63 as u8; elapsed_label[5]=0x6C as u8 102 elapsed_label[6]=0x6F as u8; elapsed_label[7]=0x63 as u8; elapsed_label[8]=0x6B as u8 103 elapsed_label[9]=0x5F as u8; elapsed_label[10]=0x65 as u8; elapsed_label[11]=0x6C as u8 104 elapsed_label[12]=0x61 as u8; elapsed_label[13]=0x70 as u8; elapsed_label[14]=0x73 as u8 105 elapsed_label[15]=0x65 as u8; elapsed_label[16]=0x64 as u8; elapsed_label[17]=0x5F as u8 106 elapsed_label[18]=0x75 as u8; elapsed_label[19]=0x73 as u8; elapsed_label[20]=0x09 as u8 107 sys_write(1, elapsed_label, 21) 108 109 // Decimal-emit elapsed_us value 110 let n_emit: *u8 = sys_mmap(32) 111 var v: i64 = timed.wallclock_elapsed_us 112 if v < 0 { v = 0 - v } 113 var k: i64 = 0 114 if v == 0 { 115 n_emit[0] = 0x30 as u8 116 k = 1 117 } 118 while v > 0 { 119 n_emit[k] = (0x30 + (v - (v / 10) * 10)) as u8 120 v = v / 10 121 k = k + 1 122 } 123 // Reverse + write 124 let rev: *u8 = sys_mmap(32) 125 var j: i64 = 0 126 while j < k { 127 rev[j] = n_emit[k - 1 - j] 128 j = j + 1 129 } 130 rev[k] = 0x0A as u8 // \n 131 sys_write(1, rev, k + 1) 132 133 // ===== Substrate-honest invariant checks ===== 134 if base.total_actor_steps != 30 { return 110 } 135 if base.actors_completed != 1 { return 111 } 136 if nx_ms_pending(s.bus, LIS) != N_ITERS { return 112 } 137 if timed.wallclock_elapsed_us < 0 { return 113 } 138 if nx_bctm_verdict(timed) != NX_BCTM_OK { return 114 } 139 140 return 0 141}