code wiki / (root) / nx_costest_lib.nx

nx_costest_lib.nx

buildroot/runtime/nx_costest_lib.nx

30849 B672 linesdepth 2pulls 2 transitivereach 43 importersview sourcekind library
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_costest_lib.nx nx_assets_gate.nx nx_assets_lib.nx nx_costcal.nx nx_costest_gate.nx nx_rungclose_lib.nx nx_seat.nx nx_swcompare_lib.nx nx_workmix.nx nx_workmix_lib.nx

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

47const CES_I64: i64 = 8
48const CES_NONE: i64 = 0 - 1 // malformed or unavailable
49const CES_ABSENT: i64 = 0 - 2 // the field is "-": declared absent, not malformed
50const CES_DECI: i64 = 10 // u held as deci-u
51const CES_CENTI: i64 = 100 // hours held as centi-hours
52const CES_PERMIL: i64 = 1000
53const CES_P10: i64 = 100
54const CES_P50: i64 = 500
55const CES_P90: i64 = 900
56const CES_SEC_PER_HOUR: i64 = 3600
57const CES_TRAIN_FLOPS_PER_PT: i64 = 6 // forward + backward per parameter per token (the 6ND rule)
58const CES_INFER_FLOPS_PER_PT: i64 = 2 // forward only
59const CES_CH_PIPE: i64 = 124
60const CES_CH_NL: i64 = 10
61const CES_CH_DOT: i64 = 46
62const CES_CH_DASH: i64 = 45
63const CES_CH_COLON: i64 = 58
64const CES_CH_0: i64 = 48
65const CES_CH_9: i64 = 57
66const CES_CH_HASH: i64 = 35
67const CES_NUM_CAP: i64 = 24
69const CES_A_P10: i64 = 0
70const CES_A_P50: i64 = 1
71const CES_A_P90: i64 = 2
72const CES_A_SUM10: i64 = 3
73const CES_A_SUM50: i64 = 4
74const CES_A_SUM90: i64 = 5
75const CES_A_N: i64 = 6
76const CES_A_SLOTS: i64 = 8
78const CES_S_ASOF: i64 = 0
79const CES_S_BOARDS: i64 = 1
80const CES_S_RUNGS: i64 = 2
81const CES_S_LOGROWS: i64 = 3
82const CES_S_UNMATCHED: i64 = 4
83const CES_S_WITHLAND: i64 = 5
84const CES_S_CALIBRATED: i64 = 6
85const CES_S_EXCL_ZERO: i64 = 7
86const CES_S_EXCL_NOCOST: i64 = 8
87const CES_S_P10: i64 = 9
88const CES_S_P50: i64 = 10
89const CES_S_P90: i64 = 11
90const CES_S_MINN: i64 = 12
91const CES_S_OVERFLOW: i64 = 13 // samples or rows that did not fit their reserve: announced, never silent
92const CES_S_LANDEVENTS: i64 = 14 // land event rows written (EC56): every land on a declared rung, not only the calibrated ones
93const CES_S_RETRACTS: i64 = 15 // retract event rows written (EC56): the rework record
94const CES_S_OPEN: i64 = 16 // rungs with journal rows and NO land row: started, not landed (EC57 WIP)
95const CES_S_SLOTS: i64 = 24
96const CES_CAL_TAG: *u8 = "cal"
97const CES_CAL_NF: i64 = 14
99const CES_RUNG_TAG: *u8 = "rung"
100const CES_LOG_TAG: *u8 = "log"
101const CES_LAND: *u8 = "land"
102const CES_RETRACT: *u8 = "retract"
105const CES_EV_F_DOM: i64 = 1
106const CES_EV_F_RUNG: i64 = 2
107const CES_EV_F_EPOCH: i64 = 3
108const CES_EV_F_COST: i64 = 4
109const CES_EV_LAND_NF: i64 = 5
110const CES_EV_RETRACT_NF: i64 = 4
111const CES_MIN_EVENT_ROW: i64 = 13 // "land|a|b|1|0" plus its newline: the shortest well-formed event row
112const CES_W_LAND_EVENTS: i64 = 0 // ces_events_window accumulator slots
113const CES_W_LANDED_RUNGS: i64 = 1
114const CES_W_RETRACTS: i64 = 2
115const CES_W_LANDED_DECIU: i64 = 3
116const CES_W_EVENTS_TOTAL: i64 = 4
117const CES_W_RELANDED: i64 = 5
118const CES_W_SLOTS: i64 = 8
119const CES_OPEN_TAG: *u8 = "open" // ledger WIP row (EC57): open|domain|rung|first_epoch|last_epoch|cost_deciu
120const CES_OPEN_NF: i64 = 6
121const CES_OPEN_F_FIRST: i64 = 3
122const CES_OPEN_F_LAST: i64 = 4
123const CES_WIP_ACTIVE_S: i64 = 604800 // seven days, the shift gauge's own window: touched inside it is in flight, outside it is stale
124const CES_WIP_TOP: i64 = 3 // the most recently touched open rungs a WIP line names
125const CES_O_OPEN: i64 = 0 // ces_open_summary accumulator slots
126const CES_O_ACTIVE: i64 = 1
127const CES_O_STALE: i64 = 2
128const CES_O_OLDEST: i64 = 3
129const CES_O_SLOTS: i64 = 8
130const CES_RUNG_F_ID: i64 = 1
131const CES_RUNG_F_COST: i64 = 6
132const CES_RUNG_NF: i64 = 8
133const CES_LOG_F_EPOCH: i64 = 1
134const CES_LOG_F_RUNG: i64 = 2
135const CES_LOG_F_KIND: i64 = 3
136const CES_LOG_NF: i64 = 5
137const CES_MIN_RUNG_ROW: i64 = 18 // "rung|a|b|c|d|e|1|-" plus its newline: the shortest well-formed rung row
138const CES_ROW_RESERVE: i64 = 256 // one ledger row: domain, rung and six numbers

functions

140func ces_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
141func 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 }
144func ces_isqrt(v: i64) -> i64
called by 2: mainces_agg3
154func ces_agg3(p10: *i64, p50: *i64, p90: *i64, n: i64, out: *i64) -> i64
called by 2: fas_totalsmain calls 1: ces_isqrt
180func ces_line_end(buf: *u8, n: i64, p: i64) -> i64
192func ces_field(buf: *u8, p: i64, e: i64, k: i64, off: *i64) -> i64
208func ces_nfields(buf: *u8, p: i64, e: i64) -> i64
217func ces_span_is(buf: *u8, off: i64, len: i64, lit: *u8) -> i64
225func ces_span_eq(buf: *u8, aoff: i64, alen: i64, boff: i64, blen: i64) -> i64
234func ces_parse_int(buf: *u8, off: i64, len: i64) -> i64
249func ces_parse_deciu(buf: *u8, off: i64, len: i64) -> i64
280func ces_fmt_int(dst: *u8, o: i64, v: i64) -> i64
294func ces_fmt_deci(dst: *u8, o: i64, v: i64) -> i64
called by 1: main calls 1: ces_fmt_int
306func ces_fmt_centi(dst: *u8, o: i64, v: i64) -> i64
called by 1: main calls 1: ces_fmt_int
320func ces_sort(v: *i64, n: i64) -> i64
called by 2: mainmain
335func ces_qidx(n: i64, permil: i64) -> i64
340func ces_quantile(sorted: *i64, n: i64, permil: i64) -> i64
called by 2: mainmain calls 1: ces_qidx
346func ces_cal_min_n() -> i64
called by 2: mainmain calls 1: ces_qidx
362func ces_hours_centi(deciu: i64, rate_centih: i64) -> i64
called by 1: main
368func ces_train_flops(params: i64, tokens: i64) -> i64 { return CES_TRAIN_FLOPS_PER_PT * params * tokens }
called by 1: main
369func ces_infer_flops(params: i64, tokens: i64) -> i64 { return CES_INFER_FLOPS_PER_PT * params * tokens }
called by 1: main
371func ces_seconds(flops: i64, flops_per_s: i64) -> i64
called by 1: main
379func 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
401func ces_events_window(buf: *u8, n: i64, lo: i64, hi: i64, acc: *i64) -> i64
455func 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
477func ces_open_summary(buf: *u8, n: i64, now: i64, active_s: i64, acc: *i64, toff: *i64, tlen: *i64) -> i64
522func ces_census_plan(buf: *u8, n: i64, dom: *u8, samples: *i64, scap: i64, sc: *i64, st: *i64, rows: *u8, rcap: i64, ro: *i64) -> i64
625func ces_cal_line(dst: *u8, o: i64, st: *i64) -> i64
called by 2: mainmain calls 2: ces_catces_fmt_int
639func ces_cal_parse(buf: *u8, n: i64, st: *i64) -> i64
663func ces_cal_load(path: *u8, st: *i64) -> i64