okami.mom/strata

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

Niko1221Merge f-0212-df2choice (final 181b668e5) into f-0212-int081edf2d9

main
16.4 KiB221 linesraw
1/**
2 * Several requests at once in the installed app: the default, how it is sized, what a person's own number does, and the
3 * retry with one when a load with several slots fails. Nothing here starts an engine; Control is a fake that records what
4 * the host saved and asked it to start.
5 */
6import './helpers/isolate-profile.mjs';   // a test run never writes the owner's real Strata profile
7import test from 'node:test';
8import assert from 'node:assert/strict';
9import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
10import { join, basename } from 'node:path';
11import { tmpdir } from 'node:os';
12import { createNativeControl } from '../server/native-control.mjs';
13import { chooseParallel, applyParallel, withoutParallel, slotGb, DEFAULT_SLOTS, PARALLEL_POLICY, BATCH_MTP_POLICY, FALLBACK_NOTE } from '../server/native-parallel.mjs';
14import { buildProfile } from '../server/agent/profile.mjs';
15
16const hw = (gb, n = 1) => ({ gpus: Array.from({ length: n }, (_, i) => ({ index: i, vram_gb: gb })) });
17const cfg = (context, extra = {}) => ({ args: ['--max-context', String(context), '--mtp', '/m', '--spec', '4'], gpu: 0, ...extra });
18
19test('the default aims for 4 request slots; the policy is one switch; with no hardware read it is one at a time', () => {
20  assert.equal(DEFAULT_SLOTS, 4);
21  assert.ok(['fit', 'fixed'].includes(PARALLEL_POLICY));
22  const unknown = chooseParallel({ config: cfg(32768) });
23  assert.equal(unknown.slots, PARALLEL_POLICY === 'fixed' ? 4 : 1, 'nothing to size against: the safe default');
24});
25
26test('sizing: the slots may take at most a fifth of the expert cache the card would have; said in plain words', () => {
27  // measured on the 0.1.45 engine: 0.92 GiB per slot at 32K; a 12 GB card ends with 5.1 GiB of expert cache
28  assert.ok(Math.abs(slotGb({ context: 32768 }) * 0.931 - 0.92) < 0.03);
29  if (PARALLEL_POLICY !== 'fit') return;
30  const slots = (gb, context, extra = {}, cards = 1) => chooseParallel({ config: cfg(context, extra), hardware: hw(gb, cards) });
31  assert.deepEqual([12, 16, 20, 24, 32].map(gb => slots(gb, 32768).slots), [1, 1, 2, 3, 4]);
32  assert.deepEqual([12, 24, 32, 48].map(gb => slots(gb, 65536).slots), [1, 1, 2, 4], 'the context is per slot: doubling it halves the slots');
33  assert.equal(slots(24, 65536, { gpu: [0, 1] }, 2).slots, 4, 'two cards share the load');
34  const small = slots(12, 32768);
35  assert.equal(small.slots, 1); assert.match(small.note, /one request at a time/); assert.match(small.note, /Helper agents and a second chat take their turn/);
36  const some = slots(20, 32768);
37  assert.match(some.note, /room for 2 requests at once/); assert.match(some.note, /smaller context size leaves room for more/);
38  assert.equal(slots(32, 32768).note, null, 'no note when the default fits');
39  assert.doesNotMatch(small.note + some.note, /--|batch|VRAM|GiB|tok|cache/i, 'no engine words');
40});
41
42test('batch-mtp is automatic on big single cards only: 24 GB and up with room for the drafters, never on small cards or a layer split; Expert mode overrides either way', () => {
43  assert.equal(BATCH_MTP_POLICY, 'auto');
44  const mtp = (gb, extra = {}, cards = 1, values = {}, context = 32768) => chooseParallel({ config: cfg(context, extra), hardware: hw(gb, cards), values });
45  assert.deepEqual([12, 16, 20].map(gb => mtp(gb).batchMtp), [false, false, false], 'small cards: it did not pay (3060: -6% for one chat, 400 MiB less cache)');
46  assert.deepEqual([24, 32, 48, 72].map(gb => mtp(gb).batchMtp), [true, true, true, true]);
47  assert.equal(mtp(72).slots, 4);
48  assert.equal(mtp(24, {}, 1, {}, 131072).batchMtp, false, 'one slot only at this context: nothing to batch');
49  assert.equal(mtp(72, { gpu: [0, 1] }, 2).batchMtp, false, 'a layer split: -22% with all experts in VRAM');
50  assert.equal(chooseParallel({ config: { args: ['--max-context', '32768'], gpu: 0 }, hardware: hw(72) }).batchMtp, false, 'no draft layer, nothing to batch');
51  assert.equal(chooseParallel({ config: cfg(32768, { args: ['--max-context', '32768', '--mtp', '/m', '--batch-mtp'] }), hardware: hw(72) }).batchMtp, false, 'already there: not added twice');
52  const applied = applyParallel(cfg(32768), mtp(72));
53  assert.ok(applied.cfg.args.includes('--batch-mtp')); assert.equal(applied.cfg.parallel, 4);
54  // Expert mode: off wins on a big card; on wins on a small one-card setup; an explicit slot count can still ask for it
55  assert.equal(mtp(72, {}, 1, { batchMtp: false }).batchMtp, false);
56  assert.equal(mtp(72, { batchMtp: false }).batchMtp, false);
57  assert.equal(mtp(20, {}, 1, { batchMtp: true }).batchMtp, true, '20 GB: 2 slots, asked for by hand');
58  assert.equal(mtp(72, { gpu: [0, 1] }, 2, { batchMtp: true }).batchMtp, false, 'never forced on a layer split');
59  const manual = chooseParallel({ config: cfg(32768), values: { parallel: 2, batchMtp: true }, hardware: hw(12) });
60  assert.deepEqual([manual.slots, manual.explicit, manual.batchMtp], [2, true, true]);
61  assert.ok(applyParallel(cfg(32768), manual).cfg.args.includes('--batch-mtp'));
62  assert.ok(!applyParallel(cfg(32768, { parallel: 2 }), chooseParallel({ config: cfg(32768, { parallel: 2 }), hardware: hw(72) })).cfg.args.includes('--batch-mtp'), 'a number the person chose alone does not add it');
63  // the retry with one request at a time takes it away again
64  assert.ok(!withoutParallel(applied.cfg).args.includes('--batch-mtp'));
65});
66
67test("a number the person chose is kept exactly: Expert mode's value, the saved settings, an explicit --batch", () => {
68  assert.deepEqual(applyParallel(cfg(32768, { parallel: 1 }), chooseParallel({ config: cfg(32768, { parallel: 1 }), hardware: hw(24) })).cfg.parallel, 1);
69  const six = chooseParallel({ config: cfg(32768), values: { parallel: 6 }, hardware: hw(8) });
70  assert.deepEqual([six.slots, six.explicit, six.batchMtp], [6, true, false], 'recommend, never force: 6 on an 8 GB card is allowed');
71  const one = chooseParallel({ config: cfg(32768), values: { parallel: 1 }, hardware: hw(24) });
72  assert.deepEqual([one.slots, one.explicit], [1, true]);
73  assert.equal(chooseParallel({ config: cfg(32768, { args: ['--max-context', '32768', '--batch', '3'] }), hardware: hw(24) }).explicit, true);
74  const saved = cfg(32768, { parallel: 3 });
75  assert.equal(applyParallel(saved, chooseParallel({ config: saved })).cfg, saved, 'untouched');
76});
77
78test('the retry configuration has no slots and no batch-mtp', () => {
79  const next = withoutParallel({ ...cfg(32768), parallel: 4, args: [...cfg(32768).args, '--batch-mtp'] });
80  assert.equal(next.parallel, undefined); assert.ok(!next.args.includes('--batch-mtp'));
81});
82
83test('helper agents: at least as many as the engine slots, and extra ones wait for their turn instead of being refused', () => {
84  const profile = slots => buildProfile({ local: [{ id: 'a', baseUrl: 'http://127.0.0.1:1/v1', modelId: 'm', slots }] });
85  const cap = slots => JSON.parse(profile(slots).patch.split('\n').slice(1).join('\n')).find(e => e.id === 'subagent').config.maxActiveSubagents;
86  assert.deepEqual([cap(1), cap(2), cap(4), cap(6)], [4, 4, 4, 6]);
87  assert.notEqual(profile(6).signature, profile(2).signature, 'a changed helper limit restarts the agent when idle');
88});
89
90// ---------------------------------------------------------------------------------------- the installed app's load
91
92function fixture({ failStarts = 0, config = {} } = {}) {
93  const root = mkdtempSync(join(tmpdir(), 'strata-par-test-'));
94  const install = join(root, 'install'), data = join(root, 'data');
95  mkdirSync(install, { recursive: true });
96  process.env.STRATA_INSTALL_DIR = install;      // the app's own install folder: the host always names it to Control
97  const path = join(install, 'strata-iq3_s.json');
98  writeFileSync(path, JSON.stringify({ exe: 'component:strata-engine', tokenizer: join(install, 'tok'), args: ['--max-context', '32768', '--mtp', '/m', '--spec', '4'], model_name: 'Qwen iq3_s', port: 8080, ...config }));
99  const saved = []; const calls = [];
100  let starts = 0;
101  const control = {
102    control: { dir: join(root, 'control') }, version: '0.1.45',
103    async installedModels() { return { install_dir: install, models: [{ id: 'iq3_s', name: 'Qwen iq3_s', source_config: path, component_status: 'present', context_tokens: 32768 }] }; },
104    async engine() { return { usable: true, backend: 'cuda', components_dir: 'E:\\c', engine: { version: '0.1.45', platform: 'windows-x86_64', backend: 'cuda', exe: 'E:\\e\\strata.exe', root: 'E:\\e' } }; },
105    async saveProfile(name, file) { saved.push(JSON.parse(readFileSync(file, 'utf8'))); calls.push(['saveProfile', name, basename(file)]); return {}; },
106    async start(name) { calls.push(['start', name]); starts++; if (starts <= failStarts) throw new Error('server_exited: out of memory'); return { ok: true, state: 'ready' }; },
107    async stop(name) { calls.push(['stop', name]); return { ok: true }; },
108  };
109  const fetcher = async () => Response.json({ status: 'ok' });
110  const native = createNativeControl({ control, hardwareProbe: () => [{ index: 0, vram_gb: 32 }], directory: data, spawnProcess: () => { throw new Error('the host starts no process'); }, fetcher, requests: () => [] });
111  return { native, saved, calls, cleanup: () => rmSync(root, { recursive: true, force: true }) };
112}
113const 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'); };
114
115test('the installed app loads with 4 request slots by default', async () => {
116  const f = fixture();
117  try {
118    await f.native.refresh(true);
119    const job = await finish(f.native, f.native.operation('load', { model: 'iq3_s' }).id);
120    assert.equal(job.state, 'done', job.error);
121    assert.equal(f.saved.length, 1);
122    assert.equal(f.saved[0].parallel, 4);
123    assert.ok(f.saved[0].args.includes('--batch-mtp'), 'a 32 GB card: MTP per request is on automatically');
124    assert.equal(f.native.state().instances[0].parallel, 4, 'the agent reads the slot count from here');
125  } finally { await f.native.close(); f.cleanup(); }
126});
127
128test("the person's own number wins: saved settings and Expert mode's value", async () => {
129  for (const [config, values, expected] of [[{ parallel: 1 }, {}, 1], [{}, { parallel: 3 }, 3], [{ parallel: 2 }, { parallel: 6 }, 6]]) {
130    const f = fixture({ config });
131    try {
132      await f.native.refresh(true);
133      const job = await finish(f.native, f.native.operation('load', { model: 'iq3_s', values }).id);
134      assert.equal(job.state, 'done', job.error);
135      assert.equal(f.saved[0].parallel, expected);
136      assert.ok(!f.saved[0].args.includes('--batch-mtp'));
137    } finally { await f.native.close(); f.cleanup(); }
138  }
139});
140
141test('a load that fails with several slots is tried again with one, and the person is told in plain words', async () => {
142  const f = fixture({ failStarts: 1 });
143  try {
144    await f.native.refresh(true);
145    const job = await finish(f.native, f.native.operation('load', { model: 'iq3_s' }).id);
146    assert.equal(job.state, 'done', job.error);
147    assert.equal(f.saved.length, 2);
148    assert.equal(f.saved[0].parallel, 4); assert.equal(f.saved[1].parallel, undefined);
149    assert.ok(!f.saved[1].args.includes('--batch-mtp'));
150    assert.match(job.result.summary, /one request at a time/);
151    assert.equal(job.result.summary.includes(FALLBACK_NOTE), true);
152    assert.equal(f.native.state().instances[0].parallel, 1);
153  } finally { await f.native.close(); f.cleanup(); }
154});
155
156test('a load that fails even with one slot reports the failure, and a chosen number is never silently changed', async () => {
157  const both = fixture({ failStarts: 2 });
158  try {
159    await both.native.refresh(true);
160    const job = await finish(both.native, both.native.operation('load', { model: 'iq3_s' }).id);
161    assert.equal(job.state, 'error');
162    assert.equal(both.saved.length, 2);
163  } finally { await both.native.close(); both.cleanup(); }
164  const chosen = fixture({ failStarts: 1, config: { parallel: 3 } });
165  try {
166    await chosen.native.refresh(true);
167    const job = await finish(chosen.native, chosen.native.operation('load', { model: 'iq3_s' }).id);
168    assert.equal(job.state, 'error', 'the person asked for 3: no quiet fallback');
169    assert.equal(chosen.saved.length, 1);
170  } finally { await chosen.native.close(); chosen.cleanup(); }
171});
172
173test('the draft layer (MTP) is part of what the card is sized for; switched off, its 1 GB goes to the slots', () => {
174  if (PARALLEL_POLICY !== 'fit') return;
175  const slots = (gb, extra = {}, args) => chooseParallel({ config: { ...cfg(32768, extra), ...(args ? { args } : {}) }, hardware: hw(gb) }).slots;
176  assert.equal(slots(26.5), 3, 'MTP on (the default): the measured sizing stands');
177  assert.equal(slots(26.5, { mtp: false }), 4, 'a card with the draft layer off has 1 GB more cache');
178  assert.equal(slots(26.5, {}, ['--max-context', '32768', '--spec', '4']), 4, 'no draft layer in the arguments');
179  assert.equal(slots(12, { mtp: false }), 1, 'a 12 GB card still gets one request at a time');
180});
181
182test('DFlash2 chosen on purpose means one request at a time unless the person picked a number (a number they picked is kept and DFlash2 serves that many)', () => {
183  const big = hw(72);
184  const auto = chooseParallel({ config: cfg(32768), hardware: big });
185  assert.ok(auto.slots > 1, 'a card with room gets slots by default');
186  const chosen = chooseParallel({ config: cfg(32768), values: { drafting: 'dflash2' }, hardware: big });
187  assert.equal(chosen.slots, 1);
188  assert.equal(chosen.explicit, false);
189  assert.match(chosen.note, /DFlash2 answers one request at a time/);
190  assert.equal(chooseParallel({ config: cfg(32768, { drafting: 'dflash2' }), hardware: big }).slots, 1, 'a saved choice counts');
191  assert.equal(chooseParallel({ config: cfg(32768), values: { drafting: 'mtp' }, hardware: big }).slots, auto.slots, 'MTP and Auto keep the slots');
192  assert.equal(chooseParallel({ config: cfg(32768), values: { drafting: 'dflash2', parallel: 3 }, hardware: big }).slots, 3, 'a number the person chose is theirs');
193  assert.equal(applyParallel(cfg(32768, { parallel: 4 }), chosen).cfg.parallel, undefined, 'and the saved config carries no slots');
194});
195
196test('DFlash2 named with a number of requests the person chose: that many, no note, the number stays in the run config (owner 2026-11-03)', () => {
197  const big = hw(72);
198  for (const n of [2, 4, 8]) {
199    const viaValues = chooseParallel({ config: cfg(32768), values: { drafting: 'dflash2', parallel: n }, hardware: big });
200    assert.deepEqual([viaValues.slots, viaValues.explicit, viaValues.note], [n, true, null], `values ${n}`);
201    const viaConfig = chooseParallel({ config: cfg(32768, { drafting: 'dflash2', parallel: n }), hardware: big });
202    assert.deepEqual([viaConfig.slots, viaConfig.explicit, viaConfig.note], [n, true, null], `saved ${n}`);
203    const applied = applyParallel(cfg(32768, { drafting: 'dflash2', parallel: n }), viaConfig);
204    assert.equal(applied.cfg.parallel, n, 'the number stays');
205    assert.equal(applied.note, null);
206    assert.ok(!applied.cfg.args.includes('--batch-mtp'), 'no wish, no --batch-mtp');
207  }
208  // the same through the resolved drafter the load passes, and a --batch in the engine arguments
209  assert.equal(chooseParallel({ config: cfg(32768, { drafting: 'dflash2', parallel: 4 }), drafter: 'dflash2', hardware: big }).slots, 4);
210  const withBatch = chooseParallel({ config: { ...cfg(32768, { drafting: 'dflash2' }), args: ['--max-context', '32768', '--mtp', '/m', '--batch', '3'] }, drafter: 'dflash2', hardware: big });
211  assert.deepEqual([withBatch.explicit, withBatch.note], [true, null]);
212  // Expert mode's batch-mtp wish is passed on exactly as in 0.2.10 for every drafter: the server drops --batch-mtp when DFlash2 runs and keeps it when the MTP runs (Metal)
213  const wish = (extra, drafter) => chooseParallel({ config: cfg(32768), values: { parallel: 4, batchMtp: true, ...extra }, drafter, hardware: big }).batchMtp;
214  assert.equal(wish({ drafting: 'dflash2' }, 'dflash2'), true);
215  assert.equal(wish({ drafting: 'mtp' }, 'mtp'), true);
216  assert.equal(wish({}, 'dflash2'), true, 'Auto that resolved to DFlash2 is not named: the MTP serves the several requests');
217  assert.equal(wish({ drafting: 'dflash2' }, 'mtp'), true, 'named, but the drafter is not here yet: the MTP runs and keeps its slot switch');
218  // one request chosen is one request, and nothing chosen under DFlash2 is still one with the note
219  assert.equal(chooseParallel({ config: cfg(32768), values: { drafting: 'dflash2', parallel: 1 }, hardware: big }).slots, 1);
220  assert.match(chooseParallel({ config: cfg(32768), values: { drafting: 'dflash2' }, hardware: big }).note, /DFlash2 answers one request at a time/);
221});