mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
include/strata/image/qi21_pixels.hpp, src/image/vae/qi21_pixels.cpp: the decoded float picture to 8-bit pixels as sd.cpp 2988060's tensor_to_sd_image / preprocessing_float_to_u8 (0 at or below 0, 255 at or above 1, else (uint8)(v x 255 + 0.5) with two roundings: no fused multiply-add, contraction off for the file), planar in, interleaved out. The latent's preparation for the VAE is step 2's wan_vae_denormalize (sd.cpp's diffusion_to_vae_latents for Qwen-Image-2.1: 64 channels, x std / 1 + mean; sd.cpp unpatchifies nothing before its VAE). Nothing joined or hooked anywhere. Test-only: stage_hooks.py dumps vae_in (what sd.cpp's VAE decoder gets) and vae_out (its float picture before the 8-bit step) (+ test_stage_hooks.py); strata-vae-io-check (src/image/vae/qi21_vae_io_check_main.cpp, nvcc, links wan_vae.cu); strata-pixels-check; tools/image/gate/png_pixels.py (a PNG reader with all five row filters, for sd-cli's PNGs). GB10 (dgxspark-20261111-0610-e738), sd-cli with the stage hooks (build te5bvio), prompt 0 at 512 (seeds 42, 1234) and 1024 (seed 42): wan_vae_denormalize(latent) byte-identical to vae_in on all three; our 8-bit picture of vae_out identical to the pixels of sd-cli's own PNG on all three (4 channels; a fused-multiply-add variant gave the same bytes here). CPU test tools/test_qi21_pixels.py: 8,184 values of that picture with sd-cli's PNG bytes (tools/image/gate/data/qi21_pixels_fixture.bin, 729 within 1e-3 of a rounding boundary) and edge values from numpy float32, built -O2 and -O3 -ffp-contract=fast; p1006 p1006-20261111-0618-a6f6: 58 passed (with the other image tests).
60 lines
2.8 KiB
Python
60 lines
2.8 KiB
Python
"""tools/test_qi21_pixels.py - the decoded picture to 8-bit pixels (src/image/vae/qi21_pixels.cpp) as
|
|
stable-diffusion.cpp 2988060 makes them: tools/image/gate/data/qi21_pixels_fixture.bin holds 8,184 values of sd.cpp's
|
|
decoded float picture (512x512, prompt 0, seed 42; 729 of them within 1e-3 of a rounding boundary) with the bytes of
|
|
sd-cli's own PNG at the same places; plus edge values whose expected bytes are computed here with numpy's float32
|
|
(v x 255 rounded, + 0.5 rounded, truncated; 0 / 255 at or outside the ends). Compiled twice (-O2; -O3
|
|
-ffp-contract=fast: the file must not fuse). No GPU, no sd.cpp, no model file.
|
|
|
|
python -m pytest tools/test_qi21_pixels.py"""
|
|
import shutil
|
|
import struct
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
CXX = shutil.which("g++") or shutil.which("c++") or shutil.which("clang++")
|
|
FIXTURE = ROOT / "tools" / "image" / "gate" / "data" / "qi21_pixels_fixture.bin"
|
|
|
|
|
|
def edge_fixture(path: Path) -> None:
|
|
k = np.arange(256, dtype=np.float32)
|
|
v = np.concatenate([
|
|
np.array([-1.0, -0.0, 0.0, 1e-45, 1.0, 1.5, 0.99999994], dtype=np.float32),
|
|
k / np.float32(255), (k + np.float32(0.5)) / np.float32(255),
|
|
np.nextafter((k + np.float32(0.5)) / np.float32(255), np.float32(0)),
|
|
np.nextafter((k + np.float32(0.5)) / np.float32(255), np.float32(1))]).astype(np.float32)
|
|
m = (v * np.float32(255)).astype(np.float32)
|
|
s = (m + np.float32(0.5)).astype(np.float32)
|
|
want = np.where(v <= 0, 0, np.where(v >= 1, 255, np.clip(s, 0, 255).astype(np.int64))).astype(np.uint8)
|
|
path.write_bytes(struct.pack("<i", len(v)) + v.astype("<f4").tobytes() + want.tobytes())
|
|
|
|
|
|
@unittest.skipUnless(CXX, "no C++ compiler")
|
|
class PixelsMatchSdcpp(unittest.TestCase):
|
|
def build_and_check(self, flags):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
exe = Path(td) / "strata-pixels-check"
|
|
r = subprocess.run([CXX, "-std=c++17", *flags, "-I", str(ROOT / "include"),
|
|
str(ROOT / "src" / "image" / "vae" / "qi21_pixels.cpp"),
|
|
str(ROOT / "src" / "image" / "vae" / "qi21_pixels_check_main.cpp"), "-o", str(exe)],
|
|
capture_output=True, text=True, timeout=300)
|
|
self.assertEqual(r.returncode, 0, r.stderr[-3000:])
|
|
edges = Path(td) / "edges.bin"
|
|
edge_fixture(edges)
|
|
r = subprocess.run([str(exe), str(FIXTURE), str(edges)], capture_output=True, text=True, timeout=60)
|
|
self.assertIn("PIXELS-CHECK PASS", r.stdout, r.stdout)
|
|
|
|
def test_o2(self):
|
|
self.build_and_check(["-O2"])
|
|
|
|
def test_o3_contract_fast(self):
|
|
self.build_and_check(["-O3", "-ffp-contract=fast"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|