okami.mom/strata

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

Niko1221Merge f-own-5b adfcf9028 into f-own-int (round 16: the strata-image test program and its parts, DiT runner, sampler, noise, PNG writer; hooked into no build)e4309aaf3

main
4.1 KiB94 linesraw
1// src/image/vae/qi21_vae_io_check_main.cpp - test program strata-vae-io-check (not shipped): the two byte-comparable
2// steps around the VAE decoder against stable-diffusion.cpp's own stage dumps (tools/image/gate/stage_hooks.py):
3//   latent.bin -> wan_vae_denormalize -> must equal vae_in.bin (what sd.cpp's VAE decoder got);
4//   vae_out.bin (sd.cpp's decoded float picture) -> qi21_picture_to_u8 -> written to <out dir>/<tag>.rgb for the
5//   comparison with the pixels of sd-cli's own PNG (tools/image/gate/png_pixels.py); a fused-multiply-add variant
6//   (v * 255 + 0.5 in one rounding, how GCC may compile sd.cpp on aarch64) is written beside it as <tag>.fma.rgb.
7//
8//   strata-vae-io-check <out dir> <dump dir> [<dump dir> ...]
9#include "strata/image/qi21_pixels.hpp"
10#include "strata/image/wan_vae.hpp"
11
12#include <cmath>
13#include <cstdio>
14#include <cstring>
15#include <filesystem>
16#include <fstream>
17#include <string>
18#include <vector>
19
20namespace fs = std::filesystem;
21using namespace strata::image;
22
23namespace {
24
25bool read_f32(const fs::path& p, std::vector<float>& v) {
26    std::ifstream f(p, std::ios::binary);
27    if (!f) return false;
28    std::vector<char> raw((std::istreambuf_iterator<char>(f)), {});
29    v.resize(raw.size() / 4);
30    std::memcpy(v.data(), raw.data(), v.size() * 4);
31    return true;
32}
33
34std::vector<long long> read_shape(const fs::path& p) {
35    std::vector<long long> s;
36    std::ifstream f(p);
37    long long x;
38    while (f >> x) s.push_back(x);
39    return s;
40}
41
42uint8_t fused_u8(float v) {   // the aarch64 GCC reading of sd.cpp's expression
43    if (v <= 0.0f) return 0;
44    if (v >= 1.0f) return 255;
45    return static_cast<uint8_t>(std::fmaf(v, 255.0f, 0.5f));
46}
47
48}  // namespace
49
50int main(int argc, char** argv) {
51    if (argc < 3) {
52        std::fprintf(stderr, "usage: strata-vae-io-check <out dir> <dump dir> [...]\n");
53        return 2;
54    }
55    const fs::path out(argv[1]);
56    fs::create_directories(out);
57    int bad = 0;
58    for (int i = 2; i < argc; ++i) {
59        const fs::path d(argv[i]);
60        const std::string tag = d.parent_path().filename().string() + "__" + d.filename().string();
61        std::vector<float> lat, vin, vout;
62        const auto ls = read_shape(d / "latent.shape"), os = read_shape(d / "vae_out.shape");
63        if (!read_f32(d / "latent.bin", lat) || !read_f32(d / "vae_in.bin", vin) || !read_f32(d / "vae_out.bin", vout) ||
64            ls.size() < 3 || os.size() < 3) {
65            std::printf("%s: dumps missing\n", tag.c_str());
66            ++bad;
67            continue;
68        }
69        const int lw = (int) ls[0], lh = (int) ls[1];
70        wan_vae_denormalize(lat.data(), lh, lw);
71        size_t differ = 0;
72        for (size_t k = 0; k < lat.size() && k < vin.size(); ++k) differ += std::memcmp(&lat[k], &vin[k], 4) != 0;
73        const bool in_ok = lat.size() == vin.size() && differ == 0;
74        std::printf("%s: VAE input (%d x %d x %lld) %s", tag.c_str(), lw, lh, (long long) ls[2],
75                    in_ok ? "byte-identical" : "DIFFERS");
76        if (!in_ok) std::printf(" (%zu of %zu values; sizes %zu / %zu)", differ, vin.size(), lat.size(), vin.size());
77        std::printf("\n");
78        bad += !in_ok;
79        const int w = (int) os[0], h = (int) os[1], c = (int) os[2];
80        std::vector<uint8_t> px((size_t) w * h * c), fx((size_t) w * h * c);
81        qi21_picture_to_u8(vout.data(), w, h, c, px.data());
82        const size_t np = (size_t) w * h;
83        for (size_t p = 0; p < np; ++p)
84            for (int k = 0; k < c; ++k) fx[p * c + k] = fused_u8(vout[(size_t) k * np + p]);
85        std::ofstream(out / (tag + ".rgb"), std::ios::binary).write((const char*) px.data(), px.size());
86        std::ofstream(out / (tag + ".fma.rgb"), std::ios::binary).write((const char*) fx.data(), fx.size());
87        size_t fd = 0;
88        for (size_t k = 0; k < px.size(); ++k) fd += px[k] != fx[k];
89        std::printf("%s: picture %d x %d x %d -> %s.rgb (separate roundings) and .fma.rgb (fused): %zu bytes differ "
90                    "between the two\n", tag.c_str(), w, h, c, tag.c_str(), fd);
91    }
92    std::printf("VAE-IO-CHECK (VAE input) %s\n", bad ? "FAIL" : "PASS");
93    return bad ? 1 : 0;
94}