Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Workers: a claim can ask for no streaming from RAM; name the memory pause

Adarsh1c2ca17436a8
diff --git a/src/app/api/gpu/computer/claim/route.ts b/src/app/api/gpu/computer/claim/route.ts
index 9725a2b4..6f7b4489 100644
--- a/src/app/api/gpu/computer/claim/route.ts
+++ b/src/app/api/gpu/computer/claim/route.ts
@@ -12,7 +12,7 @@ import { refreshTicketsBeforeClaim } from "@/server/computer/ticket-refresh";
export async function POST(request: Request) {
const principal = principalOf(request);
if (principal instanceof Response) return principal;
- const body = (await request.json().catch(() => null)) as { device?: unknown; running?: unknown; slot?: unknown } | null;
+ const body = (await request.json().catch(() => null)) as { device?: unknown; running?: unknown; slot?: unknown; noStream?: unknown } | null;
// Each of a worker's slots claims for itself, each counted on its own: an idle CPU slot never uses up its GPU's claims.
const limited = rateOf(callerKey(request, principal), body?.slot === "cpu" ? "claim-cpu" : "claim", 60);
if (limited) return limited;
@@ -22,8 +22,9 @@ export async function POST(request: Request) {
// Job tickets made for the catalog before this version of the studio are made again first (ticket-refresh.ts).
if (worker?.kind === "pool") await refreshTicketsBeforeClaim();
// A shared GPU takes the best task it can run from its pool (pool.ts), for its GPU or for its CPU slot (`slot`:
- // "cpu"); a person's computer, their own oldest.
- const task = worker?.kind === "pool" ? workerClaim(worker, running, Date.now(), body?.slot === "cpu" ? "cpu" : "gpu") : claim(principal, String(body?.device ?? ""), running);
+ // "cpu"); a person's computer, their own oldest. A worker whose PC's memory is in use (`noStream`, its RAM guard)
+ // takes only a job whose weights fit whole on its card.
+ const task = worker?.kind === "pool" ? workerClaim(worker, running, Date.now(), body?.slot === "cpu" ? "cpu" : "gpu", { noStream: body?.noStream === true }) : claim(principal, String(body?.device ?? ""), running);
if (!task) return new Response(null, { status: 204, headers: { "Cache-Control": "no-store" } });
return Response.json({ task }, { headers: { "Cache-Control": "no-store" } });
} catch (e) {
diff --git a/src/lib/computer-runner.ts b/src/lib/computer-runner.ts
index 66e006e0..48b13935 100644
--- a/src/lib/computer-runner.ts
+++ b/src/lib/computer-runner.ts
@@ -791,9 +791,10 @@ export class ComputerRunner {
/**
* Ask for a job and run it (one at a time). True when there was one. Only while ComfyUI here answers to this site: a
- * computer whose ComfyUI is closed, or runs without Fuobe's setting, keeps its place but takes nothing.
+ * computer whose ComfyUI is closed, or runs without Fuobe's setting, keeps its place but takes nothing. `noStream`: a
+ * worker whose PC's memory is in use (its RAM guard) asks for no job that streams its weights from RAM.
*/
- async claimAndRun(o: { onlyHeld?: boolean } = {}): Promise<boolean> {
+ async claimAndRun(o: { onlyHeld?: boolean; noStream?: boolean } = {}): Promise<boolean> {
if (!this.alive || this.busy || this.o.access) return false;
this.busy = true;
try {
@@ -801,7 +802,7 @@ export class ComputerRunner {
if (!o.onlyHeld && (await this.look()).comfy !== "ok") return false;
const running = this.view.jobs.filter((j) => j.phase !== "done" && j.phase !== "failed").map((j) => j.taskId);
// A worker's CPU slot claims for itself (`slot`): its work never waits for its GPU's job, nor holds it up.
- const res = await this.deps.api("/api/gpu/computer/claim", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ device: this.o.device, running, ...(this.o.slot && { slot: this.o.slot }) }) });
+ const res = await this.deps.api("/api/gpu/computer/claim", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ device: this.o.device, running, ...(this.o.slot && { slot: this.o.slot }), ...(o.noStream && !o.onlyHeld && { noStream: true }) }) });
if (res.status !== 200) {
if (res.status === 401) this.stop("signed-out");
if (res.status === 204) await this.deps.worker?.claimed(null, { onlyHeld: !!o.onlyHeld });
diff --git a/src/lib/relay.ts b/src/lib/relay.ts
index d218bbb3..c0678da8 100644
--- a/src/lib/relay.ts
+++ b/src/lib/relay.ts
@@ -314,7 +314,7 @@ export interface WorkerBeat {
why: string | null;
/**
* Its own pause switch: by hand ("here", "here-until:<ms>", "here-now"), by itself ("gpu-busy:<process>",
- * "comfy-crashes", "port-busy", "models-not-ready": a model folder isn't there yet, "self-check": its face detector's
+ * "ram-busy:<process>": its RAM guard, other programs use the PC's memory; its job in hand goes on, "comfy-crashes", "port-busy", "models-not-ready": a model folder isn't there yet, "self-check": its face detector's
* check failed twice). Not paused, `why` may say that it waits for local ComfyUI work to make room
* ("waiting-for-yield", "waiting-for-yield:no-answer").
*/
diff --git a/src/server/computer/pool.ts b/src/server/computer/pool.ts
index 76732470..b47f3a10 100644
--- a/src/server/computer/pool.ts
+++ b/src/server/computer/pool.ts
@@ -223,6 +223,8 @@ export function workerState(w: WorkerRow, now = Date.now()): { state: WorkerStat
const why = w.local_why ?? "";
if (w.paused_local) {
if (why.startsWith("gpu-busy")) return { state: "gpu-busy", words: `Your GPU is busy${why.includes(":") ? ` (${why.split(":").slice(1).join(":")})` : ""}` };
+ // Its RAM guard: other programs use the PC's memory. The same kind of pause as a busy GPU: it resumes by itself.
+ if (why.startsWith("ram-busy")) return { state: "gpu-busy", words: `This PC's memory is in use${why.includes(":") ? ` (${why.split(":").slice(1).join(":")})` : ""}` };
if (why === "comfy-crashes") return { state: "comfy-down", words: "ComfyUI keeps stopping" };
if (why === "port-busy") return { state: "comfy-down", words: "Its ComfyUI port is taken by another program" };
if (why === "self-check") return { state: "comfy-down", words: "Its face check didn't pass after a restart: it checks again within 5 minutes" };
@@ -828,9 +830,10 @@ const slotWhere = (w: Pick<WorkerRow, "cpu_slot_json">, slot: WorkerSlot) => (sl
* the owner's own jobs only: its warm lane goes first until another lane's best task has waited longer than
* switch_after_ms (and is older). A person's run never waits for a warm lane.
*/
-export function workerClaim(w: WorkerRow, running: string[], now = Date.now(), slot: WorkerSlot = "gpu"): ClaimedTask | null {
+export function workerClaim(w: WorkerRow, running: string[], now = Date.now(), slot: WorkerSlot = "gpu", o: { noStream?: boolean } = {}): ClaimedTask | null {
const d = db();
const device = slotDevice(w, slot);
+ const noStream = !!o.noStream && slot === "gpu";
return d.transaction((): ClaimedTask | null => {
sweepPool(now);
const fresh = d.prepare("SELECT * FROM relay_workers WHERE id = ?").get(w.id) as WorkerRow;
@@ -847,14 +850,26 @@ export function workerClaim(w: WorkerRow, running: string[], now = Date.now(), s
// First pick: its GPU leaves a job for a free worker with a higher pick order that would take it next, for a short
// grace (firstPicks), and takes its own next best.
const left = slot === "gpu" ? firstPicks(fresh, now) : new Map<string, string>();
- const { task: t, looked } = pick(fresh, now, slot, new Set(left.keys()));
+ // Its PC's memory is in use (the worker's RAM guard): this claim streams nothing from its RAM, so it takes only a job
+ // whose weights fit whole on its card (and a model variant that does), and leaves the rest for another worker.
+ const asking = noStream ? withoutStreaming(fresh) : fresh;
+ const { task: t, looked } = pick(asking, now, slot, new Set(left.keys()));
const many = `${looked}${looked >= LOOK_AT ? "+" : ""}`;
const none = left.size ? `Asked for work: ${many} waiting, ${left.size === 1 ? "1 left" : `${left.size} left`} for a worker that picks first` : `Asked for work: ${many} waiting, none it can run now`;
- if (slot === "gpu") noteAsked(fresh, t ? "found" : "nothing", t ? `Asked for work: took 1 of ${many} waiting` : looked ? none : "Asked for work: nothing waiting", now, { waiting: looked, ...(left.size && { leftFor: [...new Set(left.values())] }) });
- return t ? claimed(t, fresh, device, now) : null;
+ const memory = noStream ? " (its PC's memory is in use: only jobs that fit whole on its card)" : "";
+ if (slot === "gpu") noteAsked(fresh, t ? "found" : "nothing", `${t ? `Asked for work: took 1 of ${many} waiting` : looked ? none : "Asked for work: nothing waiting"}${memory}`, now, { waiting: looked, ...(left.size && { leftFor: [...new Set(left.values())] }), ...(noStream && { noStream: true }) });
+ return t ? claimed(t, asking, device, now) : null;
}).immediate();
}
+/**
+ * The worker as one claim sees it when its PC's memory is in use (its RAM guard, the claim's `noStream`): no RAM to
+ * stream weights from, so the memory rule (memoryFit) passes only what fits whole on its card. Nothing is saved.
+ */
+export function withoutStreaming(w: WorkerRow): WorkerRow {
+ return { ...w, facts_json: JSON.stringify({ ...factsOf(w), ramBytes: 0 }) };
+}
+
/** Why a worker may not take work now, in a few words (its claim's answer). */
function blockedWhy(w: WorkerRow, now: number): string {
if (w.revoked_at || (w.expires_at && w.expires_at <= now)) return "its token no longer works";
diff --git a/tests/stream-needs.test.ts b/tests/stream-needs.test.ts
index e5a3c37d..00123ba0 100644
--- a/tests/stream-needs.test.ts
+++ b/tests/stream-needs.test.ts
@@ -347,4 +347,19 @@ describe("which workers qualify on memory (the claim)", () => {
job(KREA);
expect(claim(worker("ampere-low-ram", KREA, { vramBytes: 10 * GB, ramBytes: 16 * GB, gpuGen: "8.6" }))).toBeNull();
});
+
+ it("a claim that asks for no streaming (the worker's RAM guard): only a job whose weights fit whole on its card, said in its claim's words", () => {
+ const id = job(FLEX);
+ const small = worker("flex-shared-pc", FLEX, { vramBytes: 10 * GB, ramBytes: 31.9 * GB });
+ expect(pool.workerClaim(row(small), [], clock, "gpu", { noStream: true })).toBeNull();
+ expect(row(small).asked_words).toMatch(/its PC's memory is in use: only jobs that fit whole on its card/);
+ expect(tasks.taskById(id)!.state).toBe("waiting");
+ // Its facts are as they were: the next claim without it streams as before.
+ expect(JSON.parse(row(small).facts_json ?? "{}").ramBytes).toBe(31.9 * GB);
+ expect(pool.workerClaim(row(small), [], clock)?.id).toBe(id);
+ expect(JSON.parse(tasks.taskById(id)!.extra_json ?? "{}").streamed).toBe(true);
+ // A card that holds it whole takes it whatever the claim says.
+ const whole = job(FLEX);
+ expect(pool.workerClaim(row(worker("flex-whole", FLEX, { vramBytes: 32 * GB, ramBytes: 16 * GB })), [], clock, "gpu", { noStream: true })?.id).toBe(whole);
+ });
});