code wiki / _hdl_build / nx_eff_conv.nx
nx_eff_conv.nx source
↩ module page · 62 lines · 3946 B
1// nx_eff_conv.nx -- pure spend->money/energy conversion core (eff lane). SYSCALL-FREE integer math,
2// so it compiles anywhere (NAS buildroot incl older toolchains) -- decoupled from the getdents/fstatat
3// SCAN code in nx_eff_lib so the board (a plane READER) never drags scan syscalls into its compile.
4// Shared by nx_eff_lib (scan CLI) AND nx_eff_board_lib (ROI board) = DRY (rule 15).
5// Coefficients DECLARED + emitted transparently by every consumer (rule 11, operator "non-fake numbers").
6// license_tier: ORIGINAL No hw writes (Rule 26).
7
8// weights x100 (basis points) = per-token cost relative to uncached input, Anthropic pricing shape.
9const EFL_W_IN: i64 = 100
10const EFL_W_OUT: i64 = 500
11const EFL_W_CR: i64 = 10
12const EFL_W_CW: i64 = 125
13const EFL_CENTS_PER_MTOK_IN: i64 = 1500 // uncached input $15.00/Mtok (Opus-class UPPER-BOUND default; editable)
14const EFL_MONEY_DIV: i64 = 100000000 // bp(x100) x per-Mtok(1e6) normalizer
15// ENERGY: compute-weighted (energy tracks FLOPs). We reuse the SAME class multipliers as cost (output 5x,
16// cache-read 0.1x, cache-write 1.25x vs input 1x) because both scale with compute -> cache reads (KV lookups)
17// are correctly cheap, not full-energy (the old flat all-tokens rate OVER-counted cache energy ~24x).
18// CALIBRATED to fetched research (2026-07-19): ~5e-4 Wh per OUTPUT token (Joule 2026 S2542-4351(26)00114-5;
19// TokenPowerBench arXiv 2512.03024; measured GPT-4o 3e-4, LLaMA-3.1-405B 8.6e-4 Wh/tok). At weight 500 (output),
20// energy_wh = weighted_bp / EFL_WH_ENERGY_DIV yields exactly 5e-4 Wh/output-tok. ESTIMATE, conservative-mid:
21// reasoning-heavy models (Opus-class) trend HIGHER (research: up to ~100x for long reasoning).
22const EFL_WH_ENERGY_DIV: i64 = 1000000
23
24func efl_weighted_bp(ti: i64, to_: i64, cr: i64, cw: i64) -> i64 { return ti*EFL_W_IN + to_*EFL_W_OUT + cr*EFL_W_CR + cw*EFL_W_CW }
25// money in cents = weighted-bp x cents/Mtok / normalizer (integer; no float ever)
26func efl_cost_cents(ti: i64, to_: i64, cr: i64, cw: i64) -> i64 {
27 let bp: i64 = efl_weighted_bp(ti, to_, cr, cw)
28 return bp * EFL_CENTS_PER_MTOK_IN / EFL_MONEY_DIV
29}
30// energy Wh estimate: compute-weighted (cache reads cheap, output expensive), calibrated to research ~5e-4 Wh/output-tok
31func efl_energy_wh(ti: i64, to_: i64, cr: i64, cw: i64) -> i64 {
32 let bp: i64 = efl_weighted_bp(ti, to_, cr, cw)
33 return bp / EFL_WH_ENERGY_DIV
34}
35
36// ---- PER-MODEL ATTRIBUTION (real cost, not the flat ceiling) ----
37// Pricing FETCHED 2026-07-19 from platform.claude.com/docs/en/docs/about-claude/pricing (DECLARED, editable).
38// The output/cache multipliers are UNIVERSAL (output 5x, cache-write 1.25x, cache-read 0.1x of base input),
39// so per-model cost = weighted-bp(that model's tokens) x that MODEL's base-input rate. Only the input rate differs.
40const EFL_T_FABLE: i64 = 0
41const EFL_T_OPUS: i64 = 1
42const EFL_T_SONNET: i64 = 2
43const EFL_T_HAIKU: i64 = 3
44const EFL_T_OTHER: i64 = 4
45const EFL_NTIER: i64 = 5
46const EFL_RATE_FABLE: i64 = 1000 // Fable 5 / Mythos 5 base input $10.00/Mtok
47const EFL_RATE_OPUS: i64 = 500 // Opus 4.5-4.8 base input $5.00/Mtok
48const EFL_RATE_SONNET: i64 = 300 // Sonnet 4.5/4.6 $3 (Sonnet 5 intro $2 is lower -> conservative-high)
49const EFL_RATE_HAIKU: i64 = 100 // Haiku 4.5 base input $1.00/Mtok
50const EFL_RATE_OTHER: i64 = 500 // unknown model -> Opus-ish conservative
51func efl_tier_rate(tier: i64) -> i64 {
52 if tier == EFL_T_FABLE { return EFL_RATE_FABLE }
53 if tier == EFL_T_OPUS { return EFL_RATE_OPUS }
54 if tier == EFL_T_SONNET { return EFL_RATE_SONNET }
55 if tier == EFL_T_HAIKU { return EFL_RATE_HAIKU }
56 return EFL_RATE_OTHER
57}
58// cost cents for ONE tier's tokens at that tier's input rate (universal weighted-bp x per-model input rate)
59func efl_cost_tier(tier: i64, ti: i64, to_: i64, cr: i64, cw: i64) -> i64 {
60 let bp: i64 = efl_weighted_bp(ti, to_, cr, cw)
61 return bp * efl_tier_rate(tier) / EFL_MONEY_DIV
62}