okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e28889d12
main
1import { tr } from '../lib/i18n' ; 2import { useId , useState } from 'react' ; 3import { Btn , Icon } from './ui' ; 4import { 5CPU_ONLY_LINE , NO_CARD_LINE , OFF_AT_OR_BELOW_GB , gpuLabel , noteText , order , recommend , setRole , shortName , vramOn , warnings , 6type Role , type RoleCard , type Roles , type RoleWarning , 7} from '../lib/gpu-roles' ; 8 9const ROLE_WORD :Record < Role , string > = { main :'Main' , helper :'Helper' , off :'Off' }; 10const HINT :Record < Role , string > = { main :'Does the main work' , helper :'Holds part of the model' , off :'Not used' }; 11 12/** The one plain line above the cards, built from translated pieces. */ 13export function recommendationLine ( cards :RoleCard [], vendor ?:string | null ) :string { 14const rec = recommend ( cards , vendor ); 15if ( rec . main === null ) return tr ( 'No graphics card that Strata can use was found.' ); 16const byi = new Map ( cards . map ( c => [ c . index , c ])); 17const m = byi . get ( rec . main ) ! ; 18if ( cards . length === 1 ) return tr ( 'Strata will use your {0}.' , [ shortName ( m )]); 19const why = { same :'the cards are alike' , memory :'it has more memory' , faster :'it is faster' , only :'it is the only card' , none :'' }[ rec . why ]; 20let s = tr ( '{0} is Main: {1}.' , [ shortName ( m ), tr ( why )]); 21const helpers = Object . keys ( rec . roles ). map ( Number ). filter ( i => rec . roles [ i ] === 'helper' ). map ( i => shortName ( byi . get ( i ) ! )); 22if ( helpers . length ) s += ' ' + tr ( helpers . length > 1 ?'{0} help.' :'{0} helps.' , [ helpers . join ( ', ' )]); 23for ( const i of Object . keys ( rec . roles ). map ( Number )) if ( rec . roles [ i ] === 'off' ) { 24const code = rec . reasons [ i ], c = byi . get ( i ) ! ; 25s += ' ' + tr ( '{0} starts Off: {1}' , [ shortName ( c ), code === 'cannot_run' ?tr ( 'Strata cannot run it.' ) :code === 'igpu' ?tr ( 'built-in graphics.' ) :tr ( '{0} GB or less.' , [ OFF_AT_OR_BELOW_GB ])]); 26} 27return s ; 28} 29 30function warningText ( w :RoleWarning , cards :RoleCard []) :string { 31const c = w . card !== null ?cards . find ( x => x . index === w . card ) :undefined ; 32if ( w . code === 'cannot_run' ) return tr ( '{0} cannot run Strata.' , [ c ?shortName ( c ) :'' ]); 33if ( w . code === 'igpu_main' ) return tr ( '{0} is built-in graphics; it is slow as Main.' , [ c ?shortName ( c ) :'' ]); 34if ( w . code === 'cpu_only' ) return tr ( w . text === CPU_ONLY_LINE ?CPU_ONLY_LINE :NO_CARD_LINE ); 35return tr ( 'Not enough memory for this setup: about {0} GB is read from the drive.' , [ w . text . match ( / about (\d+) GB / )?.[ 1 ] ?? '' ]); 36} 37 38export function noteLine ( code :string | null , cards :RoleCard []) :string { 39if ( ! code ) return '' ; 40const kind = code . split ( ':' )[ 0 ], c = cards . find ( x => x . index === Number ( code . split ( ':' )[ 1 ])); 41if ( kind === 'old_main_helper' ) return tr ( '{0} is now a Helper.' , [ c ?shortName ( c ) :'' ]); 42if ( kind === 'promoted' ) return tr ( '{0} is now Main.' , [ c ?shortName ( c ) :'' ]); 43if ( kind === 'only_main' ) return tr ( 'The only card that is on has to be Main.' ); 44return noteText ( code , cards ); 45} 46 47/** 48* Main / Helper / Off for every graphics card (the mockup GpuRoles-mockup.html). One click changes a role; "Back to recommended" restores the 49* recommendation. Warnings are short and never block Load. `roles` are the roles in force (the recommendation until the person changes one). 50*/ 51export function GpuRoles ({ cards, roles, custom, recommendedRoles, vendor, onChange, onBack, memoryGb, ramGb, spillGb= 0 , engineRunsCpuOnly= false , disabled= false , usedBy} :{ 52cards :RoleCard []; 53roles :Roles ; 54/** The person changed a role: "Your choice", and "Back to recommended" is offered. */ 55custom :boolean ; 56/** The roles Strata recommends (the tags and "Back to recommended" use them); the pure recommendation when not given. */ 57recommendedRoles ?:Roles ; 58vendor ?:string | null ; 59onChange :( next :Roles ) => void ; 60onBack :() => void ; 61memoryGb ?:number | null ; 62ramGb ?:number | null ; 63/** About how many GB of the model are read from the drive with these cards (0 when everything fits or it is unknown). */ 64spillGb ?:number ; 65engineRunsCpuOnly ?:boolean ; 66disabled ?:boolean ; 67/** Names of the loaded models that already use a card ("In use by ..."). */ 68usedBy ?:( card :number ) => string []; 69}) { 70const titleId = useId (); 71const [ note , setNote ] = useState ( '' ); 72const rec = { ...recommend ( cards , vendor ), ...( recommendedRoles ?{ roles :recommendedRoles } :{}) }; 73const single = cards . length === 1 ; 74const warns = warnings ( roles , cards , { spillGb, engineRunsCpuOnly}); 75const cardWarn = ( i :number ) => warns . filter ( w => w . card === i ); 76const globalWarn = warns . filter ( w => w . card === null ); 77const lst = order ( roles , cards ); 78const change = ( idx :number , role :Role ) => { 79const out = setRole ( roles , cards , idx , role ); 80setNote ( noteLine ( out . note , cards )); 81onChange ( out . roles ); 82}; 83const back = () => { setNote ( '' ); onBack (); }; 84const fix = ( w :RoleWarning ) => w . fix && change ( w . fix . card , w . fix . role ); 85const fixLabel = ( w :RoleWarning ) => { 86if ( ! w . fix ) return '' ; 87const target = cards . find ( c => c . index === w . fix ! . card ); 88return w . fix . role === 'off' ?tr ( 'Turn it off' ) :tr ( 'Make {0} Main' , [ target ?shortName ( target ) :'' ]); 89}; 90const have = vramOn ( roles , cards ) + Math . max ( 0 , ( ramGb ?? 0 ) - 10 ); 91return < div className = "gpuroles" aria-labelledby = { titleId } data-custom = { custom }> 92< div className = "gpuroles__head" > 93< span className = "native-choice__label" id = { titleId }>{ tr ( 'Graphics cards' )}</ span > 94{ custom ?< button type = "button" className = "gpuroles__link" disabled = { disabled } onClick = { back }>{ tr ( 'Back to recommended' )}</ button > :null } 95</ div > 96< div className = "gpuroles__reco" data-custom = { custom }> 97< Icon name = "bolt" size = { 18 }/> 98< p >< strong >{ tr ( custom ?'Your choice.' :'Recommended.' )}</ strong > { custom ?tr ( 'You changed the roles. Strata will use exactly these.' ) :recommendationLine ( cards , vendor )}</ p > 99</ div > 100< div className = "gpuroles__rows" role = "list" > 101{ cards . map ( c => { 102const role = roles [ c . index ] ?? 'off' , isRec = rec . roles [ c . index ] === role , w = cardWarn ( c . index ), other = usedBy ?.( c . index ) ?? []; 103const shared = !! ( c . integrated || c . uma || c . unified_gb ); 104const options :Role [] = single ?[ 'main' , 'off' ] :[ 'main' , 'helper' , 'off' ]; 105return < div className = "gpuroles__row" role = "listitem" data-role = { role } key = { c . index }> 106< div className = "gpuroles__top" > 107< span className = "gpuroles__ico" >< Icon name = "gpu" size = { 20 }/></ span > 108< span className = "gpuroles__name" > 109< strong >{ gpuLabel ( c )} · { c . name }{ isRec ?< span className = "gpuroles__tag" >{ tr ( 'Recommended' )}</ span > :< span className = "gpuroles__tag gpuroles__tag--neutral" >{ tr ( 'Recommended: {0}' , [ tr ( ROLE_WORD [ rec . roles [ c . index ] ?? 'off' ])])}</ span >}</ strong > 110< small >{ shared ?tr ( "Uses your computer's memory (built-in graphics)" ) :tr ( '{0} GB memory' , [ Math . round ( c . vram_gb )])} · { tr ( HINT [ role ])}</ small > 111</ span > 112< span className = "gpuroles__roles" role = "radiogroup" aria-label = { tr ( 'Role of {0}' , [ gpuLabel ( c )])}> 113{ options . map ( r => < button type = "button" role = "radio" key = { r } data-r = { r } aria-checked = { role === r } disabled = { disabled } onClick = {() => change ( c . index , r )}>{ tr ( ROLE_WORD [ r ])}</ button >)} 114</ span > 115</ div > 116{ other . length ?< div className = "gpuroles__note gpuroles__note--info" >< Icon name = "info" size = { 16 }/>< p >{ tr ( 'In use by {0}' , [ other . join ( ', ' )])}</ p ></ div > :null } 117{ w . map ( x => < div className = "gpuroles__note gpuroles__note--warn" key = { x . code }>< Icon name = "warning" size = { 16 }/>< p >{ warningText ( x , cards )}</ p >{ x . fix ?< Btn size = "sm" disabled = { disabled } onClick = {() => fix ( x )}>{ fixLabel ( x )}</ Btn > :null }</ div >)} 118</ div >; 119})} 120</ div > 121< div className = "gpuroles__toast" role = "status" aria-live = "polite" >{ note ?< div className = "gpuroles__note gpuroles__note--info" >< Icon name = "info" size = { 16 }/>< p >{ note }</ p ></ div > :null }</ div > 122{ globalWarn . map ( x => < div className = "gpuroles__note gpuroles__note--warn" key = { x . code }>< Icon name = "warning" size = { 16 }/>< p >{ warningText ( x , cards )}</ p >{ x . code === 'cpu_only' && custom ?< Btn size = "sm" disabled = { disabled } onClick = { back }>{ tr ( 'Back to recommended' )}</ Btn > :null }</ div >)} 123< details className = "gpuroles__details" > 124< summary >{ tr ( 'Details' )}</ summary > 125< div className = "gpuroles__body" > 126< dl > 127< dt >{ tr ( 'Main' )}</ dt >< dd >{ tr ( 'The card that does the final steps of every answer: it writes the next words, runs the draft-ahead step and checks it, and keeps your conversation. Pick your fastest card with plenty of memory. There is only ever one.' )}</ dd > 128< dt >{ tr ( 'Helper' )}</ dt >< dd >{ tr ( 'Holds a share of the model and its fast memory, so more of it fits on graphics cards instead of being read from your drive. Prompts are read about 20% faster.' )}</ dd > 129< dt >{ tr ( 'Off' )}</ dt >< dd >{ tr ( 'Strata does not touch this card. Good for a card you use for something else, such as a game or your screen.' )}</ dd > 130</ dl > 131{ memoryGb ?< div className = "gpuroles__fit" data-state = { lst . length ?( spillGb >= 1 ?'bad' :'ok' ) :'bad' }>< span className = "gpuroles__dot" aria-hidden = "true" />< span >{ lst . length ?( spillGb >= 1 ?tr ( 'The model needs about {0} GB. These cards and your memory give about {1} GB, so about {2} GB would be read from your drive.' , [ Math . round ( memoryGb ), Math . round ( have ), Math . round ( spillGb )]) :tr ( 'The model needs about {0} GB. These cards and your memory give about {1} GB. It fits.' , [ Math . round ( memoryGb ), Math . round ( have )])) :tr ( 'Only your processor and memory are used.' )}</ span ></ div > :null } 132< div className = "gpuroles__expert xs muted" >{ lst . length ?tr ( 'For experts: the engine starts on cards {0} (Main last).' , [ lst . join ( ',' )]) :null } { custom ?'' :tr ( 'Not saved while you keep the recommendation: Strata picks again if you add or remove a card.' )}</ div > 133</ div > 134</ details > 135</ div >; 136}