leejet / stable-diffusion.cpp

Stable Diffusion and Flux in pure C/C++
MIT License
3.31k stars 279 forks source link

Fixed random generator #266

Open wonkyoc opened 4 months ago

wonkyoc commented 4 months ago

The current random generator is fixed by the designated seed number (default: 42). Although this is fine with generating a latent image, it is quite not right for ancestral sampling. The ancestral sampling adds a random noise at every step, but due to the fixed seed, every execution generates the same image, which should not happen.

SA-j00u commented 4 months ago

and all reproducibility will gone... 👎

grauho commented 4 months ago

Looking at rng.hpp and the de-noising code I believe this is already implemented correctly. Euler_a and dpm++2s_a get their random noise using the ggml_tensor_set_f32_randn function which sources it's rng from the RNG randn class method which in turn uses the base seed. Therefore, each de-noising step is "random" relative to each other de-noising step, but deterministic to the overall seed which I believe is the desired behavior.