You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Completely reproducible results are not guaranteed across PyTorch releases, individual commits, or different platforms. Furthermore, results may not be reproducible between CPU and GPU executions, even when using identical seeds.
24
+
25
+
</Tip>
25
26
26
27
## Inference
27
28
28
-
During inference, diffusion pipelines heavily rely on random sampling operations, such as the creating the
29
-
gaussian noise tensors to be denoised and adding noise to the scheduling step.
29
+
During inference, pipelines rely heavily on random sampling operations which include creating the
30
+
Gaussian noise tensors to denoise and adding noise to the scheduling step.
30
31
31
-
Let's have a look at an example. We run the [DDIM pipeline](./api/pipelines/ddim.mdx)
32
-
for just two inference steps and return a numpy tensor to look into the numerical values of the output.
32
+
Take a look at the tensor values in the [`DDIMPipeline`] after two inference steps:
Running the above prints a value of 1464.2076, but running it again prints a different
49
-
value of 1495.1768. What is going on here? Every time the pipeline is run, gaussian noise
50
-
is created and step-wise denoised. To create the gaussian noise with [`torch.randn`](https://pytorch.org/docs/stable/generated/torch.randn.html), a different random seed is taken every time, thus leading to a different result.
51
-
This is a desired property of diffusion pipelines, as it means that the pipeline can create a different random image every time it is run. In many cases, one would like to generate the exact same image of a certain
52
-
run, for which case an instance of a [PyTorch generator](https://pytorch.org/docs/stable/generated/torch.randn.html) has to be passed:
48
+
Running the code above prints one value, but if you run it again you get a different value. What is going on here?
49
+
50
+
Every time the pipeline is run, [`torch.randn`](https://pytorch.org/docs/stable/generated/torch.randn.html) uses a different random seed to create Gaussian noise which is denoised stepwise. This leads to a different result each time it is run, which is great for diffusion pipelines since it generates a different random image each time.
51
+
52
+
But if you need to reliably generate the same image, that'll depend on whether you're running the pipeline on a CPU or GPU.
53
+
54
+
### CPU
55
+
56
+
To generate reproducible results on a CPU, you'll need to use a PyTorch [`Generator`](https://pytorch.org/docs/stable/generated/torch.randn.html) and set a seed:
Running the above always prints a value of 1491.1711 - also upon running it again because we
73
-
define the generator object to be passed to all random functions of the pipeline.
76
+
Now when you run the code above, it always prints a value of `1491.1711` no matter what because the `Generator` object with the seed is passed to all the random functions of the pipeline.
74
77
75
-
If you run this code snippet on your specific hardware and version, you should get a similar, if not the same, result.
78
+
If you run this code example on your specific hardware and PyTorch version, you should get a similar, if not the same, result.
76
79
77
80
<Tip>
78
81
79
-
It might be a bit unintuitive at first to pass `generator` objects to the pipelines instead of
82
+
💡 It might be a bit unintuitive at first to pass `Generator` objects to the pipeline instead of
80
83
just integer values representing the seed, but this is the recommended design when dealing with
81
-
probabilistic models in PyTorch as generators are *random states* that are advanced and can thus be
84
+
probabilistic models in PyTorch as `Generator`'s are *random states* that can be
82
85
passed to multiple pipelines in a sequence.
83
86
84
87
</Tip>
85
88
86
-
Great! Now, we know how to write reproducible pipelines, but it gets a bit trickier since the above example only runs on the CPU. How do we also achieve reproducibility on GPU?
87
-
In short, one should not expect full reproducibility across different hardware when running pipelines on GPU
88
-
as matrix multiplications are less deterministic on GPU than on CPU and diffusion pipelines tend to require
89
-
a lot of matrix multiplications. Let's see what we can do to keep the randomness within limits across
90
-
different GPU hardware.
89
+
### GPU
91
90
92
-
To achieve maximum speed performance, it is recommended to create the generator directly on GPU when running
93
-
the pipeline on GPU:
91
+
Writing a reproducible pipeline on a GPU is a bit trickier, and full reproducibility across different hardware is not guaranteed because matrix multiplication - which diffusion pipelines require a lot of - is less deterministic on a GPU than a CPU. For example, if you run the same code example above on a GPU:
Running the above now prints a value of 1389.8634 - even though we're using the exact same seed!
115
-
This is unfortunate as it means we cannot reproduce the results we achieved on GPU, also on CPU.
116
-
Nevertheless, it should be expected since the GPU uses a different random number generator than the CPU.
112
+
The result is not the same even though you're using an identical seed because the GPU uses a different random number generator than the CPU.
113
+
114
+
To circumvent this problem, 🧨 Diffusers has a [`randn_tensor`](#diffusers.utils.randn_tensor) function for creating random noise on the CPU, and then moving the tensor to a GPU if necessary. The `randn_tensor` function is used everywhere inside the pipeline, allowing the user to **always** pass a CPU`Generator` even if the pipeline is run on a GPU.
117
115
118
-
To circumvent this problem, we created a [`randn_tensor`](#diffusers.utils.randn_tensor) function, which can create random noise
119
-
on the CPU and then move the tensor to GPU if necessary. The function is used everywhere inside the pipelines allowing the user to **always** pass a CPU generator even if the pipeline is run on GPU:
Ok, the last images has some double eyes, but the first image looks good!
56
-
Let's try to make the prompt a bit better **while keeping the first seed**
57
-
so that the images are similar to the first image.
49
+
In this example, you'll improve upon the first image - but in reality, you can use any image you want (even the image with double sets of eyes!). The first image used the `Generator` with seed `0`, so you'll reuse that `Generator` for the second round of inference. To improve the quality of the image, add some additional text to the prompt:
58
50
59
51
```python
60
52
prompt = [prompt + t for t in [", highly realistic", ", artsy", ", trending", ", colorful"]]
61
53
generator = [torch.Generator(device="cuda").manual_seed(0) for i inrange(4)]
62
54
```
63
55
64
-
We create 4 generators with seed `0`, which is the first seed we used before.
65
-
66
-
Let's run the pipeline again.
56
+
Create four generators with seed `0`, and generate another batch of images, all of which should look like the first image from the previous round!
0 commit comments