okami.mom/strata

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

Niko1221Merge f-ds-out ac306afb2 into f-own-intaff3a132f

main
13.2 KiB178 linesraw
1/**
2 * What a model version asks of this computer, from the catalog's own figures and the hardware report.
3 *
4 * The numbers are RAM + VRAM together WITHOUT the n-gram table (it stays on the SSD and is read when needed). The experts are held
5 * where there is room: first in the graphics memory beyond about 5 GB (the dense weights, buffers and a 32K cache), then in RAM beyond
6 * about 10 GB left for the system; whatever is left is read from the SSD. The same rules setup uses to choose its low-RAM mode
7 * (setup.py: low_ram_gpu_gb, LOW_RAM_HEADROOM_GB), so the page and the engine agree.
8 */
9export interface FitInput { ram_needed_gb?: number; experts_gb?: number | null; memory_gb?: number; mem_total?: boolean; /** the weight files without the lookup table, when the catalog states them */ fit_gb?: number | null; /** Qwen3.6: its own context rule (below) */ context_fit?: ContextFit | null }
10export interface FitHardware { ram_gb?: number | null; vram_gb?: number | null }
11export interface Fit {
12  /** RAM + VRAM the version needs (the n-gram table not counted). */
13  memoryGb: number;
14  /** About what it takes of the graphics memory / of RAM on this PC. */
15  vramUseGb: number;
16  ramUseGb: number;
17  /** About how much is read from the SSD while answering; 0 when it all fits in memory. */
18  spillGb: number;
19  known: boolean;
20}
21
22const DENSE_GB = 5, SYSTEM_ROOM_GB = 10, DENSE_EXTRA_GB = 1;
23export { DENSE_GB };
24/** The draft layer (MTP) takes about 1.08 GB of graphics memory (1082 MiB: the layer 840 + draft head 213 + the rest); it comes out of the expert cache while MTP is on. DFlash2 alone takes 0.89 GB (890 MiB, the MTP layer not loaded beside it): the fit uses the drafter that is chosen
25 *  (phaseA-tests/blackwell/RESULTS-DF2MEM.md section 1; strata_control/drafting.py DRAFTER_GB). */
26export const MTP_VRAM_GB = 1.08;
27export const DFLASH2_VRAM_GB = 0.89;
28/** DFlash2 alone with the converted pack (the selector's codebooks as Q4_0 rows: 715 MiB; strata_control/drafting.py DRAFTER_MIB "dflash2_selq4"). The page passes `dflash2_selq4` when `capabilities.dflash2Selq4` is true. */
29export const DFLASH2_SELQ4_VRAM_GB = 0.72;
30export const DRAFTER_VRAM_GB: Record<string, number> = { mtp: MTP_VRAM_GB, dflash2: DFLASH2_VRAM_GB, dflash2_selq4: DFLASH2_SELQ4_VRAM_GB, off: 0 };
31const round1 = (n: number) => Math.round(n * 10) / 10;
32
33export function memoryNeeded(v: FitInput): number {
34  if (typeof v.memory_gb === 'number' && Number.isFinite(v.memory_gb)) return v.memory_gb;
35  if (v.mem_total && typeof v.ram_needed_gb === 'number') return v.ram_needed_gb;
36  return round1((v.experts_gb ?? v.ram_needed_gb ?? 0) + DENSE_EXTRA_GB);
37}
38
39/** The conversation cache of Flash-Next: 13 attention layers, bytes per token per layer by cache format, 5.4% over the bare cache
40 *  (strata_control/vram_fit.py KV_BYTES, KV_MEASURED_FACTOR; setup.py UD_FIT_KV_BYTES). */
41const KV_BYTES: Record<string, number> = { fp16: 2112, int8: 1056, q4_0: 576, k8v4: 816 };
42const KV_LAYERS = 13, KV_FACTOR = 1.054;
43export const BASE_CONTEXT = 32768;
44export function cacheGb(context: number, kv = 'int8'): number {
45  return (121 + context * KV_LAYERS * (KV_BYTES[String(kv).toLowerCase()] ?? KV_BYTES.int8) * KV_FACTOR / 2 ** 20) / 1024;   // 121 MiB fixed per session
46}
47/** Qwen3.6-35B-A3B (its own engine path, #5715): the catalog's figures from strata_control/own_path_fit.py, the rule setup's context menu uses. Its cache
48 *  is fp32 for 11 layers, about 2.6x the Flash-Next int8 figure above, and fits a card when the fixed part plus the cache is at most the card's memory.
49 *  Pinned to the Python by tests/own-path-fit.test.mjs. */
50export interface ContextFit { bytes_per_token: number; fixed_gb: number }
51export function ownPathCacheGb(fit: ContextFit, context: number): number { return context * fit.bytes_per_token / 2 ** 30; }
52export function ownPathFits(fit: ContextFit, vramGb: number, context: number): boolean { return fit.fixed_gb + ownPathCacheGb(fit, context) <= vramGb; }
53/** The conversation cache of this model at this context: its own rule where the catalog gives one, else the Flash-Next figure. */
54export function contextCacheGb(context: number, kv = 'int8', fit?: ContextFit | null): number { return fit ? ownPathCacheGb(fit, context) : cacheGb(context, kv); }
55export interface CacheChoice { context: number; kv?: string; /** the drafter this run loads (resolveDrafter in lib/drafting.ts); absent: the MTP, as the `mtp` argument says */ drafter?: string }
56
57export function fitOn(v: FitInput, hw: FitHardware | null | undefined, mtp = true, cache?: CacheChoice | null): Fit {
58  const memoryGb = memoryNeeded(v);
59  // DENSE_GB already holds a 32K cache in the default format; the person's context and format replace it (#3832).
60  const denseGb = cache && cache.context > 0 ? Math.max(DENSE_GB - cacheGb(BASE_CONTEXT), 0) + contextCacheGb(cache.context, cache.kv, v.context_fit) : DENSE_GB;
61  const ram = Number(hw?.ram_gb), vram = Number(hw?.vram_gb) || 0;
62  if (!Number.isFinite(ram) || ram <= 0) return { memoryGb, vramUseGb: 0, ramUseGb: 0, spillGb: 0, known: false };
63  const experts = Math.max(0, memoryNeeded(v) - DENSE_EXTRA_GB);
64  const drafterGb = cache?.drafter !== undefined ? (DRAFTER_VRAM_GB[cache.drafter] ?? 0) : mtp ? MTP_VRAM_GB : 0;
65  const onGpu = Math.max(0, Math.min(experts, vram - denseGb - (vram > 0 ? drafterGb : 0)));
66  const ramRoom = Math.max(0, ram - SYSTEM_ROOM_GB);
67  const spillGb = Math.max(0, experts - onGpu - ramRoom);
68  const inRam = Math.max(0, experts - onGpu - spillGb);
69  const vramUseGb = vram > 0 ? Math.min(vram, denseGb + drafterGb + onGpu) : 0;
70  return { memoryGb, vramUseGb: round1(vramUseGb), ramUseGb: round1(inRam + DENSE_EXTRA_GB * (vram > 0 ? 0 : 1)), spillGb: round1(spillGb), known: true };
71}
72
73/** The family groups of the version list, in the order shown. `family` is the engine's family id. */
74export const VERSION_GROUPS: { id: string; title: string; families: string[] }[] = [
75  { id: 'swift', title: 'Swift models', families: ['swift'] },
76  { id: 'gsq', title: 'GSQ-RCO models', families: ['qwen', 'coder'] },
77  { id: 'nvfp4', title: 'NVFP4 models', families: ['nvfp4'] },
78  { id: 'gyro', title: 'Gyro models', families: ['gyro'] },
79  { id: 'unsloth', title: 'Unsloth models', families: ['unsloth'] },
80];
81
82/** The fixed "Recommended" picks, in order: engine family + model name + the short note shown beside the name. */
83export const RECOMMENDED: { family: string; model: string; note: string }[] = [
84  { family: 'gyro', model: 'Gyro-S', note: 'the smallest' },
85  { family: 'swift', model: 'IQ2_XS', note: 'good balance' },
86  { family: 'qwen', model: 'IQ3_S', note: 'best quality for its size' },
87  { family: 'unsloth', model: 'UD-Q4_K_XL', note: 'more quality' },
88];
89
90/** Is a group of the version list open? What the person chose wins; until they touch it, only the group holding the selected version is open
91 *  (the running version's group used to stay open whatever was clicked). */
92export const groupOpen = (opened: Record<string, boolean>, id: string, containsSelected: boolean): boolean => opened[id] ?? containsSelected;
93
94/** What the engine's own pick for this PC is (catalog.recommendation: the memory tiers of setup.py and tools/strata_mcp.py, the same answer `strata launch` shows). */
95export interface PcPick { family?: string | null; model?: string | null; why?: string }
96export const PICK_NOTE = 'the pick for this PC';
97export const OTHER_NOTE = 'the other choice';
98
99/**
100 * The recommended set, found in a version list (a version the catalog does not offer is skipped). The four fixed cards are only a display list;
101 * the pick comes from the engine's own recommendation for this PC (RAM + VRAM tiers: 40 GB Swift 1.5 IQ2_XS, 48 GB IQ3_XXS, 64 GB and up IQ3_S), so
102 * the page and `strata launch` agree. The pick comes first and is marked; when the engine names Gyro-M as the other choice (40 GB tier, NVIDIA and AMD only,
103 * which the engine decides) that card follows it. Without a pick the list is the fixed four.
104 */
105export function recommendedVersions<V extends { sourceFamily: string; model: string }>(versions: V[], pick?: PcPick | null): { r: typeof RECOMMENDED[number]; v: V }[] {
106  type Row = { r: typeof RECOMMENDED[number]; v: V };
107  const find = (family: string, model: string) => versions.find(v => v.sourceFamily === family && v.model === model);
108  const rows: Row[] = RECOMMENDED.map(r => ({ r, v: find(r.family, r.model) })).filter((x): x is Row => !!x.v);
109  if (!pick?.family || !pick.model) return rows;
110  const chosen = find(pick.family, pick.model);
111  if (!chosen) return rows;
112  const rest = rows.filter(x => x.v !== chosen);
113  const first: Row = { r: { family: pick.family, model: pick.model, note: PICK_NOTE }, v: chosen };
114  const out: Row[] = [first];
115  const gyroM = /Gyro-M/.test(pick.why ?? '') ? find('gyro', 'Gyro-M') : undefined;
116  if (gyroM) out.push({ r: { family: 'gyro', model: 'Gyro-M', note: OTHER_NOTE }, v: gyroM });
117  return [...out, ...rest.filter(x => x.v !== gyroM)];
118}
119
120/**
121 * The version list as the menu shows it, for one base model: first the versions to look at (the recommended ones, in the order given, then the ones already
122 * on this computer, each by download size), then every other version by download size. A version is in exactly one of the two, so none is shown twice
123 * and none is lost; the ones in `hidden` (shown elsewhere, as on the first-run page) are left out. `recommended` is one id or several.
124 */
125export function versionGroups<V extends { id: string; download_gb: number }>(versions: V[], hidden: ReadonlySet<string>, downloaded: ReadonlySet<string> | undefined, recommended?: string | readonly string[]): { top: V[]; more: V[] } {
126  const seen = new Set<string>();
127  const list = versions.filter(v => !hidden.has(v.id) && !seen.has(v.id) && !!seen.add(v.id));
128  const bySize = (a: V, b: V) => a.download_gb - b.download_gb;
129  const ids = typeof recommended === 'string' ? [recommended] : [...(recommended ?? [])];
130  const picked = ids.map(id => list.find(v => v.id === id)).filter((v): v is V => !!v);
131  const have = list.filter(v => !ids.includes(v.id) && downloaded?.has(v.id)).sort(bySize);
132  const top = [...picked, ...have];
133  const inTop = new Set(top.map(v => v.id));
134  return { top, more: list.filter(v => !inTop.has(v.id)).sort(bySize) };
135}
136
137/**
138 * What a version is on this computer (owner's one rule, 2026-11-08): its files, WITHOUT the n-gram / PLE / Engram lookup table's file (the download size minus
139 * that file: `fit_gb`, else `memory_gb`), against the graphics memory plus the RAM, nothing taken off either side. They fit: 'fits', and the list says nothing.
140 * They do not: 'ssd', and the list says "may run slower" (part is read from the SSD while it answers). Nothing here refuses anything: no card, or a Mac with
141 * little memory, gets the same two words. (Not enough free disk is a different question; the download says it.)
142 *
143 * `fitOn` (above) still places the experts for the This computer card; `on_this_pc` no longer decides anything here.
144 */
145export type VersionFit = 'fits' | 'ssd' | 'unknown';
146export function versionFit(v: FitInput & { on_this_pc?: string | null }, hw: FitHardware | null | undefined, cache?: CacheChoice | null): VersionFit {
147  const fit = fitOn(v, hw, true, cache);
148  if (!fit.known) return 'unknown';
149  const size = typeof v.fit_gb === 'number' && Number.isFinite(v.fit_gb) ? v.fit_gb : fit.memoryGb;
150  return size <= Number(hw?.ram_gb) + (Number(hw?.vram_gb) || 0) ? 'fits' : 'ssd';
151}
152
153/** A card as the installer's hardware report lists it. */
154export interface RunCard { index: number; name: string; vram_gb: number; usable?: boolean; vendor?: string; arch?: string; uma?: boolean; integrated?: boolean; unified_gb?: number; /** a unified-memory chip: its BIOS carve-out (setup.py amd_apply_uma) */ dedicated_gb?: number }
155
156/**
157 * The graphics memory that counts for the fit label, of the largest card: strata_control/fit_label.py card_room_gb, the same rule (the Terminal and the
158 * catalog's “on_this_pc” use it). A chip that shares the PC’s memory (Strix Halo: uma) counts only its own carve-out, because its “shared” part is the
159 * RAM, already counted (QAGUI-STRIX 11-09: Q8_0, 133.8 GB, against 63.6 GB RAM + 95.8 GB carve-out-plus-shared said nothing; Control says “may run slower”
160 * against 63.6 + 64). Pinned to the Python by tests/fit-label-rule.test.mjs.
161 */
162export function cardRoomGb(cards: readonly Pick<RunCard, 'vram_gb' | 'uma' | 'dedicated_gb'>[] | null | undefined): number {
163  let best = 0;
164  for (const c of cards ?? []) best = Math.max(best, c.uma ? Number(c.dedicated_gb) || 0 : Number(c.vram_gb) || 0);
165  return best;
166}
167
168/**
169 * The cards a model can be placed on: the ones this engine can drive (strata_control/gpu_fit.py `usable_cards` and `for_vendor`, same rule).
170 * A card the engine has no code for, a card of the other make (an engine drives one make; the report numbers each make from 0, so
171 * two cards could share a number) and a chip that shares the system memory while the computer has a card of its own are left out.
172 */
173export function runCards<T extends RunCard>(gpus: T[] | null | undefined, engineVendor?: string | null): T[] {
174  const sharesMemory = (g: RunCard) => !!(g.integrated || g.uma || g.unified_gb);
175  const mine = (gpus ?? []).filter(g => g.usable !== false && (!engineVendor || !g.vendor || g.vendor === engineVendor));
176  const hasReal = mine.some(g => !sharesMemory(g) && g.vram_gb >= 4);
177  return mine.filter(g => !(hasReal && sharesMemory(g)));
178}