How to Use the Seed Parameter in Stable Diffusion for Reproducible Generations
The seed parameter synchronizes Python, NumPy, and PyTorch random number generators to produce bit-identical images across runs when using the same model, configuration, and hardware.
The seed parameter in Stable Diffusion controls the random number generators (RNGs) that influence initial noise, model dropout, and sampling stochasticity. In the CompVis/stable-diffusion repository, this is implemented through PyTorch-Lightning's seed_everything utility in the command-line scripts. Setting a fixed seed enables researchers and artists to recreate identical outputs for benchmarking, experimentation, or sharing reproducible workflows.
Sources of Randomness in the Pipeline
Stable Diffusion's generation process contains multiple stochastic components that the seed must coordinate to achieve deterministic results:
- Initial latent tensor: The starting noise created via
torch.randnthat seeds the diffusion process in the latent space. - UNet random operations: Dropout layers and noise injection occurring inside the UNet model during denoising steps.
- Sampler stochasticity: Algorithms like DDIM inject random noise controlled by parameters such as
eta, consuming RNG state fromldm/models/diffusion/ddim.py.
Without synchronization via the seed parameter, these components pull from independent random streams, guaranteeing different images even with identical prompts, checkpoints, and sampler settings.
Implementing Reproducible Generations
The command-line scripts scripts/txt2img.py (lines 22–27) and scripts/img2img.py (lines 83–88) expose a --seed argument that passes the integer value to PyTorch-Lightning's seed_everything utility:
parser.add_argument(
"--seed",
type=int,
default=42,
help="the seed (for reproducible sampling)",
)
...
seed_everything(opt.seed)
The seed_everything function sets Python's built-in random module, NumPy, and PyTorch RNGs to the specified integer. It also forces torch.backends.cudnn.deterministic = True and torch.backends.cudnn.benchmark = False to minimize CUDA nondeterminism.
Command-Line Usage for txt2img
Run deterministic text-to-image generation by specifying the seed flag:
python scripts/txt2img.py \
--prompt "a serene landscape with mountains" \
--ckpt models/ldm/stable-diffusion-v1/model.ckpt \
--config configs/stable-diffusion/v1-inference.yaml \
--seed 12345 \
--ddim_steps 50 \
--n_samples 4 \
--outdir outputs/demo
Command-Line Usage for img2img
Apply the same seed to image-to-image workflows in scripts/img2img.py:
python scripts/img2img.py \
--init-img path/to/input.png \
--prompt "turn the photo into a Van Gogh painting" \
--seed 12345 \
--strength 0.8 \
--ddim_steps 75 \
--outdir outputs/img2img_demo
Programmatic Implementation
To achieve reproducible generations inside custom Python scripts, call seed_everything before invoking the generation main function:
from pytorch_lightning import seed_everything
from scripts.txt2img import main as txt2img_main
seed = 2024
seed_everything(seed) # synchronises all RNGs
txt2img_main() # runs with the same seed each call
Enforcing Deterministic CUDA Behavior
For strict reproducibility when running on GPU, explicitly configure convolution algorithms to eliminate nondeterministic optimizations:
import torch
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
Limitations and Requirements
Perfect bit-identical outputs require identical hardware, model checkpoints, and software versions. Some GPU kernels remain nondeterministic despite seed_everything flags, particularly certain convolution implementations or operations like torch.nn.functional.grid_sample with align_corners=False. Running on CPU eliminates CUDA-specific nondeterminism but sacrifices performance. Reproducibility is guaranteed only when all stochastic components—from the initial torch.randn in the latent creation to the DDIM sampler in ldm/models/diffusion/ddim.py—consume the same seeded RNG state.
Summary
- The seed parameter in Stable Diffusion coordinates Python, NumPy, and PyTorch RNGs via
seed_everythingto control stochastic components. - Source files
scripts/txt2img.pyandscripts/img2img.pyimplement the--seedCLI argument at lines 22–27 and 83–88 respectively. - Reproducible generations require fixed seeds, deterministic CUDA settings (
deterministic=True,benchmark=False), and identical hardware configurations. - Samplers like DDIM and PLMS in
ldm/models/diffusion/consume the seeded RNG state during stochastic sampling steps.
Frequently Asked Questions
Does the same seed produce identical images across different GPUs?
No. GPU-specific kernel implementations and floating-point precision variations can produce different outputs even with identical seeds. Bit-identical reproduction requires the same GPU model and CUDA version.
Why do I get different results with the same seed after updating PyTorch?
PyTorch updates may change underlying algorithms or CUDA kernel implementations. For strict reproducibility, freeze your PyTorch, CUDA, and CompVis/stable-diffusion repository versions.
Can I use the seed parameter with any sampler?
Yes. The seed initializes the global RNG state used by all samplers including DDIM and PLMS in ldm/models/diffusion/ddim.py and ldm/models/diffusion/plms.py. However, samplers with high stochasticity amplify any minor numerical differences between runs.
How do I generate random seeds instead of fixed ones?
Omit the --seed argument or set seed=None programmatically. Without explicit seeding, torch.randn and other operations pull from system entropy, producing unique images each run.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →