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
1.6 KiB50 linesraw
1// strata_philox_rng.hpp - the RNG sd-cli uses with --model-args strata_noise=1: stable-diffusion.cpp's PhiloxRNG
2// (--rng cuda) stream as its Linux x86 build makes it, computed by strata_noise.hpp on every operating system and CPU.
3// Same interface and the same counter handling as PhiloxRNG in src/core/rng_philox.hpp: the k-th randn() call after
4// manual_seed uses Philox offset k.  tools/image/sdcpp_noise/apply.py puts this file and strata_noise.hpp into
5// src/core/ of a stable-diffusion.cpp checkout and hooks it into StableDiffusionGGML::init.
6#pragma once
7
8#include <memory>
9#include <string>
10#include <vector>
11
12#include "core/rng.hpp"
13#include "core/strata_noise.hpp"
14#include "core/util.h"
15
16namespace strata_noise {
17// true when the model args ("a=1,b=2" or "a=1;b=2", sd.cpp's key=value list) hold strata_noise=1
18static inline bool requested(const char* model_args) {
19    for (const auto& [key, value] : parse_key_value_args(model_args, "model arg")) {
20        if (key == "strata_noise" && value == "1") {
21            return true;
22        }
23    }
24    return false;
25}
26}  // namespace strata_noise
27
28class StrataPhiloxRNG : public RNG {
29private:
30    uint64_t seed   = 0;
31    uint32_t offset = 0;
32
33public:
34    StrataPhiloxRNG(uint64_t seed = 0) : seed(seed) {}
35
36    std::shared_ptr<RNG> clone() const override {
37        return std::make_shared<StrataPhiloxRNG>(*this);
38    }
39
40    void manual_seed(uint64_t s) override {
41        seed   = s;
42        offset = 0;
43    }
44
45    std::vector<float> randn(uint32_t n) override {
46        std::vector<float> r = strata_noise::randn(seed, n, offset);
47        offset += 1;
48        return r;
49    }
50};