okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221host tests: fileURLToPath replaces the regex that stripped the slash before a drive letter (replaces 439339ab1; the pathname form also left %20 in paths with spaces)44d885874

main
9.9 KiB100 linesraw
1import { fileURLToPath } from 'node:url';
2import test from 'node:test';
3import assert from 'node:assert/strict';
4import {createRuntimeModels,reservationBudget,rebalanceReservations} from '../server/runtime-models.mjs';
5const hw={memory:{totalGb:64},gpus:[{id:'gpu-a',index:0,name:'GPU A',vramGb:24,unified:false},{id:'gpu-b',index:1,name:'GPU B',vramGb:24,unified:false}]};
6const model={id:'chat-q4',family:'Chat',name:'Balanced',quant:'Q4',state:'installed',diskGb:14,kvKbPerToken:24,contextMax:262144,experts:0};
7const config={beginner:{contextLength:'32k'},expert:[{id:'gpu.layers',value:'Automatic'},{id:'ctx.kvMode',value:'Q8_0'},{id:'mem.mmap',value:true}],extra:{headroom:'shared'}};
8function fixture(hardware=hw,real=false){let log=[];const service=createRuntimeModels({hardware:()=>hardware,library:()=>({variants:[model]}),requests:()=>log,real});return {service,setLog:value=>{log=value;},load:(gpuIds,patch={})=>service.load({variantId:model.id,config,gpuIds,executionMode:'multiple',...patch})};}
9test('separate GPU instances retain identity, context and assignments; identical load is idempotent',()=>{
10 const {service,load}=fixture();const a=load(['gpu-a']).instance,b=load(['gpu-b']).instance;
11 assert.notEqual(a.id,b.id);assert.notEqual(a.apiModelId,b.apiModelId);assert.equal(service.state().instances.length,2);
12 assert.deepEqual(service.resolve(a.apiModelId).gpuIds,['gpu-a']);assert.deepEqual(service.resolve(b.apiModelId).gpuIds,['gpu-b']);
13 assert.equal(load(['gpu-a']).instance.id,a.id);assert.throws(()=>service.resolve(),/Choose a model ID/);assert.throws(()=>service.resolve('unloaded'),/not loaded/);
14 assert.equal(service.resolve(undefined,{chatDefault:true}).id,a.id);
15 service.setDefault(b.id);assert.equal(service.resolve(undefined,{chatDefault:true}).id,b.id);
16});
17test('per-instance queues execute across GPUs concurrently and serialize within an instance',async()=>{
18 const {service,load}=fixture();const a=load(['gpu-a']).instance,b=load(['gpu-b']).instance;
19 const signal=new AbortController().signal;let release;const gate=new Promise(r=>release=r);const started=[];
20 const first=service.queue(a.id).run(signal,async()=>{started.push('a1');await gate;});
21 const second=service.queue(a.id).run(signal,async()=>started.push('a2'));
22 await service.queue(b.id).run(signal,async()=>started.push('b1'));
23 assert.deepEqual(started,['a1','b1']);release();await Promise.all([first,second]);assert.deepEqual(started,['a1','b1','a2']);
24});
25test('unload blocks only the target instance and preserves the other model',()=>{
26 const {service,load,setLog}=fixture();const a=load(['gpu-a']).instance,b=load(['gpu-b']).instance;
27 setLog([{instanceId:a.id,state:'generating'}]);assert.throws(()=>service.unload(a.id),/requests/);
28 service.unload(b.id);assert.equal(service.state().instances.length,1);assert.equal(service.resolve(a.apiModelId).id,a.id);
29 setLog([]);service.unload(a.id);assert.equal(service.state().defaultId,null);assert.throws(()=>service.resolve(a.apiModelId),/not loaded/);
30});
31test('dedicated VRAM budgets cannot be borrowed from an unselected GPU',()=>{
32 const {load}=fixture();load(['gpu-a']);assert.throws(()=>load(['gpu-a','gpu-b'],{config:{...config,beginner:{contextLength:'256k'}}}),/reserved this memory/);
33 assert.doesNotThrow(()=>load(['gpu-b']));assert.throws(()=>load(['missing']),/detected GPUs/);assert.throws(()=>load(['gpu-a','gpu-a']),/detected GPUs/);
34});
35test('unified memory is one RAM pool; pageable weights share space and fixed allocations cannot spill',()=>{
36 const unified={memory:{totalGb:16},gpus:[{id:'metal',name:'Apple',vramGb:16,unified:true}]};
37 const {service,load}=fixture(unified);load(['metal']);load([]);
38 const state=service.state();assert.equal(state.memory.totalGb,16);assert.equal(state.memory.budgetGb,12);assert.equal(state.memory.devices.length,0);assert.ok(state.memory.ramGb<=12.01);assert.ok(state.instances.every(i=>i.memory.spillGb>0));
39 const rows=[{memory:{reserveGb:4,ramGb:10,minimumRamGb:10,pageable:true,devices:[]}} , {memory:{reserveGb:4,ramGb:10,minimumRamGb:10,pageable:true,devices:[]}}];
40 assert.equal(rebalanceReservations(rows,unified),null);assert.equal(reservationBudget(rows,unified).fits,false);
41});
42test('load is rejected when unavailable, unsupported, or native multi-instance capability is absent',()=>{
43 const {load}=fixture(hw,true);assert.throws(()=>load(['gpu-a']),/not supported/);
44 const {load:demoLoad}=fixture();assert.throws(()=>demoLoad(['gpu-a'],{config:{...config,beginner:{contextLength:'1024k'}}}),/supported conversation context/);
45 const service=createRuntimeModels({hardware:()=>hw,library:()=>({variants:[{...model,state:'available'}]})});assert.throws(()=>service.load({variantId:model.id,config,gpuIds:['gpu-a']}),/Download/);
46});
47
48// Monitor scoping must never carry one model's context or cache into another.
49import {build} from 'esbuild';
50const bundled=await build({entryPoints:[fileURLToPath(new URL('../src/lib/runtime-models.ts',import.meta.url))],bundle:true,write:false,platform:'node',format:'esm'});
51const {scopedMonitor}=await import(`data:text/javascript;base64,${Buffer.from(bundled.outputFiles[0].text).toString('base64')}`);
52test('Monitor keeps per-model requests/context separate and host RAM readings shared',()=>{
53 const {service,load}=fixture();const a=load(['gpu-a']).instance,b=load(['gpu-b']).instance;
54 service.update(a.id,{context:{usedTokens:100,maxTokens:32768,pct:.3}});
55 const live={phase:'idle',telemetry:{context:'reported'},context:{usedTokens:999},hardware:{ramUsedGb:8},history:[],totals:{tokensIn:999,tokensOut:999,requests:8},conversationCache:{source:'engine'},expertCache:{used:999}};
56 const requests=[{instanceId:a.id,state:'done',tokensIn:100,tokensOut:20},{instanceId:b.id,state:'done',tokensIn:7,tokensOut:3}];
57 const scoped=scopedMonitor(live,service.state(),b.id,requests);
58 assert.equal(scoped.context.usedTokens,0);assert.equal(scoped.requests.length,1);assert.equal(scoped.totals.tokensIn,7);assert.equal(scoped.hardware.ramUsedGb,8);assert.equal(scoped.conversationCache,undefined);
59 const all=scopedMonitor(live,service.state(),'all',requests);assert.equal(all.context.maxTokens,0);assert.equal(all.totals.requests,2);assert.equal(all.totals.tokensOut,23);
60});
61
62test('changed settings reload only the matching instance and keep its routing identity',()=>{
63 const {service,load,setLog}=fixture();const a=load(['gpu-a']).instance,b=load(['gpu-b']).instance;
64 const changed={...config,beginner:{contextLength:'64k'}};
65 setLog([{instanceId:a.id,state:'generating'}]);assert.throws(()=>load(['gpu-a'],{config:changed}),/before reloading/);
66 setLog([{instanceId:b.id,state:'generating'}]);const reloaded=load(['gpu-a'],{config:changed}).instance;
67 assert.equal(reloaded.id,a.id);assert.equal(reloaded.apiModelId,a.apiModelId);assert.equal(reloaded.memory.context,65536);assert.equal(service.state().instances.length,2);assert.equal(service.resolve(b.apiModelId).memory.context,32768);
68});
69test('native single-engine projection retains CPU placement and reload identity',()=>{
70 const {service}=fixture(hw,true);
71 const cpu={...config,expert:config.expert.map(p=>p.id==='gpu.layers'?{...p,value:'CPU only'}:p)};
72 service.adopt(model.id,cpu);const first=service.state().instances[0];assert.deepEqual(first.gpuIds,[]);assert.equal(service.state().capabilities.multipleModels,false);
73 service.adopt(model.id,{...cpu,beginner:{contextLength:'64k'}});assert.equal(service.state().instances[0].id,first.id);assert.equal(service.state().instances[0].memory.context,65536);
74 service.adopt(null,cpu);assert.equal(service.state().instances.length,0);
75});
76
77test('single is the default and switching models releases previous instances before routing',()=>{
78 const {service,load}=fixture();const a=load(['gpu-a']).instance,b=load(['gpu-b']).instance;
79 const body={variantId:model.id,config,gpuIds:['gpu-a']};
80 const preview=service.plan(body);assert.equal(preview.executionMode,'single');assert.equal(preview.budget.devices[1].usedGb,0);assert.equal(service.state().instances.length,2);
81 const selected=service.load(body).instance;assert.equal(selected.id,a.id);assert.equal(service.state().instances.length,1);assert.equal(service.state().defaultId,a.id);
82 assert.throws(()=>service.resolve(b.apiModelId),/not loaded/);assert.equal(service.resolve().id,a.id);
83 const switched=service.load({...body,gpuIds:['gpu-b']}).instance;assert.equal(service.state().instances.length,1);assert.deepEqual(switched.gpuIds,['gpu-b']);assert.equal(service.resolve().id,switched.id);
84});
85test('single-mode validation and busy requests leave every existing model intact',()=>{
86 const {service,load,setLog}=fixture();const a=load(['gpu-a']).instance,b=load(['gpu-b']).instance;
87 const body={variantId:model.id,config,gpuIds:['gpu-b']};
88 setLog([{instanceId:a.id,state:'queued'}]);assert.throws(()=>service.load(body),/before switching/);assert.equal(service.state().instances.length,2);assert.equal(service.state().defaultId,a.id);
89 setLog([]);assert.throws(()=>service.load({...body,config:{...config,beginner:{contextLength:'1024k'}}}),/supported conversation context/);assert.equal(service.resolve(b.apiModelId).id,b.id);
90 assert.throws(()=>service.load({...body,executionMode:'invalid'}),/execution/);
91});
92test('single-mode planning fits the selected model independently of siblings and explicit opt-in still adds models',()=>{
93 const unified={memory:{totalGb:16},gpus:[{id:'metal',name:'Apple',vramGb:16,unified:true}]};
94 const {service}=fixture(unified);
95 const large={...config,beginner:{contextLength:'256k'}};
96 const body={variantId:model.id,config:large,gpuIds:['metal']};service.load(body);
97 assert.equal(service.plan({...body,gpuIds:[]}).allowed,true);assert.equal(service.plan({...body,gpuIds:[],executionMode:'multiple'}).allowed,false);
98 assert.throws(()=>service.load({...body,gpuIds:[],executionMode:'multiple'}),/reserved this memory/);assert.equal(service.state().instances.length,1);
99 service.load({...body,gpuIds:[]});assert.equal(service.state().instances.length,1);assert.deepEqual(service.state().instances[0].gpuIds,[]);
100});