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
{{ message }}
Repository navigation
Commit 2777264
Browse filesBrowse the repository at this point in the historyBrowse files
If is also possible to chain it with attention slicing for minimal memory consumption (< 2GB).
160
+
Note that this method works at the submodule level, not on whole models. This is the best way to minimize memory consumption, but inference is much slower due to the iterative nature of the process. The UNet component of the pipeline runs several times (as many as `num_inference_steps`); each time, the different submodules of the UNet are sequentially onloaded and then offloaded as they are needed, so the number of memory transfers is large.
161
+
162
+
<Tip>
163
+
Consider using <ahref="#model_offloading">model offloading</a> as another point in the optimization space: it will be much faster, but memory savings won't be as large.
164
+
</Tip>
165
+
166
+
It is also possible to chain offloading with attention slicing for minimal memory consumption (< 2GB).
**Note**: When using `enable_sequential_cpu_offload()`, it is important to **not** move the pipeline to CUDA beforehand or else the gain in memory consumption will only be minimal. See [this issue](https://github.com/huggingface/diffusers/issues/1934) for more information.
179
186
187
+
188
+
<aname="model_offloading"></a>
189
+
## Model offloading for fast inference and memory savings
190
+
191
+
[Sequential CPU offloading](#sequential_offloading), as discussed in the previous section, preserves a lot of memory but makes inference slower, because submodules are moved to GPU as needed, and immediately returned to CPU when a new module runs.
192
+
193
+
Full-model offloading is an alternative that moves whole models to the GPU, instead of handling each model's constituent _modules_. This results in a negligible impact on inference time (compared with moving the pipeline to `cuda`), while still providing some memory savings.
194
+
195
+
In this scenario, only one of the main components of the pipeline (typically: text encoder, unet and vae)
196
+
will be in the GPU while the others wait in the CPU. Compoments like the UNet that run for multiple iterations will stay on GPU until they are no longer needed.
197
+
198
+
This feature can be enabled by invoking `enable_model_cpu_offload()` on the pipeline, as shown below.
199
+
200
+
```Python
201
+
import torch
202
+
from diffusers import StableDiffusionPipeline
203
+
204
+
pipe = StableDiffusionPipeline.from_pretrained(
205
+
"runwayml/stable-diffusion-v1-5",
206
+
torch_dtype=torch.float16,
207
+
)
208
+
209
+
prompt ="a photo of an astronaut riding a horse on mars"
210
+
pipe.enable_model_cpu_offload()
211
+
image = pipe(prompt).images[0]
212
+
```
213
+
214
+
This is also compatible with attention slicing for additional memory savings.
215
+
216
+
```Python
217
+
import torch
218
+
from diffusers import StableDiffusionPipeline
219
+
220
+
pipe = StableDiffusionPipeline.from_pretrained(
221
+
"runwayml/stable-diffusion-v1-5",
222
+
torch_dtype=torch.float16,
223
+
)
224
+
225
+
prompt ="a photo of an astronaut riding a horse on mars"
226
+
pipe.enable_model_cpu_offload()
227
+
pipe.enable_attention_slicing(1)
228
+
229
+
image = pipe(prompt).images[0]
230
+
```
231
+
232
+
<Tip>
233
+
This feature requires `accelerate` version 0.17.0 or larger.
234
+
</Tip>
235
+
180
236
## Using Channels Last memory format
181
237
182
238
Channels last memory format is an alternative way of ordering NCHW tensors in memory preserving dimensions ordering. Channels last tensors ordered in such a way that channels become the densest dimension (aka storing images pixel-per-pixel). Since not all operators currently support channels last format it may result in a worst performance, so it's better to try it and see if it works for your model.
0 commit comments