Commit d697a0a for stable-diffusion.cpp

commit d697a0a230bc3e08cc86f3d0c896aa0a1619baa9
Author: farawayso <suhuawei@outlook.com>
Date:   Tue Nov 24 22:10:24 2026 +0800

    fix: keep at least one trimmed sigma in the hires refine schedule (#2361)

    Co-authored-by: leejet <leejet714@gmail.com>

diff --git a/docs/guides/c_api.md b/docs/guides/c_api.md
index 18f6391..4c3d7ce 100644
--- a/docs/guides/c_api.md
+++ b/docs/guides/c_api.md
@@ -320,9 +320,11 @@ Image and LTX/MiniMax-H3 video hires passes use the same DGL execution path.
 Set `hires.enabled=true`. The default is latent bilinear upscaling by 2,
 followed by refinement with the original model and prompt conditions.
 `hires.steps=0` inherits the base step count; denoising strength defaults to
-0.7. Without custom sigmas, a schedule with `floor(steps / strength)` intervals
-is generated and its last `steps + 1` entries are used. `hires.sigmas` instead
-supplies the complete refinement schedule and is not sliced by strength.
+0.7. The automatic schedule uses `floor(steps / strength)` intervals and keeps
+the last `steps + 1` entries. If strength is below 1 and rounding would leave
+the full schedule in place, one extra interval is generated so the first sigma
+is trimmed. Refinement still runs for the requested step count. `hires.sigmas`
+supplies the complete refinement schedule and is used directly.

 `hires.target_width` and `hires.target_height` override the scale. Supplying
 only one makes a square target. Output dimensions follow model alignment.
diff --git a/src/api/generation_hires.cpp b/src/api/generation_hires.cpp
index 0e73478..7c11b21 100644
--- a/src/api/generation_hires.cpp
+++ b/src/api/generation_hires.cpp
@@ -41,16 +41,20 @@ namespace sd::api {
                                                                                                 : std::round(request.values.at("height").get<int>() * double(hires.scale));
         if (width < 1 || height < 1 || width > 16384 || height > 16384)
             throw dgl::Error(SD_STATUS_OUT_OF_RANGE, "hires target dimensions must be in 1..16384");
-        const auto& sampling         = request.image.sample_params;
-        const int default_steps      = sampling.custom_sigmas_count ? sampling.custom_sigmas_count - 1 : sampling.sample_steps;
-        const int steps              = hires.steps > 0 ? hires.steps : default_steps;
-        const double scheduler_steps = steps / double(hires.denoising_strength);
-        if (!hires.custom_sigmas_count && scheduler_steps > 10000)
+        const auto& sampling             = request.image.sample_params;
+        const int default_steps          = sampling.custom_sigmas_count ? sampling.custom_sigmas_count - 1 : sampling.sample_steps;
+        const int steps                  = hires.steps > 0 ? hires.steps : default_steps;
+        const double raw_scheduler_steps = steps / double(hires.denoising_strength);
+        if (!hires.custom_sigmas_count && raw_scheduler_steps > 10000)
             throw dgl::Error(SD_STATUS_OUT_OF_RANGE, "hires steps / denoising_strength exceeds 10000; supply hires.sigmas or increase the strength");
+        int scheduler_steps = std::max(1, int(std::min(raw_scheduler_steps, 10000.0)));
+        // The templates keep the last steps + 1 sigmas. Partial denoising must trim at least the first sigma.
+        if (!hires.custom_sigmas_count && hires.denoising_strength < 1.f && scheduler_steps == steps)
+            ++scheduler_steps;
         request.values.update({{"hires_target_width", sd::align_image_dimension(int(width), multiple)},
                                {"hires_target_height", sd::align_image_dimension(int(height), multiple)},
                                {"hires_steps", steps},
-                               {"hires_scheduler_steps", std::max(1, int(std::min(scheduler_steps, 10000.0)))},
+                               {"hires_scheduler_steps", scheduler_steps},
                                {"hires_mode", found->second},
                                {"hires_antialias", mode == "latent_antialiased" || mode == "latent_bicubic_antialiased"},
                                {"hires_vae_scale", scale}});
diff --git a/src/runtime/denoiser.hpp b/src/runtime/denoiser.hpp
index 52d0640..2429744 100644
--- a/src/runtime/denoiser.hpp
+++ b/src/runtime/denoiser.hpp
@@ -1402,11 +1402,11 @@ struct MingImageFlowDenoiser : DiscreteFlowDenoiser {
         const float factor = std::isfinite(shift) ? shift : resolution_shift;
         std::vector<float> sigmas;
         sigmas.reserve(n + 1);
+        // The upstream grid includes zero. Sample n positive points before appending the single terminal zero.
         for (uint32_t i = 0; i < n; ++i) {
-            const float t = n == 1 ? 1.f : 1.f - static_cast<float>(i) / static_cast<float>(n - 1);
+            const float t = 1.f - static_cast<float>(i) / static_cast<float>(n);
             sigmas.push_back(time_snr_shift(factor, t));
         }
-        // Upstream sets sigma_min to zero before linspace, then appends the terminal zero.
         sigmas.push_back(0.f);
         return sigmas;
     }