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
2.9 KiB79 linesraw
1// src/image/pipeline/qi21_glue.cpp - see include/strata/image/qi21_image.hpp.  Contraction off for the file: every
2// product and sum rounds on its own, as sd.cpp's tensor operations do (one pass per operation).
3#if defined(__clang__)
4#pragma clang fp contract(off)
5#elif defined(__GNUC__)
6#pragma GCC optimize("fp-contract=off")
7#endif
8
9#include "strata/image/qi21_image.hpp"
10
11#include <algorithm>
12#include <cmath>
13#include <cstdio>
14
15namespace strata::image {
16
17std::vector<float> qi21_initial_latent(const std::vector<float>& noise, float sigma0) {
18    const size_t n = noise.size();
19    std::vector<float> a(n, 0.0f), b(n);
20    const float keep = 1.0f - sigma0;
21    for (size_t i = 0; i < n; ++i) a[i] = a[i] * keep;          // latent * (1 - sigma), latent = zeros
22    for (size_t i = 0; i < n; ++i) b[i] = noise[i] * sigma0;    // noise * sigma
23    for (size_t i = 0; i < n; ++i) a[i] = a[i] + b[i];          // the sum
24    return a;
25}
26
27namespace {
28
29float tensor_mean(const std::vector<float>& v) {   // sd::Tensor<float>::mean
30    if (v.empty()) return 0.0f;
31    double s = 0.0;
32    for (float x : v) s += static_cast<double>(x);
33    return static_cast<float>(s / static_cast<double>(v.size()));
34}
35
36}  // namespace
37
38std::vector<float> qi21_condition(std::vector<float> hidden, int n, int hidden_dim, const std::vector<float>& weights,
39                                  int n_system) {
40    bool all_one = true;
41    for (float w : weights)
42        if (w != 1.0f) {
43            all_one = false;
44            break;
45        }
46    if (!all_one && !hidden.empty()) {
47        const float m0 = tensor_mean(hidden);
48        for (int t = 0; t < n; ++t) {
49            const float w = weights[(size_t) t];
50            float* row = hidden.data() + (size_t) t * hidden_dim;
51            for (int c = 0; c < hidden_dim; ++c) row[c] = row[c] * w;
52        }
53        const float m1 = tensor_mean(hidden);
54        if (std::isfinite(m0) && std::isfinite(m1) && m1 != 0.0f) {
55            const float s = m0 / m1;
56            for (float& x : hidden) x = x * s;
57        }
58    }
59    if (n_system <= 0) return hidden;
60    return std::vector<float>(hidden.begin() + (size_t) n_system * hidden_dim, hidden.end());
61}
62
63void qi21_picture_to_unit(float* x, size_t n) {
64    for (size_t i = 0; i < n; ++i) {
65        volatile float s = x[i] + 1.0f;   // its own rounding
66        const float v = s * 0.5f;
67        x[i] = std::max(0.0f, std::min(1.0f, v));
68    }
69}
70
71std::string qi21_parameters_text(const std::string& prompt, int steps, uint64_t seed, int width, int height,
72                                 const std::string& model_name) {
73    char line[256];
74    std::snprintf(line, sizeof line, "\nSteps: %d, CFG scale: 1, Seed: %llu, Size: %dx%d, Model: %s, Sampler: euler, "
75                  "Generator: strata-image", steps, (unsigned long long) seed, width, height, model_name.c_str());
76    return prompt + line;
77}
78
79}  // namespace strata::image