okami.mom/strata

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

Niko1221Merge f-own-5 56630caca into f-own-int (round 14: Strata's own VAE decoder for sd-cli and the images "vae_decoder" switch)736266e87

main
5.5 KiB114 linesraw
1// src/image/vae/wan_vae_decode_main.cpp - test program for Strata's VAE decoder (OWN-5 gate harness).
2//
3//   strata-vae-decode <vae.safetensors> <latent.bin> <out.f32> [--dump DIR] [--repeat N] [--device D] [--stream on|off] [--band-rows N] [--threads N] [--stats FILE]
4//
5// latent.bin: the diffusion-space latent as run_set.py --dump writes it (float32 planar [64][h][w], latent.shape beside
6// it says "w h 64 1"); it is denormalized like sd.cpp's decode_first_stage does, then decoded.  out.f32: float32
7// planar [4][16h][16w] (before sd.cpp's 0-1 scaling; tools/image/gate/vae_ref.py --png makes the picture).
8// --dump DIR writes every stage as DIR/<name>.f32 (pixel-major [H][W][C]) with DIR/<name>.shape "H W C".
9// --band-rows N: the row bands N picture rows high instead of the default (the identity test only).
10// --threads N: threads for reading and converting the weights at open (default 1).
11// --stats FILE: the operand range report of the decode (wan_vae_range_report) into FILE.
12// --stream off: the one-pass reference decode (the product always uses the row bands; same bytes, more memory).
13// --repeat N decodes N times and checks that every run gives the same bytes (run to run), printing each time.
14// Prints one line per run: "decode <ms> ms, peak <MiB> MiB (weights+work <MiB>)".
15#include "strata/image/wan_vae.hpp"
16
17#include <chrono>
18#include <cstdio>
19#include <cstdlib>
20#include <cstring>
21#include <fstream>
22#include <string>
23#include <vector>
24
25using namespace strata::image;
26
27static bool read_file(const std::string& p, std::vector<float>& v, size_t n) {
28    FILE* f = std::fopen(p.c_str(), "rb");
29    if (!f) return false;
30    v.resize(n);
31    size_t got = std::fread(v.data(), 4, n, f);
32    std::fclose(f);
33    return got == n;
34}
35
36int main(int argc, char** argv) {
37    if (argc < 4) {
38        std::fprintf(stderr, "usage: strata-vae-decode <vae.safetensors> <latent.bin> <out.f32> [--dump DIR] [--repeat N] [--device D]\n");
39        return 2;
40    }
41    std::string vae = argv[1], lat = argv[2], outp = argv[3], dump_dir, stats_path;
42    int repeat = 1, device = -1, stream = 1, band_rows = 0, threads = 1;
43    for (int i = 4; i + 1 < argc; i += 2) {
44        if (!std::strcmp(argv[i], "--dump")) dump_dir = argv[i + 1];
45        else if (!std::strcmp(argv[i], "--repeat")) repeat = std::atoi(argv[i + 1]);
46        else if (!std::strcmp(argv[i], "--device")) device = std::atoi(argv[i + 1]);
47        else if (!std::strcmp(argv[i], "--stream")) stream = !std::strcmp(argv[i + 1], "on");
48        else if (!std::strcmp(argv[i], "--stats")) stats_path = argv[i + 1];
49        else if (!std::strcmp(argv[i], "--band-rows")) band_rows = std::atoi(argv[i + 1]);
50        else if (!std::strcmp(argv[i], "--threads")) threads = std::atoi(argv[i + 1]);
51    }
52    std::string shp = lat.substr(0, lat.size() - 4) + ".shape";
53    std::ifstream sf(shp);
54    int w = 0, h = 0, c = 0;
55    if (!(sf >> w >> h >> c) || c != 64) {
56        std::fprintf(stderr, "cannot read %s (expected \"w h 64 1\")\n", shp.c_str());
57        return 2;
58    }
59    std::vector<float> latent;
60    if (!read_file(lat, latent, (size_t) 64 * h * w)) {
61        std::fprintf(stderr, "cannot read %s\n", lat.c_str());
62        return 2;
63    }
64    wan_vae_denormalize(latent.data(), h, w);
65    std::string err;
66    const auto t_open = std::chrono::steady_clock::now();
67    WanVae* v = wan_vae_open(vae, device, &err, threads);
68    if (!v) {
69        std::fprintf(stderr, "%s\n", err.c_str());
70        return 3;
71    }
72    std::printf("open %.1f ms (%s)\n", std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t_open).count(),
73                wan_vae_open_report(v).c_str());
74    wan_vae_set_streaming(v, stream != 0);
75    if (band_rows > 0) wan_vae_set_band_rows(v, band_rows);
76    if (!stats_path.empty()) wan_vae_set_range_stats(v, true);
77    if (!dump_dir.empty()) {
78        wan_vae_set_dump(v, [&](const char* name, const float* data, int H, int W, int C) {
79            std::string b = dump_dir + "/" + name;
80            if (FILE* f = std::fopen((b + ".f32").c_str(), "wb")) { std::fwrite(data, 4, (size_t) H * W * C, f); std::fclose(f); }
81            if (FILE* f = std::fopen((b + ".shape").c_str(), "w")) { std::fprintf(f, "%d %d %d\n", H, W, C); std::fclose(f); }
82        });
83    }
84    const size_t n_out = (size_t) 4 * 16 * h * 16 * w;
85    std::vector<float> out(n_out), first;
86    int rc = 0;
87    for (int r = 0; r < repeat; ++r) {
88        WanVaeStats st;
89        err = wan_vae_decode(v, latent.data(), h, w, out.data(), &st);
90        if (!err.empty()) {
91            std::fprintf(stderr, "%s\n", err.c_str());
92            rc = 4;
93            break;
94        }
95        bool same = true;
96        if (r == 0) first = out;
97        else same = std::memcmp(first.data(), out.data(), n_out * 4) == 0;
98        std::printf("decode %dx%d run %d: %.1f ms, peak %.0f MiB (weights+work %.0f MiB)%s\n", 16 * w, 16 * h, r,
99                    st.decode_ms, st.peak_bytes / 1048576.0, st.weight_bytes / 1048576.0,
100                    r == 0 ? "" : (same ? ", identical to run 0" : ", DIFFERS from run 0"));
101        if (!same) rc = 5;
102        if (r == 0) wan_vae_set_dump(v, nullptr);
103    }
104    if (rc == 0 || rc == 5) {
105        FILE* f = std::fopen(outp.c_str(), "wb");
106        if (!f || std::fwrite(first.data(), 4, n_out, f) != n_out) rc = 6;
107        if (f) std::fclose(f);
108    }
109    if (!stats_path.empty()) {   // the range report of all runs, one line per convolution
110        if (FILE* f = std::fopen(stats_path.c_str(), "w")) { std::fputs(wan_vae_range_report(v).c_str(), f); std::fclose(f); }
111    }
112    wan_vae_close(v);
113    return rc;
114}