Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions src/stable-diffusion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class StableDiffusionGGML {

std::shared_ptr<RNG> rng = std::make_shared<PhiloxRNG>();
std::shared_ptr<RNG> sampler_rng = nullptr;
std::minstd_rand fallback_rng;

int n_threads = -1;
float scale_factor = 0.18215f;
float shift_factor = 0.f;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 ==================================================*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
Loading