File size: 3,732 Bytes
565b868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Several (variant, steps) renderings of the five fixed-seed moss_gen prompts in ONE process (the pipeline and its DiT stay loaded; only a variant switch
recompiles the DiT). Same file names, sidecars and summaries as moss_gen.py, one output folder per run.

Usage: python moss_gen_multi.py --root /opt/mossimage/work/steps --run sft:30:s30 --run sft:20:s20 --run sft-int8:30:int8_s30 --run sft-int8:20:int8_s20
       (--run VARIANT:STEPS:FOLDER; guidance is the model's normal 3.0, no negative prompt, 1024x1024, the same seeds as the 50-step samples)
"""
import argparse
import json
import os
import sys
import time

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from common import hwm_gb  # noqa: E402
from moss_gen import MODEL_NOTE, NEGATIVE, PROMPTS  # noqa: E402


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--root", required=True)
    ap.add_argument("--run", action="append", required=True, help="VARIANT:STEPS:FOLDER[:GUIDANCE]")
    ap.add_argument("--size", type=int, default=1024)
    ap.add_argument("--guidance", type=float, default=3.0)
    ap.add_argument("--device", default="GPU")
    ap.add_argument("--only", default="", help="comma separated prompt names")
    args = ap.parse_args()
    from PIL import Image

    from moss_pipeline import VARIANTS, MossImage

    runs = [r.split(":") for r in args.run]
    t0 = time.time()
    pipe = MossImage(device=args.device, log=lambda *a: print("[pipe]", *a, flush=True), variant=runs[0][0])
    init_s = time.time() - t0
    print(f"[init] {init_s:.1f}s", flush=True)
    first_of_process = True
    for variant, steps, folder, *gopt in runs:
        steps = int(steps)
        guidance = float(gopt[0]) if gopt else args.guidance          # optional 4th field of --run: VARIANT:STEPS:FOLDER:GUIDANCE
        out = os.path.join(args.root, folder)
        os.makedirs(out, exist_ok=True)
        pipe.cache.clear()            # the text-encoder time of every image is a real one (comparable with the 50-step samples)
        summary = []
        for name, seed, prompt in PROMPTS:
            if args.only and name not in args.only.split(","):
                continue
            ts = time.time()

            def prog(i, n, name=name, folder=folder):
                if i % 10 == 0 or i == n:
                    print(f"  [{folder}/{name}] step {i}/{n}  {time.time() - ts:.0f}s", flush=True)

            img, st = pipe.generate(prompt, NEGATIVE, args.size, args.size, steps, guidance, seed, progress=prog, variant=variant)
            stem = f"{name}_{args.size}_s{steps}"
            Image.fromarray(img).save(f"{out}/{stem}.png")
            side = {"prompt": prompt, "negative_prompt": NEGATIVE, "seed": seed, "steps": steps, "guidance": guidance if VARIANTS[variant]["sampler"] == "cfg" else None,
                    "width": args.size, "height": args.size, "model": variant, "model_note": MODEL_NOTE.get(variant, variant), "stats": st, "process_init_s": round(init_s, 1),
                    "first_image_of_process": first_of_process, "host_hwm_gb": round(hwm_gb(), 1)}
            first_of_process = False
            json.dump(side, open(f"{out}/{stem}.json", "w"), indent=1, ensure_ascii=False)
            print(f"[done] {folder}/{stem}: {json.dumps(st)}  host_hwm={hwm_gb():.1f}GB  pixel mean={img.mean():.1f} std={img.std():.1f}", flush=True)
            summary.append({"name": name, "seed": seed, "model": variant, "steps": steps, **{k: st[k] for k in ("total_s", "denoise_s", "s_per_step", "text_encoder_s", "dit_load_s", "vae_s", "peak_gpu_usm_device_mb", "finite")}})
        json.dump(summary, open(f"{out}/summary_{args.size}_s{steps}.json", "w"), indent=1)


if __name__ == "__main__":
    main()