From 3c9a8d97b2aa7492e74858196e54cb5a0d16cb3f Mon Sep 17 00:00:00 2001 From: Wagner Bruna Date: Sat, 7 Feb 2026 22:50:33 -0300 Subject: [PATCH 1/2] fix: correct sdapi handling of cfg_scale and steps - actually apply requested cfg_scale - make steps optional - get steps and cfg_scale default values from command line --- examples/server/main.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/examples/server/main.cpp b/examples/server/main.cpp index 4de46fa6a..2173b1fb8 100644 --- a/examples/server/main.cpp +++ b/examples/server/main.cpp @@ -786,8 +786,8 @@ int main(int argc, const char** argv) { std::string negative_prompt = j.value("negative_prompt", ""); int width = j.value("width", 512); int height = j.value("height", 512); - int steps = j.value("steps", -1); - float cfg_scale = j.value("cfg_scale", 7.f); + int steps = j.value("steps", default_gen_params.sample_params.sample_steps); + float cfg_scale = j.value("cfg_scale", default_gen_params.sample_params.guidance.txt_cfg); int64_t seed = j.value("seed", -1); int batch_size = j.value("batch_size", 1); int clip_skip = j.value("clip_skip", -1); @@ -883,16 +883,15 @@ int main(int argc, const char** argv) { enum scheduler_t scheduler = str_to_scheduler(scheduler_name.c_str()); - // avoid excessive resource usage - - SDGenerationParams gen_params = default_gen_params; - gen_params.prompt = prompt; - gen_params.negative_prompt = negative_prompt; - gen_params.width = width; - gen_params.height = height; - gen_params.seed = seed; - gen_params.sample_params.sample_steps = steps; - gen_params.batch_count = batch_size; + SDGenerationParams gen_params = default_gen_params; + gen_params.prompt = prompt; + gen_params.negative_prompt = negative_prompt; + gen_params.width = width; + gen_params.height = height; + gen_params.seed = seed; + gen_params.sample_params.sample_steps = steps; + gen_params.batch_count = batch_size; + gen_params.sample_params.guidance.txt_cfg = cfg_scale; if (clip_skip > 0) { gen_params.clip_skip = clip_skip; From f8a17ca6e8dc84f3489b2079ea265098080410bf Mon Sep 17 00:00:00 2001 From: Wagner Bruna Date: Sun, 8 Feb 2026 10:40:06 -0300 Subject: [PATCH 2/2] feat: use image and command-line dimensions by default on sdapi --- examples/server/main.cpp | 58 +++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/examples/server/main.cpp b/examples/server/main.cpp index 2173b1fb8..2860e3a85 100644 --- a/examples/server/main.cpp +++ b/examples/server/main.cpp @@ -886,8 +886,6 @@ int main(int argc, const char** argv) { SDGenerationParams gen_params = default_gen_params; gen_params.prompt = prompt; gen_params.negative_prompt = negative_prompt; - gen_params.width = width; - gen_params.height = height; gen_params.seed = seed; gen_params.sample_params.sample_steps = steps; gen_params.batch_count = batch_size; @@ -905,17 +903,37 @@ int main(int argc, const char** argv) { gen_params.sample_params.scheduler = scheduler; } + // re-read to avoid applying 512 as default before the provided + // images and/or server command-line + gen_params.width = j.value("width", -1); + gen_params.height = j.value("height", -1); + LOG_DEBUG("%s\n", gen_params.to_string().c_str()); - sd_image_t init_image = {(uint32_t)gen_params.width, (uint32_t)gen_params.height, 3, nullptr}; - sd_image_t control_image = {(uint32_t)gen_params.width, (uint32_t)gen_params.height, 3, nullptr}; - sd_image_t mask_image = {(uint32_t)gen_params.width, (uint32_t)gen_params.height, 1, nullptr}; + sd_image_t init_image = {0, 0, 3, nullptr}; + sd_image_t control_image = {0, 0, 3, nullptr}; + sd_image_t mask_image = {0, 0, 1, nullptr}; std::vector mask_data; std::vector pmid_images; std::vector ref_images; + auto get_resolved_width = [&gen_params, &default_gen_params]() -> int { + if (gen_params.width > 0) + return gen_params.width; + if (default_gen_params.width > 0) + return default_gen_params.width; + return 512; + }; + auto get_resolved_height = [&gen_params, &default_gen_params]() -> int { + if (gen_params.height > 0) + return gen_params.height; + if (default_gen_params.height > 0) + return default_gen_params.height; + return 512; + }; + if (img2img) { - auto decode_image = [](sd_image_t& image, std::string encoded) -> bool { + auto decode_image = [&gen_params](sd_image_t& image, std::string encoded) -> bool { // remove data URI prefix if present ("data:image/png;base64,") auto comma_pos = encoded.find(','); if (comma_pos != std::string::npos) { @@ -923,14 +941,22 @@ int main(int argc, const char** argv) { } std::vector img_data = base64_decode(encoded); if (!img_data.empty()) { - int img_w = image.width; - int img_h = image.height; + int expected_width = 0; + int expected_height = 0; + if (gen_params.width_and_height_are_set()) { + expected_width = gen_params.width; + expected_height = gen_params.height; + } + int img_w = expected_width; + int img_h = expected_height; + uint8_t* raw_data = load_image_from_memory( (const char*)img_data.data(), (int)img_data.size(), img_w, img_h, - image.width, image.height, image.channel); + expected_width, expected_height, image.channel); if (raw_data) { image = {(uint32_t)img_w, (uint32_t)img_h, image.channel, raw_data}; + gen_params.set_width_and_height_if_unset(image.width, image.height); return true; } } @@ -952,9 +978,11 @@ int main(int argc, const char** argv) { } } } else { - mask_data = std::vector(width * height, 255); - mask_image.width = width; - mask_image.height = height; + int m_width = get_resolved_width(); + int m_height = get_resolved_height(); + mask_data = std::vector(m_width * m_height, 255); + mask_image.width = m_width; + mask_image.height = m_height; mask_image.channel = 1; mask_image.data = mask_data.data(); } @@ -962,7 +990,7 @@ int main(int argc, const char** argv) { if (j.contains("extra_images") && j["extra_images"].is_array()) { for (auto extra_image : j["extra_images"]) { std::string encoded = extra_image.get(); - sd_image_t tmp_image = {(uint32_t)gen_params.width, (uint32_t)gen_params.height, 3, nullptr}; + sd_image_t tmp_image = {0, 0, 3, nullptr}; if (decode_image(tmp_image, encoded)) { ref_images.push_back(tmp_image); } @@ -988,8 +1016,8 @@ int main(int argc, const char** argv) { gen_params.auto_resize_ref_image, gen_params.increase_ref_index, mask_image, - gen_params.width, - gen_params.height, + get_resolved_width(), + get_resolved_height(), gen_params.sample_params, gen_params.strength, gen_params.seed,