Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

feat([issue-737]): scale FFLF pixel budget with unified memory for smooth interpolation

[._.]/ Adam Eivy4aaa16cc55ea
diff --git a/client/src/pages/VideoGen.jsx b/client/src/pages/VideoGen.jsx
index 723a076707..020082697e 100644
--- a/client/src/pages/VideoGen.jsx
+++ b/client/src/pages/VideoGen.jsx
@@ -100,8 +100,9 @@ const videoModelMemoryGb = (model) => {
// Mirror of server computeFflfSafeFrames (server/services/videoGen/local.js):
// the largest numFrames that fits the FFLF/ltx2 stage-2 pixel-frame budget at
// this resolution, rounded down to the LTX 8k+1 latent boundary. The budget
-// itself comes from /status (`fflfLtx2PixelBudget`, honors the env override) so
-// only this back-solve arithmetic is duplicated — not the constant. Lets the
+// itself comes from /status (`fflfLtx2PixelBudget`, which scales with the box's
+// unified memory and honors the env override) so only this back-solve arithmetic
+// is duplicated — not the constant. Lets the
// multi-keyframe picker reject out-of-budget indices before submit instead of
// letting the worker 400 mid-render. Returns numFrames when it already fits or
// the budget is unknown (fail-open — the server still enforces the real cap).
diff --git a/server/services/videoGen/local.js b/server/services/videoGen/local.js
index a4dc6e1c2e..e7cccba5e6 100644
--- a/server/services/videoGen/local.js
+++ b/server/services/videoGen/local.js
@@ -14,7 +14,7 @@ import { execFile, spawn } from 'child_process';
import { existsSync, statSync } from 'fs';
import { unlink, writeFile, copyFile } from 'fs/promises';
import { join, basename } from 'path';
-import { homedir, tmpdir } from 'os';
+import { homedir, tmpdir, totalmem } from 'os';
import { randomUUID } from 'crypto';
import { promisify } from 'util';
import { ensureDir, PATHS, readJSONFile, atomicWrite, UUID_RE } from '../../lib/fileUtils.js';
@@ -227,16 +227,43 @@ export const cancel = () => {
export const loadHistory = () => readJSONFile(HISTORY_FILE, []);
export const saveHistory = (h) => atomicWrite(HISTORY_FILE, h);
-// Effective FFLF/ltx2 stage-2 pixel-frame budget (width × height × numFrames).
-// The default fit 48 GB unified RAM in testing; FFLF_LTX2_PIXEL_BUDGET lets
-// users with more RAM raise it. This is the SINGLE source of truth for the cap
-// — `buildLtx2Args` enforces it server-side AND the /status route advertises it
+// FFLF/ltx2 stage-2 peak memory scales with the pixel-frame count
+// (width × height × numFrames), so the cap is on that product. Two anchors,
+// both measured on real renders:
+// • 48 GB unified RAM → 704×448×25 ≈ 7.9M pixel-frames is the largest that
+// fits stage 2 (704×448×97 OOMs there). This stays the floor.
+// • 128 GB unified RAM → 768×512×97 ≈ 38.1M pixel-frames renders comfortably
+// (validated for issue #737). 97 frames is the threshold below which FFLF
+// interpolation visibly strobes (frames advance in near-duplicate pairs),
+// so a budget that can't reach 97 frames at a usable resolution forces the
+// poor-motion regime — the whole reason this scales with RAM now.
+// Linearly interpolate per detected total RAM, floored at the 48 GB value so
+// machines at or below 48 GB are never given LESS than the previously-shipped
+// (tested-safe) cap — i.e. this only ever raises the budget, never lowers it.
+const FFLF_BUDGET_48GB = 704 * 448 * 25; // 7,884,800 — tested-safe floor (≤48 GB)
+const FFLF_BUDGET_128GB = 768 * 512 * 97; // 38,141,952 — validated on 128 GB (#737)
+const FFLF_BUDGET_SLOPE = (FFLF_BUDGET_128GB - FFLF_BUDGET_48GB) / (128 - 48); // px-frames per GB above 48
+const BYTES_PER_GB = 1024 ** 3;
+
+// Pure: pixel-frame budget for a machine with `totalMemBytes` of unified RAM.
+// Anchored on the two measured points above, floored at the 48 GB value.
+// Exported for unit testing; resolveFflfLtx2PixelBudget wraps it with os.totalmem().
+export const computeFflfLtx2PixelBudget = (totalMemBytes) => {
+ const gb = Number(totalMemBytes) / BYTES_PER_GB;
+ if (!(gb > 0)) return FFLF_BUDGET_48GB;
+ const scaled = FFLF_BUDGET_48GB + (gb - 48) * FFLF_BUDGET_SLOPE;
+ return Math.round(Math.max(FFLF_BUDGET_48GB, scaled));
+};
+
+// Effective FFLF/ltx2 stage-2 pixel-frame budget. FFLF_LTX2_PIXEL_BUDGET wins
+// (raise it on a big box, or lower it if a render OOMs); otherwise scale to
+// detected unified memory. This is the SINGLE source of truth for the cap —
+// `buildLtx2Args` enforces it server-side AND the /status route advertises it
// so the client can gate keyframe indices before submit (see computeFflfSafeFrames).
export const resolveFflfLtx2PixelBudget = () => {
const envBudget = Number(process.env.FFLF_LTX2_PIXEL_BUDGET);
- return Number.isFinite(envBudget) && envBudget > 0
- ? envBudget
- : 704 * 448 * 25; // ≈7.9M pixel-frames, confirmed to fit 48 GB unified RAM
+ if (Number.isFinite(envBudget) && envBudget > 0) return envBudget;
+ return computeFflfLtx2PixelBudget(totalmem());
};
// Back-solve the largest numFrames that fits `budget` at this resolution,
@@ -352,10 +379,12 @@ const buildLtx2Args = ({ model, prompt, negativePrompt, width, height, numFrames
// resolution and clamps to 768×432 in their UI. We empirically verified
// 25 frames @ 704×448 fits 48 GB; 97 frames @ 704×448 OOMs in stage 2.
//
- // Approach: cap the pixel-frame budget (width × height × numFrames) at a
- // value that fit on the test box, then back-solve numFrames. Round down
- // to the LTX 8k+1 latent-boundary so the model doesn't silently snap.
- // FFLF_LTX2_PIXEL_BUDGET env var lets users with more RAM raise the cap.
+ // Approach: cap the pixel-frame budget (width × height × numFrames), then
+ // back-solve numFrames. Round down to the LTX 8k+1 latent-boundary so the
+ // model doesn't silently snap. The cap auto-scales with detected unified
+ // memory (see resolveFflfLtx2PixelBudget) — 128 GB boxes reach the 97-frame
+ // smooth-motion regime out of the box, 48 GB boxes keep the tested-safe
+ // floor. FFLF_LTX2_PIXEL_BUDGET overrides the scaling either way.
if (helperMode === 'fflf') {
const pixelBudget = resolveFflfLtx2PixelBudget();
const requested = Number(width) * Number(height) * Number(numFrames);
diff --git a/server/services/videoGen/local.test.js b/server/services/videoGen/local.test.js
index 641bfb6f79..1426e1038b 100644
--- a/server/services/videoGen/local.test.js
+++ b/server/services/videoGen/local.test.js
@@ -7,7 +7,7 @@
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { join } from 'path';
-import { tmpdir } from 'os';
+import { tmpdir, totalmem } from 'os';
import { randomUUID } from 'crypto';
// ─── dep mocks (must be declared before the module import) ───────────────────
@@ -435,13 +435,17 @@ describe('generateVideo — PORTOS_T2V_TWO_STAGE arg threading', () => {
});
describe('FFLF/ltx2 pixel-budget helpers', () => {
- const DEFAULT_BUDGET = 704 * 448 * 25; // ≈7.9M pixel-frames
+ const DEFAULT_BUDGET = 704 * 448 * 25; // 7,884,800 — 48 GB floor
+ const BUDGET_128GB = 768 * 512 * 97; // 38,141,952 — 128 GB anchor
+ const GB = 1024 ** 3;
let resolveFflfLtx2PixelBudget;
+ let computeFflfLtx2PixelBudget;
let computeFflfSafeFrames;
beforeEach(async () => {
- ({ resolveFflfLtx2PixelBudget, computeFflfSafeFrames } = await import('./local.js'));
+ ({ resolveFflfLtx2PixelBudget, computeFflfLtx2PixelBudget, computeFflfSafeFrames } =
+ await import('./local.js'));
delete process.env.FFLF_LTX2_PIXEL_BUDGET;
});
@@ -449,9 +453,46 @@ describe('FFLF/ltx2 pixel-budget helpers', () => {
delete process.env.FFLF_LTX2_PIXEL_BUDGET;
});
+ describe('computeFflfLtx2PixelBudget (RAM-scaled, pure)', () => {
+ it('hits the measured anchors exactly: 48 GB floor and 128 GB validated', () => {
+ expect(computeFflfLtx2PixelBudget(48 * GB)).toBe(DEFAULT_BUDGET);
+ expect(computeFflfLtx2PixelBudget(128 * GB)).toBe(BUDGET_128GB);
+ });
+
+ it('never drops below the 48 GB floor on smaller machines', () => {
+ expect(computeFflfLtx2PixelBudget(32 * GB)).toBe(DEFAULT_BUDGET);
+ expect(computeFflfLtx2PixelBudget(16 * GB)).toBe(DEFAULT_BUDGET);
+ expect(computeFflfLtx2PixelBudget(8 * GB)).toBe(DEFAULT_BUDGET);
+ });
+
+ it('scales monotonically with RAM between and above the anchors', () => {
+ const b64 = computeFflfLtx2PixelBudget(64 * GB);
+ const b96 = computeFflfLtx2PixelBudget(96 * GB);
+ const b256 = computeFflfLtx2PixelBudget(256 * GB);
+ expect(b64).toBeGreaterThan(DEFAULT_BUDGET);
+ expect(b96).toBeGreaterThan(b64);
+ expect(BUDGET_128GB).toBeGreaterThan(b96);
+ expect(b256).toBeGreaterThan(BUDGET_128GB);
+ });
+
+ it('reaches the 97-frame smooth-motion regime at 768×512 by 128 GB', () => {
+ // The whole point of #737: a 128 GB box must be able to render 97 frames
+ // at 768×512 (the validated smooth config) without an env override.
+ expect(computeFflfSafeFrames(768, 512, 97, computeFflfLtx2PixelBudget(128 * GB))).toBe(97);
+ });
+
+ it('falls to the floor on invalid memory inputs', () => {
+ expect(computeFflfLtx2PixelBudget(0)).toBe(DEFAULT_BUDGET);
+ expect(computeFflfLtx2PixelBudget(NaN)).toBe(DEFAULT_BUDGET);
+ expect(computeFflfLtx2PixelBudget(-1)).toBe(DEFAULT_BUDGET);
+ });
+ });
+
describe('resolveFflfLtx2PixelBudget', () => {
- it('defaults to the 48 GB-RAM budget when the env var is unset', () => {
- expect(resolveFflfLtx2PixelBudget()).toBe(DEFAULT_BUDGET);
+ it('defaults to the RAM-scaled budget for this machine when the env var is unset', () => {
+ expect(resolveFflfLtx2PixelBudget()).toBe(computeFflfLtx2PixelBudget(totalmem()));
+ // Floor always holds, on any machine the suite runs on.
+ expect(resolveFflfLtx2PixelBudget()).toBeGreaterThanOrEqual(DEFAULT_BUDGET);
});
it('honors a positive numeric FFLF_LTX2_PIXEL_BUDGET override', () => {
@@ -459,13 +500,14 @@ describe('FFLF/ltx2 pixel-budget helpers', () => {
expect(resolveFflfLtx2PixelBudget()).toBe(12_000_000);
});
- it('ignores a non-positive or non-numeric override and falls back to the default', () => {
+ it('ignores a non-positive or non-numeric override and falls back to the RAM-scaled budget', () => {
+ const scaled = computeFflfLtx2PixelBudget(totalmem());
process.env.FFLF_LTX2_PIXEL_BUDGET = '0';
- expect(resolveFflfLtx2PixelBudget()).toBe(DEFAULT_BUDGET);
+ expect(resolveFflfLtx2PixelBudget()).toBe(scaled);
process.env.FFLF_LTX2_PIXEL_BUDGET = '-5';
- expect(resolveFflfLtx2PixelBudget()).toBe(DEFAULT_BUDGET);
+ expect(resolveFflfLtx2PixelBudget()).toBe(scaled);
process.env.FFLF_LTX2_PIXEL_BUDGET = 'lots';
- expect(resolveFflfLtx2PixelBudget()).toBe(DEFAULT_BUDGET);
+ expect(resolveFflfLtx2PixelBudget()).toBe(scaled);
});
});