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
3.3 KiB80 linesraw
1// src/image/pipeline/qi21_glue_check_main.cpp - test program strata-glue-check (not shipped): the steps of
2// include/strata/image/qi21_image.hpp on given inputs, for tools/test_qi21_glue.py (numpy float32 expectations) and
3// for the check against stable-diffusion.cpp's own stage dumps.
4//
5//   strata-glue-check latent <noise.bin> <sigma0> <out.bin>
6//   strata-glue-check cond <hidden.bin> <n> <hidden_dim> <weights.bin> <n_system> <out.bin>
7//   strata-glue-check unit <in.bin> <out.bin>
8//   strata-glue-check dumps <dump dir> [...]      noise.bin -> qi21_initial_latent(noise, 1.0) == dit_x_0.bin
9//                                                 byte for byte (prints GLUE-DUMPS PASS / FAIL)
10#include "strata/image/qi21_image.hpp"
11
12#include <cstdio>
13#include <cstdlib>
14#include <cstring>
15#include <fstream>
16#include <string>
17#include <vector>
18
19using namespace strata::image;
20
21namespace {
22
23bool read_f32(const std::string& p, std::vector<float>& v) {
24    std::ifstream f(p, std::ios::binary);
25    if (!f) return false;
26    std::vector<char> raw((std::istreambuf_iterator<char>(f)), {});
27    v.resize(raw.size() / 4);
28    std::memcpy(v.data(), raw.data(), v.size() * 4);
29    return true;
30}
31
32bool write_f32(const std::string& p, const std::vector<float>& v) {
33    std::ofstream f(p, std::ios::binary);
34    f.write(reinterpret_cast<const char*>(v.data()), (std::streamsize) (v.size() * 4));
35    return (bool) f;
36}
37
38}  // namespace
39
40int main(int argc, char** argv) {
41    const std::string mode = argc > 1 ? argv[1] : "";
42    std::vector<float> a, w;
43    if (mode == "latent" && argc == 5) {
44        if (!read_f32(argv[2], a)) return 2;
45        return write_f32(argv[4], qi21_initial_latent(a, std::strtof(argv[3], nullptr))) ? 0 : 2;
46    }
47    if (mode == "cond" && argc == 8) {
48        if (!read_f32(argv[2], a) || !read_f32(argv[5], w)) return 2;
49        const int n = std::atoi(argv[3]), hd = std::atoi(argv[4]), ns = std::atoi(argv[6]);
50        if ((size_t) n * hd != a.size() || (size_t) n != w.size()) return 2;
51        return write_f32(argv[7], qi21_condition(std::move(a), n, hd, w, ns)) ? 0 : 2;
52    }
53    if (mode == "unit" && argc == 4) {
54        if (!read_f32(argv[2], a)) return 2;
55        qi21_picture_to_unit(a.data(), a.size());
56        return write_f32(argv[3], a) ? 0 : 2;
57    }
58    if (mode == "dumps" && argc >= 3) {
59        int bad = 0;
60        for (int i = 2; i < argc; ++i) {
61            const std::string d = argv[i];
62            std::vector<float> noise, x0;
63            if (!read_f32(d + "/noise.bin", noise) || !read_f32(d + "/dit_x_0.bin", x0)) {
64                std::printf("%s: dumps missing\n", d.c_str());
65                ++bad;
66                continue;
67            }
68            const std::vector<float> got = qi21_initial_latent(noise, 1.0f);
69            size_t differ = got.size() == x0.size() ? 0 : got.size();
70            for (size_t k = 0; k < got.size() && k < x0.size(); ++k) differ += std::memcmp(&got[k], &x0[k], 4) != 0;
71            std::printf("%s: starting latent %s (%zu of %zu values differ)\n", d.c_str(),
72                        differ ? "DIFFERS" : "byte-identical to dit_x_0", differ, x0.size());
73            bad += differ != 0;
74        }
75        std::printf("GLUE-DUMPS %s\n", bad ? "FAIL" : "PASS");
76        return bad ? 1 : 0;
77    }
78    std::fprintf(stderr, "usage: see the head of src/image/pipeline/qi21_glue_check_main.cpp\n");
79    return 2;
80}