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.0 KiB75 linesraw
1// src/image/noise/qi21_noise_check_main.cpp - test program strata-noise-check (not shipped): Strata's initial noise
2// (include/strata/image/qi21_noise.hpp) against stable-diffusion.cpp's own noise dumps.
3//
4//   strata-noise-check <dump dir> [<dump dir> ...]     each dir dump_p<P>_s<SEED> with noise.bin (stage_hooks.py)
5//   strata-noise-check --write SEED N OUT              our N values for SEED into OUT (float32), for the CPU test
6//
7// Prints per dir byte-identical or the count of differing values and the largest difference in ulps, and
8// NOISE-CHECK PASS / FAIL.
9#include "strata/image/qi21_noise.hpp"
10
11#include <algorithm>
12#include <cstdint>
13#include <cstdio>
14#include <cstdlib>
15#include <cstring>
16#include <filesystem>
17#include <fstream>
18#include <string>
19#include <vector>
20
21namespace fs = std::filesystem;
22using namespace strata::image;
23
24namespace {
25
26int64_t key(float f) {
27    uint32_t u;
28    std::memcpy(&u, &f, 4);
29    return (u & 0x80000000u) ? -(int64_t) (u & 0x7fffffffu) : (int64_t) u;
30}
31
32}  // namespace
33
34int main(int argc, char** argv) {
35    if (argc == 5 && std::strcmp(argv[1], "--write") == 0) {
36        const std::vector<float> v = qi21_noise(std::strtoull(argv[2], nullptr, 10), (uint32_t) std::strtoul(argv[3], nullptr, 10));
37        std::ofstream(argv[4], std::ios::binary).write(reinterpret_cast<const char*>(v.data()), v.size() * 4);
38        return 0;
39    }
40    if (argc < 2) {
41        std::fprintf(stderr, "usage: strata-noise-check <dump dir> [...] | --write SEED N OUT\n");
42        return 2;
43    }
44    int bad = 0;
45    for (int i = 1; i < argc; ++i) {
46        const fs::path d(argv[i]);
47        const std::string name = d.filename().string(), tag = d.parent_path().filename().string() + "/" + name;
48        const size_t at = name.rfind("_s");
49        std::ifstream f(d / "noise.bin", std::ios::binary);
50        if (at == std::string::npos || !f) {
51            std::printf("%s: no seed in the name or no noise.bin\n", tag.c_str());
52            ++bad;
53            continue;
54        }
55        const uint64_t seed = std::strtoull(name.c_str() + at + 2, nullptr, 10);
56        std::vector<char> raw((std::istreambuf_iterator<char>(f)), {});
57        std::vector<float> sd(raw.size() / 4);
58        std::memcpy(sd.data(), raw.data(), sd.size() * 4);
59        const std::vector<float> ours = qi21_noise(seed, (uint32_t) sd.size());
60        size_t differ = 0;
61        int64_t max_ulp = 0;
62        for (size_t k = 0; k < sd.size(); ++k)
63            if (std::memcmp(&sd[k], &ours[k], 4) != 0) {
64                ++differ;
65                max_ulp = std::max<int64_t>(max_ulp, std::llabs(key(sd[k]) - key(ours[k])));
66            }
67        std::printf("%s: seed %llu, %zu values: %s", tag.c_str(), (unsigned long long) seed, sd.size(),
68                    differ == 0 ? "byte-identical" : "DIFFERS");
69        if (differ) std::printf(" (%zu values, largest %lld ulp)", differ, (long long) max_ulp);
70        std::printf("\n");
71        bad += differ != 0;
72    }
73    std::printf("NOISE-CHECK %s\n", bad ? "FAIL" : "PASS");
74    return bad ? 1 : 0;
75}