From 8aaf188aa9f7185c356a6428c73eab900bef353c Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 20 Oct 2026 00:11:11 +0200 Subject: [PATCH] Your own engine settings are kept: user_args, a reserve of 0 on a compute-only card, control vectors survive a load (#3215 #3270 #3216) The app and setup rewrote the run config's "args" on every Load: the resource preference forced --vram-reserve-mib to 1536 (2048 with pictures, a fifth of the card on "work") whatever the file said, and a setup run (started by the Load when the context or pictures differ) dropped every option it did not choose itself, the control vector options included. - run config: "user_args" holds the options set by hand; "args_written" is the args as setup/the app last wrote them, so a hand edit of "args" is noticed and moved into user_args (strata_control/user_args.py, apps/host/server/native-user-args.mjs: the same rule) - every start (serve/server.py engine_args), every setup run (setup.carry_user_args), every host write and load and a saved tuning put the person's options over the generated ones; the same option: theirs wins - old files without args_written: setup keeps the options the old args had, the new ones lack and setup.py never names (plus the control vector group); a reserve that is not setup's own 700 - --vram-reserve-mib: any number the person set (Expert setting, user_args, the key, a value in args) is used as it is, 0 included; the safety default (1536/1024, 2048 with pictures, 20% on "work") stays for a card that drives a display or is not known to be headless; a card the probe says drives no display keeps 512 (700 with pictures); a 0.2.0-beta file where the app wrote the number itself is told from a changed one - hardware: drives_display per card (nvidia-smi display_active; the Windows adapter with a desktop resolution for AMD) - setup --vram-reserve-mib, the web settings page and `start --vram-reserve-mib` record the value as the person's - docs/TROUBLESHOOTING.md: what is kept and what the app still chooses --- apps/host/server/native-control.mjs | 21 ++- apps/host/server/native-resources.mjs | 64 +++++++-- apps/host/server/native-user-args.mjs | 47 +++++++ apps/host/tests/user-args.test.mjs | 176 +++++++++++++++++++++++ docs/TROUBLESHOOTING.md | 24 +++- serve/runconfig.py | 9 ++ serve/server.py | 5 + setup.py | 81 ++++++++++- strata_control/tune_apply.py | 10 ++ strata_control/user_args.py | 118 ++++++++++++++++ tools/strata_mcp.py | 41 ++++++ tools/test_setup_config.py | 7 +- tools/test_setup_golden.py | 2 + tools/test_setup_qwen36.py | 2 +- tools/test_user_args.py | 192 ++++++++++++++++++++++++++ 15 files changed, 765 insertions(+), 34 deletions(-) create mode 100644 apps/host/server/native-user-args.mjs create mode 100644 apps/host/tests/user-args.test.mjs create mode 100644 strata_control/user_args.py create mode 100644 tools/test_user_args.py diff --git a/apps/host/server/native-control.mjs b/apps/host/server/native-control.mjs index ae4ec648..c59afeb1 100644 --- a/apps/host/server/native-control.mjs +++ b/apps/host/server/native-control.mjs @@ -7,7 +7,8 @@ import { join, resolve, dirname, basename } from 'node:path'; import { fileURLToPath } from 'node:url'; import { createServer } from 'node:net'; import { NATIVE_FIELDS, APP_OWNED_FIELDS, withoutDraftLayer, validateNativeSettings, validateMcpServers, publicMcpServers, publicNativeEnv, restoreNativeEnv } from './native-settings.mjs'; -import { resourceSettings, saferStart } from './native-resources.mjs'; +import { resourceSettings, saferStart, reserveChoice } from './native-resources.mjs'; +import { USER_KEY, capture as captureUserArgs, merge as mergeUserArgs, setFlag as setUserFlag, stamped as stampedUserArgs } from './native-user-args.mjs'; import { resolvePython, venvPython } from './python-resolver.mjs'; import os from 'node:os'; import { runtimeVision } from './native-vision.mjs'; @@ -129,7 +130,7 @@ export function createNativeControl({directory,requests=()=>[],changed=()=>{},sp /* A mistake in the file holds the start back with the line, instead of an engine that dies on it. Warnings (an unknown key) never do. */ function assertSettingsFile(path){const check=checkRunConfig(readFileSync(path,'utf8'));if(!check.ok)throw new Error(`The settings file for this model has a mistake, so the model was not started. ${describeIssues(check.issues,'error').join(' ')}`);} function settings(model){const {config}=record(model);const values=Object.fromEntries(NATIVE_FIELDS.map(f=>[f.id,config[f.id]??null]));if(typeof values.aliases==='string')values.aliases=values.aliases.split(',').map(v=>v.trim()).filter(Boolean);values.sampling=config.sampling??{};values.args=config.args??[];values.env=publicNativeEnv(config.env??{});return {model,values,mcp:publicMcpServers(config.mcp_servers??config.mcpServers??{}),restartNeeded:true};} - function save(model,{values,mcp}){if([...jobs.values()].some(j=>j.state==='running'))throw new Error('Finish the current operation before saving native settings.');const {path,config}=record(model);if(busy(requests().filter(r=>instances.get(r.instanceId)?.model===model)))throw new Error('Finish this model’s requests before saving its settings.');const patch=validateNativeSettings(values??{}),next={...config};for(const field of nativeFields())if(field.supported===false&&patch[field.id]!=null&&JSON.stringify(patch[field.id])!==JSON.stringify(config[field.id]))throw new Error(`${field.label} needs a newer native Strata installation. Update it in Settings → About.`);for(const [key,value] of Object.entries(patch)){if(key==='env')next.env=restoreNativeEnv(value,config.env??{});else if(value===null)delete next[key];else next[key]=value;}if(mcp!==undefined){next.mcp_servers=validateMcpServers(mcp,config.mcp_servers??config.mcpServers??{});delete next.mcpServers;}copyFileSync(path,path+'.bak');chmodSync(path+'.bak',0o600);const temp=path+'.ui-tmp';writeFileSync(temp,JSON.stringify(next,null,2)+'\n',{mode:0o600});renameSync(temp,path);return settings(model);} + function save(model,{values,mcp}){if([...jobs.values()].some(j=>j.state==='running'))throw new Error('Finish the current operation before saving native settings.');const {path,config}=record(model);if(busy(requests().filter(r=>instances.get(r.instanceId)?.model===model)))throw new Error('Finish this model’s requests before saving its settings.');const patch=validateNativeSettings(values??{}),next={...config};for(const field of nativeFields())if(field.supported===false&&patch[field.id]!=null&&JSON.stringify(patch[field.id])!==JSON.stringify(config[field.id]))throw new Error(`${field.label} needs a newer native Strata installation. Update it in Settings → About.`);for(const [key,value] of Object.entries(patch)){if(key==='env')next.env=restoreNativeEnv(value,config.env??{});else if(value===null)delete next[key];else next[key]=value;}if('vram_reserve_mib' in patch){const base=captureUserArgs(next);const mine=setUserFlag(base,'--vram-reserve-mib',patch.vram_reserve_mib);if(mine.length)next[USER_KEY]=mine;else delete next[USER_KEY];next.args=mine.length&&patch.vram_reserve_mib!==null?mergeUserArgs(next.args??[],mine):setUserFlag(next.args??[],'--vram-reserve-mib',null);delete next.ui_reserve_applied;if(Array.isArray(next.args_written))next.args_written=[...next.args];}if(mcp!==undefined){next.mcp_servers=validateMcpServers(mcp,config.mcp_servers??config.mcpServers??{});delete next.mcpServers;}copyFileSync(path,path+'.bak');chmodSync(path+'.bak',0o600);const temp=path+'.ui-tmp';writeFileSync(temp,JSON.stringify(next,null,2)+'\n',{mode:0o600});renameSync(temp,path);return settings(model);} function previousEngine(){if(packaged)return !!snapshot.engine?.previous;try{const data=snapshot.installed?.data_dir??snapshot.installed?.data_folder;return [join(preferences.repo,'engine/.previous'),data&&join(data,'engine/.previous')].filter(Boolean).some(existsSync)&&readFileSync(join(preferences.repo,'setup.py'),'utf8').includes('--rollback-engine');}catch{return false;}} async function plan(args){await refresh();if(!snapshot.available)throw new Error(snapshot.error);return tool('strata_install',installArgs(args,false));} function installArgs(args,confirm){const allowed=['family','model','context','vision','kv','gpu','gpus','backend','low_ram','port','gguf_dir'];const size=snapshot.catalog?.families?.find(f=>f.family===args.family)?.sizes.find(s=>s.model===args.model);const existing=snapshot.installed?.models?.find(m=>m.model===size?.id);/* A model adopted from the person's own GGUF folder reports that folder's parent as its data folder: it is theirs, not Strata's, so a second preparation never uses it for the packs. */const data=nativeDataDirectory(args.data_dir??(args.gguf_dir?undefined:existing?.dataFolder)??preferences.downloadDirectory??modelsDirectory?.());return {...(data?{data_dir:data}:{}),...Object.fromEntries(allowed.filter(k=>args[k]!==undefined).map(k=>[k,args[k]])),/* Picture reading is only downloaded when it was asked for (Read pictures); the default stays the smaller download. */...(packaged&&args.vision===undefined?{vision:'no'}:{}),confirm};} @@ -192,10 +193,17 @@ return {summary:result.summary??'Deleted',removed:result.plan?.gb??null};} function buildConfig(args,config){ const vision=runtimeVision(config,args.vision); const visionPreferences=args.vision!==undefined?{ui_vision_enabled:args.vision!=='no',ui_vision_device:args.vision==='no'?config.ui_vision_device??'yes':args.vision}:{}; - const resources=args.resourceProfile?resourceSettings({...config,vision},snapshot.hardware,args.resourceProfile,modelArena(args.model)):null; + /* The engine options the person set by hand (user_args, and what they wrote into "args" since the last write) go over everything the app generates, and an + explicit GPU-memory reserve (the Expert setting, user_args, a value they wrote) is never replaced by the safety default (#3215 #3270 #3216). */ + const values0=args.values??{},cleared='vram_reserve_mib' in values0&&(values0.vram_reserve_mib===null||values0.vram_reserve_mib===''),base0=cleared?{...config,vram_reserve_mib:undefined,[USER_KEY]:setUserFlag(config[USER_KEY],'--vram-reserve-mib',null)}:config; + let mine=captureUserArgs(base0);const asked=cleared?null:reserveChoice({...base0,[USER_KEY]:mine},snapshot.hardware,values0).asked; + if(asked!==null)mine=setUserFlag(mine,'--vram-reserve-mib',asked); + const resources=args.resourceProfile?resourceSettings({...base0,[USER_KEY]:mine,vision},snapshot.hardware,args.resourceProfile,modelArena(args.model),values0):null; const cfg={...config,...visionPreferences,...(resources?{...resources,ui_resource_profile:args.resourceProfile}:{}),host:'127.0.0.1',open_browser:false,...validateNativeSettings(args.values??{}),vision}; if(args.gpus!==undefined){const detected=new Set((snapshot.hardware?.gpus??[]).map(g=>g.index));if(!Array.isArray(args.gpus)||!args.gpus.length||new Set(args.gpus).size!==args.gpus.length||args.gpus.some(g=>!Number.isInteger(g)||!detected.has(g)))throw new Error('Choose distinct detected GPUs. CPU-only text inference is not supported by this native adapter.');cfg.gpu=args.gpus.length===1?args.gpus[0]:args.gpus;cfg.layer_split=args.gpus.length>1?validateNativeSettings({layer_split:args.layerSplit??'auto'}).layer_split:'auto';if(cfg.layer_split!=='auto'&&cfg.layer_split.split(',').length!==args.gpus.length-1)throw new Error('Choose one layer split point between each pair of GPUs.');} - if(cfg.vram_reserve_mib!==undefined&&cfg.vram_reserve_mib!==null){cfg.args=[...(cfg.args??[])];const at=cfg.args.indexOf('--vram-reserve-mib');if(at>=0)cfg.args.splice(at,2);cfg.args.push('--vram-reserve-mib',String(cfg.vram_reserve_mib));}if(cfg.vision){if(cfg.vision_tokens!=null)cfg.vision={...cfg.vision,max_tokens:cfg.vision_tokens};if(cfg.vision_min_tokens!=null)cfg.vision={...cfg.vision,min_tokens:cfg.vision_min_tokens};}cfg.mcp={...(cfg.mcp??{}),...(cfg.mcp_timeout_s!=null?{timeout_s:cfg.mcp_timeout_s}:{}),...(cfg.mcp_max_rounds!=null?{max_rounds:cfg.mcp_max_rounds}:{}),...(cfg.mcp_max_result_chars!=null?{max_result_chars:cfg.mcp_max_result_chars}:{})}; + if(cfg.vram_reserve_mib!==undefined&&cfg.vram_reserve_mib!==null){cfg.args=[...(cfg.args??[])];const at=cfg.args.indexOf('--vram-reserve-mib');if(at>=0)cfg.args.splice(at,2);cfg.args.push('--vram-reserve-mib',String(cfg.vram_reserve_mib));} + if(mine.length){cfg.args=mergeUserArgs(cfg.args??[],mine);cfg[USER_KEY]=mine;}else delete cfg[USER_KEY]; + if(cfg.vision){if(cfg.vision_tokens!=null)cfg.vision={...cfg.vision,max_tokens:cfg.vision_tokens};if(cfg.vision_min_tokens!=null)cfg.vision={...cfg.vision,min_tokens:cfg.vision_min_tokens};}cfg.mcp={...(cfg.mcp??{}),...(cfg.mcp_timeout_s!=null?{timeout_s:cfg.mcp_timeout_s}:{}),...(cfg.mcp_max_rounds!=null?{max_rounds:cfg.mcp_max_rounds}:{}),...(cfg.mcp_max_result_chars!=null?{max_result_chars:cfg.mcp_max_result_chars}:{})}; /* The model's own choices win over what setup wrote into the arguments: "Draft layer (MTP)" off leaves --mtp out of every start, and a picture reading that is off leaves --vision out (setup writes both again whenever it runs; the saved settings are applied here, on every load). */ if(cfg.mtp===false&&Array.isArray(cfg.args))cfg.args=withoutDraftLayer(cfg.args); if(!cfg.vision&&Array.isArray(cfg.args))cfg.args=cfg.args.filter(a=>a!=='--vision'); @@ -268,7 +276,7 @@ return {summary:result.summary??'Deleted',removed:result.plan?.gb??null};} } const deadline=Date.now()+300000;while(Date.now()setTimeout(resolve,1200)); } if(job.state!=='running'){await unload(id);return;} @@ -321,7 +329,8 @@ return {summary:result.summary??'Deleted',removed:result.plan?.gb??null};} } function updateSummary(done){const r=(done.results??[]).filter(x=>x.status==='updated').map(x=>x.component==='engine'?'The engine is updated and ready.':'Strata is updated. It takes effect the next time you open the app.');return r.length?r.join(' '):'Everything is already up to date.';} function execute(cmd,args,cwd){return new Promise((resolve,reject)=>{let output='';const process=spawnProcess(cmd,args,{cwd,stdio:['ignore','pipe','pipe']});process.stdout.on('data',c=>{output+=c;if(output.length>100000)process.kill();});process.stderr.on('data',c=>append('update',c.toString()));process.on('error',reject);process.on('exit',code=>code===0?resolve(output):reject(new Error('Git could not update this installation. Open diagnostics.')));});} - function writeModelConfig(path,next){copyFileSync(path,path+'.bak');chmodSync(path+'.bak',0o600);const temp=path+'.ui-tmp';writeFileSync(temp,JSON.stringify(next,null,2)+'\n',{mode:0o600});renameSync(temp,path);} + /* Every write of a model's run config keeps the person's options: those they wrote into "args" or "user_args" since the file was last written are read from the file itself, go over what the caller built, and "args_written" is refreshed. */ + function writeModelConfig(path,next){const mine=mergeUserArgs(captureUserArgs(readJson(path)),next[USER_KEY]??[]);next=stampedUserArgs(next,mine);copyFileSync(path,path+'.bak');chmodSync(path+'.bak',0o600);const temp=path+'.ui-tmp';writeFileSync(temp,JSON.stringify(next,null,2)+'\n',{mode:0o600});renameSync(temp,path);} async function tune(job,args){ await refresh(true);if(job.state!=='running')return; const chosen=record(args.model);resourceSettings(chosen.config,snapshot.hardware,args.profile,modelArena(args.model)); diff --git a/apps/host/server/native-resources.mjs b/apps/host/server/native-resources.mjs index 5dcf7baa..1d29620d 100644 --- a/apps/host/server/native-resources.mjs +++ b/apps/host/server/native-resources.mjs @@ -1,10 +1,59 @@ +import {USER_KEY,flagValue} from './native-user-args.mjs'; + +/** The engine's own default reserve, and the one setup writes for pictures read on the GPU: neither is a choice of the person. */ +const ENGINE_DEFAULT_RESERVE_MIB=700; +/** A card that drives no display needs little beside the engine; the engine keeps the last few hundred MiB for allocations it makes after the cache. */ +const COMPUTE_ONLY_RESERVE_MIB=512; + +const wholeMib=v=>v!==null&&v!==undefined&&v!==''&&Number.isFinite(Number(v))&&Number(v)>=0?Math.trunc(Number(v)):null; + +/** The graphics memory the person asked to keep free, or null when they asked for nothing (#3215 #3270): the Expert setting of this load, + * "user_args", the setting saved for the model ("vram_reserve_mib", unless it is the value the app itself wrote last), or a value written by hand into "args" + * (not the engine's default 700, and not the one the app wrote). Any number counts, 0 too. */ +function candidateReserve(config,values={},legacyDefaults=new Set()){ + if(values&&typeof values==='object'&&'vram_reserve_mib' in values){const v=wholeMib(values.vram_reserve_mib);if(v!==null)return v;} + const user=wholeMib(flagValue(config?.[USER_KEY],'--vram-reserve-mib'));if(user!==null)return user; + const applied=config?.ui_reserve_applied,legacy=applied===undefined&&config?.ui_resource_baseline!==undefined; // 0.2.0-beta wrote the key itself, without saying so + const key=wholeMib(config?.vram_reserve_mib),arg=wholeMib(flagValue(config?.args,'--vram-reserve-mib')); + const keyOk=key!==null&&key!==applied,argOk=arg!==null&&arg!==applied&&arg!==ENGINE_DEFAULT_RESERVE_MIB; + if(legacy)return [arg,key].map((n,i)=>(i===0?argOk:keyOk)?n:null).find(n=>n!==null&&!(n===1536||n===2048||legacyDefaults.has(n)))??null; // a 0.2.0-beta file: the app wrote both, so what is not one of its own numbers is the person's + return keyOk?key:argOk?arg:null; +} + +/** What the app keeps free when the person asked for nothing: [work, performance]. A card that drives a display (or is not known to be headless) gets no + * less than 1536 MiB on a card of 16 GB or less, 1024 above, 2048 with pictures (the engine warns below ~1-1.5 GiB of free VRAM: the driver then pages GPU memory to + * system RAM and decoding falls to a few tokens a second, #2065); "work" also keeps a fifth of the card. A card the probe says drives no display keeps 512 MiB + * (700 with pictures) on either profile. */ +export function defaultReserves(config,hardware,baselineReserve=0){ + const placement=Array.isArray(config.gpu)?config.gpu:[config.gpu??0]; + const placed=(hardware?.gpus??[]).filter(g=>placement.includes(g.index)),gpuMemory=placed.map(g=>g.vram_gb).filter(n=>Number.isFinite(n)&&n>0); + const headless=placed.length>0&&placed.every(computeOnly); // no display on the card (the desktop is on an integrated GPU): no desktop to keep room for + const floor=headless?(config.vision?ENGINE_DEFAULT_RESERVE_MIB:COMPUTE_ONLY_RESERVE_MIB):gpuMemory.length&&Math.min(...gpuMemory)>16?1024:1536; + const safety=headless?floor:Math.max(config.vision?2048:floor,baselineReserve); + return {work:gpuMemory.length&&!headless?Math.max(safety,Math.floor(Math.min(...gpuMemory)*1024*.2)):safety,performance:safety}; +} + +/** The reserve the person asked for (null: nothing) and the default for each profile. A 0.2.0-beta file has the key and the same number in "args" with no note that the + * app wrote them: a number that is one of the app's two defaults is the app's, any other is the person's. */ +export function reserveChoice(config,hardware,values={},baselineReserve=0){ + const defaults=defaultReserves(config,hardware,config.ui_resource_baseline?.reserve??baselineReserve); + const asked=candidateReserve(config,values,new Set([defaults.work,defaults.performance])); + return {asked,defaults}; +} +export const explicitReserve=(config,values={},hardware=null)=>reserveChoice(config,hardware,values).asked; + +/** True when the probe says this card drives no display: only compute runs on it. Unknown counts as a display card. */ +const computeOnly=g=>g?.drives_display===false; + /** Headroom is a worker/memory budget, not a hard utilisation throttle. */ -export function resourceSettings(config,hardware,profile,model={}){ +export function resourceSettings(config,hardware,profile,model={},values={}){ if(!['work','performance'].includes(profile))throw new Error('Choose a resource preference.'); const args=[...(config.args??[])]; const remove=flag=>{for(let at=args.indexOf(flag);at>=0;at=args.indexOf(flag))args.splice(at,2);}; const value=flag=>{const at=args.indexOf(flag);return at>=0?Number(args[at+1])||0:0;}; - const baseline=config.ui_resource_baseline??{workers:value('--pool-workers'),reserve:Math.max(Number(config.vram_reserve_mib)||0,value('--vram-reserve-mib'))}; + const first=reserveChoice(config,hardware,values).asked; + const baseline=config.ui_resource_baseline??{workers:value('--pool-workers'),reserve:first!==null?0:Math.max(Number(config.vram_reserve_mib)||0,value('--vram-reserve-mib'))}; + const {asked,defaults}=reserveChoice(config,hardware,values,baseline.reserve); if(profile==='performance'&&config.ui_resource_profile==='work'){ remove('--pool-workers');if(baseline.workers>0)args.push('--pool-workers',String(baseline.workers)); } @@ -15,13 +64,8 @@ export function resourceSettings(config,hardware,profile,model={}){ args.push('--pool-workers',String(previous>0?Math.min(previous,workers):workers)); } // Preserve calibrated worker counts and the installation's safety/vision reserve. - const placement=Array.isArray(config.gpu)?config.gpu:[config.gpu??0]; - const gpuMemory=(hardware?.gpus??[]).filter(g=>placement.includes(g.index)).map(g=>g.vram_gb).filter(n=>Number.isFinite(n)&&n>0); - /* The engine warns below ~1-1.5 GiB of free VRAM (the driver then pages GPU memory to system RAM and decoding falls to a few tokens a - second, #2065), so neither profile may reserve less: 1536 MiB on a card of 16 GB or less, 1024 above; with pictures 2048. */ - const floor=gpuMemory.length&&Math.min(...gpuMemory)>16?1024:1536; - const safety=Math.max(config.vision?2048:floor,baseline.reserve); - const reserve=profile==='work'&&gpuMemory.length?Math.max(safety,Math.floor(Math.min(...gpuMemory)*1024*.2)):safety; + /* A reserve the person set is theirs, 0 included: no floor and no share of the card (#3215 #3270). Without one the card gets the safety default (defaultReserves). */ + const reserve=asked!==null?asked:defaults[profile]; remove('--vram-reserve-mib');args.push('--vram-reserve-mib',String(reserve)); // RAM: "leave 20% for my work" keeps the model's RAM within 80% of the PC. A model whose experts do not fit there (the // setup rule: experts plus 10 GB) keeps only that many GiB of them in RAM and reads the rest from the model files @@ -37,7 +81,7 @@ export function resourceSettings(config,hardware,profile,model={}){ // available for other programs (it keeps fewer experts if the PC is busy), and it said nothing about this before. let ramHeadroom=false; if(ramBudget&&!(config.env?.STRATA_RESIDENT_HEADROOM_GIB)){env={...(env??config.env??{}),STRATA_RESIDENT_HEADROOM_GIB:String(Math.ceil(ram*.2))};ramHeadroom=true;} - return {args,vram_reserve_mib:reserve,...(env?{env}:{}),ui_resource_baseline:{...baseline,ramBudget,ramHeadroom}}; + return {args,vram_reserve_mib:reserve,...(asked!==null?{}:{ui_reserve_applied:reserve}),...(env?{env}:{}),ui_resource_baseline:{...baseline,ramBudget,ramHeadroom}}; } /* One automatic second try after a start that died. Returns null when there is nothing safer to try (the caller then reports the first diff --git a/apps/host/server/native-user-args.mjs b/apps/host/server/native-user-args.mjs new file mode 100644 index 00000000..d06557a2 --- /dev/null +++ b/apps/host/server/native-user-args.mjs @@ -0,0 +1,47 @@ +/** The engine options a person set by hand in a model's run config (strata_control/user_args.py is the same rule in Python). + * "user_args" holds them; "args_written" is the whole "args" as setup or the app last wrote it, so an option in "args" that is not in + * that copy was added or changed by hand since. Every load and every write puts the person's options over the generated ones. */ +export const USER_KEY='user_args',WRITTEN_KEY='args_written'; +/** Options the app or setup choose from their own answers (context, KV type, pictures, draft layer, layer split): never taken from "args" as hand edits. */ +export const CHOICE_FLAGS=new Set(['--max-context','--kv','--vision','--mtp','--spec-min-p','--batch-mtp','--layer-split']); + +export function groups(args){ + const out=[]; + for(const raw of Array.isArray(args)?args:[]){const a=String(raw);if(a.startsWith('--')||!out.length)out.push([a]);else out[out.length-1].push(a);} + return out; +} +export const flat=gs=>gs.flat(); +export const flagOf=g=>g?.[0]?.startsWith('--')?g[0]:''; +/** `args` with every option the person set replaced by theirs (appended after the generated ones). */ +export function merge(args,user){ + const ug=groups(user),mine=new Set(ug.map(flagOf).filter(Boolean)); + return flat([...groups(args).filter(g=>!mine.has(flagOf(g))),...ug]); +} +function byFlag(gs){const m=new Map();for(const g of gs){const f=flagOf(g);if(!m.has(f))m.set(f,[]);m.get(f).push(g);}return m;} +const same=(a,b)=>JSON.stringify(a??null)===JSON.stringify(b??null); +/** The person's options of a config: its user_args, with the options of "args" that differ from "args_written" on top. */ +export function capture(cfg,skip=CHOICE_FLAGS){ + const mine=byFlag(groups(cfg?.[USER_KEY])); + if(Array.isArray(cfg?.[WRITTEN_KEY])&&Array.isArray(cfg?.args)){ + const cur=byFlag(groups(cfg.args)),was=byFlag(groups(cfg[WRITTEN_KEY])); + for(const [flag,gs] of cur)if(flag&&!skip.has(flag)&&!same(was.get(flag),gs))mine.set(flag,gs); + for(const flag of [...mine.keys()])if(was.has(flag)&&!cur.has(flag))mine.delete(flag); + } + return flat([...mine.values()].flat()); +} +export function setFlag(user,flag,value){ + const rest=groups(user).filter(g=>flagOf(g)!==flag); + if(value!==null&&value!==undefined)rest.push([flag,String(value)]); + return flat(rest); +} +export function flagValue(args,flag){ + const list=(Array.isArray(args)?args:[]).map(String),at=list.indexOf(flag); + return at>=0&&at+1 gpus, directory: data, spawnProcess: () => { throw new Error('no process'); }, fetcher: async () => Response.json({ status: 'ok' }), requests: () => [] }); + return { native, saved, path, read: () => JSON.parse(readFileSync(path, 'utf8')), write: c => writeFileSync(path, JSON.stringify(c)), cleanup: () => rmSync(root, { recursive: true, force: true }) }; +} +const finish = async (native, id) => { for (let i = 0; i < 500; i++) { const job = native.state().jobs.find(j => j.id === id); if (job.state !== 'running') return job; await new Promise(r => setTimeout(r, 10)); } throw new Error('job did not finish'); }; +const reserveOf = args => args[args.indexOf('--vram-reserve-mib') + 1]; +const load = async (f, args = {}) => { const job = await finish(f.native, f.native.operation('load', { model: 'q2_0', vision: 'no', ...args }).id); assert.equal(job.state, 'done', job.error); return f.saved.at(-1); }; + +test('the rule: groups, merge and capture', () => { + assert.deepEqual(groups(['--a', '1', '--b', '--c', 'x', 'y']), [['--a', '1'], ['--b'], ['--c', 'x', 'y']]); + assert.deepEqual(merge(['--a', '1', '--b', '2'], ['--a', '9']), ['--b', '2', '--a', '9']); + const written = [...ARGS, '--vram-reserve-mib', '1536']; + assert.deepEqual(capture({ args: [...written, ...CVEC], args_written: written }), CVEC); + assert.deepEqual(capture({ args: written, args_written: written }), []); + assert.deepEqual(capture({ args: written.map(a => (a === '1536' ? '0' : a)), args_written: written }), ['--vram-reserve-mib', '0']); + assert.deepEqual(capture({ args: ['--max-context', '1'], args_written: ['--max-context', '2'] }), [], 'the context is the app\'s own choice'); +}); + +test('explicitReserve: any number the person set counts (0 too); the engine default, the app\'s own value and beta leftovers do not', () => { + assert.equal(explicitReserve({ args: ['--vram-reserve-mib', '0'] }), 0); + assert.equal(explicitReserve({ args: [], vram_reserve_mib: 0 }), 0); + assert.equal(explicitReserve({ args: [], user_args: ['--vram-reserve-mib', '256'], vram_reserve_mib: 3000 }), 256); + assert.equal(explicitReserve({ args: [], vram_reserve_mib: 3000 }, { vram_reserve_mib: 128 }), 128, 'the Expert setting of this load'); + assert.equal(explicitReserve({ args: ['--vram-reserve-mib', '700'] }), null, 'setup writes 700 for pictures: not a choice'); + assert.equal(explicitReserve({ args: [] }), null); + // what the app wrote last is not the person's: the value and the note that it was the app's + assert.equal(explicitReserve({ args: ['--vram-reserve-mib', '3276'], vram_reserve_mib: 3276, ui_reserve_applied: 3276 }), null); + assert.equal(explicitReserve({ args: ['--vram-reserve-mib', '1024'], vram_reserve_mib: 1024, ui_reserve_applied: 3276 }), 1024, 'changed since: the person\'s'); + // a 0.2.0-beta file: the app wrote the same number in both places, without a note + const beta = { args: ['--vram-reserve-mib', '1536'], vram_reserve_mib: 1536, ui_resource_baseline: { workers: 0, reserve: 700 } }; + assert.equal(explicitReserve(beta), null); + assert.equal(explicitReserve({ ...beta, args: ['--vram-reserve-mib', '0'] }), 0, 'a different number in args is the person\'s'); +}); + +test('3215: a reserve of 0 is kept on both resource profiles; the safety default stays where nothing was asked', () => { + const display = { cpu: { cores: 12 }, ram_gb: 64, gpus: [{ index: 0, vram_gb: 12, drives_display: true }] }; + const headless = { cpu: { cores: 12 }, ram_gb: 64, gpus: [{ index: 0, vram_gb: 12, drives_display: false }] }; + const unknown = { cpu: { cores: 12 }, ram_gb: 64, gpus: [{ index: 0, vram_gb: 12 }] }; + const zero = { gpu: 0, args: ['--vram-reserve-mib', '0'] }; + for (const profile of ['work', 'performance']) for (const hw of [display, headless, unknown]) { + const r = resourceSettings(zero, hw, profile); + assert.equal(reserveOf(r.args), '0', `${profile}`); + assert.equal(r.vram_reserve_mib, 0); + assert.equal(r.ui_reserve_applied, undefined, 'the app did not choose it'); + } + assert.equal(resourceSettings({ gpu: 0, args: [], vision: { gpu: true } }, display, 'performance').vram_reserve_mib, 2048, 'a display card with pictures: the safety default'); + assert.equal(resourceSettings({ gpu: 0, args: [] }, display, 'performance').vram_reserve_mib, 1536); + assert.equal(resourceSettings({ gpu: 0, args: [] }, unknown, 'performance').vram_reserve_mib, 1536, 'not known to be headless: the safety default'); + assert.equal(resourceSettings({ gpu: 0, args: [] }, display, 'work').vram_reserve_mib, Math.floor(12 * 1024 * 0.2)); + // a card that drives no display: 512 MiB (700 with pictures), and "Leave 20% for my work" has no desktop to keep room for + for (const profile of ['work', 'performance']) { + assert.equal(resourceSettings({ gpu: 0, args: [] }, headless, profile).vram_reserve_mib, 512, profile); + assert.equal(resourceSettings({ gpu: 0, args: [], vision: { gpu: true } }, headless, profile).vram_reserve_mib, 700, `${profile} with pictures`); + } + // two cards, one of them on the desktop + const mixed = { ...display, gpus: [{ index: 0, vram_gb: 12, drives_display: false }, { index: 1, vram_gb: 12, drives_display: true }] }; + assert.equal(resourceSettings({ gpu: [0, 1], args: [] }, mixed, 'performance').vram_reserve_mib, 1536); +}); + +test('3215: the app load keeps --vram-reserve-mib 0 typed into the file, on every load', async () => { + const f = fixture({ args: [...ARGS, '--vram-reserve-mib', '0'] }); + try { + await f.native.refresh(true); + for (let i = 0; i < 3; i++) { + const run = await load(f, { resourceProfile: i === 1 ? 'performance' : 'work' }); + assert.equal(reserveOf(run.args), '0', `load ${i + 1}`); + assert.equal(run.args.filter(a => a === '--vram-reserve-mib').length, 1); + const file = f.read(); + assert.equal(reserveOf(file.args), '0', 'the file keeps it too'); + assert.deepEqual(file.user_args, ['--vram-reserve-mib', '0']); + } + } finally { await f.native.close(); f.cleanup(); } +}); + +test('3270: the Expert setting "GPU memory for other apps" is kept across loads, a lowered value is not reset, and clearing it brings the default back', async () => { + const f = fixture({ args: [...ARGS, '--vram-reserve-mib', '2048'], args_written: [...ARGS, '--vram-reserve-mib', '2048'], vram_reserve_mib: 2048 }); + try { + await f.native.refresh(true); + await load(f, { resourceProfile: 'work' }); // the app's own load first: its numbers are in the file now + f.native.save('q2_0', { values: { vram_reserve_mib: 1024 } }); + for (let i = 0; i < 2; i++) { + const run = await load(f, { resourceProfile: 'work' }); + assert.equal(reserveOf(run.args), '1024', `load ${i + 1}`); + assert.equal(f.read().vram_reserve_mib, 1024); + assert.equal(reserveOf(f.read().args), '1024'); + } + f.native.save('q2_0', { values: { vram_reserve_mib: 300 } }); + assert.equal(reserveOf((await load(f, { resourceProfile: 'performance' })).args), '300'); + // edited by hand in both places of the file + const hand = f.read(); hand.vram_reserve_mib = 900; hand.args = hand.args.map(a => (a === '300' ? '900' : a)); f.write(hand); + assert.equal(reserveOf((await load(f, { resourceProfile: 'work' })).args), '900'); + // the value of this load alone + assert.equal(reserveOf((await load(f, { resourceProfile: 'work', values: { vram_reserve_mib: 128 } })).args), '128'); + f.native.save('q2_0', { values: { vram_reserve_mib: null } }); + const cleared = f.read(); + assert.equal('vram_reserve_mib' in cleared, false); + assert.equal(cleared.args.includes('--vram-reserve-mib'), false); + assert.equal(cleared.user_args, undefined); + assert.equal(reserveOf((await load(f, { resourceProfile: 'performance' })).args), '1536', 'nothing asked: the safety default'); + } finally { await f.native.close(); f.cleanup(); } +}); + +test('3270: a 0.2.0-beta file (the app wrote 1536 itself) is not taken for the person\'s choice, but a number they changed is', async () => { + const f = fixture({ args: [...ARGS, '--vram-reserve-mib', '1536'], vram_reserve_mib: 1536, ui_resource_profile: 'work', ui_resource_baseline: { workers: 0, reserve: 700, ramBudget: false, ramHeadroom: false } }); + try { + await f.native.refresh(true); + assert.equal(reserveOf((await load(f, { resourceProfile: 'performance' })).args), '1536'); + const edited = f.read(); edited.args = edited.args.map(a => (a === '1536' ? '1024' : a)); f.write(edited); + assert.equal(reserveOf((await load(f, { resourceProfile: 'performance' })).args), '1024'); + } finally { await f.native.close(); f.cleanup(); } +}); + +test('3216: engine arguments the app does not know (control vectors) survive a load and a setup run that rewrites "args"', async () => { + // setup wrote ARGS (and a copy of them); the person typed the control vector options into the JSON editor + const f = fixture({ args: [...ARGS, ...CVEC], args_written: ARGS }); + try { + await f.native.refresh(true); + const run = await load(f, { resourceProfile: 'work' }); + assert.deepEqual(run.args.slice(run.args.indexOf('--control-vector-scaled'), run.args.indexOf('--control-vector-scaled') + 9), CVEC); + const file = f.read(); + assert.deepEqual(file.user_args, CVEC); + assert.ok(file.args.join(' ').includes(CVEC.join(' ')), 'the file keeps them in "args" too'); + // a setup run rewrites "args" from its own choices and carries user_args along (setup.py carry_user_args) + f.write({ ...file, args: [...ARGS, '--max-context', '65536'], args_written: undefined }); + const again = await load(f, { resourceProfile: 'work' }); + assert.ok(again.args.join(' ').includes(CVEC.join(' ')), 'still there after the rewrite'); + assert.equal(again.args.filter(a => a === '--cvec-mode').length, 1); + assert.deepEqual(f.read().user_args, CVEC); + // removing them from "user_args" and "args" by hand takes them off for good + const bare = f.read(); bare.user_args = undefined; bare.args = [...ARGS]; bare.args_written = [...ARGS]; f.write(bare); + assert.ok(!(await load(f, { resourceProfile: 'work' })).args.includes('--cvec-mode')); + } finally { await f.native.close(); f.cleanup(); } +}); + +test('3216: without any resource preference the load keeps them too, and the person\'s value beats a generated one for the same option', async () => { + const f = fixture({ args: ARGS.map(a => (a === '4' ? '2' : a)), args_written: ARGS }); + try { + await f.native.refresh(true); + const run = await load(f); // no resourceProfile: the plain path + assert.equal(run.args.filter(a => a === '--spec').length, 1); + assert.equal(run.args[run.args.indexOf('--spec') + 1], '2', 'a hand-changed --spec is the person\'s'); + assert.deepEqual(f.read().user_args, ['--spec', '2']); + } finally { await f.native.close(); f.cleanup(); } +}); diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md index ac541308..e2c206a9 100644 --- a/docs/TROUBLESHOOTING.md +++ b/docs/TROUBLESHOOTING.md @@ -66,13 +66,23 @@ for one model: in the app, Models, Expert settings, "Draft layer (MTP)": Off; in model's `strata-.json`. The choice is kept per model and applied on every start. **I changed `strata-.json` by hand and the change came back.** -The app and `START-HERE` own the `args` list and the `vision` section of that file: whenever you prepare a model, change its -context size or its picture setting, or use "Download / repair", setup writes both again from its own choices. It keeps every -other key (`sampling`, `mcp_servers`, `mtp`, `ui_vision_enabled`, `parallel`, ...) and the earlier file as -`strata-.json.bak`. So put your choice where it is kept: `"mtp": false` for the draft layer, the picture tick in the -Models screen (saved as `ui_vision_enabled`), and the context size in the Models screen. Hand-editing `args` still works for -a model you only start (the `START-HERE` run file and `strata launch` both read the file as it is); setup says which options it -dropped and where the earlier file is. +Engine options you add to `args` by hand (control vectors, `--spec`, `--vram-reserve-mib 0`, anything the app does not offer) +are kept. The file has two helper keys for this. `user_args` lists your options; every start and every setup run puts them +over the ones the app generates, and for the same option your value wins. `args_written` is a copy of `args` as the app last +wrote it, so an option you added or changed in `args` is noticed and moved into `user_args` the next time the app or setup +touches the file. To undo one, delete it from both `args` and `user_args`. A value you save in the app's Expert settings +("GPU memory for other apps") is put in `user_args` for you. + +Two things the app still chooses itself: the context size, KV type, picture reading and draft layer. Set them in the Models +screen, or with `"mtp": false` (draft layer). The app keeps the earlier file as `strata-.json.bak`. + +**The app keeps more graphics memory free than I want (decode is slower than in 0.1.45).** +Without a value of yours the app keeps 1536 MiB free (2048 with picture reading) on a card that drives a display, and 512 MiB +(700 with pictures) on a card that has no display attached, such as a second GPU with the screen on the integrated one. It +reads that from the driver (nvidia-smi `display_active`, or the Windows adapter that has the desktop); when it cannot tell, it +assumes a display. Your own value is never raised or lowered: set "GPU memory for other apps" in Expert settings, or +`--vram-reserve-mib 0` in `args`. The engine's own warning about low free memory stays; a card that also draws the desktop +can freeze or slow to a few tokens a second with a very small reserve. **It stopped while downloading or installing.** Run `START-HERE.bat` (Linux: `./setup.sh`) again. It continues where it stopped. diff --git a/serve/runconfig.py b/serve/runconfig.py index 741680d9..1d84ba8e 100644 --- a/serve/runconfig.py +++ b/serve/runconfig.py @@ -14,6 +14,8 @@ import os import shutil from pathlib import Path +from strata_control import user_args + # (key, kind, help). kind: "bool", "int>=0", "num>=0", ("enum", values), "names", or ("sampling", check) for a key # of the "sampling" block, ("arg", flag) for an engine option kept in "args". EDITABLE = [ @@ -155,6 +157,13 @@ def apply(cfg: dict, changes: dict) -> tuple[dict, list[str]]: elif v is not None: a += [flag, str(v)] new["args"] = a + mine = user_args.set_flag(user_args.capture(new), flag, v) # a value set here is the person's: it survives setup and the app + if mine: + new[user_args.USER_KEY] = mine + else: + new.pop(user_args.USER_KEY, None) + if user_args.WRITTEN_KEY in new: + new[user_args.WRITTEN_KEY] = list(a) elif v is None: new.pop(key, None) else: diff --git a/serve/server.py b/serve/server.py index 94a488c6..68433809 100644 --- a/serve/server.py +++ b/serve/server.py @@ -69,6 +69,7 @@ from strata_control import engine_component # noqa: E402 from strata_control import hipblaslt_tuning # noqa: E402 from strata_control import vision_reader # noqa: E402 from strata_control import draft_layer # noqa: E402 +from strata_control import user_args # noqa: E402 from serve import ready as ready_file # noqa: E402 from serve import owner_guard # noqa: E402 from serve import launch_identity # noqa: E402 @@ -2873,6 +2874,10 @@ def engine_args(cfg: dict) -> list[str]: config: "auto" by default, or the first layer of each later GPU's share, e.g. "18" or "16,32"; see layer_split_value).""" args = list(cfg["args"]) + # "user_args" (strata_control/user_args.py): the options the person set by hand, over the generated ones at every start, + # so a setup run or the app rewriting "args" cannot undo them (#3216 #3215 #3270) + if cfg.get("user_args"): + args = user_args.merge(args, cfg["user_args"]) # "mtp": false (the Models settings, `strata launch --mtp off`, or by hand): no draft layer, whatever the args say. A setup run # that writes --mtp back into the args cannot undo it, because this is applied at every start. if draft_layer.is_off(cfg): diff --git a/setup.py b/setup.py index aa39757b..8cede166 100644 --- a/setup.py +++ b/setup.py @@ -64,6 +64,7 @@ from pathlib import Path from strata_control import acquisition from strata_control import draft_layer +from strata_control import user_args ROOT = Path(__file__).resolve().parent # The desktop app runs this installer under Strata Control (its bundled Python, STRATA_CONTROL_INSTALL=1). The source @@ -5487,8 +5488,10 @@ def write_config(path: Path, cfg: dict): # #629: the run config's keys setup writes itself (and rewrites on every setup run); any other key is the user's - a # "sampling" or "mcp_servers" block, "allowed_hosts", "cors_origins", "open_browser" - and is kept when setup runs again +# "args_written" (strata_control/user_args.py) is setup's own; "user_args" is not in SETUP_KEYS on purpose: carry_over keeps it, +# then applies the person's options over the generated ones SETUP_KEYS = frozenset({"exe", "args", "cwd", "tokenizer", "model_name", "log", "lib_dirs", "port", "backend", "env", - "gpu", "gpus_asked", "layer_split", "host", "api_key", "draft_vocab", "vision"}) + "gpu", "gpus_asked", "layer_split", "host", "api_key", "draft_vocab", "vision", "args_written"}) SETUP_ENV = frozenset({"STRATA_HIPBLASLT_TUNING", "STRATA_ROCBLAS_TUNING", "STRATA_RESIDENT_PIN", "STRATA_NO_ARENA_THP", "STRATA_MMQ_KQ", "STRATA_HC_Q8", "STRATA_ROUTE_TAIL_SKIP"}) | frozenset(GFX1103_SWITCHES) | frozenset(GFX1150_SWITCHES) # the "env" entries setup writes (the gfx1103 switches are setup's too: a re-run refreshes them, they do not linger) # 0.1.44 added these three, written for Unsloth's files only (unsloth_default_env). In any other model's config they are the @@ -5498,12 +5501,13 @@ SETUP_ENV_UNSLOTH = frozenset({"STRATA_MMQ_KQ", "STRATA_HC_Q8", "STRATA_ROUTE_TA SETUP_VISION = frozenset({"exe", "cpu_exe", "mmproj", "model", "gpu", "max_tokens", "threads"}) -def carry_over(old: dict, cfg: dict) -> list[str]: +def carry_over(old: dict, cfg: dict, explicit: list[str] | None = None) -> list[str]: """#629: setup run again for an installed model keeps what the user added to its run config: every key setup does not write (`SETUP_KEYS`), the "env" entries setup does not write, and in "vision" the keys setup does not write plus an mmproj of their own (a Q8_0 one, #625) that still exists. `cfg` (the new config) is updated in place; the names of what was kept are returned. Engine options added by hand to "args" are not merged (setup chooses - those): `args_dropped` names them.""" + those): `args_dropped` names them. `explicit`: engine options given on this run's command line (they win and are + kept from now on, see carry_user_args).""" kept = [] for k, v in old.items(): if k not in SETUP_KEYS and k not in cfg: @@ -5526,12 +5530,69 @@ def carry_over(old: dict, cfg: dict) -> list[str]: nv["mmproj"] = mm kept.append("vision mmproj") kept += carry_profile_args(old, cfg) + kept += carry_user_args(old, cfg, explicit) off = draft_layer.apply(cfg) # "mtp": false was the user's (a key setup does not write): the new args honour it if off: kept.append("mtp off (" + " ".join(off) + " left out of args)") return kept +# the options setup chooses itself on every run from its answers (context, KV type, pictures, the draft layer, the layer split): +# a hand edit of one of these is carried by the answers (choices_from_config) or by its own key ("mtp", "vision"), not by user_args +SETUP_CHOICE_FLAGS = frozenset({"--max-context", "--kv", "--vision", "--mtp", "--spec-min-p", "--batch-mtp", "--layer-split"}) + + +def setup_flags() -> frozenset: + """Every engine option this file names (setup.py's own source): the options setup can write. Used for a run config + that has no `args_written` (written by an earlier Strata): an option in its args that is not in this set was added by hand.""" + global _SETUP_FLAGS + if _SETUP_FLAGS is None: + try: + text = Path(__file__).read_text(encoding="utf-8") + _SETUP_FLAGS = frozenset(re.findall(r'"(--[a-z][a-z0-9-]+)"', text)) + except OSError: + _SETUP_FLAGS = frozenset() + return _SETUP_FLAGS + + +_SETUP_FLAGS = None + + +def explicit_args(a) -> list[str]: + """The engine options this run's command line sets that the person keeps from now on (user_args): --vram-reserve-mib.""" + n = getattr(a, "vram_reserve_mib", None) + return ["--vram-reserve-mib", str(n)] if n is not None else [] + + +def carry_user_args(old: dict, cfg: dict, explicit: list[str] | None = None) -> list[str]: + """#3216 #3215 #3270: the engine options the person set by hand in the earlier run config are put back over the + generated ones, and recorded in the new one's "user_args" (strata_control/user_args.py). They are the earlier + "user_args", the options of "args" that differ from "args_written" (hand edits), for a config with no "args_written" + the options its args had and the new ones do not (control vectors, --spec, ...) and an explicit --vram-reserve-mib of + the earlier file. `explicit`: options given on this run's command line, which win. The names kept are returned.""" + nargs = cfg.get("args") + if not isinstance(nargs, list): + return [] + mine = user_args.capture(old, SETUP_CHOICE_FLAGS) + oargs = old.get("args") + if user_args.WRITTEN_KEY not in old and isinstance(oargs, list): + have = {user_args.flag_of(g) for g in user_args.groups(mine)} + mine += user_args.flat([g for g in user_args.groups(user_args.dropped_by_setup(oargs, nargs, setup_flags(), SETUP_CHOICE_FLAGS)) + if user_args.flag_of(g) not in have]) + reserve = flag_value(oargs, "--vram-reserve-mib") # an old file: the reserve it names, unless it is the vision default or the app's own + vis = isinstance(old.get("vision"), dict) + if reserve is not None and reserve.isdigit() and not (vis and int(reserve) == VISION["gpu"]["reserve_mib"]) and "ui_resource_baseline" not in old and "--vram-reserve-mib" not in mine: + mine += ["--vram-reserve-mib", reserve] + for g in user_args.groups(explicit or []): + mine = user_args.merge(mine, g) + if not mine: + user_args.stamp(cfg, []) + return [] + cfg["args"] = user_args.merge(nargs, mine) + user_args.stamp(cfg, mine) + return ["args " + f for f in dict.fromkeys(g[0] for g in user_args.groups(mine) if g[0].startswith("--"))] + + def flag_value(args: list, flag: str): """The value after `flag` in an argument list, or None.""" for i, a in enumerate(args[:-1]): @@ -5575,7 +5636,7 @@ def engine_exe(eng: Path) -> str: return "component:strata-engine" if CONTROL else str(eng / EXE) -def write_setup_config(cfg_path: Path, cfg: dict, source: Path | None = None) -> None: +def write_setup_config(cfg_path: Path, cfg: dict, source: Path | None = None, explicit: list[str] | None = None) -> None: """#629: setup's run config, written over an earlier one for the same model without losing what the user added to it: the keys setup does not write are carried over (carry_over), and the earlier file is kept as strata-.json.bak when it changes. `source`: an earlier install's config to carry the keys over from when @@ -5589,7 +5650,10 @@ def write_setup_config(cfg_path: Path, cfg: dict, source: Path | None = None) -> pass if not isinstance(old, dict): old = None - kept = carry_over(old, cfg) if old is not None else [] + kept = carry_over(old, cfg, explicit) if old is not None else [] + if old is None: + carry_user_args({}, cfg, explicit) # a first install: only what this run's command line sets (nothing to report as kept) + user_args.stamp(cfg) # args_written: what setup wrote, for the next hand-edit check if "images" in cfg and cfg["images"] is None: # --images no (or not downloaded): no image section cfg.pop("images") bak = None @@ -5994,6 +6058,9 @@ def _start(cfg_path: Path, port: int | None, gpu: int | list | None = None, open args[args.index("--vram-reserve-mib") + 1] = str(reserve) else: args += ["--vram-reserve-mib", str(reserve)] + cfg[user_args.USER_KEY] = user_args.set_flag(user_args.capture(cfg), "--vram-reserve-mib", reserve) # the person's own value: kept by every later setup run and start + if user_args.WRITTEN_KEY in cfg: + cfg[user_args.WRITTEN_KEY] = list(cfg["args"]) write_config(cfg_path, cfg) ok(f"saved for this model: {reserve} MiB of VRAM kept free for other programs (--vram-reserve-mib)") if keep and any(cfg.get(k) != v for k, v in keep.items()): # #179: a --host/--api-key on a start was ignored @@ -6946,7 +7013,7 @@ def install_own_path(a, family, gpu, hip, amd, cuda_tk, multi, port, ram, adopte else: cfg["parallel"] = 1 ok("parallel requests: one at a time (--parallel 1)") - write_setup_config(cfg_path, cfg, adopted if adopted is not None and adopted.name == cfg_path.name else None) + write_setup_config(cfg_path, cfg, adopted if adopted is not None and adopted.name == cfg_path.name else None, explicit_args(a)) script = write_run_script(tag, cfg_path, port, cfg.get("open_browser") is not False) ok(f"start script: {script.name}") say() @@ -8443,7 +8510,7 @@ def main() -> int: say(" " + line) for line in bench_tips(cfg["args"], cfg.get("env"), ram, MODELS[model]["ram_gb"], gpu.get("vram_gb", 0.0), vision, WIN): say(" " + line) - write_setup_config(cfg_path, cfg, adopted if adopted is not None and adopted.name == cfg_path.name else None) + write_setup_config(cfg_path, cfg, adopted if adopted is not None and adopted.name == cfg_path.name else None, explicit_args(a)) script = write_run_script(tag, cfg_path, port, cfg.get("open_browser") is not False) # offered only when someone answers: --yes installs and adopted earlier installs are not held up by it if cal is None and not hip and not a.no_start and not a.yes and ask( diff --git a/strata_control/tune_apply.py b/strata_control/tune_apply.py index 6aced3fa..70d4c664 100644 --- a/strata_control/tune_apply.py +++ b/strata_control/tune_apply.py @@ -41,6 +41,8 @@ from dataclasses import dataclass from pathlib import Path from typing import Any, Callable, Sequence +from strata_control import user_args + SCHEMA_VERSION = 1 @@ -152,6 +154,14 @@ def apply_args_to_config(config: dict[str, Any], argv: Sequence[str]) -> dict[st for flag in sorted(wanted): stripped.extend(wanted[flag]) candidate["args"] = stripped + # The person's own options (user_args, strata_control/user_args.py) are kept, except the flags this recommendation sets: a tuning + # the person asks for is their newer choice. The hand edits so far are recorded first, so writing "args" here loses none. + if candidate.get(user_args.WRITTEN_KEY) is not None or candidate.get(user_args.USER_KEY): + mine = user_args.capture(config) + mine = user_args.flat([g for g in user_args.groups(mine) if user_args.flag_of(g) not in wanted]) + user_args.stamp(candidate, mine) + if user_args.WRITTEN_KEY not in config: + candidate.pop(user_args.WRITTEN_KEY, None) return candidate diff --git a/strata_control/user_args.py b/strata_control/user_args.py new file mode 100644 index 00000000..ce698aa1 --- /dev/null +++ b/strata_control/user_args.py @@ -0,0 +1,118 @@ +"""The engine options a person set by hand in a model's run config (strata-.json). + +Setup and the app write ``"args"`` from their own choices, and used to write it again on every run, so an option added by +hand (the control vector options, a --vram-reserve-mib of 0, --spec, ...) was gone after the next Load (#3216 #3215 #3270). +The run config now keeps what the person set apart from what was generated: + +* ``"user_args"``: the person's options, in the same form as ``"args"`` (``["--vram-reserve-mib", "0", ...]``). Every start + (serve/server.py engine_args) and every setup run applies them on top of the generated ones; for the same flag the + person's value wins. The person can edit this list by hand; the app's settings window writes the options it shows here. +* ``"args_written"``: the whole ``"args"`` as setup or the app last wrote it. An option in ``"args"`` that is not in this + copy was added or changed by hand since: ``capture`` moves it into ``"user_args"``. A config without the copy (one an + older Strata wrote) has nothing to compare with: setup then keeps the options of the old file that its new arguments do not + have (see ``dropped_by_setup``). + +Writers that change ``"args"`` themselves (setup, the app's resource profile, a tuning run) call ``stamp`` after they have +captured, so their own changes are never mistaken for the person's. +""" + +from __future__ import annotations + +from typing import Any + +USER_KEY = "user_args" +WRITTEN_KEY = "args_written" + +#: option groups the engine takes more than one of, or that make sense only together: kept whole +#: (the control vector options of the experimental speed projection and the person's own vectors, #3216) +USER_KEPT_FLAGS = frozenset({"--control-vector", "--control-vector-scaled", "--control-vector-layer-range", "--cvec-mode", + "--cvec-dir"}) + + +def groups(args: Any) -> list[list[str]]: + """Split an argument list into groups: an option and the values after it (up to the next ``--option``).""" + out: list[list[str]] = [] + for a in args if isinstance(args, list) else []: + a = str(a) + if a.startswith("--") or not out: + out.append([a]) + else: + out[-1].append(a) + return out + + +def flat(gs: list[list[str]]) -> list[str]: + return [t for g in gs for t in g] + + +def flag_of(group: list[str]) -> str: + return group[0] if group and group[0].startswith("--") else "" + + +def merge(args: Any, user: Any) -> list[str]: + """``args`` with the person's options on top: every group of ``args`` whose option the person set is replaced.""" + ug = groups(user) + mine = {flag_of(g) for g in ug if flag_of(g)} + kept = [g for g in groups(args) if flag_of(g) not in mine] + return flat(kept + ug) + + +def apply(cfg: dict) -> list[str]: + """Puts the config's ``user_args`` over its ``args`` (in place); returns the options applied.""" + ug = groups(cfg.get(USER_KEY)) + if not ug or not isinstance(cfg.get("args"), list): + return [] + cfg["args"] = merge(cfg["args"], flat(ug)) + return [flag_of(g) for g in ug if flag_of(g)] + + +def _by_flag(gs: list[list[str]]) -> dict[str, list[list[str]]]: + d: dict[str, list[list[str]]] = {} + for g in gs: + d.setdefault(flag_of(g), []).append(g) + return d + + +def capture(cfg: dict, skip=frozenset()) -> list[str]: + """The person's options of a config: its ``user_args``, with the options of ``args`` that differ from ``args_written`` + (added or changed by hand since the last write) on top. Without ``args_written`` only ``user_args`` is returned. + ``skip``: options the caller chooses itself (setup's context, KV type, pictures, draft layer), never taken from ``args``.""" + mine = _by_flag(groups(cfg.get(USER_KEY))) + written = cfg.get(WRITTEN_KEY) + if isinstance(written, list) and isinstance(cfg.get("args"), list): + cur, was = _by_flag(groups(cfg["args"])), _by_flag(groups(written)) + for flag, gs in cur.items(): + if flag and flag not in skip and was.get(flag) != gs: + mine[flag] = gs # added, or its value changed, by hand + for flag in list(mine): + if flag in was and flag not in cur: + del mine[flag] # taken out of "args" by hand: the person no longer wants it + return flat([g for gs in mine.values() for g in gs]) + + +def stamp(cfg: dict, user: list[str] | None = None) -> None: + """Records ``cfg["args"]`` as written (call after the last change to it) and ``user`` as the person's options.""" + if user is not None: + if user: + cfg[USER_KEY] = list(user) + else: + cfg.pop(USER_KEY, None) + if isinstance(cfg.get("args"), list): + cfg[WRITTEN_KEY] = list(cfg["args"]) + + +def dropped_by_setup(old_args: Any, new_args: Any, setup_flags: set[str] | frozenset[str], skip=frozenset()) -> list[str]: + """For an old config with no ``args_written``: its options that the new arguments do not have and that setup never + writes (``setup_flags``: every option setup.py names), plus the control vector options. These were added by hand.""" + have = {flag_of(g) for g in groups(new_args)} + out = [g for g in groups(old_args) if flag_of(g) and flag_of(g) not in have and flag_of(g) not in skip + and (flag_of(g) not in setup_flags or flag_of(g) in USER_KEPT_FLAGS)] + return flat(out) + + +def set_flag(user: list[str], flag: str, value: Any) -> list[str]: + """``user`` with ``flag`` set to ``value`` (None: the person's option removed).""" + rest = [g for g in groups(user) if flag_of(g) != flag] + if value is not None: + rest.append([flag, str(value)]) + return flat(rest) diff --git a/tools/strata_mcp.py b/tools/strata_mcp.py index f9ddcc04..37e14b8b 100644 --- a/tools/strata_mcp.py +++ b/tools/strata_mcp.py @@ -868,6 +868,10 @@ class Strata: for g in amd: # a Ryzen's integrated graphics beside an NVIDIA card: the engine runs on the NVIDIA card if g.get("uma") and g.get("usable"): g["usable"], g["problem"] = False, "integrated graphics: Strata uses the NVIDIA card in this computer" + try: # does the card drive a display? (the app keeps less VRAM free on one that does not, #3215) + mark_display(nv + amd, nvidia_display_active() if nv else {}, windows_desktop_adapters() if WIN and amd else []) + except Exception: # noqa: BLE001 - unknown stays unknown: the app then keeps its safety default + pass hw["gpus"] = nv + amd self._engine_vendor(hw) if WIN: @@ -1436,6 +1440,43 @@ def own_nvidia_gpus() -> list: return found +def nvidia_display_active() -> dict: + """{GPU index: True when a display is attached to the card} from nvidia-smi (display_active Enabled); {} when it says nothing.""" + found = {} + for line in run_quiet(["nvidia-smi", "--query-gpu=index,display_active", "--format=csv,noheader"]).strip().splitlines(): + idx, _, state = line.partition(",") + if idx.strip().isdigit() and state.strip().lower() in ("enabled", "disabled"): + found[int(idx)] = state.strip().lower() == "enabled" + return found + + +def windows_desktop_adapters() -> list: + """The PNPDeviceIDs of the Windows display adapters that have a desktop resolution (a monitor is attached to them).""" + out = [] + for line in run_quiet(["powershell", "-NoProfile", "-NonInteractive", "-Command", + "Get-CimInstance Win32_VideoController | ForEach-Object { $_.PNPDeviceID + '|' + $_.CurrentHorizontalResolution }"] + ).splitlines(): + pnp, _, res = line.strip().rpartition("|") + if pnp and res.strip().isdigit() and int(res) > 0: + out.append(pnp) + return out + + +def mark_display(gpus: list, nvidia_active: dict, desktop_pnp: list) -> None: + """Sets g["drives_display"] (True / False) on the cards that can be told, from nvidia-smi (NVIDIA) and the Windows adapters that have a desktop + resolution (AMD and Intel, matched by PCI device id; two cards of one model are not told apart: left unknown). A card left without the key is + unknown, and the app treats an unknown card as one that drives a display.""" + desk = [str(p).upper() for p in desktop_pnp] + for g in gpus: + if g.get("vendor") == "nvidia" and g.get("index") in nvidia_active: + g["drives_display"] = nvidia_active[g["index"]] + elif g.get("vendor") == "amd" and isinstance(g.get("pci_id"), int) and desktop_pnp is not None and desktop_pnp: + tag = f"DEV_{g['pci_id']:04X}" + twins = [x for x in gpus if x.get("vendor") == "amd" and x.get("pci_id") == g["pci_id"]] + if len(twins) == 1: + g["drives_display"] = any(tag in p and "VEN_1002" in p for p in desk) + + def windows_video_controllers() -> list: s = run_quiet(["powershell", "-NoProfile", "-NonInteractive", "-Command", "Get-CimInstance Win32_VideoController | Select-Object Name,DriverVersion | ConvertTo-Json -Compress"]) diff --git a/tools/test_setup_config.py b/tools/test_setup_config.py index daf0ced2..25c9457d 100644 --- a/tools/test_setup_config.py +++ b/tools/test_setup_config.py @@ -53,11 +53,12 @@ class CarryOver(unittest.TestCase): self.assertEqual(cfg[k], v, k) a = cfg["args"] self.assertEqual(a[a.index("--max-context") + 1], "131072") # the new choice is used - self.assertNotIn("--hand-option", a) # setup chooses the engine options... + self.assertEqual(a[a.index("--hand-option") + 1], "7") # an option added by hand is kept (#3216)... + self.assertEqual(cfg["user_args"], ["--hand-option", "7"]) # ...and recorded as the person's self.assertIn("kept from your earlier strata-q2_0.json: sampling, mcp_servers, mcp, cors_origins, " "reasoning_effort", out) self.assertIn("kept as strata-q2_0.json.bak", out) - self.assertIn("--hand-option", out) # ...and says which ones it did not keep + self.assertIn("args --hand-option", out) # ...and the run says it was kept def test_a_first_install_is_unchanged(self): code, out, cfg, _ = self.run_setup() @@ -111,7 +112,7 @@ class CarryOver(unittest.TestCase): p = Path(d) / "strata-x.json" p.write_text("{cut off", encoding="utf-8") quiet(setup.write_setup_config, p, {"exe": "e", "args": []}) - self.assertEqual(json.loads(p.read_text(encoding="utf-8")), {"exe": "e", "args": []}) + self.assertEqual(json.loads(p.read_text(encoding="utf-8")), {"exe": "e", "args": [], "args_written": []}) self.assertEqual((Path(d) / "strata-x.json.bak").read_text(encoding="utf-8"), "{cut off") def test_display_tip_and_typed_ahead_flush(self): diff --git a/tools/test_setup_golden.py b/tools/test_setup_golden.py index 18239701..03a439d1 100644 --- a/tools/test_setup_golden.py +++ b/tools/test_setup_golden.py @@ -140,6 +140,8 @@ def install(ram, found, argv, answers=None, extra=(), avx512=False, configs=()): cfg = None if written and code == 0: cfg = normalize(json.loads(written[-1].read_text(encoding="utf-8")), t) + # "args_written" (strata_control/user_args.py): the arguments as setup wrote them, so a later hand edit can be told; the golden configs keep their own keys + assert cfg.pop("args_written", cfg.get("args")) == cfg.get("args"), "args_written is not the written args" return code, out.getvalue(), cfg, asked diff --git a/tools/test_setup_qwen36.py b/tools/test_setup_qwen36.py index 1af98b3e..2d47302b 100644 --- a/tools/test_setup_qwen36.py +++ b/tools/test_setup_qwen36.py @@ -101,7 +101,7 @@ class InstallOwnPath(unittest.TestCase): (eng / setup.HVEXE).write_text("x") return self.root / "llama", eng, {"version": "0.1.44"}, ["/libs"], gpu, vision - def write_cfg(path, cfg, source=None): + def write_cfg(path, cfg, source=None, explicit=None): self.cfg = cfg self.calls.append(("config", path.name)) diff --git a/tools/test_user_args.py b/tools/test_user_args.py new file mode 100644 index 00000000..83878cb9 --- /dev/null +++ b/tools/test_user_args.py @@ -0,0 +1,192 @@ +"""The engine options a person set by hand survive setup, the app and every start (#3216 control vectors, #3215 and #3270 +--vram-reserve-mib). strata_control/user_args.py is the rule; setup.carry_over, serve/server.py engine_args, serve/runconfig.py +and strata_control/tune_apply.py use it. Every outside effect is mocked (tools/test_setup_golden.py's install()). + + python -m unittest tools.test_user_args +""" +from __future__ import annotations + +import contextlib +import io +import sys +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) +sys.path.insert(0, str(ROOT / "tools")) +import setup # noqa: E402 +import strata_mcp # noqa: E402 +from serve import runconfig # noqa: E402 +from serve.server import engine_args # noqa: E402 +from strata_control import tune_apply, user_args # noqa: E402 +from test_setup_golden import PROFILES, install # noqa: E402 + +CVEC = ["--control-vector-scaled", "C:/vectors/mine.gguf:1.0", "--control-vector-layer-range", "4", "44", + "--cvec-mode", "project", "--cvec-dir", "per-layer"] + + +def value(args, flag): + return args[args.index(flag) + 1] if flag in args[:-1] else None + + +class Rule(unittest.TestCase): + def test_groups_and_merge(self): + self.assertEqual(user_args.groups(["--a", "1", "--b", "--c", "x", "y"]), [["--a", "1"], ["--b"], ["--c", "x", "y"]]) + self.assertEqual(user_args.merge(["--a", "1", "--b", "2", "--a", "3"], ["--a", "9", "--z"]), ["--b", "2", "--a", "9", "--z"]) + self.assertEqual(user_args.merge(["--a", "1"], []), ["--a", "1"]) + + def test_capture_tells_hand_edits_from_what_was_written(self): + written = ["--max-context", "32768", "--vram-reserve-mib", "700", "--spec", "4"] + cfg = {"args": written + CVEC, "args_written": written} + self.assertEqual(user_args.capture(cfg), CVEC) + cfg = {"args": ["--max-context", "32768", "--vram-reserve-mib", "0", "--spec", "4"], "args_written": written} + self.assertEqual(user_args.capture(cfg), ["--vram-reserve-mib", "0"]) + self.assertEqual(user_args.capture({**cfg, "args": written}), []) # nothing changed + # the context is setup's own choice: a hand edit of it is not taken as an override + self.assertEqual(user_args.capture({"args": ["--max-context", "1"], "args_written": ["--max-context", "2"]}, + setup.SETUP_CHOICE_FLAGS), []) + # an option taken out of "args" by hand is the person's no more + self.assertEqual(user_args.capture({"args": written[:4], "args_written": written, "user_args": ["--spec", "2"]}), []) + self.assertEqual(user_args.capture({"args": written, "args_written": written, "user_args": ["--spec", "2"]}), ["--spec", "2"]) # user_args stands + # no copy of what was written: only user_args is known + self.assertEqual(user_args.capture({"args": CVEC, "user_args": ["--a", "1"]}), ["--a", "1"]) + + +class SetupRerun(unittest.TestCase): + """The app's Load runs setup again whenever the context, KV type or pictures differ from the installed ones.""" + + def setUp(self): + self.ram, self.found = PROFILES["64GB-1x32GB"] + + def run_setup(self, *extra, configs=()): + return install(self.ram, self.found, ["--family", "qwen", "--model", "Q2_0", "--no-start", *extra], configs=configs) + + def first(self): + code, out, first, _ = self.run_setup("--context", "65536") + self.assertEqual(code, 0, out) + return first + + def test_3216_control_vector_arguments_survive_a_load(self): + first = self.first() + old = {**first, "args": first["args"] + CVEC, "args_written": first["args"]} # typed into the JSON editor + code, out, cfg, _ = self.run_setup("--context", "131072", configs=[("strata-q2_0.json", old)]) + self.assertEqual(code, 0, out) + a = cfg["args"] + self.assertEqual(a[a.index("--control-vector-scaled"):][:9], CVEC) + self.assertEqual(value(a, "--max-context"), "131072") # setup's own choice still applies + self.assertEqual(cfg["user_args"], CVEC) + # the second Load: the options are in user_args, so they stay + code, out, again, _ = self.run_setup("--context", "65536", configs=[("strata-q2_0.json", cfg)]) + self.assertEqual(again["user_args"], CVEC) + self.assertEqual(value(again["args"], "--cvec-mode"), "project") + + def test_3216_a_config_without_args_written_keeps_them_too(self): + first = self.first() + old = {**first, "args": first["args"] + CVEC} # 0.2.0-beta: no copy of what was written + code, out, cfg, _ = self.run_setup("--context", "131072", configs=[("strata-q2_0.json", old)]) + self.assertEqual(code, 0, out) + self.assertEqual(cfg["user_args"], CVEC) + self.assertEqual(cfg["args"][-len(CVEC):], CVEC) + + def test_3215_a_reserve_of_zero_is_kept(self): + first = self.first() + written = list(first["args"]) + edited = [*written] + edited[edited.index("--vram-reserve-mib") + 1] = "0" + for old in ({**first, "args": edited, "args_written": written}, # with the copy of what was written + {**first, "args": edited}): # a 0.2.0-beta file + code, out, cfg, _ = self.run_setup("--context", "131072", configs=[("strata-q2_0.json", old)]) + self.assertEqual(code, 0, out) + self.assertEqual(value(cfg["args"], "--vram-reserve-mib"), "0") + self.assertEqual(cfg["user_args"], ["--vram-reserve-mib", "0"]) + + def test_3270_a_lowered_reserve_is_not_reset(self): + first = self.first() + written = list(first["args"]) + for mib in ("1024", "256"): + edited = [*written] + edited[edited.index("--vram-reserve-mib") + 1] = mib + code, out, cfg, _ = self.run_setup("--context", "131072", configs=[("strata-q2_0.json", {**first, "args": edited, "args_written": written})]) + self.assertEqual(value(cfg["args"], "--vram-reserve-mib"), mib) + + def test_the_vision_default_is_not_taken_for_a_choice(self): + first = self.first() # pictures on the GPU write 700 themselves + self.assertEqual(value(first["args"], "--vram-reserve-mib"), "700") + code, out, cfg, _ = self.run_setup("--context", "131072", "--vision", "cpu", configs=[("strata-q2_0.json", first)]) + self.assertNotIn("user_args", cfg) + self.assertNotIn("--vram-reserve-mib", cfg["args"]) # the CPU reader needs no reserve + + def test_an_option_given_on_the_command_line_wins_and_is_kept(self): + first = self.first() + old = {**first, "args": first["args"] + ["--vram-reserve-mib", "0"], "user_args": ["--vram-reserve-mib", "0"]} + code, out, cfg, _ = self.run_setup("--context", "131072", "--vram-reserve-mib", "256", configs=[("strata-q2_0.json", old)]) + self.assertEqual(code, 0, out) + self.assertEqual(cfg["user_args"], ["--vram-reserve-mib", "256"]) + self.assertEqual(value(cfg["args"], "--vram-reserve-mib"), "256") + + def test_a_first_install_with_a_reserve_records_it(self): + code, out, cfg, _ = self.run_setup("--vram-reserve-mib", "300") + self.assertEqual(cfg["user_args"], ["--vram-reserve-mib", "300"]) + + def test_setup_writes_args_written_and_a_rerun_with_no_edit_makes_no_user_args(self): + first = self.first() + code, out, cfg, _ = self.run_setup("--context", "131072", configs=[("strata-q2_0.json", {**first, "args_written": first["args"]})]) + self.assertNotIn("user_args", cfg) + + +class Start(unittest.TestCase): + def test_every_start_puts_the_persons_options_over_the_generated_ones(self): + cfg = {"args": ["--pack", "/p", "--vram-reserve-mib", "1536", "--spec", "4"], + "user_args": ["--vram-reserve-mib", "0"] + CVEC} + with contextlib.redirect_stdout(io.StringIO()): + args = engine_args(cfg) + self.assertEqual(value(args, "--vram-reserve-mib"), "0") + self.assertEqual(args.count("--vram-reserve-mib"), 1) + self.assertEqual(args[args.index("--control-vector-scaled"):][:9], CVEC) + self.assertEqual(cfg["args"][3], "1536") # the config in memory is untouched + + def test_the_settings_page_records_the_reserve_as_the_persons(self): + cfg = {"args": ["--pack", "/p", "--vram-reserve-mib", "1536"], "args_written": ["--pack", "/p", "--vram-reserve-mib", "1536"]} + new, changed = runconfig.apply(cfg, {"vram_reserve_mib": 0}) + self.assertEqual(changed, ["vram_reserve_mib"]) + self.assertEqual(new["user_args"], ["--vram-reserve-mib", "0"]) + self.assertEqual(new["args_written"], new["args"]) + back, _ = runconfig.apply(new, {"vram_reserve_mib": None}) + self.assertNotIn("user_args", back) + self.assertNotIn("--vram-reserve-mib", back["args"]) + + def test_a_tuning_replaces_only_the_flags_it_sets(self): + cfg = {"args": ["--pack", "/p", "--vram-reserve-mib", "0", *CVEC], "args_written": ["--pack", "/p"], + "user_args": ["--vram-reserve-mib", "0", *CVEC]} + out = tune_apply.apply_args_to_config(cfg, ["--vram-reserve-mib", "512", "--pcie-frac", "0.5"]) + self.assertEqual(value(out["args"], "--vram-reserve-mib"), "512") + self.assertEqual(out["user_args"], CVEC) # the tuned flag is no longer the old pin + self.assertEqual(out["args_written"], out["args"]) + + +class DisplayCards(unittest.TestCase): + """The app keeps the safety reserve for a card that drives a display and less on one that does not (#3215).""" + + def test_nvidia_by_nvidia_smi(self): + gpus = [{"vendor": "nvidia", "index": 0}, {"vendor": "nvidia", "index": 1}, {"vendor": "nvidia", "index": 2}] + strata_mcp.mark_display(gpus, {0: False, 1: True}, []) + self.assertIs(gpus[0]["drives_display"], False) + self.assertIs(gpus[1]["drives_display"], True) + self.assertNotIn("drives_display", gpus[2]) # not told: unknown + + def test_amd_by_the_windows_adapter_with_a_desktop(self): + amd = {"vendor": "amd", "index": 1, "pci_id": 0x744C} + gpus = [{"vendor": "amd", "index": 0, "pci_id": 0x164E}, amd] + strata_mcp.mark_display(gpus, {}, ["PCI\\VEN_1002&DEV_164E&SUBSYS_1"]) + self.assertIs(gpus[0]["drives_display"], True) + self.assertIs(amd["drives_display"], False) + twins = [{"vendor": "amd", "pci_id": 0x744C}, {"vendor": "amd", "pci_id": 0x744C}] + strata_mcp.mark_display(twins, {}, ["PCI\\VEN_1002&DEV_744C"]) + self.assertNotIn("drives_display", twins[0]) # two of one model: not told apart + strata_mcp.mark_display(gpus[:1] + [{"vendor": "amd", "pci_id": 0x1}], {}, []) + self.assertTrue(True) + + +if __name__ == "__main__": + unittest.main()