sandeep85's picture
MLX port of FastFrames-S (bf16): weights, inference code, licences, samples
5786760 verified
Raw History Blame Contribute Delete
4.43 kB
"""Generate a batch of FastFrames-S clips: several seeds per prompt, umT5 loaded once.
python batch.py prompts.txt out_dir [--seeds 0,1,2] [--reference] [--opts k=v,...]
prompts file: one prompt per line (blank lines and lines starting with # are skipped), or JSON
lines {"prompt": "...", "seeds": [1, 2], "name": "optional-slug"}; --seeds is the default list.
Outputs out_dir/<slug>_s<seed>.mp4 + .json (timings), and out_dir/batch.json with all records.
Same prompt + seed gives the same clip as run.py. MP4 encoding runs on a CPU thread while the
GPU starts the next clip. Existing outputs are skipped (pass --overwrite to redo them).
Weights are found as in run.py (--weights, $FASTFRAMES_MLX_WEIGHTS, this directory, or the Hub).
"""
import argparse
import json
import re
import threading
import time
from pathlib import Path
import mlx.core as mx
import run
def slug(text, n=48):
s = re.sub(r"[^a-z0-9]+", "-", text.lower()).strip("-")
return (s[:n].rstrip("-") or "prompt")
def read_prompts(path, default_seeds):
jobs = []
for line in Path(path).read_text().splitlines():
line = line.strip()
if not line or line.startswith("#"):
continue
if line.startswith("{"):
d = json.loads(line)
jobs.append((d["prompt"], d.get("seeds", default_seeds), d.get("name") or slug(d["prompt"])))
else:
jobs.append((line, default_seeds, slug(line)))
names = [j[2] for j in jobs]
for i, (p, s, n) in enumerate(jobs): # two prompts with the same slug: suffix
if names.count(n) > 1:
jobs[i] = (p, s, f"{n}-{run.emb_key(p)[:6]}")
return jobs
def main():
ap = argparse.ArgumentParser()
ap.add_argument("prompts")
ap.add_argument("out_dir")
ap.add_argument("--seeds", default="0,1,2")
ap.add_argument("--weights", default=None, help="directory with the .safetensors files")
ap.add_argument("--frames", type=int, default=21)
ap.add_argument("--decoder-dtype", default="fp32", choices=["fp32", "bf16"])
ap.add_argument("--reference", action="store_true")
ap.add_argument("--opts", default="")
ap.add_argument("--overwrite", action="store_true")
a = ap.parse_args()
out = Path(a.out_dir)
out.mkdir(parents=True, exist_ok=True)
wd = run.resolve_weights(a.weights)
jobs = read_prompts(a.prompts, [int(s) for s in a.seeds.split(",")])
todo = [(p, seed, out / f"{name}_s{seed}.mp4") for p, seeds, name in jobs for seed in seeds]
if not a.overwrite:
todo = [j for j in todo if not j[2].exists()]
print(f"{len(todo)} clips to make ({len(jobs)} prompts)", flush=True)
if not todo:
return
t0 = time.perf_counter()
embs, info = run.encode_prompts([p for p, _, _ in todo], wd, a.reference)
text_s = round(time.perf_counter() - t0, 2)
print(f"text: {text_s}s {info}", flush=True)
t0 = time.perf_counter()
ff = run.load_models(wd, a.reference, a.frames, a.decoder_dtype, run.parse_opts(a.opts))
load_s = round(time.perf_counter() - t0, 2)
records, writer = [], None
t_all = time.perf_counter()
for i, (prompt, seed, path) in enumerate(todo):
frames, rec = run.generate(ff, embs[prompt], seed, a.frames)
rec = {"prompt": prompt, "seed": seed, "file": path.name,
"path": "reference" if a.reference else "fast", **rec}
if writer is not None:
writer.join()
def write(frames=frames, path=path, rec=rec):
t = time.perf_counter()
run.write_mp4(frames, path)
rec["mp4_s"] = round(time.perf_counter() - t, 2)
path.with_suffix(".json").write_text(json.dumps(rec, indent=1))
writer = threading.Thread(target=write)
writer.start()
records.append(rec)
print(f"[{i + 1}/{len(todo)}] {path.name}: rollout {rec['rollout_s']}s decode {rec['decode_s']}s", flush=True)
del frames
mx.clear_cache()
writer.join()
summary = {"clips": len(records), "text_s": text_s, "load_s": load_s, **info,
"gen_wall_s": round(time.perf_counter() - t_all, 2), "records": records}
summary["per_clip_s"] = round(summary["gen_wall_s"] / len(records), 2)
(out / "batch.json").write_text(json.dumps(summary, indent=1))
print(json.dumps({k: v for k, v in summary.items() if k != "records"}))
if __name__ == "__main__":
main()