nx_costest_lib.nx
buildroot/runtime/nx_costest_lib.nx
about
nx_costest_lib.nx -- THE ONE COST ESTIMATOR (2026-09-16): three-point estimates (P10, P50, P90) in the boards'
own unit, their aggregation over components, the CALIBRATION of that unit against RECORDED ACTUALS, and the
compute-cost rules that turn a model size into hours at a measured rate.
WHY. Operator 2026-09-16: "if we need 3rd party today then id like to know via the /compare capabilities what the
lift would be to replace a 3rd party we are using thats open source etc with our own in cost", and "if the
capability to estimate cost gets us to sota for our nishi analyst and other analysis systems like flipping cars
or investing then im fine investing". The estate already prices a car flip by its P10 downside (nx_carflip_lib
cf_score, nx_flip_score) and every board declares a cost per rung in u ("1 u = one measured session-leg", the
plan's own unit row) -- but nothing CALIBRATED u against what the boards themselves recorded, and nothing
aggregated a set of estimates honestly. Ranker rung PR7 (rk_cost_recal, "actuals write back") named this gap
on 2026-08-20 and it stayed _ABSENT_. This lib is that primitive, shared by the foreign-asset ledger
(nx_assets_lib), the ranker's cost model, the car-flip and investing lanes and the analyst.
THE UNIT. Estimates are held as DECI-U (1.5 u is the integer 15), so the plan's "1.5" and "8.5" compare as
integers; hours are held as CENTI-HOURS (12.34 h is 1234). A calibration rate is centi-hours PER U.
AGGREGATION (ces_agg3). Components estimated independently combine as: P50 = sum of medians; the lower and
upper half-widths (P50-P10, P90-P50) combine by root-sum-square, which is the three-point (PERT-class) rule
for independent uncertainties. The straight sums of P10 and P90 are returned BESIDE the RSS figures as the
fully-correlated bound, because a reader who sees only the RSS band will read it as the worst case and it is
not: when every component slips together, the correlated sum is the honest ceiling.
CALIBRATION (ces_census_plan, ces_cal_*). The boards journal `log|<epoch>|<rung>|<kind>|<text>` rows and every
rung declares its cost in u. For a rung with at least one `land` row, the ACTUAL is the wall-clock from its
FIRST journal row of any kind to its LAST land row -- a ceiling on effort, since it includes waiting -- and the
ratio actual-hours per declared-u is one calibration sample. The quantiles of that sample (P10, P50, P90) are
the calibration; a rung whose land row is its first row (zero elapsed) or whose cost is absent is EXCLUDED
BY NAME and counted, never silently dropped, and the partition WITHLAND = CALIBRATED + EXCL_ZERO + EXCL_NOCOST
is printed so a reader can check it sums. The ledger line (ces_cal_line) is written and parsed by ONE grammar in
this file, so the writer (nx_costcal) and every reader (the compare emitter) cannot drift apart.
QUANTILE RANK. index = permil * (n - 1) / 1000 (nearest rank, the same convention nx_bench_stats uses for its
p95). ces_cal_min_n derives the smallest n at which P10, P50 and P90 index THREE DISTINCT RANKS -- below it a
"P10" is the same sample as the median and the band is decoration. That bound is computed, not typed.
COMPUTE RULES. Training costs about 6 * params * tokens FLOPs (forward and backward) and inference about
2 * params * tokens (the standard 6ND / 2ND rules of the scaling-law literature); ces_seconds divides by a
MEASURED rate the caller passes in. The rate is data: the llm board measures the CPU engine at 819 ms per
token for a 7B model, and a GPU rate is a bar until the estate measures one.
dependencies 1 imports · 9 importers
imports: nx_syscalls.nx
imported by: nx_assets_gate.nxnx_assets_lib.nxnx_costcal.nxnx_costest_gate.nxnx_rungclose_lib.nxnx_seat.nxnx_swcompare_lib.nxnx_workmix.nxnx_workmix_lib.nx
structs
| none |
consts
| 47 | const CES_I64: i64 = 8 |
| 48 | const CES_NONE: i64 = 0 - 1 // malformed or unavailable |
| 49 | const CES_ABSENT: i64 = 0 - 2 // the field is "-": declared absent, not malformed |
| 50 | const CES_DECI: i64 = 10 // u held as deci-u |
| 51 | const CES_CENTI: i64 = 100 // hours held as centi-hours |
| 52 | const CES_PERMIL: i64 = 1000 |
| 53 | const CES_P10: i64 = 100 |
| 54 | const CES_P50: i64 = 500 |
| 55 | const CES_P90: i64 = 900 |
| 56 | const CES_SEC_PER_HOUR: i64 = 3600 |
| 57 | const CES_TRAIN_FLOPS_PER_PT: i64 = 6 // forward + backward per parameter per token (the 6ND rule) |
| 58 | const CES_INFER_FLOPS_PER_PT: i64 = 2 // forward only |
| 59 | const CES_CH_PIPE: i64 = 124 |
| 60 | const CES_CH_NL: i64 = 10 |
| 61 | const CES_CH_DOT: i64 = 46 |
| 62 | const CES_CH_DASH: i64 = 45 |
| 63 | const CES_CH_COLON: i64 = 58 |
| 64 | const CES_CH_0: i64 = 48 |
| 65 | const CES_CH_9: i64 = 57 |
| 66 | const CES_CH_HASH: i64 = 35 |
| 67 | const CES_NUM_CAP: i64 = 24 |
| 69 | const CES_A_P10: i64 = 0 |
| 70 | const CES_A_P50: i64 = 1 |
| 71 | const CES_A_P90: i64 = 2 |
| 72 | const CES_A_SUM10: i64 = 3 |
| 73 | const CES_A_SUM50: i64 = 4 |
| 74 | const CES_A_SUM90: i64 = 5 |
| 75 | const CES_A_N: i64 = 6 |
| 76 | const CES_A_SLOTS: i64 = 8 |
| 78 | const CES_S_ASOF: i64 = 0 |
| 79 | const CES_S_BOARDS: i64 = 1 |
| 80 | const CES_S_RUNGS: i64 = 2 |
| 81 | const CES_S_LOGROWS: i64 = 3 |
| 82 | const CES_S_UNMATCHED: i64 = 4 |
| 83 | const CES_S_WITHLAND: i64 = 5 |
| 84 | const CES_S_CALIBRATED: i64 = 6 |
| 85 | const CES_S_EXCL_ZERO: i64 = 7 |
| 86 | const CES_S_EXCL_NOCOST: i64 = 8 |
| 87 | const CES_S_P10: i64 = 9 |
| 88 | const CES_S_P50: i64 = 10 |
| 89 | const CES_S_P90: i64 = 11 |
| 90 | const CES_S_MINN: i64 = 12 |
| 91 | const CES_S_OVERFLOW: i64 = 13 // samples or rows that did not fit their reserve: announced, never silent |
| 92 | const CES_S_LANDEVENTS: i64 = 14 // land event rows written (EC56): every land on a declared rung, not only the calibrated ones |
| 93 | const CES_S_RETRACTS: i64 = 15 // retract event rows written (EC56): the rework record |
| 94 | const CES_S_OPEN: i64 = 16 // rungs with journal rows and NO land row: started, not landed (EC57 WIP) |
| 95 | const CES_S_SLOTS: i64 = 24 |
| 96 | const CES_CAL_TAG: *u8 = "cal" |
| 97 | const CES_CAL_NF: i64 = 14 |
| 99 | const CES_RUNG_TAG: *u8 = "rung" |
| 100 | const CES_LOG_TAG: *u8 = "log" |
| 101 | const CES_LAND: *u8 = "land" |
| 102 | const CES_RETRACT: *u8 = "retract" |
| 105 | const CES_EV_F_DOM: i64 = 1 |
| 106 | const CES_EV_F_RUNG: i64 = 2 |
| 107 | const CES_EV_F_EPOCH: i64 = 3 |
| 108 | const CES_EV_F_COST: i64 = 4 |
| 109 | const CES_EV_LAND_NF: i64 = 5 |
| 110 | const CES_EV_RETRACT_NF: i64 = 4 |
| 111 | const CES_MIN_EVENT_ROW: i64 = 13 // "land|a|b|1|0" plus its newline: the shortest well-formed event row |
| 112 | const CES_W_LAND_EVENTS: i64 = 0 // ces_events_window accumulator slots |
| 113 | const CES_W_LANDED_RUNGS: i64 = 1 |
| 114 | const CES_W_RETRACTS: i64 = 2 |
| 115 | const CES_W_LANDED_DECIU: i64 = 3 |
| 116 | const CES_W_EVENTS_TOTAL: i64 = 4 |
| 117 | const CES_W_RELANDED: i64 = 5 |
| 118 | const CES_W_SLOTS: i64 = 8 |
| 119 | const CES_OPEN_TAG: *u8 = "open" // ledger WIP row (EC57): open|domain|rung|first_epoch|last_epoch|cost_deciu |
| 120 | const CES_OPEN_NF: i64 = 6 |
| 121 | const CES_OPEN_F_FIRST: i64 = 3 |
| 122 | const CES_OPEN_F_LAST: i64 = 4 |
| 123 | const CES_WIP_ACTIVE_S: i64 = 604800 // seven days, the shift gauge's own window: touched inside it is in flight, outside it is stale |
| 124 | const CES_WIP_TOP: i64 = 3 // the most recently touched open rungs a WIP line names |
| 125 | const CES_O_OPEN: i64 = 0 // ces_open_summary accumulator slots |
| 126 | const CES_O_ACTIVE: i64 = 1 |
| 127 | const CES_O_STALE: i64 = 2 |
| 128 | const CES_O_OLDEST: i64 = 3 |
| 129 | const CES_O_SLOTS: i64 = 8 |
| 130 | const CES_RUNG_F_ID: i64 = 1 |
| 131 | const CES_RUNG_F_COST: i64 = 6 |
| 132 | const CES_RUNG_NF: i64 = 8 |
| 133 | const CES_LOG_F_EPOCH: i64 = 1 |
| 134 | const CES_LOG_F_RUNG: i64 = 2 |
| 135 | const CES_LOG_F_KIND: i64 = 3 |
| 136 | const CES_LOG_NF: i64 = 5 |
| 137 | const CES_MIN_RUNG_ROW: i64 = 18 // "rung|a|b|c|d|e|1|-" plus its newline: the shortest well-formed rung row |
| 138 | const CES_ROW_RESERVE: i64 = 256 // one ledger row: domain, rung and six numbers |
functions
| 140 | func ces_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } |
| 141 | func ces_cat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; var oo: i64 = o; while s[i] != (0 as u8) { d[oo] = s[i]; oo = oo + 1; i = i + 1 } d[oo] = 0 as u8; return oo } |
| 144 | func ces_isqrt(v: i64) -> i64 |
| 154 | func ces_agg3(p10: *i64, p50: *i64, p90: *i64, n: i64, out: *i64) -> i64 |
| 180 | func ces_line_end(buf: *u8, n: i64, p: i64) -> i64 |
| 192 | func ces_field(buf: *u8, p: i64, e: i64, k: i64, off: *i64) -> i64 |
| 208 | func ces_nfields(buf: *u8, p: i64, e: i64) -> i64 |
| 217 | func ces_span_is(buf: *u8, off: i64, len: i64, lit: *u8) -> i64 called by 34: fas_state_codefas_kind_codefas_loadfas_plan_has_rungmainces_events_window+28 calls 1: ces_slen |
| 225 | func ces_span_eq(buf: *u8, aoff: i64, alen: i64, boff: i64, blen: i64) -> i64 |
| 234 | func ces_parse_int(buf: *u8, off: i64, len: i64) -> i64 |
| 249 | func ces_parse_deciu(buf: *u8, off: i64, len: i64) -> i64 |
| 280 | func ces_fmt_int(dst: *u8, o: i64, v: i64) -> i64 |
| 294 | func ces_fmt_deci(dst: *u8, o: i64, v: i64) -> i64 |
| 306 | func ces_fmt_centi(dst: *u8, o: i64, v: i64) -> i64 |
| 320 | func ces_sort(v: *i64, n: i64) -> i64 |
| 335 | func ces_qidx(n: i64, permil: i64) -> i64 |
| 340 | func ces_quantile(sorted: *i64, n: i64, permil: i64) -> i64 |
| 346 | func ces_cal_min_n() -> i64 |
| 362 | func ces_hours_centi(deciu: i64, rate_centih: i64) -> i64 called by 1: main |
| 368 | func ces_train_flops(params: i64, tokens: i64) -> i64 { return CES_TRAIN_FLOPS_PER_PT * params * tokens } called by 1: main |
| 369 | func ces_infer_flops(params: i64, tokens: i64) -> i64 { return CES_INFER_FLOPS_PER_PT * params * tokens } called by 1: main |
| 371 | func ces_seconds(flops: i64, flops_per_s: i64) -> i64 called by 1: main |
| 379 | func ces_event_row(rows: *u8, rcap: i64, ro: *i64, st: *i64, tag: *u8, dom: *u8, buf: *u8, goff: i64, gl: i64, epoch: i64, cost: i64) -> i64 |
| 401 | func ces_events_window(buf: *u8, n: i64, lo: i64, hi: i64, acc: *i64) -> i64 called by 2: mainst_shift calls 7: sys_mmapces_line_endces_fieldces_nfieldsces_span_isces_parse_int+1 |
| 455 | func ces_open_row(rows: *u8, rcap: i64, ro: *i64, st: *i64, dom: *u8, buf: *u8, goff: i64, gl: i64, first: i64, last: i64, cost: i64) -> i64 |
| 477 | func ces_open_summary(buf: *u8, n: i64, now: i64, active_s: i64, acc: *i64, toff: *i64, tlen: *i64) -> i64 |
| 522 | func ces_census_plan(buf: *u8, n: i64, dom: *u8, samples: *i64, scap: i64, sc: *i64, st: *i64, rows: *u8, rcap: i64, ro: *i64) -> i64 called by 2: cc_walkmain calls 13: sys_mmapces_line_endces_fieldces_span_isces_nfieldsces_parse_deciu+7 |
| 625 | func ces_cal_line(dst: *u8, o: i64, st: *i64) -> i64 |
| 639 | func ces_cal_parse(buf: *u8, n: i64, st: *i64) -> i64 called by 2: ces_cal_loadst_shift calls 6: sys_mmapces_line_endces_fieldces_span_isces_nfieldsces_parse_int |
| 663 | func ces_cal_load(path: *u8, st: *i64) -> i64 |