Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 7

Deterministic vs. Stochastic Sampling: A Practical Comparison

Recommended resources

Hello! Welcome back to our series on advanced sampling.

In the last lesson, we implemented DDIM sampling and saw how its deterministic nature (when ) allows for much faster inference than the original stochastic DDPM sampler. This naturally raises a question: if deterministic sampling is faster and reproducible, why would we ever choose a stochastic approach?

Today, we'll answer that question. Our learning outcome is to demonstrate the differences between deterministic and stochastic sampling strategies through practical experiments. We will explore sampling not as a binary choice, but as a spectrum, and understand the practical trade-offs between speed, reproducibility, and sample quality.

1. The ODE vs. SDE Perspective: Smooth Flow vs. Guided Random Walk

The core difference between deterministic and stochastic samplers can be understood by framing them as solvers for two different types of differential equations.

  • Deterministic Samplers (like DDIM with ) are essentially solving an Ordinary Differential Equation (ODE). They follow a smooth, predictable vector field from a point in the noise space to a point on the data manifold (the space of realistic images).
  • Stochastic Samplers (like DDPM) solve a Stochastic Differential Equation (SDE). They also follow a guiding vector field, but at each step, they add a small amount of random noise. This makes the path a "guided random walk."

This distinction is not just theoretical; it has direct visual consequences.

Deterministic vs. Stochastic Dynamics in Diffusion Models
This image visualizes the difference between a deterministic and a stochastic process. The deterministic flow (left, labeled PF-ODE) follows smooth, predictable streamlines. The stochastic flow (right, labeled Rev-SDE) has more erratic paths due to the injection of randomness at each step.

To build a stronger intuition, let's revisit the 2D spiral example.

But how do AI images and videos actually work? | Guest video by Welch Labs

The guest video by Welch Labs on the 3Blue1Brown channel provides a superb visualization of this exact concept. It shows how the two types of samplers behave when generating data points for a simple 2D spiral.

Please watch from 17:25 to 24:56. As you watch, focus on: DDPM (SDE) Behavior (17:25 - 21:40): Observe the 'chaos' as points are guided by the vector field but also jump around due to added noise. Notice the key conclusion: without this noise, the samples collapse to an 'average', blurry result. DDIM (ODE) Behavior (21:40 - 24:56): See how the points now follow a smooth trajectory. The video explains this is possible due to a different differential equation that yields the same final distribution, but through a deterministic path.

As the video powerfully illustrates, the stochasticity in DDPM is not an arbitrary choice; it's a direct consequence of the underlying theory, preventing the model from collapsing to the mean of the learned distribution. DDIM provides a different theoretical path (the ODE) to arrive at the same target distribution without requiring this randomness, producing a sharp image deterministically.


2. Practical Consequences: Converging vs. Ancestral Samplers

In practice, this theoretical difference manifests in a very concrete way. Samplers are often categorized based on whether their output "converges."

  • Converging Samplers (Deterministic): If you run the sampler with a fixed seed for 20 steps, and then run it again for 50 steps, the 50-step image will look like a more refined version of the 20-step image. The overall composition is fixed early on. Examples include DDIM and DPM++ 2M.
  • Non-Converging / Ancestral Samplers (Stochastic): These samplers add noise at each step (hence "ancestral"). An image generated with 50 steps might look completely different from one generated with 20 steps, even with the same seed. Examples include Euler a and DPM++ 2S a. The 'a' often stands for "ancestral."

Stable Diffusion Samplers - Which samplers are the best and all settings explained!

The YouTube video 'Stable Diffusion Samplers - Which samplers are the best' by SiliconThaumaturgy gives an excellent practical overview of this distinction within a popular UI.

Watch the following segments: Converging vs. Non-converging (2:14 - 3:05): This introduces the core practical difference. Group 1 Samplers (4:57 - 6:38): These are the deterministic, converging samplers. Group 2 Samplers (6:38 - 7:53): These are the stochastic, ancestral samplers. Focus on the relationship between adding noise, the 'a' in the name, and whether the image converges.

So, why have both?

  • Deterministic Pros: Fast, reproducible, and enable features like DDIM inversion for image editing.
  • Stochastic Pros: The added noise can act as a form of error correction. If the deterministic path (ODE) starts to veer off course early on, the error can amplify. The random "kick" from a stochastic sampler can nudge the trajectory back toward a more plausible region of the latent space, sometimes resulting in higher-quality images, especially at a low number of steps.

3. A Practical Experiment: Controlling Stochasticity

We can directly control the amount of stochasticity in many samplers. In DDIM, this is done with the eta () parameter we briefly mentioned last lesson.

  • eta = 0: Fully deterministic DDIM.
  • eta = 1: Fully stochastic, behaving like a DDPM sampler.
  • 0 < eta < 1: A hybrid, semi-stochastic sampler.

This gives you a knob to turn, trading reproducibility for the potential error-correction benefits of noise. Let's see this in action.

Stable Diffusion Samplers - Which samplers are the best and all settings explained!

Let's return to the SiliconThaumaturgy video, which demonstrates exactly how to manipulate this control knob.

Watch from 10:44 to 11:59. This section explains the ETA (or Ada) parameter. Notice how setting ETA to 0 makes an ancestral sampler behave deterministically, and setting DDIM's ETA to a non-zero value makes it behave stochastically.

This video provides the basis for a simple but powerful experiment you can run in any modern diffusion library like diffusers:

  1. Select DDIM sampler with a fixed seed and prompt.
  2. Set eta = 0.0. Generate an image with num_inference_steps=20, then another with num_inference_steps=50. Observe that the images are compositionally identical.
  3. Now, set eta = 1.0. Keep the same seed and prompt. Generate again at 20 and 50 steps. Observe that the images are now completely different, demonstrating stochastic behavior.

This experiment directly shows the practical impact of moving between deterministic and stochastic strategies.

Test your understanding!

You are trying to debug a complex prompt where a specific object is consistently malformed. You are using a fast, deterministic sampler (DDIM, eta=0) with 25 steps. You suspect the sampler might be getting "stuck" in a bad part of the latent space early in the process.

Based on this lesson, what is a simple change you could make to your sampling strategy to test this hypothesis, and what is the underlying mechanism you hope to leverage?

Show answer

A simple change would be to introduce some stochasticity. You could switch to an ancestral sampler (like Euler a) or, more simply, just increase the eta parameter in your DDIM sampler to a value like 0.5 or 1.0.

The underlying mechanism is error correction. By adding random noise at each step, you are no longer following a rigid, deterministic path. This noise can "kick" the sampling trajectory out of the problematic region where the object is being malformed, potentially allowing it to find a better path that leads to a correctly formed object.


4. The Theory Behind the Knob: Churn and SDEs

The concept of injecting a controllable amount of noise is formalized in recent literature. Let's briefly connect our practical experiment back to the underlying theory.

Diffusion Meets Flow Matching

The article 'Diffusion Meets Flow Matching' provides a concise theoretical explanation for this behavior, introducing the term 'churn' and showing the formal SDE equation.

Read the sections 'Diving deeper into samplers' and 'SDE and ODE Perspective'. Focus on these key ideas: 'Deterministic sampler vs. stochastic sampler': This section explains that a stochastic DDPM step is equivalent to a larger deterministic DDIM step followed by a 'renoising' step. This renoising is the source of stochasticity. 'Churn': This is the hyperparameter that controls how much 'renoising' is done. It's conceptually identical to the eta parameter in DDIM. The SDE Equation: In the final section, look at the reverse SDE formula. The parameter η_t is explicitly shown to control the magnitude of the random term. When η_t = 0, the random term vanishes, and you recover a deterministic ODE (like DDIM). When η_t = 1, you recover the stochastic DDPM process.

This connects everything: the practical eta knob in your code is a direct implementation of the term in the formal stochastic differential equation. As an engineer, this gives you a solid theoretical grounding for your experimental choices. You're not just tweaking a random parameter; you're controlling the stochasticity of the differential equation solver.


Conclusion

In this lesson, we explored the crucial differences between deterministic and stochastic sampling strategies. We went beyond the simple "DDIM is fast" view to a more nuanced understanding of the trade-offs.

Key Takeaways:

  • The ODE/SDE Spectrum: Deterministic samplers solve an ODE for a smooth, reproducible path, while stochastic samplers solve an SDE, following a guided random walk.
  • Converging vs. Ancestral: Deterministic samplers converge to a final image composition early, while stochastic (ancestral) samplers change with each step.
  • Error Correction vs. Speed: Stochasticity can help correct errors by "kicking" the sampler out of bad regions, but it comes at the cost of speed and reproducibility. Deterministic sampling is fast and reproducible but can amplify early errors.
  • Practical Control: You can control this trade-off directly using parameters like eta (in DDIM) or by choosing samplers explicitly designed to be stochastic (those often ending in 'a').

Next Lesson Preview:

We've now covered the foundational sampling methods (DDPM, DDIM) and the principles of stochasticity. In the next lesson, we will build on this to explore a state-of-the-art family of samplers: DPM-Solvers. These are highly efficient ODE solvers that are now standard in many diffusion pipelines. We'll see how they provide excellent results in very few steps and how they also incorporate ideas like "churn" to get the best of both worlds.