Skip to content

Free, open-source AI video generator for Mac. Run LTX-2.5 and MiniMax H3 (Hailuo) locally on Apple Silicon: text-to-video and image-to-video with sound, lip-sync, character LoRA training, songs from lyrics (YuE2) and image editing (Qwen-Image-Edit). One-click install via Pinokio. Offline: no cloud, no API key, no NVIDIA GPU.

perf(image): single-invocation multi-seed batching + Lightning LoRA + 8-step iteration default

mrbizarro8e3827760b82
diff --git a/agent/image_engine.py b/agent/image_engine.py
index 7858b55..46a5148 100644
--- a/agent/image_engine.py
+++ b/agent/image_engine.py
@@ -48,7 +48,7 @@ import json
import time
import urllib.error
import urllib.request
-from dataclasses import asdict, dataclass
+from dataclasses import asdict, dataclass, field
from pathlib import Path
@@ -93,6 +93,15 @@ class ImageEngineConfig:
mflux_quantize: int = 4 # 4 | 8 | 16 — 4-bit fits comfortably on 64 GB
mflux_guidance: float | None = None # None = use family default (1.0 / 0.0 / 4.5 / 5.0 per family)
mflux_python_path: str = "" # optional override for the mflux CLI location
+ # Optional Lightning / acceleration LoRAs. With qwen_edit + a 4-step
+ # Lightning LoRA, generation drops from ~5 min to ~10-15 sec per
+ # image. Passed straight through to mflux's `--lora-paths` and
+ # `--lora-scales` flags (lengths must match if both set; mflux
+ # rejects the mismatch). Each path is a HuggingFace repo id, a
+ # collection-format string (`repo:filename.safetensors`), or a
+ # local file path.
+ mflux_lora_paths: list[str] = field(default_factory=list)
+ mflux_lora_scales: list[float] = field(default_factory=list)
def to_public_dict(self) -> dict:
d = asdict(self)
@@ -225,11 +234,13 @@ MFLUX_FAMILY_DEFAULTS = {
"z_image_turbo": {"steps": 9, "guidance": 0.0, "base_model": ""},
"fibo": {"steps": 30, "guidance": 5.0, "base_model": ""},
"qwen": {"steps": 30, "guidance": 5.0, "base_model": ""},
- # qwen_edit defaults match the Qwen team's recommended 40 steps + true_cfg
- # at guidance 4.0 from the model card. 30/4.0 trades ~25% wall-time for
- # only marginally lower quality on 1-3 ref jobs and is a better default
- # for the agent's iterate-fast loop. Bump steps to 40 for final keyframes.
- "qwen_edit": {"steps": 30, "guidance": 4.0, "base_model": ""},
+ # qwen_edit default: 8 steps. The Qwen card recommends 30-40 for
+ # final-quality, but the agent + Image Studio are iteration tools —
+ # ~1 min/image at Q4-8steps, then bump to 30 steps once the user
+ # picks a composition they like. Pair with a Lightning 4-step LoRA
+ # (mflux_lora_paths) to drop further to ~10-15 s. Guidance 4.0 from
+ # the model card.
+ "qwen_edit": {"steps": 8, "guidance": 4.0, "base_model": ""},
"kontext": {"steps": 30, "guidance": 4.5, "base_model": ""},
}
@@ -400,31 +411,42 @@ def _generate_mflux(prompt: str, n: int, width: int, height: int,
output_dir: Path, base_seed: int | None,
config: ImageEngineConfig,
refs: list[str] | None = None) -> list[dict]:
- """Subprocess the right `mflux-generate-*` binary once per candidate.
+ """Subprocess the right `mflux-generate-*` binary ONCE for all `n` seeds.
+
+ Pre-2026-05-08 this spawned one subprocess per candidate, paying the
+ ~30-60 s model-load cost N times. mflux's per-family CLIs already
+ loop internally over `args.seed`, loading the model exactly once
+ and looping over `for seed in args.seed: image = qwen.generate_image(...)`.
+ By passing every seed in a single invocation and using mflux's
+ `{seed}` template in `--output`, we drop wall-time on n=4 batches
+ from "4 × (load + gen)" to "1 × load + 4 × gen", which is a
+ 3-4× speedup on Lightning configs and a 1.3-1.6× speedup on
+ raw 8-step Q4. Bigger when generation itself is fast (load dominates).
Family is inferred from `config.mflux_model` (or taken from
- `config.mflux_family` when set). The legacy `mflux-generate` is
- used for FLUX.1 (krea-dev / dev / schnell); FLUX.2 / Z-Image / FIBO /
- Qwen / Kontext each get their dedicated CLI from mflux 0.17.x.
-
- Loads the model fresh on every call (mflux's CLI doesn't keep it
- warm), so first call eats ~10-30 s of model-load time per
- candidate. For batch generation this is the simplest path; if it
- becomes a real bottleneck, swap in a long-lived `mflux_warm_helper.py`
- subprocess that mirrors `mlx_warm_helper.py`.
+ `config.mflux_family` when set). FLUX.1 / FLUX.2 / Z-Image /
+ Z-Image-Turbo / FIBO / Qwen / Qwen-Edit / Kontext each have their
+ dedicated CLI from mflux 0.17.x.
+
+ `config.mflux_lora_paths` + `config.mflux_lora_scales` plumb through
+ to `--lora-paths` and `--lora-scales`. The intended use is a
+ Lightning LoRA on qwen_edit:
+ mflux_lora_paths = ["lightx2v/Qwen-Image-Edit-2511-Lightning"]
+ mflux_lora_scales = [1.0]
+ mflux_steps = 4
+ which lands at ~10-15 s/image vs ~5 min for raw 30-step gen.
Recommended defaults (May 2026):
- - **FLUX.2 [klein] 4B 4-bit** — Apache 2.0, 4 steps, ~4.3 GB. The
- new default; Comfortable+ tiers.
- - **Z-Image-Turbo 4-bit** — Apache 2.0, 9 steps, ~5.9 GB. Compact
- tier.
- - **FLUX.1 Krea Dev 4-bit** — non-commercial gated, 25 steps,
- ~9.6 GB. Kept for users on the legacy default.
-
- First run downloads ~4-10 GB to ~/.cache/huggingface depending on
- the family.
+ - **Qwen-Image-Edit-2509** — Apache 2.0, multi-ref. Iteration:
+ Q4 + 8 steps, ~1 min/image. Final: Q8 + 30 steps, ~5 min.
+ - **FLUX.2 [klein] 4B 4-bit** — Apache 2.0, 4 steps, ~4.3 GB.
+ - **Z-Image-Turbo 4-bit** — Apache 2.0, 9 steps, ~5.9 GB.
+
+ First run downloads weights (4-34 GB depending on family) to
+ ~/.cache/huggingface. Subsequent calls use the cached copy.
"""
import os
+ import random
import subprocess
bin_path = _resolve_mflux_bin(config)
@@ -433,105 +455,137 @@ def _generate_mflux(prompt: str, n: int, width: int, height: int,
fam_defaults = MFLUX_FAMILY_DEFAULTS.get(fam, MFLUX_FAMILY_DEFAULTS["flux1"])
if not bin_path:
- # Surface a family-specific install hint — mflux 0.17.x ships
- # all families via one `pip install mflux`; older mflux pins
- # may be missing the new binaries.
raise RuntimeError(
f"{bin_name} not found (family: {fam}). Install or upgrade "
f"mflux into the panel's venv: "
f"`ltx-2-mlx/env/bin/pip install -U mflux>=0.17` "
- f"(see docs/AGENTIC_FLOWS.md § Image generation backends + "
- f"docs/IMAGE_GEN_RESEARCH_2026-05.md). First-run model "
- f"download is ~4-10 GB to ~/.cache/huggingface."
+ f"(see docs/AGENTIC_FLOWS.md § Image generation backends). "
+ f"First-run model download is 4-34 GB to ~/.cache/huggingface "
+ f"depending on family."
)
# Effective steps + guidance: prefer user-set values if non-zero,
- # otherwise fall back to the family default. This way a user who
- # just picks "FLUX.2 [klein]" from the dropdown without adjusting
- # other knobs gets the right step count for free.
+ # otherwise fall back to the family default.
eff_steps = config.mflux_steps if config.mflux_steps > 0 else fam_defaults["steps"]
eff_guidance = (config.mflux_guidance
if config.mflux_guidance is not None
else fam_defaults["guidance"])
- results: list[dict] = []
- for i in range(n):
- seed = (base_seed + i) if base_seed is not None else None
- out_path = output_dir / f"cand_{i:02d}_mflux.png"
- cmd = [
- bin_path,
- "--model", config.mflux_model,
- "--prompt", prompt,
- "--output", str(out_path),
- "--steps", str(eff_steps),
- "--width", str(width),
- "--height", str(height),
- "-q", str(config.mflux_quantize),
- "--guidance", str(eff_guidance),
- ]
- if seed is not None:
- cmd.extend(["--seed", str(seed)])
- # Multi-reference input — only the qwen_edit family consumes
- # --image-paths today (mflux v0.11.1+). For other families we
- # silently drop refs but tag the result so the caller can detect
- # the no-op. This lets the agent pass refs unconditionally and
- # the engine choice decides whether they're honored.
- refs_used: list[str] = []
- if refs and fam == "qwen_edit":
- refs_used = [str(Path(r).resolve()) for r in refs]
- cmd.extend(["--image-paths", *refs_used])
- # When `--model` is a HuggingFace id or local path (contains a
- # slash or starts with `~` or `/`), mflux needs `--base-model`
- # to know which architecture to instantiate. Fall through to
- # the per-family default if the user hasn't set one.
- looks_like_path = ("/" in config.mflux_model
- or config.mflux_model.startswith("~"))
- if looks_like_path:
- base = (config.mflux_base_model
- or fam_defaults.get("base_model") or "")
- if base:
- cmd.extend(["--base-model", base])
-
- # Inherit env so HF_HOME / HF_TOKEN are honored for the
- # one-time weight download. Capture stderr so the user gets a
- # readable error if anything explodes. Family-aware timeout —
- # 4-bit klein-4B and Z-Image-Turbo finish in 60-90 s; flux1-dev
- # / krea-dev / fibo / qwen take much longer at high resolution.
- env = os.environ.copy()
- timeout_s = 240 if fam in ("flux2", "z_image_turbo") else 600
- try:
- result = subprocess.run(
- cmd, capture_output=True, env=env, timeout=timeout_s,
- )
- except subprocess.TimeoutExpired as e:
- raise RuntimeError(
- f"{bin_name} timed out after {timeout_s}s on candidate {i}. "
- f"First run downloads weights — give it longer for the "
- f"first candidate, or pre-download with: "
- f"`{bin_path} --model {config.mflux_model} "
- f"--prompt 'warmup' --steps 1 --output /tmp/_mflux_warmup.png`"
- ) from e
- if result.returncode != 0:
- err = result.stderr.decode("utf-8", errors="replace")
- raise RuntimeError(
- f"{bin_name} failed (exit {result.returncode}) "
- f"on candidate {i}: {err[:600]}"
- )
- if not out_path.is_file():
- raise RuntimeError(
- f"{bin_name} exited 0 but produced no file at {out_path}"
- )
+ # Build the seed list. mflux's CLI loops `for seed in args.seed`
+ # AFTER loading the model exactly once, so passing all seeds at
+ # once amortizes the cold-start cost across N images.
+ if base_seed is not None:
+ seeds = [base_seed + i for i in range(n)]
+ else:
+ # Equivalent to mflux's --auto-seeds N but explicit — gives us
+ # the seed values up-front so we can map them to output paths.
+ seeds = [random.randint(0, 2**31 - 1) for _ in range(n)]
+
+ # Output path uses mflux's `{seed}` template — `--output cand_{seed}_mflux.png`
+ # writes one file per seed value (see qwen_image_edit_generate.py:61
+ # `Path(args.output.format(seed=seed))`). Including the seed in the
+ # filename also guarantees no collisions if the user resubmits the
+ # same shot label without `append: true`.
+ output_template = str(output_dir / "cand_{seed}_mflux.png")
+
+ cmd = [
+ bin_path,
+ "--model", config.mflux_model,
+ "--prompt", prompt,
+ "--output", output_template,
+ "--steps", str(eff_steps),
+ "--width", str(width),
+ "--height", str(height),
+ "-q", str(config.mflux_quantize),
+ "--guidance", str(eff_guidance),
+ "--seed", *[str(s) for s in seeds],
+ ]
+ # Multi-reference input — only the qwen_edit family consumes
+ # --image-paths today (mflux v0.11.1+). For other families we
+ # silently drop refs and tag refs_ignored=True on each result.
+ refs_used: list[str] = []
+ if refs and fam == "qwen_edit":
+ refs_used = [str(Path(r).resolve()) for r in refs]
+ cmd.extend(["--image-paths", *refs_used])
+ # Optional Lightning / acceleration LoRAs
+ if config.mflux_lora_paths:
+ cmd.append("--lora-paths")
+ cmd.extend(config.mflux_lora_paths)
+ if config.mflux_lora_scales:
+ cmd.append("--lora-scales")
+ cmd.extend(str(s) for s in config.mflux_lora_scales)
+ # When `--model` is a HuggingFace id or local path (contains a
+ # slash or starts with `~`), mflux needs `--base-model` to know
+ # which architecture to instantiate. Fall through to the per-family
+ # default if the user hasn't set one.
+ looks_like_path = ("/" in config.mflux_model
+ or config.mflux_model.startswith("~"))
+ if looks_like_path:
+ base = (config.mflux_base_model
+ or fam_defaults.get("base_model") or "")
+ if base:
+ cmd.extend(["--base-model", base])
+
+ # Inherit env so HF_HOME / HF_TOKEN are honored for the one-time
+ # weight download. Family-aware total timeout — first run can pull
+ # 22-34 GB for qwen_edit which makes the cold-start much longer
+ # than steady-state. Per-family steady-state per-image is roughly:
+ # flux2 / z_image_turbo: ~15 s @ 4-9 steps Q4
+ # qwen_edit: ~30 s @ 8 steps Q4 / ~5 min @ 30 steps Q8
+ # Plus the one-time load, plus first-run download.
+ env = os.environ.copy()
+ per_image_budget = 60 if fam in ("flux2", "z_image_turbo") else 360
+ cold_start_budget = 240 if fam in ("flux2", "z_image_turbo") else 1800
+ timeout_s = cold_start_budget + per_image_budget * n
+ try:
+ result = subprocess.run(
+ cmd, capture_output=True, env=env, timeout=timeout_s,
+ )
+ except subprocess.TimeoutExpired as e:
+ raise RuntimeError(
+ f"{bin_name} timed out after {timeout_s}s on a batch of {n} seeds. "
+ f"First run downloads weights — for qwen_edit the Q8 weights "
+ f"are ~34 GB; give the first batch longer or pre-pull with: "
+ f"`{bin_path} --model {config.mflux_model} "
+ f"--prompt warmup --steps 1 --output /tmp/_mflux_warmup.png "
+ f"--image-paths /tmp/anything.png` (qwen_edit needs --image-paths)"
+ ) from e
+ if result.returncode != 0:
+ err = result.stderr.decode("utf-8", errors="replace")
+ raise RuntimeError(
+ f"{bin_name} failed (exit {result.returncode}) on batch of {n} seeds: "
+ f"{err[:800]}"
+ )
+ # Build results from whatever files actually landed. If the
+ # subprocess errored partway through (e.g. one seed's generation
+ # raised), some seeds may not have produced an output — return the
+ # ones that did rather than failing the whole batch. Order by seed
+ # so candidate numbering is deterministic.
+ results: list[dict] = []
+ for i, seed in enumerate(seeds):
+ path = Path(output_template.format(seed=seed))
+ if not path.is_file():
+ # Surface an explicit error only if zero candidates landed —
+ # partial success returns whatever we got.
+ continue
results.append({
- "png_path": str(out_path),
- "seed": seed if seed is not None else 0,
+ "png_path": str(path),
+ "seed": seed,
"engine": "mflux",
"family": fam,
"model": config.mflux_model,
"width": width, "height": height,
"refs": refs_used,
"refs_ignored": (bool(refs) and fam != "qwen_edit"),
+ "lora_paths": list(config.mflux_lora_paths or []),
})
+
+ if not results:
+ raise RuntimeError(
+ f"{bin_name} exited 0 but produced no files for any of {n} seeds "
+ f"({seeds!r}). Check the panel log for mflux warnings."
+ )
return results