Tutorial: What is a Sampler in Stable Diffusion?

5 min readNov 30, 2023

In the world of artificial intelligence, especially in image generation like Stable Diffusion, a sampler is a crucial component. These sampling methods define how data is selected and generated, directly influencing the quality and style of the resulting images. Let’s explore what a sampler is, where they are used, and why they are so important.

What is a Sampler?

A sampler in generative AI is a method of generating data in a specific way. In the context of diffusion-based models such as Stable Diffusion, samplers dictate how a noisy, random representation is transformed into a detailed, coherent image.

Where are Samplers Used?

Samplers are essential in generative AI models that use diffusion processes to create images. The type of sampler chosen affects how the data transformation is performed, from a noisy initial state to a clear and detailed final image.

Importance of Choosing the Right Sampler..?

Selecting the correct sampler is vital to achieving the desired balance between speed and image quality. Some samplers are optimized for image fidelity, while others focus on generation efficiency. The choice will depend on the specific use case and user preferences.

DDIM (Denoising Diffusion Implicit Models)
Theoretical Foundations

Description: DDIM is an iterative implicit probabilistic model that allows for faster sampling while maintaining an equivalent training objective. The generative process is the reverse of a Markovian diffusion process in DDPMs. DDIM is competitive with GANs in terms of model size and sample quality​​.

Implementation: DDIM samples images by removing noise step by step, using a specific equation involving random noise, subsequences, and alpha and sigma coefficients​​.
Performance: DDIMs can produce high-quality samples 10 to 50 times faster in real-time terms compared to DDPMs, allowing a balance between calculation and sample quality​​.
Features: Deterministic sampling method, reduces randomness compared to other samplers.
Typical Uses: Generation of images where consistency and reproducibility are required.
Advantages: Provides a good balance between image quality and generation efficiency.
Disadvantages: Less variability in results, which can limit diversity in image generation.

PNDM (Predictor-corrector Newtonian Dynamics Model)

Theoretical Foundations:
: PNDM is a fresh perspective on DDPMs, treating them as solutions of differential equations on manifolds. From this perspective, pseudo numerical methods for diffusion models (PNDMs) are proposed.
Performance: PNDMs can generate higher quality synthetic images with only 50 steps compared to 1000 steps for DDIMs, significantly outperforming DDIMs with 250 steps.
Features: It uses principles of Newtonian dynamics in its sampling process.
Typical Uses: Efficient in tasks that require fast image generation and decent quality.
Advantages: Offers an effective combination of speed and quality, being faster than other methods while maintaining acceptable image quality.
Disadvantages: Quality may not be as high as slower, more detailed samplers.

LMS (Langevin Monte Carlo Sampler)

Theoretical Foundations:
: Generative models depend on the ability to sample a distribution. The LMS uses Langevin Dynamics to sample from a distribution, describing the evolution of a system subject to random forces.
Implementation: Langevin Dynamics is based on the Langevin equation for Brownian motion, and the Fokker-Planck equation is used to describe the time evolution of a probability density function.
Sampling: The Euler-Maruyama method is used to numerically approximate the solution to the stochastic differential equation related to the target distribution.
Features: Combines Monte Carlo techniques with the Langevin algorithm for detailed sampling.
Typical Uses: Ideal for generating very detailed and complex images.
Advantages: Excellent at generating high quality images.
Disadvantages: Generally slower than other samplers, which can be a drawback in applications that require speed.


Theoretical Foundations:
: Euler is the simplest possible sampler, mathematically identical to Euler’s method for solving ordinary differential equations. It is completely deterministic, meaning that no random noise is added during sampling.
Implementation: Involves the estimation of the noise in the latent image, the calculation of the amount of noise to be subtracted, and the subtraction of the latent image by the normalized noise image, multiplied by the amount of noise to be reduced.
Features: Based on Euler’s numerical integration method.
Typical Uses: Suitable for a diverse range of imaging tasks.
Advantages: Good balance between quality and speed, making it versatile.
Disadvantages: May lack accuracy in certain scenarios compared to more sophisticated methods.

Euler_A (Ancestral Euler)

Theoretical Background: An improvement on the Euler method, Euler_A increases image fidelity by adjusting the integration process.
Features: An improved variant of the Euler sampler that emphasizes image fidelity.
Typical Uses: Similar to Euler, but with slightly superior results in terms of quality.
Advantages: Provides better image quality compared to the standard Euler method.
Disadvantages: Slightly slower than regular Euler due to its focus on quality improvement.


Theoretical Background: Uses Heun’s method, a second-order numerical integration technique, for more precise image generation.
Features: Uses the Heun method, a variant of the Runge-Kutta integration scheme.
Typical Uses: Effective for generating high fidelity and precision images.
Advantages: Greater precision and fidelity in image generation.
Disadvantages: Tends to be slower compared to methods like Euler, which could be a limiting factor in certain applications.
Each of these samplers has its unique set of strengths and limitations, and your choice depends on the specific needs of the imaging task at hand.

Tests with different samplers

Euler A