okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
dc87d45eb
main
1import { localeTag } from './i18n' ; 2/** Shared, deterministic UI estimates. Demo curves are never benchmark evidence. */ 3import type { Hardware , ModelVariant } from './types' ; 4export type Profile = 'agentic' | 'balanced' | 'performance' ; 5export type Workload = 'short' | 'document' | 'agent' ; 6export const PROFILES = [ 7{ id :'agentic' as const , label :'Agentic' , icon :'tool' as const , purpose :'Tools and long prompts' }, 8{ id :'balanced' as const , label :'Balanced' , icon :'layers' as const , purpose :'Everyday work' }, 9{ id :'performance' as const , label :'Performance' , icon :'bolt' as const , purpose :'Faster answers' }, 10]; 11export const WORKLOADS = [ 12{ id :'short' as const , label :'Short text' , tokens :1024 }, 13{ id :'document' as const , label :'Document text' , tokens :4096 }, 14{ id :'agent' as const , label :'Long context' , tokens :16384 }, 15]; 16export const OUTPUT_TOKENS = 256 ; 17export type Range = { 18low :number ; 19high :number ; 20}; 21export interface Execution { 22context :number ; 23batch :number ; 24kv :string ; 25placement :string ; 26threads :number ; 27} 28export interface SpeedEvidence { 29source :'estimated' | 'measured' ; 30hardware :string ; 31backend :string ; 32config :string ; 33workload :Workload ; 34cache :'cold-prompt' ; 35reading :Range ; 36answer :Range ; 37date :string ; 38attribution :string ; 39} 40export interface Experience { 41config :Execution ; 42workload :Workload ; 43identity :string ; 44reading :Range | null ; 45answer :Range | null ; 46source :'demo' | 'estimated' | 'measured' | 'unavailable' ; 47provenance :string ; 48memory :{ 49weights :number ; 50cache :number ; 51runtime :number ; 52total :number ; 53budget :number | null ; 54remaining :number | null ; 55unified :boolean ; 56systemBudget :number | null ; 57pool :string ; 58placementKnown :boolean ; 59} | null ; 60fit :'comfortable' | 'tight' | 'too-large' | 'unknown' | 'unsupported' ; 61fitsWorkload :boolean ; 62} 63export const DEFAULT_EXECUTION :Execution = { context :32768 , batch :2048 , kv :'INT8' , placement :'Automatic' , threads :8 }; 64export const RECOMMENDED_CONTEXT_TOKENS = 128 * 1024 ; 65export function contextTokens ( value :string ) { return value === 'recommended' ?RECOMMENDED_CONTEXT_TOKENS :Number . parseInt ( value , 10 ) * 1024 ; } 66export function execution ( values :Record < string , unknown > = {}, context = 32768 ) :Execution { 67return { context, batch :Number ( values [ 'perf.batch' ] ?? 2048 ), kv :String ( values [ 'ctx.kvMode' ] ?? 'INT8' ), placement :String ( values [ 'gpu.layers' ] ?? 'Automatic' ), threads :Number ( values [ 'perf.threads' ] ?? 8 ) }; 68} 69export function hardwareKey ( hw :Hardware | null ) { return hw ?` ${ hw . cpu . model } | ${ hw . gpus . map ( g => g . name ). join ( '+' )} | ${ hw . memory . totalGb } ` :'unknown' ; } 70export function configKey ( v :ModelVariant , c :Execution ) { return ` ${ v . id } | ${ c . context } | ${ c . batch } | ${ c . kv } | ${ c . placement } | ${ c . threads } ` ; } 71export function metadata ( v :ModelVariant ) { 72return v . catalog ?? { family :v . family . split ( '-' )[ 0 ], version :v . family , purpose :v . blurb , capabilities :[ 'Text' ], agentApproved :false , balancedRank :999 , review :'Not reviewed for agent tasks' }; 73} 74function validRange ( value :Range ) { return Number . isFinite ( value . low ) && Number . isFinite ( value . high ) && value . low > 0 && value . high >= value . low ; } 75export function estimate ( v :ModelVariant | undefined , hw :Hardware | null , config :Execution , workload :Workload = 'document' , demo = false ) :Experience { 76const needed = WORKLOADS . find ( w => w . id === workload ) ! . tokens + OUTPUT_TOKENS ; 77const result :Experience = { config, workload, identity :v ?configKey ( v , config ) :'none' , reading :null , answer :null , source :'unavailable' , provenance :'No comparable speed evidence for this configuration.' , memory :null , fit :'unknown' , fitsWorkload :needed <= config . context }; 78if ( ! v || ! Number . isFinite ( config . context ) || config . context <= 0 || ! Number . isFinite ( config . batch ) || config . batch <= 0 ) 79return result ; 80if ( config . context > v . contextMax ) 81result . fit = 'unsupported' ; 82if ( v . kvKbPerToken !== undefined && Number . isFinite ( config . context ) && config . context > 0 ) { 83const cpu = hw ?. accelerator . backend === 'cpu' || config . placement === 'CPU only' ; 84const unified = !! hw ?. accelerator . unified ; 85const placementKnown = cpu || unified || [ 'Automatic' , 'All layers' ]. includes ( config . placement ); 86const budget = ! hw ?null :cpu || unified ?hw . memory . totalGb * .72 :hw . accelerator . usableVramGb ; 87const cache = v . kvKbPerToken * config . context / 1024 ** 2 * ( config . kv === 'FP16' ?2 :config . kv === 'Q4_0' ?.5 :1 ); 88const runtime = v . experts > 0 ?2.5 :1.2 ; 89const total = v . diskGb + cache + runtime ; 90result . memory = { weights :v . diskGb , cache, runtime, total, budget :placementKnown ?budget :null , remaining :budget !== null && placementKnown ?budget - total :null , unified, systemBudget :hw && ! unified && ! cpu ?hw . memory . totalGb * .72 :null , pool :cpu ?'System memory' :unified ?'Unified memory' :'GPU memory' , placementKnown}; 91if ( result . fit !== 'unsupported' ) 92result . fit = budget === null || ! placementKnown ?'unknown' :total > budget ?'too-large' :total > budget * .85 ?'tight' :'comfortable' ; 93} 94if ( ! result . fitsWorkload || result . fit === 'unsupported' ) { 95result . provenance = 'The sample or context is not supported by this setup.' ; 96return result ; 97} 98const evidence = v . performance ?. find ( e => e . hardware === hardwareKey ( hw ) && e . backend === hw ?. accelerator . backend && e . config === result . identity && e . workload === workload && e . cache === 'cold-prompt' && validRange ( e . reading ) && validRange ( e . answer )); 99if ( evidence ) { 100result . reading = evidence . reading ; 101result . answer = evidence . answer ; 102result . source = evidence . source ; 103result . provenance = ` ${ evidence . attribution } · ${ evidence . date } · ${ evidence . backend } · uncached prompt` ; 104} 105else if ( demo && hw ) { 106// Illustrative curves only: deliberately isolated from real evidence. 107const weight = Math . max ( .5 , v . diskGb ); 108const batchEffect = Math . max ( .65 , 1 - Math . abs ( Math . log2 ( Math . max ( 1 , config . batch ) / 2048 )) * .08 ); 109const contextEffect = 1 / ( 1 + Math . max ( 0 , config . context - 8192 ) / 262144 ); 110const reading = 1100 / Math . sqrt ( weight + 1 ) * batchEffect * contextEffect ; 111const answer = 80 / Math . sqrt ( weight + 1 ) * contextEffect ; 112result . reading = { low :Math . round ( reading * .85 ), high :Math . round ( reading * 1.15 ) }; 113result . answer = { low :Math . max ( 1 , Math . round ( answer * .85 )), high :Math . max ( 2 , Math . round ( answer * 1.15 )) }; 114result . source = 'demo' ; 115result . provenance = 'Illustrative UI fixture · not a measurement or a hardware prediction' ; 116} 117return result ; 118} 119export const FIT_TEXT = { comfortable :'Comfortable fit · estimated' , tight :'Memory may be tight' , 'too-large' :'Exceeds memory budget' , unknown :'Fit unknown' , unsupported :'Context not supported' }; 120export function speedText ( range :Range | null ) { return range ?` ${ Math . round ( range . low ). toLocaleString ( localeTag ())} – ${ Math . round ( range . high ). toLocaleString ( localeTag ())} ` :'Not estimated' ; } 121export function durationText ( tokens :number , range :Range | null ) { return range ?` ${( tokens / range . high ). toFixed ( 1 )} – ${( tokens / range . low ). toFixed ( 1 )} s` :'Not estimated' ; } 122export function sourceText ( e :Experience ) { return e . source === 'demo' ?'Illustrative estimate' :e . source === 'estimated' ?'Estimated' :e . source === 'measured' ?'Measured' :'Unavailable' ; } 123export interface Recommendation { 124unavailableReason ?:'hardware' | 'fit' ; 125profile :Profile ; 126variant ?:ModelVariant ; 127experience ?:Experience ; 128provisional :boolean ; 129explanation :string ; 130} 131export function recommend ( variants :ModelVariant [], hw :Hardware | null , config :Execution , workload :Workload , demo :boolean ) :Recommendation [] { 132const all = variants . map ( variant => ({ variant, experience :estimate ( variant , hw , config , workload , demo ) })); 133const viable = all . filter ( x => [ 'comfortable' , 'tight' ]. includes ( x . experience . fit ) && x . experience . fitsWorkload ); 134return PROFILES . map (({ id}) => { 135const candidates = viable . filter ( x => id !== 'agentic' || metadata ( x . variant ). agentApproved || ( demo && x . variant . experts > 0 )); 136const ranked = [ ...candidates ]. sort (( a , b ) => { 137if ( id === 'balanced' ) 138return Number ( b . experience . fit === 'comfortable' ) - Number ( a . experience . fit === 'comfortable' ) || metadata ( a . variant ). balancedRank - metadata ( b . variant ). balancedRank || a . variant . id . localeCompare ( b . variant . id ); 139const metric = id === 'agentic' ?'reading' :'answer' ; 140return ( b . experience [ metric ]?. low ?? - 1 ) - ( a . experience [ metric ]?. low ?? - 1 ) || a . variant . id . localeCompare ( b . variant . id ); 141}); 142const chosen = ranked [ 0 ]; 143if ( ! chosen ) 144return { profile :id , unavailableReason :! hw ?'hardware' :'fit' , provisional :true , explanation :! hw ?'Check your computer to assess model fit.' :id === 'agentic' ?'No approved agent candidate fits this setup. Try Balanced or review a shorter context.' :'No compatible model fits this context and memory budget. Review a shorter context.' }; 145const provisional = chosen . experience . source !== 'measured' || id === 'balanced' && ! chosen . variant . catalog ?. review . startsWith ( 'Reviewed' ); 146const explanation = demo ?'Example selection using illustrative speeds and catalog preferences. Agent approval is simulated; this is not a production recommendation.' :id === 'balanced' ?`Catalog preference among fitting models. ${ metadata ( chosen . variant ). review } .` :chosen . experience . source === 'unavailable' ?'Provisional fit-based option; speed has not been established. No fastest-model claim.' :`Highest supported ${ id === 'agentic' ? 'prompt-reading' : 'answer-speed' } lower bound among comparable, fitting candidates. ${ metadata ( chosen . variant ). review } .` ; 147return { profile :id , ...chosen , provisional, explanation}; 148}); 149} 150export function groupVariants ( variants :ModelVariant []) { 151const families = new Map < string , Map < string , ModelVariant []>>(); 152for ( const v of variants ) { 153const meta = metadata ( v ); 154const versions = families . get ( meta . family ) ?? new Map < string , ModelVariant []>(); 155versions . set ( meta . version , [ ...( versions . get ( meta . version ) ?? []), v ]); 156families . set ( meta . family , versions ); 157} 158return [ ...families ]. map (([ family , versions ]) => ({ family, versions :[ ...versions ]. map (([ version , variants ]) => ({ version, variants})) })); 159} 160/** Merge recommendations only when their exact execution identities match. */ 161export function groupRecommendations ( items :Recommendation []) { 162const groups = new Map < string , Recommendation []>(); 163for ( const item of items ) { 164const key = item . variant && item . experience ?configKey ( item . variant , item . experience . config ) :`missing: ${ item . profile } ` ; 165groups . set ( key , [ ...( groups . get ( key ) ?? []), item ]); 166} 167return [ ...groups . entries ()]. map (([ identity , recommendations ]) => ({ identity, recommendations})); 168}