Skip to content

Commit 45d0ebb

Browse files
committed
style: format code
1 parent b1cc40c commit 45d0ebb

File tree

11 files changed

+92
-99
lines changed

11 files changed

+92
-99
lines changed

conditioner.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,14 +1224,15 @@ struct PixArtCLIPEmbedder : public Conditioner {
12241224
T5UniGramTokenizer t5_tokenizer;
12251225
std::shared_ptr<T5Runner> t5;
12261226
size_t chunk_len = 512;
1227-
bool use_mask = false;
1228-
int mask_pad = 1;
1227+
bool use_mask = false;
1228+
int mask_pad = 1;
12291229

12301230
PixArtCLIPEmbedder(ggml_backend_t backend,
12311231
std::map<std::string, enum ggml_type>& tensor_types,
12321232
int clip_skip = -1,
12331233
bool use_mask = false,
1234-
int mask_pad = 1) : use_mask(use_mask), mask_pad(mask_pad) {
1234+
int mask_pad = 1)
1235+
: use_mask(use_mask), mask_pad(mask_pad) {
12351236
t5 = std::make_shared<T5Runner>(backend, tensor_types, "text_encoders.t5xxl.transformer");
12361237
}
12371238

denoiser.hpp

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ static void sample_k_diffusion(sample_method_t method,
10191019
// also needed to invert the behavior of CompVisDenoiser
10201020
// (k-diffusion's LMSDiscreteScheduler)
10211021
float beta_start = 0.00085f;
1022-
float beta_end = 0.0120f;
1022+
float beta_end = 0.0120f;
10231023
std::vector<double> alphas_cumprod;
10241024
std::vector<double> compvis_sigmas;
10251025

@@ -1030,8 +1030,9 @@ static void sample_k_diffusion(sample_method_t method,
10301030
(i == 0 ? 1.0f : alphas_cumprod[i - 1]) *
10311031
(1.0f -
10321032
std::pow(sqrtf(beta_start) +
1033-
(sqrtf(beta_end) - sqrtf(beta_start)) *
1034-
((float)i / (TIMESTEPS - 1)), 2));
1033+
(sqrtf(beta_end) - sqrtf(beta_start)) *
1034+
((float)i / (TIMESTEPS - 1)),
1035+
2));
10351036
compvis_sigmas[i] =
10361037
std::sqrt((1 - alphas_cumprod[i]) /
10371038
alphas_cumprod[i]);
@@ -1061,7 +1062,8 @@ static void sample_k_diffusion(sample_method_t method,
10611062
// - pred_prev_sample -> "x_t-1"
10621063
int timestep =
10631064
roundf(TIMESTEPS -
1064-
i * ((float)TIMESTEPS / steps)) - 1;
1065+
i * ((float)TIMESTEPS / steps)) -
1066+
1;
10651067
// 1. get previous step value (=t-1)
10661068
int prev_timestep = timestep - TIMESTEPS / steps;
10671069
// The sigma here is chosen to cause the
@@ -1086,10 +1088,9 @@ static void sample_k_diffusion(sample_method_t method,
10861088
float* vec_x = (float*)x->data;
10871089
for (int j = 0; j < ggml_nelements(x); j++) {
10881090
vec_x[j] *= std::sqrt(sigma * sigma + 1) /
1089-
sigma;
1091+
sigma;
10901092
}
1091-
}
1092-
else {
1093+
} else {
10931094
// For the subsequent steps after the first one,
10941095
// at this point x = latents or x = sample, and
10951096
// needs to be prescaled with x <- sample / c_in
@@ -1127,9 +1128,8 @@ static void sample_k_diffusion(sample_method_t method,
11271128
float alpha_prod_t = alphas_cumprod[timestep];
11281129
// Note final_alpha_cumprod = alphas_cumprod[0] due to
11291130
// trailing timestep spacing
1130-
float alpha_prod_t_prev = prev_timestep >= 0 ?
1131-
alphas_cumprod[prev_timestep] : alphas_cumprod[0];
1132-
float beta_prod_t = 1 - alpha_prod_t;
1131+
float alpha_prod_t_prev = prev_timestep >= 0 ? alphas_cumprod[prev_timestep] : alphas_cumprod[0];
1132+
float beta_prod_t = 1 - alpha_prod_t;
11331133
// 3. compute predicted original sample from predicted
11341134
// noise also called "predicted x_0" of formula (12)
11351135
// from https://arxiv.org/pdf/2010.02502.pdf
@@ -1145,7 +1145,7 @@ static void sample_k_diffusion(sample_method_t method,
11451145
vec_pred_original_sample[j] =
11461146
(vec_x[j] / std::sqrt(sigma * sigma + 1) -
11471147
std::sqrt(beta_prod_t) *
1148-
vec_model_output[j]) *
1148+
vec_model_output[j]) *
11491149
(1 / std::sqrt(alpha_prod_t));
11501150
}
11511151
}
@@ -1159,8 +1159,8 @@ static void sample_k_diffusion(sample_method_t method,
11591159
// sigma_t = sqrt((1 - alpha_t-1)/(1 - alpha_t)) *
11601160
// sqrt(1 - alpha_t/alpha_t-1)
11611161
float beta_prod_t_prev = 1 - alpha_prod_t_prev;
1162-
float variance = (beta_prod_t_prev / beta_prod_t) *
1163-
(1 - alpha_prod_t / alpha_prod_t_prev);
1162+
float variance = (beta_prod_t_prev / beta_prod_t) *
1163+
(1 - alpha_prod_t / alpha_prod_t_prev);
11641164
float std_dev_t = eta * std::sqrt(variance);
11651165
// 6. compute "direction pointing to x_t" of formula
11661166
// (12) from https://arxiv.org/pdf/2010.02502.pdf
@@ -1179,8 +1179,8 @@ static void sample_k_diffusion(sample_method_t method,
11791179
std::pow(std_dev_t, 2)) *
11801180
vec_model_output[j];
11811181
vec_x[j] = std::sqrt(alpha_prod_t_prev) *
1182-
vec_pred_original_sample[j] +
1183-
pred_sample_direction;
1182+
vec_pred_original_sample[j] +
1183+
pred_sample_direction;
11841184
}
11851185
}
11861186
if (eta > 0) {
@@ -1208,7 +1208,7 @@ static void sample_k_diffusion(sample_method_t method,
12081208
// by Semi-Linear Consistency Function with Trajectory
12091209
// Mapping", arXiv:2402.19159 [cs.CV]
12101210
float beta_start = 0.00085f;
1211-
float beta_end = 0.0120f;
1211+
float beta_end = 0.0120f;
12121212
std::vector<double> alphas_cumprod;
12131213
std::vector<double> compvis_sigmas;
12141214

@@ -1219,8 +1219,9 @@ static void sample_k_diffusion(sample_method_t method,
12191219
(i == 0 ? 1.0f : alphas_cumprod[i - 1]) *
12201220
(1.0f -
12211221
std::pow(sqrtf(beta_start) +
1222-
(sqrtf(beta_end) - sqrtf(beta_start)) *
1223-
((float)i / (TIMESTEPS - 1)), 2));
1222+
(sqrtf(beta_end) - sqrtf(beta_start)) *
1223+
((float)i / (TIMESTEPS - 1)),
1224+
2));
12241225
compvis_sigmas[i] =
12251226
std::sqrt((1 - alphas_cumprod[i]) /
12261227
alphas_cumprod[i]);
@@ -1235,13 +1236,10 @@ static void sample_k_diffusion(sample_method_t method,
12351236
for (int i = 0; i < steps; i++) {
12361237
// Analytic form for TCD timesteps
12371238
int timestep = TIMESTEPS - 1 -
1238-
(TIMESTEPS / original_steps) *
1239-
(int)floor(i * ((float)original_steps / steps));
1239+
(TIMESTEPS / original_steps) *
1240+
(int)floor(i * ((float)original_steps / steps));
12401241
// 1. get previous step value
1241-
int prev_timestep = i >= steps - 1 ? 0 :
1242-
TIMESTEPS - 1 - (TIMESTEPS / original_steps) *
1243-
(int)floor((i + 1) *
1244-
((float)original_steps / steps));
1242+
int prev_timestep = i >= steps - 1 ? 0 : TIMESTEPS - 1 - (TIMESTEPS / original_steps) * (int)floor((i + 1) * ((float)original_steps / steps));
12451243
// Here timestep_s is tau_n' in Algorithm 4. The _s
12461244
// notation appears to be that from C. Lu,
12471245
// "DPM-Solver: A Fast ODE Solver for Diffusion
@@ -1258,10 +1256,9 @@ static void sample_k_diffusion(sample_method_t method,
12581256
float* vec_x = (float*)x->data;
12591257
for (int j = 0; j < ggml_nelements(x); j++) {
12601258
vec_x[j] *= std::sqrt(sigma * sigma + 1) /
1261-
sigma;
1259+
sigma;
12621260
}
1263-
}
1264-
else {
1261+
} else {
12651262
float* vec_x = (float*)x->data;
12661263
for (int j = 0; j < ggml_nelements(x); j++) {
12671264
vec_x[j] *= std::sqrt(sigma * sigma + 1);
@@ -1294,15 +1291,14 @@ static void sample_k_diffusion(sample_method_t method,
12941291
// DPM-Solver. In fact, we have alpha_{t_n} =
12951292
// \sqrt{\hat{alpha_n}}, [...]"
12961293
float alpha_prod_t = alphas_cumprod[timestep];
1297-
float beta_prod_t = 1 - alpha_prod_t;
1294+
float beta_prod_t = 1 - alpha_prod_t;
12981295
// Note final_alpha_cumprod = alphas_cumprod[0] since
12991296
// TCD is always "trailing"
1300-
float alpha_prod_t_prev = prev_timestep >= 0 ?
1301-
alphas_cumprod[prev_timestep] : alphas_cumprod[0];
1297+
float alpha_prod_t_prev = prev_timestep >= 0 ? alphas_cumprod[prev_timestep] : alphas_cumprod[0];
13021298
// The subscript _s are the only portion in this
13031299
// section (2) unique to TCD
13041300
float alpha_prod_s = alphas_cumprod[timestep_s];
1305-
float beta_prod_s = 1 - alpha_prod_s;
1301+
float beta_prod_s = 1 - alpha_prod_s;
13061302
// 3. Compute the predicted noised sample x_s based on
13071303
// the model parameterization
13081304
//
@@ -1317,7 +1313,7 @@ static void sample_k_diffusion(sample_method_t method,
13171313
vec_pred_original_sample[j] =
13181314
(vec_x[j] / std::sqrt(sigma * sigma + 1) -
13191315
std::sqrt(beta_prod_t) *
1320-
vec_model_output[j]) *
1316+
vec_model_output[j]) *
13211317
(1 / std::sqrt(alpha_prod_t));
13221318
}
13231319
}
@@ -1339,9 +1335,9 @@ static void sample_k_diffusion(sample_method_t method,
13391335
// pred_epsilon = model_output
13401336
vec_x[j] =
13411337
std::sqrt(alpha_prod_s) *
1342-
vec_pred_original_sample[j] +
1338+
vec_pred_original_sample[j] +
13431339
std::sqrt(beta_prod_s) *
1344-
vec_model_output[j];
1340+
vec_model_output[j];
13451341
}
13461342
}
13471343
// 4. Sample and inject noise z ~ N(0, I) for
@@ -1357,7 +1353,7 @@ static void sample_k_diffusion(sample_method_t method,
13571353
// In this case, x is still pred_noised_sample,
13581354
// continue in-place
13591355
ggml_tensor_set_f32_randn(noise, rng);
1360-
float* vec_x = (float*)x->data;
1356+
float* vec_x = (float*)x->data;
13611357
float* vec_noise = (float*)noise->data;
13621358
for (int j = 0; j < ggml_nelements(x); j++) {
13631359
// Corresponding to (35) in Zheng et
@@ -1366,10 +1362,10 @@ static void sample_k_diffusion(sample_method_t method,
13661362
vec_x[j] =
13671363
std::sqrt(alpha_prod_t_prev /
13681364
alpha_prod_s) *
1369-
vec_x[j] +
1365+
vec_x[j] +
13701366
std::sqrt(1 - alpha_prod_t_prev /
1371-
alpha_prod_s) *
1372-
vec_noise[j];
1367+
alpha_prod_s) *
1368+
vec_noise[j];
13731369
}
13741370
}
13751371
}

diffusion_model.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct DiffusionModel {
1313
struct ggml_tensor* c_concat,
1414
struct ggml_tensor* y,
1515
struct ggml_tensor* guidance,
16-
std::vector<ggml_tensor*> ref_latents = {},
16+
std::vector<ggml_tensor*> ref_latents = {},
1717
int num_video_frames = -1,
1818
std::vector<struct ggml_tensor*> controls = {},
1919
float control_strength = 0.f,
@@ -69,7 +69,7 @@ struct UNetModel : public DiffusionModel {
6969
struct ggml_tensor* c_concat,
7070
struct ggml_tensor* y,
7171
struct ggml_tensor* guidance,
72-
std::vector<ggml_tensor*> ref_latents = {},
72+
std::vector<ggml_tensor*> ref_latents = {},
7373
int num_video_frames = -1,
7474
std::vector<struct ggml_tensor*> controls = {},
7575
float control_strength = 0.f,
@@ -120,7 +120,7 @@ struct MMDiTModel : public DiffusionModel {
120120
struct ggml_tensor* c_concat,
121121
struct ggml_tensor* y,
122122
struct ggml_tensor* guidance,
123-
std::vector<ggml_tensor*> ref_latents = {},
123+
std::vector<ggml_tensor*> ref_latents = {},
124124
int num_video_frames = -1,
125125
std::vector<struct ggml_tensor*> controls = {},
126126
float control_strength = 0.f,
@@ -173,7 +173,7 @@ struct FluxModel : public DiffusionModel {
173173
struct ggml_tensor* c_concat,
174174
struct ggml_tensor* y,
175175
struct ggml_tensor* guidance,
176-
std::vector<ggml_tensor*> ref_latents = {},
176+
std::vector<ggml_tensor*> ref_latents = {},
177177
int num_video_frames = -1,
178178
std::vector<struct ggml_tensor*> controls = {},
179179
float control_strength = 0.f,

examples/cli/main.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ struct SDParams {
133133
float skip_layer_start = 0.01f;
134134
float skip_layer_end = 0.2f;
135135

136-
bool chroma_use_dit_mask = true;
137-
bool chroma_use_t5_mask = false;
138-
int chroma_t5_mask_pad = 1;
136+
bool chroma_use_dit_mask = true;
137+
bool chroma_use_t5_mask = false;
138+
int chroma_t5_mask_pad = 1;
139139
};
140140

141141
void print_params(SDParams params) {
@@ -919,7 +919,7 @@ int main(int argc, const char* argv[]) {
919919
input_image_buffer = resized_image_buffer;
920920
}
921921
} else if (params.mode == EDIT) {
922-
vae_decode_only = false;
922+
vae_decode_only = false;
923923
for (auto& path : params.ref_image_paths) {
924924
int c = 0;
925925
int width = 0;
@@ -1113,7 +1113,7 @@ int main(int argc, const char* argv[]) {
11131113
params.skip_layer_start,
11141114
params.skip_layer_end);
11151115
}
1116-
} else { // EDIT
1116+
} else { // EDIT
11171117
results = edit(sd_ctx,
11181118
ref_images.data(),
11191119
ref_images.size(),
@@ -1176,19 +1176,19 @@ int main(int argc, const char* argv[]) {
11761176

11771177
std::string dummy_name, ext, lc_ext;
11781178
bool is_jpg;
1179-
size_t last = params.output_path.find_last_of(".");
1179+
size_t last = params.output_path.find_last_of(".");
11801180
size_t last_path = std::min(params.output_path.find_last_of("/"),
11811181
params.output_path.find_last_of("\\"));
1182-
if (last != std::string::npos // filename has extension
1183-
&& (last_path == std::string::npos || last > last_path)) {
1182+
if (last != std::string::npos // filename has extension
1183+
&& (last_path == std::string::npos || last > last_path)) {
11841184
dummy_name = params.output_path.substr(0, last);
11851185
ext = lc_ext = params.output_path.substr(last);
11861186
std::transform(ext.begin(), ext.end(), lc_ext.begin(), ::tolower);
11871187
is_jpg = lc_ext == ".jpg" || lc_ext == ".jpeg" || lc_ext == ".jpe";
11881188
} else {
11891189
dummy_name = params.output_path;
11901190
ext = lc_ext = "";
1191-
is_jpg = false;
1191+
is_jpg = false;
11921192
}
11931193
// appending ".png" to absent or unknown extension
11941194
if (!is_jpg && lc_ext != ".png") {
@@ -1200,7 +1200,7 @@ int main(int argc, const char* argv[]) {
12001200
continue;
12011201
}
12021202
std::string final_image_path = i > 0 ? dummy_name + "_" + std::to_string(i + 1) + ext : dummy_name + ext;
1203-
if(is_jpg) {
1203+
if (is_jpg) {
12041204
stbi_write_jpg(final_image_path.c_str(), results[i].width, results[i].height, results[i].channel,
12051205
results[i].data, 90, get_image_params(params, params.seed + i).c_str());
12061206
printf("save result JPEG image to '%s'\n", final_image_path.c_str());

0 commit comments

Comments
 (0)