Skip to content

Commit 16304cc

Browse files
authored
fix: guard against missing sampler/scheduler names (#1887)
1 parent 760717a commit 16304cc

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

examples/common/common.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,24 +1555,37 @@ ArgOptions SDGenerationParams::get_options() {
15551555
return 1;
15561556
};
15571557

1558+
std::string sample_methods = sample_method_to_str[0];
1559+
for (int i = 1; i < SAMPLE_METHOD_COUNT; i++)
1560+
{
1561+
sample_methods += ", " + std::string(sample_method_to_str[i]);
1562+
}
1563+
1564+
std::string schedulers = scheduler_to_str[0];
1565+
for (int i = 1; i < SCHEDULER_COUNT; i++)
1566+
{
1567+
schedulers += ", " + std::string(scheduler_to_str[i]);
1568+
}
1569+
15581570
options.manual_options = {
15591571
{"-s",
15601572
"--seed",
15611573
"RNG seed (default: 42, use random seed for < 0)",
15621574
on_seed_arg},
15631575
{"",
15641576
"--sampling-method",
1565-
"sampling method, one of [euler, euler_a, heun, dpm2, dpm++2s_a, dpm++2m, dpm++2mv2, dpm++2m_sde, dpm++2m_sde_bt, ipndm, ipndm_v, lcm, ddim_trailing, tcd, res_multistep, res_2s, er_sde, euler_cfg_pp, euler_a_cfg_pp, lms]"
1566-
"(default: euler for Flux/SD3/Wan, euler_a otherwise)",
1577+
"sampling method, one of [" + sample_methods + "], "
1578+
"default: euler for Flux/SD3/Wan, euler_a otherwise",
15671579
on_sample_method_arg},
15681580
{"",
15691581
"--high-noise-sampling-method",
1570-
"(high noise) sampling method, one of [euler, euler_a, heun, dpm2, dpm++2s_a, dpm++2m, dpm++2mv2, dpm++2m_sde, dpm++2m_sde_bt, ipndm, ipndm_v, lcm, ddim_trailing, tcd, res_multistep, res_2s, er_sde, euler_cfg_pp, euler_a_cfg_pp, lms]"
1571-
" default: euler for Flux/SD3/Wan, euler_a otherwise",
1582+
"(high noise) sampling method, one of [" + sample_methods + "], "
1583+
"default: euler for Flux/SD3/Wan, euler_a otherwise",
15721584
on_high_noise_sample_method_arg},
15731585
{"",
15741586
"--scheduler",
1575-
"denoiser sigma scheduler, one of [discrete, karras, exponential, ays, gits, smoothstep, sgm_uniform, simple, kl_optimal, lcm, bong_tangent, ltx2, logit_normal, flux2, flux, beta], alias: normal=discrete, default: model-specific",
1587+
"denoiser sigma scheduler, one of [" + schedulers + "], "
1588+
"alias: normal=discrete, default: model-specific",
15761589
on_scheduler_arg},
15771590
{"",
15781591
"--sigmas",

include/stable-diffusion.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ enum sample_method_t {
6060
SAMPLE_METHOD_COUNT
6161
};
6262

63+
extern SD_API const char* sample_method_to_str[];
64+
6365
enum scheduler_t {
6466
DISCRETE_SCHEDULER,
6567
KARRAS_SCHEDULER,
@@ -80,6 +82,8 @@ enum scheduler_t {
8082
SCHEDULER_COUNT
8183
};
8284

85+
extern SD_API const char* scheduler_to_str[];
86+
8387
enum prediction_t {
8488
EPS_PRED,
8589
V_PRED,

src/stable-diffusion.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ const char* sampling_methods_str[] = {
150150
"LMS",
151151
};
152152

153+
static_assert(SAMPLE_METHOD_COUNT == sizeof(sampling_methods_str) / sizeof(sampling_methods_str[0]),
154+
"\nnumber of elements in sampling_methods_str[] != SAMPLE_METHOD_COUNT");
155+
153156
/*================================================== Helper Functions ================================================*/
154157

155158
static bool sd_version_supports_ref_latent_img_cfg(SDVersion version) {
@@ -3306,6 +3309,9 @@ const char* sample_method_to_str[] = {
33063309
"lms",
33073310
};
33083311

3312+
static_assert(SAMPLE_METHOD_COUNT == sizeof(sample_method_to_str) / sizeof(sample_method_to_str[0]),
3313+
"\nnumber of elements in sample_method_to_str[] != SAMPLE_METHOD_COUNT");
3314+
33093315
const char* sd_sample_method_name(enum sample_method_t sample_method) {
33103316
if (sample_method < SAMPLE_METHOD_COUNT) {
33113317
return sample_method_to_str[sample_method];
@@ -3341,6 +3347,9 @@ const char* scheduler_to_str[] = {
33413347
"beta",
33423348
};
33433349

3350+
static_assert(SCHEDULER_COUNT == sizeof(scheduler_to_str) / sizeof(scheduler_to_str[0]),
3351+
"\nnumber of elements in scheduler_to_str[] != SCHEDULER_COUNT");
3352+
33443353
const char* sd_scheduler_name(enum scheduler_t scheduler) {
33453354
if (scheduler < SCHEDULER_COUNT) {
33463355
return scheduler_to_str[scheduler];

0 commit comments

Comments
 (0)