@@ -1019,7 +1019,7 @@ static void sample_k_diffusion(sample_method_t method,
1019
1019
// also needed to invert the behavior of CompVisDenoiser
1020
1020
// (k-diffusion's LMSDiscreteScheduler)
1021
1021
float beta_start = 0 .00085f ;
1022
- float beta_end = 0 .0120f ;
1022
+ float beta_end = 0 .0120f ;
1023
1023
std::vector<double > alphas_cumprod;
1024
1024
std::vector<double > compvis_sigmas;
1025
1025
@@ -1030,8 +1030,9 @@ static void sample_k_diffusion(sample_method_t method,
1030
1030
(i == 0 ? 1 .0f : alphas_cumprod[i - 1 ]) *
1031
1031
(1 .0f -
1032
1032
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 ));
1035
1036
compvis_sigmas[i] =
1036
1037
std::sqrt ((1 - alphas_cumprod[i]) /
1037
1038
alphas_cumprod[i]);
@@ -1061,7 +1062,8 @@ static void sample_k_diffusion(sample_method_t method,
1061
1062
// - pred_prev_sample -> "x_t-1"
1062
1063
int timestep =
1063
1064
roundf (TIMESTEPS -
1064
- i * ((float )TIMESTEPS / steps)) - 1 ;
1065
+ i * ((float )TIMESTEPS / steps)) -
1066
+ 1 ;
1065
1067
// 1. get previous step value (=t-1)
1066
1068
int prev_timestep = timestep - TIMESTEPS / steps;
1067
1069
// The sigma here is chosen to cause the
@@ -1086,10 +1088,9 @@ static void sample_k_diffusion(sample_method_t method,
1086
1088
float * vec_x = (float *)x->data ;
1087
1089
for (int j = 0 ; j < ggml_nelements (x); j++) {
1088
1090
vec_x[j] *= std::sqrt (sigma * sigma + 1 ) /
1089
- sigma;
1091
+ sigma;
1090
1092
}
1091
- }
1092
- else {
1093
+ } else {
1093
1094
// For the subsequent steps after the first one,
1094
1095
// at this point x = latents or x = sample, and
1095
1096
// needs to be prescaled with x <- sample / c_in
@@ -1127,9 +1128,8 @@ static void sample_k_diffusion(sample_method_t method,
1127
1128
float alpha_prod_t = alphas_cumprod[timestep];
1128
1129
// Note final_alpha_cumprod = alphas_cumprod[0] due to
1129
1130
// 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 ;
1133
1133
// 3. compute predicted original sample from predicted
1134
1134
// noise also called "predicted x_0" of formula (12)
1135
1135
// from https://arxiv.org/pdf/2010.02502.pdf
@@ -1145,7 +1145,7 @@ static void sample_k_diffusion(sample_method_t method,
1145
1145
vec_pred_original_sample[j] =
1146
1146
(vec_x[j] / std::sqrt (sigma * sigma + 1 ) -
1147
1147
std::sqrt (beta_prod_t ) *
1148
- vec_model_output[j]) *
1148
+ vec_model_output[j]) *
1149
1149
(1 / std::sqrt (alpha_prod_t ));
1150
1150
}
1151
1151
}
@@ -1159,8 +1159,8 @@ static void sample_k_diffusion(sample_method_t method,
1159
1159
// sigma_t = sqrt((1 - alpha_t-1)/(1 - alpha_t)) *
1160
1160
// sqrt(1 - alpha_t/alpha_t-1)
1161
1161
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);
1164
1164
float std_dev_t = eta * std::sqrt (variance);
1165
1165
// 6. compute "direction pointing to x_t" of formula
1166
1166
// (12) from https://arxiv.org/pdf/2010.02502.pdf
@@ -1179,8 +1179,8 @@ static void sample_k_diffusion(sample_method_t method,
1179
1179
std::pow (std_dev_t , 2 )) *
1180
1180
vec_model_output[j];
1181
1181
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;
1184
1184
}
1185
1185
}
1186
1186
if (eta > 0 ) {
@@ -1208,7 +1208,7 @@ static void sample_k_diffusion(sample_method_t method,
1208
1208
// by Semi-Linear Consistency Function with Trajectory
1209
1209
// Mapping", arXiv:2402.19159 [cs.CV]
1210
1210
float beta_start = 0 .00085f ;
1211
- float beta_end = 0 .0120f ;
1211
+ float beta_end = 0 .0120f ;
1212
1212
std::vector<double > alphas_cumprod;
1213
1213
std::vector<double > compvis_sigmas;
1214
1214
@@ -1219,8 +1219,9 @@ static void sample_k_diffusion(sample_method_t method,
1219
1219
(i == 0 ? 1 .0f : alphas_cumprod[i - 1 ]) *
1220
1220
(1 .0f -
1221
1221
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 ));
1224
1225
compvis_sigmas[i] =
1225
1226
std::sqrt ((1 - alphas_cumprod[i]) /
1226
1227
alphas_cumprod[i]);
@@ -1235,13 +1236,10 @@ static void sample_k_diffusion(sample_method_t method,
1235
1236
for (int i = 0 ; i < steps; i++) {
1236
1237
// Analytic form for TCD timesteps
1237
1238
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));
1240
1241
// 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));
1245
1243
// Here timestep_s is tau_n' in Algorithm 4. The _s
1246
1244
// notation appears to be that from C. Lu,
1247
1245
// "DPM-Solver: A Fast ODE Solver for Diffusion
@@ -1258,10 +1256,9 @@ static void sample_k_diffusion(sample_method_t method,
1258
1256
float * vec_x = (float *)x->data ;
1259
1257
for (int j = 0 ; j < ggml_nelements (x); j++) {
1260
1258
vec_x[j] *= std::sqrt (sigma * sigma + 1 ) /
1261
- sigma;
1259
+ sigma;
1262
1260
}
1263
- }
1264
- else {
1261
+ } else {
1265
1262
float * vec_x = (float *)x->data ;
1266
1263
for (int j = 0 ; j < ggml_nelements (x); j++) {
1267
1264
vec_x[j] *= std::sqrt (sigma * sigma + 1 );
@@ -1294,15 +1291,14 @@ static void sample_k_diffusion(sample_method_t method,
1294
1291
// DPM-Solver. In fact, we have alpha_{t_n} =
1295
1292
// \sqrt{\hat{alpha_n}}, [...]"
1296
1293
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 ;
1298
1295
// Note final_alpha_cumprod = alphas_cumprod[0] since
1299
1296
// 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 ];
1302
1298
// The subscript _s are the only portion in this
1303
1299
// section (2) unique to TCD
1304
1300
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;
1306
1302
// 3. Compute the predicted noised sample x_s based on
1307
1303
// the model parameterization
1308
1304
//
@@ -1317,7 +1313,7 @@ static void sample_k_diffusion(sample_method_t method,
1317
1313
vec_pred_original_sample[j] =
1318
1314
(vec_x[j] / std::sqrt (sigma * sigma + 1 ) -
1319
1315
std::sqrt (beta_prod_t ) *
1320
- vec_model_output[j]) *
1316
+ vec_model_output[j]) *
1321
1317
(1 / std::sqrt (alpha_prod_t ));
1322
1318
}
1323
1319
}
@@ -1339,9 +1335,9 @@ static void sample_k_diffusion(sample_method_t method,
1339
1335
// pred_epsilon = model_output
1340
1336
vec_x[j] =
1341
1337
std::sqrt (alpha_prod_s) *
1342
- vec_pred_original_sample[j] +
1338
+ vec_pred_original_sample[j] +
1343
1339
std::sqrt (beta_prod_s) *
1344
- vec_model_output[j];
1340
+ vec_model_output[j];
1345
1341
}
1346
1342
}
1347
1343
// 4. Sample and inject noise z ~ N(0, I) for
@@ -1357,7 +1353,7 @@ static void sample_k_diffusion(sample_method_t method,
1357
1353
// In this case, x is still pred_noised_sample,
1358
1354
// continue in-place
1359
1355
ggml_tensor_set_f32_randn (noise, rng);
1360
- float * vec_x = (float *)x->data ;
1356
+ float * vec_x = (float *)x->data ;
1361
1357
float * vec_noise = (float *)noise->data ;
1362
1358
for (int j = 0 ; j < ggml_nelements (x); j++) {
1363
1359
// Corresponding to (35) in Zheng et
@@ -1366,10 +1362,10 @@ static void sample_k_diffusion(sample_method_t method,
1366
1362
vec_x[j] =
1367
1363
std::sqrt (alpha_prod_t_prev /
1368
1364
alpha_prod_s) *
1369
- vec_x[j] +
1365
+ vec_x[j] +
1370
1366
std::sqrt (1 - alpha_prod_t_prev /
1371
- alpha_prod_s) *
1372
- vec_noise[j];
1367
+ alpha_prod_s) *
1368
+ vec_noise[j];
1373
1369
}
1374
1370
}
1375
1371
}
0 commit comments