Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

raea cloud render farm: a long job goes to its GPUs as a queue of parts, so a slow card draws fewer frames

shinyobjectz3067b55fe1ec
diff --git a/sdk/cloud/deploy/modal/farm_plan.py b/sdk/cloud/deploy/modal/farm_plan.py
index 57ad1c9a..f2fa99d7 100644
--- a/sdk/cloud/deploy/modal/farm_plan.py
+++ b/sdk/cloud/deploy/modal/farm_plan.py
@@ -48,6 +48,23 @@ def gpus_for(frames, frame_s, start_s, most):
return max(1, min(most, frames, k))
+# a GPU takes up to this many parts of a long job, one after another from the job's queue, so a slow card (an L4 among
+# L40Ss) ends up drawing fewer frames instead of holding up the job
+PARTS = 4
+
+
+def parts_per_gpu(frames, k, frame_s, start_s):
+ """How many parts each of `k` GPUs takes from the queue: the most (up to PARTS) that keep a part's own start
+ (start_s, more than a warm GPU needs) at most SHARE of the part. Above 1 only when the GPU limit capped `k`."""
+ if k <= 0 or frame_s <= 0:
+ return 1
+ least = start_s * (1 - SHARE) / SHARE / frame_s # the frames a part needs for its start to be worth it
+ m = 1
+ while m < PARTS and frames / (k * (m + 1)) >= least:
+ m += 1
+ return m
+
+
def runs(first, last, k, warm=0):
"""Frames first..last (inclusive) as `k` contiguous runs, each led by `warm` frames to drop (a renderer's history,
Solari's light, settling): [(frames to render, the first one kept)]."""
@@ -120,9 +137,12 @@ def keep(frames_dir, out, start=0):
shutil.move(os.path.join(frames_dir, name), os.path.join(out, f"frame-{i:05d}.png"))
-def spread(fn, kind, calls, log, what, bill=None):
- """`calls` on `fn` at once; each finished run said in the log as it lands. The first failure's code, else 0."""
+def spread(fn, kind, calls, log, what, bill=None, most=None):
+ """`calls` on `fn`, at most `most` GPUs at once when there are more calls than that (each GPU takes the next call
+ as it finishes one); each finished run said in the log as it lands. The first failure's code, else 0."""
code = 0
+ if most and len(calls) > most:
+ fn = fn.with_options(max_containers=most)
for r in fn.starmap(calls, order_outputs=False, return_exceptions=True):
if isinstance(r, Exception):
log(f"raea farm: a run failed: {r!r}", err=True)
@@ -162,14 +182,16 @@ def raea_render(args, opts, work, log, render, cpu_env, run, reload=lambda: None
start_s = max(0.0, r0["s"] - frame_s * n0)
left = length - n0
k = gpus_for(left, frame_s, start_s, opts["gpus"])
- cost = price(kind, p["s"] + r0["s"] + k * start_s + left * frame_s)
- log(f"raea farm: {frame_s * 1000:.0f} ms a frame, {start_s:.1f} s to start; {left} frames left on {k} {kind}s, about ${cost:.3f} in all")
+ m = parts_per_gpu(left, k, frame_s, start_s)
+ cost = price(kind, p["s"] + r0["s"] + k * m * start_s + left * frame_s)
+ parts = f" in {k * m} parts" if m > 1 else ""
+ log(f"raea farm: {frame_s * 1000:.0f} ms a frame, {start_s:.1f} s to start; {left} frames left on {k} {kind}s{parts}, about ${cost:.3f} in all")
if opts.get("max_usd") is not None and cost > opts["max_usd"]:
log(f"raea farm: about ${cost:.3f} is over max_usd ${opts['max_usd']:.3f}, so the rest don't start", err=True)
return 3
# a video's runs can't drop warm-up frames without encoding again, so they have none
warm = 0 if video else opts.get("warm", 0)
- code = spread(render, kind, [(id, rest, f, at, False, video) for f, at in runs(n0, length - 1, k, warm)], log, "frames", bill) if left else 0
+ code = spread(render, kind, [(id, rest, f, at, False, video) for f, at in runs(n0, length - 1, k * m, warm)], log, "frames", bill, k) if left else 0
if code:
return code
reload() # the other containers' frames
@@ -230,13 +252,15 @@ def blender(args, opts, work, log, render, run, reload=lambda: None, bill=None):
start_s = max(0.0, r0["s"] - frame_s)
left = last - first
k = gpus_for(left, frame_s, start_s, opts["gpus"])
- cost = price(kind, r0["s"] + k * start_s + left * frame_s)
- log(f"raea farm: frame {first} on a {kind} in {r0['s']:.1f} s ({frame_s:.1f} s rendering); {left} left on {k} {kind}s, about ${cost:.3f} in all")
+ m = parts_per_gpu(left, k, frame_s, start_s)
+ cost = price(kind, r0["s"] + k * m * start_s + left * frame_s)
+ parts = f" in {k * m} parts" if m > 1 else ""
+ log(f"raea farm: frame {first} on a {kind} in {r0['s']:.1f} s ({frame_s:.1f} s rendering); {left} left on {k} {kind}s{parts}, about ${cost:.3f} in all")
if opts.get("max_usd") is not None and cost > opts["max_usd"]:
log(f"raea farm: about ${cost:.3f} is over max_usd ${opts['max_usd']:.3f}, so the rest don't start", err=True)
return 3
- calls = [(id, blend, f[0], f[-1], o) for f, _ in runs(first + 1, last, k)] if left else []
- code = spread(render, kind, calls, log, "frames", bill) if calls else 0
+ calls = [(id, blend, f[0], f[-1], o) for f, _ in runs(first + 1, last, k * m)] if left else []
+ code = spread(render, kind, calls, log, "frames", bill, k) if calls else 0
if code:
return code
reload() # the other containers' frames
diff --git a/sdk/cloud/test/farm_plan.py b/sdk/cloud/test/farm_plan.py
index e69d905f..ae125201 100644
--- a/sdk/cloud/test/farm_plan.py
+++ b/sdk/cloud/test/farm_plan.py
@@ -22,7 +22,7 @@ class FakeGpu:
"""raea_run's stand-in: a 60-frame piece, frame 0 in 12 s (the start), 0.5 s a frame after."""
def __init__(self, root, length=60):
- self.root, self.length, self.calls = root, length, []
+ self.root, self.length, self.calls, self.options = root, length, [], {}
def png(self, path):
# a 16x16 grey PNG, made once by ffmpeg
@@ -46,6 +46,10 @@ class FakeGpu:
self.png(f"{dest}/frame-{f:05d}.png")
return {"code": 0, "out": "", "s": 12 + 0.5 * len(frames), "frames": len(frames), "ms": 500.0}
+ def with_options(self, **kw):
+ self.options = kw
+ return self
+
def starmap(self, calls, order_outputs=False, return_exceptions=True):
return [self.remote(*c) for c in calls]
@@ -99,6 +103,19 @@ assert code == 0 and len(out) == 1000 and "frame-00999.png" in out, (code, len(o
assert len(long.calls) == 2 + 2 + 10, len(long.calls)
ok("a 1000-frame render to a folder: 10 GPUs, the frames named as raea names them")
+# below the GPU limit each GPU already gets just enough frames for its start: one part each
+assert fp.parts_per_gpu(19, 10, 8, 5) == 1 and fp.parts_per_gpu(992, 9, 0.22, 6) == 1
+# capped by the limit, a long job goes in up to 4 parts a GPU while each part still pays for its start
+assert fp.parts_per_gpu(250, 10, 8, 5) == 4 and fp.parts_per_gpu(60, 10, 8, 5) == 2
+lines.clear()
+os.makedirs(os.path.join(root, "jobs", "j3", "work"))
+q = FakeGpu(root, length=1000)
+code = fp.raea_render(["render", "a.tsx", "-o", "stills"], {"id": "j3", "gpu": "L4", "gpus": 2}, os.path.join(root, "jobs", "j3", "work"), log, q, {}, run)
+out = os.listdir(os.path.join(root, "jobs", "j3", "work", "stills"))
+assert code == 0 and len(out) == 1000 and q.options == {"max_containers": 2} and len(q.calls) == 2 + 8, (code, len(out), q.options, len(q.calls))
+assert "on 2 L4s in 8 parts" in " ".join(lines), lines
+ok("a long job on too few GPUs: 4 parts a GPU from a queue of 2 at a time, every frame back")
+
b = fp.Bill()
t = b.t0
b.add({"task": "a", "kind": "L40S", "boot": t - 100, "end": t + 10}) # left up by an earlier job: from this job's start