okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
d51d71e7c
main
1import type { GpuInfo , GpuReading } from './types' ; 2export const hasReading = ( v :unknown ) :v isnumber => typeof v === 'number' && Number . isFinite ( v ); 3export const gpuId = ( gpu :GpuInfo ) => gpu . id ?? ` ${ gpu . vendor } : ${ gpu . index } ` ; 4 5/** A total, mean, or maximum must cover every selected device. Partial totals are misleading. */ 6export function gpuMetric ( devices :GpuInfo [], readings :GpuReading [] | undefined , key :keyof GpuReading , mode :'sum' | 'mean' | 'max' = 'sum' ) :number | null { 7const values = devices . map ( device => readings ?. find ( reading => reading . id === gpuId ( device ))?.[ key ]); 8if ( ! values . length ||! values . every ( hasReading )) return null ; 9if ( key === 'memoryUsedGb' && mode === 'sum' ) { 10const shared = values . filter (( _ , i ) => devices [ i ]. unified ), dedicated = values . filter (( _ , i ) =>! devices [ i ]. unified ); 11return dedicated . reduce (( n , v ) => n + v , 0 ) + ( shared . length ?Math . max ( ...shared ) :0 ); 12} 13return mode === 'max' ?Math . max ( ...values ) :values . reduce (( n , v ) => n + v , 0 ) / ( mode === 'mean' ?values . length :1 ); 14} 15 16/** The card's name without its maker: "NVIDIA GeForce RTX 5070" -> "RTX 5070". */ 17export const shortGpuName = ( name :string ) => String ( name ?? '' ). replace ( / ^(NVIDIA|AMD|Intel)(\(R\)|\(TM\))?\s+ / i , '' ). replace ( / ^(GeForce|Radeon|Arc)(\(TM\))?\s+ / i , '' ). trim () || String ( name ?? '' ); 18 19/** 20* The reading over the cards that DO report this value. A card that cannot report it (an integrated one without a temperature sensor) is left 21* out instead of blanking the figure; `partial` says some were left out, and `devices` names the cards the figure is from. 22*/ 23export function gpuReported ( devices :GpuInfo [], readings :GpuReading [] | undefined , key :keyof GpuReading , mode :'sum' | 'mean' | 'max' = 'sum' ) :{ value :number | null ; devices :GpuInfo []; partial :boolean } { 24const reporting = devices . filter ( device => hasReading ( readings ?. find ( reading => reading . id === gpuId ( device ))?.[ key ])); 25return { value :reporting . length ?gpuMetric ( reporting , readings , key , mode ) :null , devices :reporting , partial :reporting . length > 0 && reporting . length < devices . length }; 26} 27 28export function gpuCapacity ( devices :GpuInfo []) { 29const shared = devices . filter ( g => g . unified ). map ( g => g . vramGb ); 30return devices . filter ( g =>! g . unified ). reduce (( n , g ) => n + g . vramGb , 0 ) + ( shared . length ?Math . max ( ...shared ) :0 ); 31} 32 33/** 34* The cards "All GPUs" covers by default. A small integrated chip (an AMD or Intel adapter on the Windows/Vulkan list with a carve-out under 35* 2 GB) beside a real card is left out: Strata cannot run a model on it (the native engine report marks it unusable), and counting it adds a 36* second "GPU" and half a gigabyte to every total. It stays in the picker. Alone (a Strix Halo, an iGPU-only PC) it stays the default. 37*/ 38export function defaultGpuSet ( devices :GpuInfo []) :GpuInfo [] { 39const real = ( g :GpuInfo ) => g . vramGb >= 4 || g . vendor === 'nvidia' || g . backend === 'cuda' ; 40const small = ( g :GpuInfo ) => g . backend === 'vulkan' && ( g . vendor === 'amd' || g . vendor === 'intel' ) && g . vramGb < 2 &&! g . unified ; 41if ( ! devices . some ( real )) return devices ; 42const kept = devices . filter ( g =>! small ( g )); 43return kept . length ?kept :devices ; 44}