@@ -113,6 +113,8 @@ class StableDiffusionGGML {
113113
114114 std::shared_ptr<RNG> rng = std::make_shared<PhiloxRNG>();
115115 std::shared_ptr<RNG> sampler_rng = nullptr ;
116+ std::minstd_rand fallback_rng;
117+
116118 int n_threads = -1 ;
117119 float scale_factor = 0 .18215f ;
118120 float shift_factor = 0 .f;
@@ -239,6 +241,8 @@ class StableDiffusionGGML {
239241 use_tiny_autoencoder = taesd_path.size () > 0 ;
240242 offload_params_to_cpu = sd_ctx_params->offload_params_to_cpu ;
241243
244+ fallback_rng.seed (std::chrono::system_clock::now ().time_since_epoch ().count ());
245+
242246 rng = get_rng (sd_ctx_params->rng_type );
243247 if (sd_ctx_params->sampler_rng_type != RNG_TYPE_COUNT && sd_ctx_params->sampler_rng_type != sd_ctx_params->rng_type ) {
244248 sampler_rng = get_rng (sd_ctx_params->sampler_rng_type );
@@ -2745,6 +2749,17 @@ class StableDiffusionGGML {
27452749 flow_denoiser->set_shift (flow_shift);
27462750 }
27472751 }
2752+
2753+ int64_t get_seed (int64_t seed) {
2754+ if (seed < 0 ) {
2755+ // prevent potential issues if 'stable-diffusion.cpp' is invoked
2756+ // as a library by a third party with a seed <0
2757+ seed = fallback_rng ();
2758+ LOG_DEBUG (" generated %" PRId64 " as random seed" , seed);
2759+ }
2760+ return seed;
2761+ }
2762+
27482763};
27492764
27502765/* ================================================= SD API ==================================================*/
@@ -3267,13 +3282,6 @@ sd_image_t* generate_image_internal(sd_ctx_t* sd_ctx,
32673282 ggml_tensor* concat_latent = nullptr ,
32683283 ggml_tensor* denoise_mask = nullptr ,
32693284 const sd_cache_params_t * cache_params = nullptr ) {
3270- if (seed < 0 ) {
3271- // Generally, when using the provided command line, the seed is always >0.
3272- // However, to prevent potential issues if 'stable-diffusion.cpp' is invoked as a library
3273- // by a third party with a seed <0, let's incorporate randomization here.
3274- srand ((int )time (nullptr ));
3275- seed = rand ();
3276- }
32773285
32783286 if (!std::isfinite (guidance.img_cfg )) {
32793287 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
35543562 return nullptr ;
35553563 }
35563564
3557- int64_t seed = sd_img_gen_params->seed ;
3558- if (seed < 0 ) {
3559- srand ((int )time (nullptr ));
3560- seed = rand ();
3561- }
3565+ int64_t seed = sd_ctx->sd ->get_seed (sd_img_gen_params->seed );
35623566 sd_ctx->sd ->rng ->manual_seed (seed);
35633567 sd_ctx->sd ->sampler_rng ->manual_seed (seed);
35643568
@@ -3910,11 +3914,7 @@ SD_API sd_image_t* generate_video(sd_ctx_t* sd_ctx, const sd_vid_gen_params_t* s
39103914 return nullptr ;
39113915 }
39123916
3913- int64_t seed = sd_vid_gen_params->seed ;
3914- if (seed < 0 ) {
3915- seed = (int )time (nullptr );
3916- }
3917-
3917+ int64_t seed = sd_ctx->sd ->get_seed (sd_vid_gen_params->seed );
39183918 sd_ctx->sd ->rng ->manual_seed (seed);
39193919 sd_ctx->sd ->sampler_rng ->manual_seed (seed);
39203920
0 commit comments