code wiki / _hdl_build / nx_cost_oracle.nx
nx_cost_oracle.nx source
↩ module page · 53 lines · 2845 B
1// nx_cost_oracle.nx -- bits-up DETERMINISTIC measurement for fair performance head-to-heads (operator:
2// "avoid the pitfalls of emulators and all the other stuff"). The emulator/timing pitfall: wall-clock
3// and emulated cycle counts are NON-deterministic (vary by machine, emulator, cache, frequency scaling),
4// so judging speed by time is BIASED -- the same two programs can flip winners across runs. The fix,
5// from the hardware up: judge by a COUNTED INVARIANT that is identical every run -- retired instruction
6// count, gate-toggle (switching) activity, or bytes moved. The cost oracle REFUSES to render a verdict
7// on a non-deterministic metric. This is the performance arm of the Referee's DETERMINISM gate.
8// license_tier: ORIGINAL Pairs with nx_referee (fair judging) + nx_gate_energy (toggles) + nx_uops_cost.
9
10import "nx_syscalls.nx"
11
12const CO_WALLCLOCK: i64 = 0 // wall-clock / emulated time -- NON-deterministic -> UNFAIR to judge on
13const CO_INSN: i64 = 1 // retired instruction count -- deterministic
14const CO_TOGGLES: i64 = 2 // gate switching activity -- deterministic, hardware-portable
15const CO_BYTES: i64 = 3 // memory bytes moved -- deterministic
16
17const CO_REJECT: i64 = 0 - 1 // refuse to judge (non-deterministic metric)
18const CO_TIE: i64 = 0
19const CO_A_FASTER: i64 = 1
20const CO_B_FASTER: i64 = 2
21
22// a metric is FAIR for head-to-head only if DETERMINISTIC (same input -> same value every run).
23func co_metric_deterministic(kind: i64) -> i64 { if kind == CO_WALLCLOCK { return 0 } return 1 }
24
25// are these repeated measurements reproducible (all identical)? the empirical determinism check.
26func co_reproducible(samples: *i64, n: i64) -> i64 {
27 if n <= 1 { return 1 }
28 var i: i64 = 1
29 while i < n { if samples[i] != samples[0] { return 0 } i = i + 1 }
30 return 1
31}
32
33func co_abs(x: i64) -> i64 { if x < 0 { return 0 - x } return x }
34
35// fair performance verdict -- ONLY on a deterministic metric; lower cost wins if the margin clears noise.
36func co_fair_verdict(kind: i64, cost_a: i64, cost_b: i64, margin: i64) -> i64 {
37 if co_metric_deterministic(kind) == 0 { return CO_REJECT } // never judge speed on emulated time
38 if co_abs(cost_a - cost_b) < margin { return CO_TIE }
39 if cost_a < cost_b { return CO_A_FASTER }
40 return CO_B_FASTER
41}
42
43// would judging on wall-clock have FLIPPED across runs? (true bias warning: min/max disagree on winner)
44func co_wallclock_flips(a_samples: *i64, b_samples: *i64, n: i64) -> i64 {
45 var a_ahead: i64 = 0; var b_ahead: i64 = 0; var i: i64 = 0
46 while i < n {
47 if a_samples[i] < b_samples[i] { a_ahead = 1 }
48 if b_samples[i] < a_samples[i] { b_ahead = 1 }
49 i = i + 1
50 }
51 if a_ahead == 1 { if b_ahead == 1 { return 1 } } // both led on some run -> the verdict is noise
52 return 0
53}