From 18d93cef1f2e0b629b197d619892dc1eef5e7007 Mon Sep 17 00:00:00 2001 From: Wagner Bruna Date: Thu, 26 Feb 2026 16:38:30 -0300 Subject: [PATCH] chore: replace rand and srand at the library level These functions have global state, so they could interfere with application behavior. --- src/stable-diffusion.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/stable-diffusion.cpp b/src/stable-diffusion.cpp index 717fec18e..02865d2d9 100644 --- a/src/stable-diffusion.cpp +++ b/src/stable-diffusion.cpp @@ -113,6 +113,8 @@ class StableDiffusionGGML { std::shared_ptr rng = std::make_shared(); std::shared_ptr sampler_rng = nullptr; + std::minstd_rand fallback_rng; + int n_threads = -1; float scale_factor = 0.18215f; float shift_factor = 0.f; @@ -239,6 +241,8 @@ class StableDiffusionGGML { use_tiny_autoencoder = taesd_path.size() > 0; offload_params_to_cpu = sd_ctx_params->offload_params_to_cpu; + fallback_rng.seed(std::chrono::system_clock::now().time_since_epoch().count()); + rng = get_rng(sd_ctx_params->rng_type); if (sd_ctx_params->sampler_rng_type != RNG_TYPE_COUNT && sd_ctx_params->sampler_rng_type != sd_ctx_params->rng_type) { sampler_rng = get_rng(sd_ctx_params->sampler_rng_type); @@ -2745,6 +2749,17 @@ class StableDiffusionGGML { flow_denoiser->set_shift(flow_shift); } } + + int64_t get_seed(int64_t seed) { + if (seed < 0) { + // prevent potential issues if 'stable-diffusion.cpp' is invoked + // as a library by a third party with a seed <0 + seed = fallback_rng(); + LOG_DEBUG("generated %" PRId64 " as random seed", seed); + } + return seed; + } + }; /*================================================= SD API ==================================================*/ @@ -3267,13 +3282,6 @@ sd_image_t* generate_image_internal(sd_ctx_t* sd_ctx, ggml_tensor* concat_latent = nullptr, ggml_tensor* denoise_mask = nullptr, const sd_cache_params_t* cache_params = nullptr) { - if (seed < 0) { - // Generally, when using the provided command line, the seed is always >0. - // However, to prevent potential issues if 'stable-diffusion.cpp' is invoked as a library - // by a third party with a seed <0, let's incorporate randomization here. - srand((int)time(nullptr)); - seed = rand(); - } if (!std::isfinite(guidance.img_cfg)) { guidance.img_cfg = guidance.txt_cfg; @@ -3554,11 +3562,7 @@ sd_image_t* generate_image(sd_ctx_t* sd_ctx, const sd_img_gen_params_t* sd_img_g return nullptr; } - int64_t seed = sd_img_gen_params->seed; - if (seed < 0) { - srand((int)time(nullptr)); - seed = rand(); - } + int64_t seed = sd_ctx->sd->get_seed(sd_img_gen_params->seed); sd_ctx->sd->rng->manual_seed(seed); sd_ctx->sd->sampler_rng->manual_seed(seed); @@ -3910,11 +3914,7 @@ SD_API sd_image_t* generate_video(sd_ctx_t* sd_ctx, const sd_vid_gen_params_t* s return nullptr; } - int64_t seed = sd_vid_gen_params->seed; - if (seed < 0) { - seed = (int)time(nullptr); - } - + int64_t seed = sd_ctx->sd->get_seed(sd_vid_gen_params->seed); sd_ctx->sd->rng->manual_seed(seed); sd_ctx->sd->sampler_rng->manual_seed(seed);