modelbeast/server/operators/trellis_mac/run.py
m3ultra b1d37b1629 feat(trellis_mac): close the fal quality gap — RAM-sized bake cap + forced-opaque alpha
Local GLBs were crushed to exactly 94,535 faces (fal ships ~495k) by the
200k laptop mtlbvh guard + o_voxel cleanup, and noisy baked alpha flipped
materials to BLEND (speckle-veil hair, 'broken face'). Neither was the
diffusion — local decode was already 2.2M tris.

- run.py: default TRELLIS2_MAX_BAKE_FACES by node RAM (>=96GB -> 500k,
  else 200k laptop-safe); pass max_bake_faces/alpha_mode/steps/baker
  params through (server forwards only client-sent params — manifest
  defaults are UI-only)
- manifest: new params, quality defaults
- BENCHMARKS.md: 2026-07-22 entry — 500k cap verified crash-free on M3
  Ultra, 351,898-face GLB, bake 7s->14s, farm-path e2e verified

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-22 19:04:23 +10:00

80 lines
3.1 KiB
Python

import argparse
import json
import os
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[3]
VENDOR = ROOT / "vendor" / "trellis-mac"
ap = argparse.ArgumentParser()
ap.add_argument("--input", action="append", default=[])
ap.add_argument("--outdir", required=True)
ap.add_argument("--params", default="{}")
a = ap.parse_args()
p = json.loads(a.params)
if not a.input:
print("ERROR: no input image")
sys.exit(1)
if not (VENDOR / "generate.py").exists():
print(f"ERROR: trellis-mac not installed at {VENDOR}. Run scripts/install_trellis_mac.sh")
sys.exit(1)
outdir = Path(a.outdir)
out_stem = outdir / f"trellis2_{Path(a.input[0]).stem}"
cmd = [
sys.executable, "generate.py", str(Path(a.input[0]).resolve()),
"--pipeline-type", str(p.get("pipeline_type", "1024")),
"--texture-size", str(p.get("texture_size", 2048)),
"--seed", str(p.get("seed", 0)),
"--output", str(out_stem.resolve()),
]
if p.get("no_texture"):
cmd.append("--no-texture")
if p.get("steps"):
cmd += ["--steps", str(p["steps"])]
env = os.environ.copy()
# xet chunked downloader fails on these BFL/Meta repos ("Unable to parse string
# as hex hash value") — force the reliable HTTP path (same fix as flux_local).
env["HF_HUB_DISABLE_XET"] = "1"
# guard the torch + compiled-kernel OpenMP collision like sf3d does.
env["KMP_DUPLICATE_LIB_OK"] = "TRUE"
env.setdefault("OMP_NUM_THREADS", "1")
env.setdefault("MKL_NUM_THREADS", "1")
# Pre-bake simplify cap. The 200k generate.py default is a laptop-derived
# mtlbvh guard that costs most of the mesh detail (2.2M-tri decode → 95k GLB
# vs fal's 495k). 500k verified crash-free on the M3 Ultra Studio 2026-07-22.
# The server passes only client-sent params (manifest defaults are UI-only),
# so default here, sized by node RAM: big-memory Studios get the quality cap.
if p.get("max_bake_faces") is not None:
env["TRELLIS2_MAX_BAKE_FACES"] = str(p["max_bake_faces"])
else:
mem_gb = os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES") / 1e9
env.setdefault("TRELLIS2_MAX_BAKE_FACES", "500000" if mem_gb >= 96 else "200000")
if p.get("baker"):
env["TRELLIS2_BAKER"] = str(p["baker"])
# "auto" keeps o_voxel's baked-alpha BLEND detection (glass etc.); default
# opaque — the Metal baker's noisy alpha turns solid hair into speckles.
if p.get("alpha_mode"):
env["TRELLIS2_ALPHA_MODE"] = str(p["alpha_mode"])
print("+", " ".join(cmd), flush=True)
print("(first run downloads TRELLIS.2-4B + dinov3 + RMBG-2.0 weights)", flush=True)
res = subprocess.run(cmd, cwd=str(VENDOR), stdout=sys.stdout, stderr=subprocess.STDOUT, env=env)
if res.returncode != 0:
sys.exit(res.returncode)
# generate.py writes <output>.glb (and often a .obj); collect any glb under outdir
glbs = sorted(outdir.rglob("*.glb"))
if not glbs:
print("ERROR: trellis-mac produced no .glb")
sys.exit(1)
outputs = [{"path": str(glbs[0]), "name": f"trellis2_{Path(a.input[0]).stem}.glb",
"meta": {"tool": "trellis_mac"}}]
(outdir / "result.json").write_text(json.dumps({"outputs": outputs}))
print("done:", glbs[0], flush=True)