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
6.0 KiB128 linesraw
1// src/image/dit/qi21_dit_step_main.cpp - test program strata-dit-step (not shipped): one Qwen-Image-2.1 DiT step in
2// Strata's own code (include/strata/image/qi21_dit.hpp) on the inputs stable-diffusion.cpp gave its DiT.
3//
4//   strata-dit-step <dit.gguf> <out dir> <dump dir> [<dump dir> ...] [--repeat N] [--call K] [--f16-resident]
5//
6// Each dump dir holds the stage-hook dumps of one picture (tools/image/gate/stage_hooks.py): dit_x_<K> (the latent,
7// shape "W H C 1"), dit_t_<K> (the timestep) and cond (shape "context L").  Writes <out dir>/<dump parent name>/<dump name>/vel.bin, the
8// velocity as sd.cpp's vel_<K>.bin ([C][H][W] float32), and vel.shape.  With --repeat N the step runs N times on the
9// same inputs (the first runs the text prefix, the rest reuse it, like sampling steps 2..8) and every run must give
10// the same bytes.  Prints the open report, per run the step and prefix time, the memory peak of the DiT's own buffers
11// against the GGUF size, and DIT-STEP PASS / FAIL (FAIL = an error or runs that differ).
12#include "strata/image/qi21_dit.hpp"
13
14#include <algorithm>
15#include <cstdio>
16#include <cstdlib>
17#include <cstring>
18#include <filesystem>
19#include <fstream>
20#include <string>
21#include <vector>
22
23namespace fs = std::filesystem;
24using namespace strata::image;
25
26namespace {
27
28bool read_f32(const fs::path& p, std::vector<float>& v) {
29    std::ifstream f(p, std::ios::binary);
30    if (!f) return false;
31    std::vector<char> raw((std::istreambuf_iterator<char>(f)), {});
32    v.resize(raw.size() / 4);
33    std::memcpy(v.data(), raw.data(), v.size() * 4);
34    return true;
35}
36
37std::vector<long long> read_shape(const fs::path& p) {
38    std::vector<long long> s;
39    std::ifstream f(p);
40    long long x;
41    while (f >> x) s.push_back(x);
42    return s;
43}
44
45}  // namespace
46
47int main(int argc, char** argv) {
48    if (argc < 4) {
49        std::fprintf(stderr, "usage: strata-dit-step <dit.gguf> <out dir> <dump dir> [...] [--repeat N] [--call K]\n");
50        return 2;
51    }
52    int repeat = 1, call = 0, flags = 0;
53    std::vector<fs::path> dumps;
54    for (int i = 3; i < argc; ++i) {
55        if (std::strcmp(argv[i], "--repeat") == 0 && i + 1 < argc) repeat = std::max(1, std::atoi(argv[++i]));
56        else if (std::strcmp(argv[i], "--call") == 0 && i + 1 < argc) call = std::atoi(argv[++i]);
57        else if (std::strcmp(argv[i], "--f16-resident") == 0) flags |= QI21_DIT_F16_RESIDENT;
58        else dumps.emplace_back(argv[i]);
59    }
60    const unsigned long long gguf_bytes = fs::file_size(argv[1]);
61    std::string err;
62    Qi21Dit* dit = qi21_dit_open(argv[1], -1, &err, flags);
63    if (!dit) {
64        std::fprintf(stderr, "%s\n", err.c_str());
65        std::printf("DIT-STEP FAIL\n");
66        return 2;
67    }
68    std::printf("open: %s\n", qi21_dit_open_report(dit).c_str());
69    int bad = 0;
70    size_t peak = 0;
71    for (const auto& d : dumps) {
72        const std::string tag = d.parent_path().filename().string() + "/" + d.filename().string();
73        const std::string xs = "dit_x_" + std::to_string(call), ts = "dit_t_" + std::to_string(call);
74        std::vector<float> x, t, cond;
75        const auto sx = read_shape(d / (xs + ".shape")), sc = read_shape(d / "cond.shape");
76        if (!read_f32(d / (xs + ".bin"), x) || !read_f32(d / (ts + ".bin"), t) || !read_f32(d / "cond.bin", cond) ||
77            sx.size() < 3 || sc.size() < 2 || t.empty()) {
78            std::printf("%s: inputs missing\n", tag.c_str());
79            ++bad;
80            continue;
81        }
82        const int W = (int) sx[0], H = (int) sx[1], C = (int) sx[2], ctx = (int) sc[0], L = (int) sc[1];
83        if (C != qi21_dit_in_channels(dit) || ctx != qi21_dit_context_dim(dit) || x.size() != (size_t) W * H * C ||
84            cond.size() != (size_t) ctx * L) {
85            std::printf("%s: shapes do not match the model\n", tag.c_str());
86            ++bad;
87            continue;
88        }
89        const int oc = qi21_dit_out_channels(dit);
90        std::vector<float> vel((size_t) oc * W * H), first;
91        std::vector<double> later;   // step times of runs 2.. (the text prefix reused, as in sampling steps 2..8)
92        for (int r = 0; r < repeat; ++r) {
93            Qi21DitStats st;
94            err = qi21_dit_step(dit, x.data(), W, H, cond.data(), L, t[0], vel.data(), &st);
95            if (!err.empty()) {
96                std::printf("%s: %s\n", tag.c_str(), err.c_str());
97                ++bad;
98                break;
99            }
100            peak = std::max(peak, st.peak_bytes);
101            if (r > 0) later.push_back(st.step_ms);
102            std::printf("%s run %d: %dx%d latent, %d text tokens, t %.4f: step %.1f ms%s; weights %.3f GB, work %.3f GB, "
103                        "peak %.3f GB (GGUF %.3f GB)\n",
104                        tag.c_str(), r + 1, W, H, L, t[0], st.step_ms,
105                        st.prefix_ran ? (" (text prefix " + std::to_string((int) st.prefix_ms) + " ms of it)").c_str() : "",
106                        st.weight_bytes / 1e9, st.work_bytes / 1e9, st.peak_bytes / 1e9, gguf_bytes / 1e9);
107            if (r == 0) {
108                first = vel;
109            } else if (std::memcmp(first.data(), vel.data(), vel.size() * 4) != 0) {
110                std::printf("%s run %d: DIFFERS from run 1\n", tag.c_str(), r + 1);
111                ++bad;
112            }
113        }
114        if (!later.empty()) {
115            std::sort(later.begin(), later.end());
116            std::printf("%s: median step of runs 2..%d: %.1f ms (min %.1f, max %.1f)\n", tag.c_str(), repeat,
117                        later[later.size() / 2], later.front(), later.back());
118        }
119        const fs::path od = fs::path(argv[2]) / d.parent_path().filename() / d.filename();
120        fs::create_directories(od);
121        std::ofstream(od / "vel.bin", std::ios::binary).write(reinterpret_cast<const char*>(first.data()), first.size() * 4);
122        std::ofstream(od / "vel.shape") << W << " " << H << " " << oc << " 1\n";
123    }
124    std::printf("peak of the DiT's own device memory: %.3f GB (GGUF %.3f GB)\n", peak / 1e9, gguf_bytes / 1e9);
125    qi21_dit_close(dit);
126    std::printf("DIT-STEP %s\n", bad == 0 ? "PASS" : "FAIL");
127    return bad == 0 ? 0 : 1;
128}