@@ -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 }
0 commit comments