Skip to content

Commit

Permalink
chore: clear the msvc compilation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
leejet committed Oct 28, 2023
1 parent 3001c23 commit 3bf1665
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions rng.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class STDDefaultRNG : public RNG {

public:
void manual_seed(uint64_t seed) {
generator.seed(seed);
generator.seed((unsigned int)seed);
}

std::vector<float> randn(uint32_t n) {
std::vector<float> result;
float mean = 0.0;
float stddev = 1.0;
std::normal_distribution<float> 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);
}
Expand Down
18 changes: 9 additions & 9 deletions rng_philox.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class PhiloxRNG : public RNG {
private:
std::vector<uint32_t> philox_m = {0xD2511F53, 0xCD9E8D57};
std::vector<uint32_t> 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_t> uint32(uint64_t x) {
std::vector<uint32_t> result(2);
Expand All @@ -27,10 +27,10 @@ class PhiloxRNG : public RNG {
}

std::vector<std::vector<uint32_t>> uint32(const std::vector<uint64_t>& x) {
int N = x.size();
uint32_t N = (uint32_t)x.size();
std::vector<std::vector<uint32_t>> result(2, std::vector<uint32_t>(N));

for (int i = 0; i < N; ++i) {
for (uint32_t i = 0; i < N; ++i) {
result[0][i] = static_cast<uint32_t>(x[i] & 0xFFFFFFFF);
result[1][i] = static_cast<uint32_t>(x[i] >> 32);
}
Expand All @@ -41,7 +41,7 @@ class PhiloxRNG : public RNG {
// A single round of the Philox 4x32 random number generator.
void philox4_round(std::vector<std::vector<uint32_t>>& counter,
const std::vector<std::vector<uint32_t>>& 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<uint32_t> v1 = uint32(static_cast<uint64_t>(counter[0][i]) * static_cast<uint64_t>(philox_m[0]));
std::vector<uint32_t> v2 = uint32(static_cast<uint64_t>(counter[2][i]) * static_cast<uint64_t>(philox_m[1]));
Expand All @@ -63,7 +63,7 @@ class PhiloxRNG : public RNG {
std::vector<std::vector<uint32_t>> philox4_32(std::vector<std::vector<uint32_t>>& counter,
std::vector<std::vector<uint32_t>>& 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);

Expand All @@ -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;
Expand Down Expand Up @@ -115,8 +115,8 @@ class PhiloxRNG : public RNG {
std::vector<std::vector<uint32_t>> g = philox4_32(counter, key_uint32);

std::vector<float> 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;
}
Expand Down
48 changes: 24 additions & 24 deletions stable-diffusion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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> rng) {
uint32_t n = ggml_nelements(tensor);
uint32_t n = (uint32_t)ggml_nelements(tensor);
std::vector<float> 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]);
}
}
Expand Down Expand Up @@ -438,12 +438,12 @@ std::vector<std::pair<std::string, float>> 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()) {
Expand Down Expand Up @@ -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<float>(t_max) / static_cast<float>(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));
}
Expand All @@ -2726,17 +2726,17 @@ struct KarrasSchedule : SigmaSchedule {
std::vector<float> 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<float> 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;
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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];
}
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 3bf1665

Please sign in to comment.