diff --git a/rng.h b/rng.h index e8942605..73d998bb 100644 --- a/rng.h +++ b/rng.h @@ -16,7 +16,7 @@ class STDDefaultRNG : public RNG { public: void manual_seed(uint64_t seed) { - generator.seed(seed); + generator.seed((unsigned int)seed); } std::vector randn(uint32_t n) { @@ -24,7 +24,7 @@ class STDDefaultRNG : public RNG { float mean = 0.0; float stddev = 1.0; std::normal_distribution distribution(mean, stddev); - for (int i = 0; i < n; i++) { + for (uint32_t i = 0; i < n; i++) { float random_number = distribution(generator); result.push_back(random_number); } diff --git a/rng_philox.h b/rng_philox.h index c9b70fc2..e3347685 100644 --- a/rng_philox.h +++ b/rng_philox.h @@ -16,8 +16,8 @@ class PhiloxRNG : public RNG { private: std::vector philox_m = {0xD2511F53, 0xCD9E8D57}; std::vector philox_w = {0x9E3779B9, 0xBB67AE85}; - float two_pow32_inv = 2.3283064e-10; - float two_pow32_inv_2pi = 2.3283064e-10 * 6.2831855; + float two_pow32_inv = 2.3283064e-10f; + float two_pow32_inv_2pi = 2.3283064e-10f * 6.2831855f; std::vector uint32(uint64_t x) { std::vector result(2); @@ -27,10 +27,10 @@ class PhiloxRNG : public RNG { } std::vector> uint32(const std::vector& x) { - int N = x.size(); + uint32_t N = (uint32_t)x.size(); std::vector> result(2, std::vector(N)); - for (int i = 0; i < N; ++i) { + for (uint32_t i = 0; i < N; ++i) { result[0][i] = static_cast(x[i] & 0xFFFFFFFF); result[1][i] = static_cast(x[i] >> 32); } @@ -41,7 +41,7 @@ class PhiloxRNG : public RNG { // A single round of the Philox 4x32 random number generator. void philox4_round(std::vector>& counter, const std::vector>& key) { - uint32_t N = counter[0].size(); + uint32_t N = (uint32_t)counter[0].size(); for (uint32_t i = 0; i < N; i++) { std::vector v1 = uint32(static_cast(counter[0][i]) * static_cast(philox_m[0])); std::vector v2 = uint32(static_cast(counter[2][i]) * static_cast(philox_m[1])); @@ -63,7 +63,7 @@ class PhiloxRNG : public RNG { std::vector> philox4_32(std::vector>& counter, std::vector>& key, int rounds = 10) { - uint32_t N = counter[0].size(); + uint32_t N = (uint32_t)counter[0].size(); for (int i = 0; i < rounds - 1; ++i) { philox4_round(counter, key); @@ -81,7 +81,7 @@ class PhiloxRNG : public RNG { float u = x * two_pow32_inv + two_pow32_inv / 2; float v = y * two_pow32_inv_2pi + two_pow32_inv_2pi / 2; - float s = sqrt(-2.0 * log(u)); + float s = sqrt(-2.0f * log(u)); float r1 = s * sin(v); return r1; @@ -115,8 +115,8 @@ class PhiloxRNG : public RNG { std::vector> g = philox4_32(counter, key_uint32); std::vector result; - for (int i = 0; i < n; ++i) { - result.push_back(box_muller(g[0][i], g[1][i])); + for (uint32_t i = 0; i < n; ++i) { + result.push_back(box_muller((float)g[0][i], (float)g[1][i])); } return result; } diff --git a/stable-diffusion.cpp b/stable-diffusion.cpp index 80f3c219..35e298ab 100644 --- a/stable-diffusion.cpp +++ b/stable-diffusion.cpp @@ -18,7 +18,7 @@ #include "rng_philox.h" #include "stable-diffusion.h" -#define EPS 1e-05 +#define EPS 1e-05f static SDLogLevel log_level = SDLogLevel::INFO; @@ -122,9 +122,9 @@ ggml_tensor* load_tensor_from_file(ggml_context* ctx, const std::string& file_pa } void ggml_tensor_set_f32_randn(struct ggml_tensor* tensor, std::shared_ptr rng) { - uint32_t n = ggml_nelements(tensor); + uint32_t n = (uint32_t)ggml_nelements(tensor); std::vector random_numbers = rng->randn(n); - for (int i = 0; i < n; i++) { + for (uint32_t i = 0; i < n; i++) { ggml_set_f32_1d(tensor, i, random_numbers[i]); } } @@ -438,12 +438,12 @@ std::vector> parse_prompt_attention(const std::str std::string weight = m[1]; if (text == "(") { - round_brackets.push_back(res.size()); + round_brackets.push_back((int)res.size()); } else if (text == "[") { - square_brackets.push_back(res.size()); + square_brackets.push_back((int)res.size()); } else if (!weight.empty()) { if (!round_brackets.empty()) { - multiply_range(round_brackets.back(), std::stod(weight)); + multiply_range(round_brackets.back(), std::stof(weight)); round_brackets.pop_back(); } } else if (text == ")" && !round_brackets.empty()) { @@ -2707,13 +2707,13 @@ struct DiscreteSchedule : SigmaSchedule { if (n == 0) { return result; } else if (n == 1) { - result.push_back(t_to_sigma(t_max)); + result.push_back(t_to_sigma((float)t_max)); result.push_back(0); return result; } float step = static_cast(t_max) / static_cast(n - 1); - for (int i = 0; i < n; ++i) { + for (uint32_t i = 0; i < n; ++i) { float t = t_max - step * i; result.push_back(t_to_sigma(t)); } @@ -2726,17 +2726,17 @@ struct KarrasSchedule : SigmaSchedule { std::vector get_sigmas(uint32_t n) { // These *COULD* be function arguments here, // but does anybody ever bother to touch them? - float sigma_min = 0.1; - float sigma_max = 10.; - float rho = 7.; + float sigma_min = 0.1f; + float sigma_max = 10.f; + float rho = 7.f; std::vector result(n + 1); - float min_inv_rho = pow(sigma_min, (1. / rho)); - float max_inv_rho = pow(sigma_max, (1. / rho)); - for (int i = 0; i < n; i++) { + float min_inv_rho = pow(sigma_min, (1.f / rho)); + float max_inv_rho = pow(sigma_max, (1.f / rho)); + for (uint32_t i = 0; i < n; i++) { // Eq. (5) from Karras et al 2022 - result[i] = pow(max_inv_rho + (float)i / ((float)n - 1.) * (min_inv_rho - max_inv_rho), rho); + result[i] = pow(max_inv_rho + (float)i / ((float)n - 1.f) * (min_inv_rho - max_inv_rho), rho); } result[n] = 0.; return result; @@ -3748,7 +3748,7 @@ class StableDiffusionGGML { } } else { // DPM-Solver-2 - float sigma_mid = exp(0.5 * (log(sigmas[i]) + log(sigmas[i + 1]))); + float sigma_mid = exp(0.5f * (log(sigmas[i]) + log(sigmas[i + 1]))); float dt_1 = sigma_mid - sigmas[i]; float dt_2 = sigmas[i + 1] - sigmas[i]; @@ -3811,7 +3811,7 @@ class StableDiffusionGGML { float t = t_fn(sigmas[i]); float t_next = t_fn(sigma_down); float h = t_next - t; - float s = t + 0.5 * h; + float s = t + 0.5f * h; float* vec_d = (float*)d->data; float* vec_x = (float*)x->data; @@ -3820,7 +3820,7 @@ class StableDiffusionGGML { // First half-step for (int j = 0; j < ggml_nelements(x); j++) { - vec_x2[j] = (sigma_fn(s) / sigma_fn(t)) * vec_x[j] - (exp(-h * 0.5) - 1) * vec_denoised[j]; + vec_x2[j] = (sigma_fn(s) / sigma_fn(t)) * vec_x[j] - (exp(-h * 0.5f) - 1) * vec_denoised[j]; } denoise(x2, sigmas[i + 1], i + 1); @@ -3862,7 +3862,7 @@ class StableDiffusionGGML { float t_next = t_fn(sigmas[i + 1]); float h = t_next - t; float a = sigmas[i + 1] / sigmas[i]; - float b = exp(-h) - 1.; + float b = exp(-h) - 1.f; float* vec_x = (float*)x->data; float* vec_denoised = (float*)denoised->data; float* vec_old_denoised = (float*)old_denoised->data; @@ -3876,7 +3876,7 @@ class StableDiffusionGGML { float h_last = t - t_fn(sigmas[i - 1]); float r = h_last / h; for (int j = 0; j < ggml_nelements(x); j++) { - float denoised_d = (1. + 1. / (2. * r)) * vec_denoised[j] - (1. / (2. * r)) * vec_old_denoised[j]; + float denoised_d = (1.f + 1.f / (2.f * r)) * vec_denoised[j] - (1.f / (2.f * r)) * vec_old_denoised[j]; vec_x[j] = a * vec_x[j] - b * denoised_d; } } @@ -3910,7 +3910,7 @@ class StableDiffusionGGML { if (i == 0 || sigmas[i + 1] == 0) { // Simpler step for the edge cases - float b = exp(-h) - 1.; + float b = exp(-h) - 1.f; for (int j = 0; j < ggml_nelements(x); j++) { vec_x[j] = a * vec_x[j] - b * vec_denoised[j]; } @@ -3919,10 +3919,10 @@ class StableDiffusionGGML { float h_min = std::min(h_last, h); float h_max = std::max(h_last, h); float r = h_max / h_min; - float h_d = (h_max + h_min) / 2.; - float b = exp(-h_d) - 1.; + float h_d = (h_max + h_min) / 2.f; + float b = exp(-h_d) - 1.f; for (int j = 0; j < ggml_nelements(x); j++) { - float denoised_d = (1. + 1. / (2. * r)) * vec_denoised[j] - (1. / (2. * r)) * vec_old_denoised[j]; + float denoised_d = (1.f + 1.f / (2.f * r)) * vec_denoised[j] - (1.f / (2.f * r)) * vec_old_denoised[j]; vec_x[j] = a * vec_x[j] - b * denoised_d; } }