okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
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 ) { 18const size_t n = noise .size (); 19 std::vector < float > a (n ,0.0f ),b (n ); 20const float keep = 1.0f - sigma0 ; 21for (size_t i = 0 ;i < n ;++ i )a [i ]= a [i ]* keep ;// latent * (1 - sigma), latent = zeros 22for (size_t i = 0 ;i < n ;++ i )b [i ]= noise [i ]* sigma0 ;// noise * sigma 23for (size_t i = 0 ;i < n ;++ i )a [i ]= a [i ]+ b [i ];// the sum 24return a ; 25} 26 27namespace { 28 29float tensor_mean (const std::vector < float >& v ) {// sd::Tensor<float>::mean 30if (v .empty ())return 0.0f ; 31double s = 0.0 ; 32for (float x :v )s += static_cast < double > (x ); 33return 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 , 39int n_system ) { 40bool all_one = true; 41for (float w :weights ) 42if (w != 1.0f ) { 43all_one = false; 44break ; 45 } 46if (!all_one && !hidden .empty ()) { 47const float m0 = tensor_mean (hidden ); 48for (int t = 0 ;t < n ;++ t ) { 49const float w = weights [(size_t )t ]; 50float * row = hidden .data ()+ (size_t )t * hidden_dim ; 51for (int c = 0 ;c < hidden_dim ;++ c )row [c ]= row [c ]* w ; 52 } 53const float m1 = tensor_mean (hidden ); 54if (std::isfinite (m0 )&& std::isfinite (m1 )&& m1 != 0.0f ) { 55const float s = m0 /m1 ; 56for (float & x :hidden )x = x * s ; 57 } 58 } 59if (n_system <=0 )return hidden ; 60return 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 ) { 64for (size_t i = 0 ;i < n ;++ i ) { 65volatile float s = x [i ]+ 1.0f ;// its own rounding 66const float v = s * 0.5f ; 67x [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 , 72const std::string & model_name ) { 73char 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 ()); 76return prompt + line ; 77} 78 79}// namespace strata::image