Files
Strata/tools/image/gate/vae_identity.py
T
Niko1221 cab47f3d2c Image generation, VAE decoder: f16 headroom for the block-4 shortcut (fixed shift 11), GEMM blocks on the picture's pixel grid (row bands byte-identical on HIP too), and no broken picture on a non-finite value
Operand range (strata-vae-decode --stats over the 16 gate latents, merged per convolution): the only layer within
16x of the f16 maximum under stable-diffusion.cpp's 2^-7 is up_blocks.4.resnets.0.conv_shortcut (largest input
3.48e6, 2.4x headroom); it gets the fixed shift 11 (38.5x).  The shift is a constant per layer (conv_shift()), never
from the picture's data; vae_ref.py carries the same table and a test checks they agree.  Next closest layers:
62.8x and 113x, kept at 7.

The row bands were not byte-identical on HIP (R9700: 8 of 16 differed; hipBLAS gives a row another reduction order
at another row position of the call).  Every convolution now runs its GEMM blocks on the picture's pixel grid, also
in a row window (rows outside the window are zero and dropped), so a pixel sits in the same row of a same-shape call
in both paths.  wan_vae_set_band_rows / --band-rows: vae_identity.py also checks band heights 48 and 100.

A picture with a non-finite value is an error (the sd-cli hook then logs "...; using stable-diffusion.cpp's decoder,
which may fail the same way", the approved line).  Test-only: wan_vae_set_range_stats / wan_vae_range_report.

Gates: identity (bands of 128, 48 and 100 rows against one pass, bands run to run) PASS 16 of 16 on CUDA
(dgxspark-20261110-1832-ea13) and on HIP (p620-20261110-1832-6e66).  Against sd.cpp's CUDA pictures, PSNR worst /
mean: CUDA 56.80 / 57.72, 56.20 / 57.50, 57.02 / 57.37 dB at 512, 1024, 1536x1024 (vae_same_tiling pass);
HIP 56.81 / 57.71, 56.21 / 57.50, 57.02 / 57.37 dB.  Against the previous commit's pictures 79.4-81.2 dB (max 1).
Stress, 1024 p4's latent x2 / x4: ours finite, 65.36 / 63.99 dB from the numpy f32 reference (sd.cpp's decoder
53.91 / 49.59 dB); the shortcut's headroom 113x / 133x.  Decode (bands) CUDA 0.46-0.48 / 1.03-1.08 / 1.46-1.50 s,
HIP 0.82-0.84 / 1.19-1.23 / 1.49-1.50 s.  CPU tests 98 passed with the other image tests (boston-20261110-1837-287a).
2026-11-10 18:52:32 +01:00

66 lines
3.2 KiB
Python

"""tools/image/gate/vae_identity.py - TEST-ONLY (OWN-5 gate harness): the row-band decode (what Strata ships) against
the one-pass reference decode, byte for byte, for every fixed latent of the stage sets named; and run to run.
python -I -B vae_identity.py <strata-vae-decode> <vae.safetensors> <pics root> <latent tag> [<latent tag> ...]
For each <pics root>/<tag>/dump_<name>/latent.bin: decode with --stream off (one pass) and --stream on --repeat 2
(row bands, twice in one process), and with two other band heights (--band-rows 48 and 100: other positions of the
pixels in their GEMM blocks than the default 128 rows); prints the peak memory and time and IDENTICAL / DIFFERENT;
exit 1 if anything differs (gate: all identical at 512, 1024 and 1536x1024, on CUDA and on HIP)."""
from __future__ import annotations
import re
import subprocess
import sys
import tempfile
from pathlib import Path
OTHER_BAND_ROWS = (48, 100)
LINE = re.compile(r"decode (\d+)x(\d+) run (\d+): ([\d.]+) ms, peak (\d+) MiB(?:.*(identical|DIFFERS))?")
def run(exe, vae, lat, out, extra):
r = subprocess.run([exe, vae, str(lat), str(out)] + extra, capture_output=True, text=True)
runs = [m.groups() for m in map(LINE.search, r.stdout.splitlines()) if m]
return r.returncode, runs, r.stderr
def main(argv) -> int:
if len(argv) < 4:
print(__doc__)
return 2
exe, vae, root, tags = argv[0], argv[1], Path(argv[2]), argv[3:]
bad = n = 0
with tempfile.TemporaryDirectory() as tmp:
for tag in tags:
for d in sorted((root / tag).glob("dump_p*_s*")):
name = f"{tag}/{d.name[len('dump_'):]}"
one, band = Path(tmp) / "one.f32", Path(tmp) / "band.f32"
rc1, r1, e1 = run(exe, vae, d / "latent.bin", one, ["--stream", "off"])
rc2, r2, e2 = run(exe, vae, d / "latent.bin", band, ["--stream", "on", "--repeat", "2"])
n += 1
if rc1 or rc2 or not r1 or not r2:
bad += 1
print(f"{name}: FAILED rc {rc1}/{rc2} {(e1 + e2).strip()[-300:]}")
continue
ref = one.read_bytes()
same = ref == band.read_bytes()
others = []
for rows in OTHER_BAND_ROWS:
rc3, r3, e3 = run(exe, vae, d / "latent.bin", band, ["--stream", "on", "--band-rows", str(rows)])
ok = rc3 == 0 and bool(r3) and band.read_bytes() == ref
others.append(f"{rows} rows {'IDENTICAL' if ok else 'DIFFERENT'}")
same = same and ok
rtr = all(g[5] == "identical" for g in r2[1:])
bad += (not same) or (not rtr)
print(f"{name} {r1[0][0]}x{r1[0][1]}: one pass {float(r1[0][3]):.0f} ms peak {r1[0][4]} MiB, "
f"bands {float(r2[0][3]):.0f} ms peak {r2[0][4]} MiB: "
f"{'IDENTICAL' if same else 'DIFFERENT'} ({', '.join(others)}), bands run to run "
f"{'identical' if rtr else 'DIFFERENT'}")
print(f"VAE-IDENTITY {'PASS' if bad == 0 else 'FAIL'}: {n - bad} of {n}")
return 0 if bad == 0 else 1
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))