okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2b36e8a02
main
1#!/usr/bin/env python3 2"""Decode bounds of a quant from its real per-layer formats (tools/quant_formats.py --json output). 3 4python research/tools/ab-harnesses-2026-10/quant_bound.py FMT.json [...] --ram-gbs 120 [--vram-gbs 640] [--hit 0.3] [--tokens-per-pass 1] 5 6Per token: the routed experts (10 of 512 per layer: each layer's gate + up + down bytes / 512 x 10), the shared expert 7and the dense side (attention, SSM, hyper-connections, router, head: read once per pass on the GPU). The bound for a 8pass of P tokens (a verify window) with a VRAM hit fraction h of the routed experts: 9t_cpu = (1 - h) x routed bytes x U(P) / RAM bandwidth (the CPU pool reads every missed expert once per pass) 10t_gpu = (h x routed x U(P) + dense) / VRAM bandwidth 11tok/s <= P / max(t_cpu, t_gpu) 12U(P) is the distinct experts a pass of P tokens reads per expert a single token reads (1 for P = 1; measured from 13the engine's window logs where known, else --union). The CPU line alone (h = 0) is the floor of the all-on-CPU case. 14""" 15from __future__import annotations 16 17import argparse 18import json 19import pathlib 20 21TOPK ,N_EXPERT = 10 ,512 22 23 24def bytes_of (js :dict )-> dict : 25roles = js ["roles" ] 26tot = lambda role :sum (v ["bytes" ]for v in roles .get (role , {}).values ()) 27routed_all = tot ("routed gate" )+ tot ("routed up" )+ tot ("routed down" ) 28per_token = routed_all / N_EXPERT * TOPK 29dense = sum (tot (r )for r in ("shared expert" ,"router" ,"attention" ,"SSM" ,"hyper-connections" ,"indexer" , 30"PLE key/value" ,"output head" )) 31return {"routed_all" :routed_all ,"routed_per_token" :per_token ,"dense_per_pass" :dense , 32"expert_bytes" :routed_all / 48 / N_EXPERT } 33 34 35def main ()-> int : 36ap = argparse .ArgumentParser (description = __doc__ ,formatter_class = argparse .RawDescriptionHelpFormatter ) 37ap .add_argument ("fmt" ,nargs = "+" ) 38ap .add_argument ("--ram-gbs" ,type = float ,required = True ,help = "measured RAM read bandwidth, GB/s" ) 39ap .add_argument ("--vram-gbs" ,type = float ,default = 0 ,help = "GPU memory bandwidth, GB/s (0: no GPU line)" ) 40ap .add_argument ("--hit" ,type = float ,default = 0.0 ,help = "VRAM hit fraction of the routed experts" ) 41ap .add_argument ("--tokens-per-pass" ,type = float ,default = 1.0 ) 42ap .add_argument ("--union" ,type = float ,default = 1.0 ,help = "distinct experts per pass / experts per token" ) 43a = ap .parse_args () 44f" { 'quant' :12s } { 'expert MB' :>9s } { 'routed MB/tok' :>13s } { 'dense MB' :>9s } { 'CPU-only tok/s' :>15s } " 45f" { 'bound tok/s' :>11s } " ) 46for f in a .fmt : 47b = bytes_of (json .loads (pathlib .Path (f ).read_text ())) 48r = b ["routed_per_token" ]* a .union 49t_cpu = (1 - a .hit )* r / (a .ram_gbs * 1e9 ) 50t_gpu = (a .hit * r + b ["dense_per_pass" ])/ (a .vram_gbs * 1e9 )if a .vram_gbs else 0.0 51cpu_only = 1 / (b ["routed_per_token" ]/ (a .ram_gbs * 1e9 )) 52bound = a .tokens_per_pass / max (t_cpu ,t_gpu )if max (t_cpu ,t_gpu )> 0 else float ("inf" ) 53f" { pathlib . Path ( f ). stem :12s } { b [ 'expert_bytes' ] / 1e6 :9.2f } { b [ 'routed_per_token' ] / 1e6 :13.1f } " 54f" { b [ 'dense_per_pass' ] / 1e6 :9.1f } { cpu_only :15.1f } { bound :11.1f } " ) 55return 0 56 57 58if __name__ == "__main__" : 59raise SystemExit (main ())