okami.mom/strata

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

Niko1221Merge f-own-5 fccdb48a3 (image noise switch, step 1 only) into f-own-int150c2394f

main
2.9 KiB62 linesraw
1// seeds.cpp - TEST-ONLY (OWN-5): strata_noise.hpp against stable-diffusion.cpp's PhiloxRNG over many seeds.
2//   seeds <first> <count> <n> [threads]       prints one line per seed: seed, fnv64(ours), fnv64(ref), equal
3// Build with -DWITH_REF and -I<dir with sd.cpp's core/rng_philox.hpp> to include the reference; without it only
4// our hashes are printed (for platforms whose own sd.cpp stream differs, compare with the x86 reference list).
5#include <atomic>
6#include <cinttypes>
7#include <cstdio>
8#include <cstdlib>
9#include <thread>
10#include <vector>
11
12#include "strata_noise.hpp"
13#ifdef WITH_REF
14#include "core/rng_philox.hpp"
15#endif
16
17static uint64_t fnvb(const std::vector<float>& v) {
18    const unsigned char* p = (const unsigned char*) v.data();
19    uint64_t h = 1469598103934665603ull;
20    for (size_t i = 0; i < v.size() * 4; ++i) h = (h ^ p[i]) * 1099511628211ull;
21    return h;
22}
23
24int main(int argc, char** argv) {
25    std::fprintf(stderr, "sin exception table (compiled in): %zu entries\n", strata_noise::sin_exc().keys.size());
26    const uint64_t first = std::strtoull(argv[1], nullptr, 10);
27    const uint32_t count = (uint32_t) std::strtoul(argv[2], nullptr, 10);
28    const uint32_t n = (uint32_t) std::strtoul(argv[3], nullptr, 10);
29    const int T = argc > 4 ? std::atoi(argv[4]) : (int) std::thread::hardware_concurrency();
30    std::vector<uint64_t> seeds;
31    for (uint32_t i = 0; i < count; ++i) seeds.push_back(first + i);
32    // edge seeds: the 2^31 random default range end, a 64-bit seed (both key words used), the API maximum 2^62
33    for (uint64_t s : {2147483647ull, 4294967295ull, 4294967296ull, 0x123456789abcdefull, (1ull << 62)}) seeds.push_back(s);
34    std::vector<uint64_t> ho(seeds.size()), hr(seeds.size());
35    std::vector<uint32_t> ndiff(seeds.size());
36    std::atomic<size_t> next{0};
37    std::vector<std::thread> th;
38    for (int t = 0; t < T; ++t) th.emplace_back([&] {
39        for (size_t k; (k = next++) < seeds.size();) {
40            std::vector<float> o = strata_noise::randn(seeds[k], n);
41            ho[k] = fnvb(o);
42#ifdef WITH_REF
43            PhiloxRNG r;
44            r.manual_seed(seeds[k]);
45            std::vector<float> f = r.randn(n);
46            hr[k] = fnvb(f);
47            uint32_t d = 0;
48            for (uint32_t i = 0; i < n; ++i) d += strata_noise::fbits(o[i]) != strata_noise::fbits(f[i]);
49            ndiff[k] = d;
50#endif
51        }
52    });
53    for (auto& t : th) t.join();
54    uint64_t all = 1469598103934665603ull, bad = 0;
55    for (size_t k = 0; k < seeds.size(); ++k) {
56        std::printf("%" PRIu64 " %016" PRIx64 " %016" PRIx64 " %u\n", seeds[k], ho[k], hr[k], ndiff[k]);
57        for (int b = 0; b < 8; ++b) all = (all ^ ((ho[k] >> (8 * b)) & 0xff)) * 1099511628211ull;
58        bad += ndiff[k] != 0;
59    }
60    std::printf("SEEDS %zu n %u hash-of-hashes %016" PRIx64 " seeds-with-differences %" PRIu64 "\n", seeds.size(), n, all, bad);
61    return 0;
62}