okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-5b 9b242bb5b into f-own-int (round 15: the own text encoder and tokenizer as test programs, the DiT reference tool and position tables; committed work only)11c8c701a

main
2.8 KiB59 linesraw
1"""tools/image/gate/te_tok_collect.py - TEST-ONLY (OWN-5 gate harness): stable-diffusion.cpp's own token ids for every
2case of te_tok_cases.py, from a stage_hooks.py build: one sd-cli process per prompt with OWN5_EXIT_AFTER_TOKENS=1, so
3it stops right after LLMEmbedder built the ids (no model weights are loaded).
4
5    python3 -I -B te_tok_collect.py <sd-cli> <models dir> <cases dir> <out.json>
6
7out.json: [{"name", "prompt_b64", "ids": [...], "weights": [...], "n_system": int} or {..., "error": "<last log line>"}];
8the command is the server's (the three model files, 8 steps, Turbo sigmas), at 256x256."""
9import json
10import os
11import shutil
12import subprocess
13import sys
14import tempfile
15from pathlib import Path
16
17import numpy as np
18
19SIGMAS = "1.0,0.978453,0.95418,0.926626,0.89508,0.845148,0.704534,0.414568,0.0"
20
21
22def main() -> int:
23    exe, models, cdir, out = sys.argv[1], Path(sys.argv[2]), Path(sys.argv[3]), Path(sys.argv[4])
24    man = json.loads((cdir / "cases.json").read_text())
25    res, errors = [], 0
26    with tempfile.TemporaryDirectory(dir=str(out.parent)) as td:
27        for c in man:
28            d = Path(td) / c["name"]
29            d.mkdir()
30            cmd = [exe, "--diffusion-model", str(models / "qwen-image-2.1-turbo-Q8_0.gguf"),
31                   "--vae", str(models / "qwen_image_2.1_vae_bf16.safetensors"),
32                   "--llm", str(models / "Qwen3-VL-8B-Instruct-UD-Q4_K_XL.gguf"),
33                   "--prompt-file", str(cdir / f"{c['name']}.txt"), "--steps", "8", "--cfg-scale", "1.0",
34                   "--sampling-method", "euler", "-W", "256", "-H", "256", "--sigmas", SIGMAS, "-s", "42",
35                   "-o", str(d / "img_%d.png"), "--diffusion-fa"]
36            env = dict(os.environ, OWN5_DUMP_DIR=str(d), OWN5_EXIT_AFTER_TOKENS="1")
37            p = subprocess.run(cmd, env=env, cwd=str(d), capture_output=True, text=True, errors="replace", timeout=600)
38            r = dict(c)
39            if (d / "tokens.bin").is_file():
40                r["ids"] = np.fromfile(d / "tokens.bin", dtype="<i4").tolist()
41                r["weights"] = [float(x) for x in np.fromfile(d / "tokens.weights", dtype="<f4")]
42                r["n_system"] = int((d / "tokens.start").read_text().split()[0])
43            else:
44                lines = [ln for ln in (p.stdout + p.stderr).splitlines() if ln.strip()]
45                r["error"] = (lines[-1] if lines else f"rc {p.returncode}")[:300]
46                r["rc"] = p.returncode
47                errors += 1
48            res.append(r)
49            shutil.rmtree(d, ignore_errors=True)
50    out.write_text(json.dumps(res, ensure_ascii=True))
51    print(f"{len(res)} cases, {len(res) - errors} with ids, {errors} without (sd.cpp refused or failed)")
52    for r in res:
53        if "error" in r:
54            print(f"  {r['name']}: rc {r['rc']}: {r['error']}")
55    return 0
56
57
58if __name__ == "__main__":
59    sys.exit(main())