@@ -99,10 +99,11 @@ class StableDiffusionGGML {
9999 bool vae_decode_only = false ;
100100 bool free_params_immediately = false ;
101101
102- std::shared_ptr<RNG> rng = std::make_shared<STDDefaultRNG>();
103- int n_threads = -1 ;
104- float scale_factor = 0 .18215f ;
105- float shift_factor = 0 .f;
102+ std::shared_ptr<RNG> rng = std::make_shared<PhiloxRNG>();
103+ std::shared_ptr<RNG> sampler_rng = nullptr ;
104+ int n_threads = -1 ;
105+ float scale_factor = 0 .18215f ;
106+ float shift_factor = 0 .f;
106107
107108 std::shared_ptr<Conditioner> cond_stage_model;
108109 std::shared_ptr<FrozenCLIPVisionEmbedder> clip_vision; // for svd or wan2.1 i2v
@@ -188,6 +189,16 @@ class StableDiffusionGGML {
188189 }
189190 }
190191
192+ std::shared_ptr<RNG> get_rng (rng_type_t rng_type) {
193+ if (rng_type == STD_DEFAULT_RNG) {
194+ return std::make_shared<STDDefaultRNG>();
195+ } else if (rng_type == CPU_RNG) {
196+ return std::make_shared<MT19937RNG>();
197+ } else { // default: CUDA_RNG
198+ return std::make_shared<PhiloxRNG>();
199+ }
200+ }
201+
191202 bool init (const sd_ctx_params_t * sd_ctx_params) {
192203 n_threads = sd_ctx_params->n_threads ;
193204 vae_decode_only = sd_ctx_params->vae_decode_only ;
@@ -197,12 +208,11 @@ class StableDiffusionGGML {
197208 use_tiny_autoencoder = taesd_path.size () > 0 ;
198209 offload_params_to_cpu = sd_ctx_params->offload_params_to_cpu ;
199210
200- if (sd_ctx_params->rng_type == STD_DEFAULT_RNG) {
201- rng = std::make_shared<STDDefaultRNG>();
202- } else if (sd_ctx_params->rng_type == CUDA_RNG) {
203- rng = std::make_shared<PhiloxRNG>();
204- } else if (sd_ctx_params->rng_type == CPU_RNG) {
205- rng = std::make_shared<MT19937RNG>();
211+ rng = get_rng (sd_ctx_params->rng_type );
212+ if (sd_ctx_params->sampler_rng_type != RNG_TYPE_COUNT) {
213+ sampler_rng = get_rng (sd_ctx_params->sampler_rng_type );
214+ } else {
215+ sampler_rng = rng;
206216 }
207217
208218 ggml_log_set (ggml_log_callback_default, nullptr );
@@ -1736,7 +1746,7 @@ class StableDiffusionGGML {
17361746 return denoised;
17371747 };
17381748
1739- sample_k_diffusion (method, denoise, work_ctx, x, sigmas, rng , eta);
1749+ sample_k_diffusion (method, denoise, work_ctx, x, sigmas, sampler_rng , eta);
17401750
17411751 if (inverse_noise_scaling) {
17421752 x = denoiser->inverse_noise_scaling (sigmas[sigmas.size () - 1 ], x);
@@ -2291,6 +2301,7 @@ void sd_ctx_params_init(sd_ctx_params_t* sd_ctx_params) {
22912301 sd_ctx_params->n_threads = get_num_physical_cores ();
22922302 sd_ctx_params->wtype = SD_TYPE_COUNT;
22932303 sd_ctx_params->rng_type = CUDA_RNG;
2304+ sd_ctx_params->sampler_rng_type = RNG_TYPE_COUNT;
22942305 sd_ctx_params->prediction = DEFAULT_PRED;
22952306 sd_ctx_params->lora_apply_mode = LORA_APPLY_AUTO;
22962307 sd_ctx_params->offload_params_to_cpu = false ;
@@ -2332,6 +2343,7 @@ char* sd_ctx_params_to_str(const sd_ctx_params_t* sd_ctx_params) {
23322343 " n_threads: %d\n "
23332344 " wtype: %s\n "
23342345 " rng_type: %s\n "
2346+ " sampler_rng_type: %s\n "
23352347 " prediction: %s\n "
23362348 " offload_params_to_cpu: %s\n "
23372349 " keep_clip_on_cpu: %s\n "
@@ -2362,6 +2374,7 @@ char* sd_ctx_params_to_str(const sd_ctx_params_t* sd_ctx_params) {
23622374 sd_ctx_params->n_threads ,
23632375 sd_type_name (sd_ctx_params->wtype ),
23642376 sd_rng_type_name (sd_ctx_params->rng_type ),
2377+ sd_rng_type_name (sd_ctx_params->sampler_rng_type ),
23652378 sd_prediction_name (sd_ctx_params->prediction ),
23662379 BOOL_STR (sd_ctx_params->offload_params_to_cpu ),
23672380 BOOL_STR (sd_ctx_params->keep_clip_on_cpu ),
@@ -2823,6 +2836,7 @@ sd_image_t* generate_image_internal(sd_ctx_t* sd_ctx,
28232836 LOG_INFO (" generating image: %i/%i - seed %" PRId64, b + 1 , batch_count, cur_seed);
28242837
28252838 sd_ctx->sd ->rng ->manual_seed (cur_seed);
2839+ sd_ctx->sd ->sampler_rng ->manual_seed (cur_seed);
28262840 struct ggml_tensor * x_t = init_latent;
28272841 struct ggml_tensor * noise = ggml_new_tensor_4d (work_ctx, GGML_TYPE_F32, W, H, C, 1 );
28282842 ggml_ext_im_set_randn_f32 (noise, sd_ctx->sd ->rng );
@@ -2949,6 +2963,7 @@ sd_image_t* generate_image(sd_ctx_t* sd_ctx, const sd_img_gen_params_t* sd_img_g
29492963 seed = rand ();
29502964 }
29512965 sd_ctx->sd ->rng ->manual_seed (seed);
2966+ sd_ctx->sd ->sampler_rng ->manual_seed (seed);
29522967
29532968 int sample_steps = sd_img_gen_params->sample_params .sample_steps ;
29542969
@@ -3240,6 +3255,7 @@ SD_API sd_image_t* generate_video(sd_ctx_t* sd_ctx, const sd_vid_gen_params_t* s
32403255 }
32413256
32423257 sd_ctx->sd ->rng ->manual_seed (seed);
3258+ sd_ctx->sd ->sampler_rng ->manual_seed (seed);
32433259
32443260 int64_t t0 = ggml_time_ms ();
32453261
0 commit comments