Skip to content

Commit db21c97

Browse files
authored
[docs] Flux group offload (#10847)
* flux group-offload * feedback
1 parent 3fdf173 commit db21c97

File tree

1 file changed

+68
-2
lines changed
  • docs/source/en/api/pipelines

1 file changed

+68
-2
lines changed

docs/source/en/api/pipelines/flux.md

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,74 @@ image.save('flux_ip_adapter_output.jpg')
359359
<figcaption class="mt-2 text-sm text-center text-gray-500">IP-Adapter examples with prompt "wearing sunglasses"</figcaption>
360360
</div>
361361

362+
## Optimize
362363

363-
## Running FP16 inference
364+
Flux is a very large model and requires ~50GB of RAM/VRAM to load all the modeling components. Enable some of the optimizations below to lower the memory requirements.
365+
366+
### Group offloading
367+
368+
[Group offloading](../../optimization/memory#group-offloading) lowers VRAM usage by offloading groups of internal layers rather than the whole model or weights. You need to use [`~hooks.apply_group_offloading`] on all the model components of a pipeline. The `offload_type` parameter allows you to toggle between block and leaf-level offloading. Setting it to `leaf_level` offloads the lowest leaf-level parameters to the CPU instead of offloading at the module-level.
369+
370+
On CUDA devices that support asynchronous data streaming, set `use_stream=True` to overlap data transfer and computation to accelerate inference.
371+
372+
> [!TIP]
373+
> It is possible to mix block and leaf-level offloading for different components in a pipeline.
374+
375+
```py
376+
import torch
377+
from diffusers import FluxPipeline
378+
from diffusers.hooks import apply_group_offloading
379+
380+
model_id = "black-forest-labs/FLUX.1-dev"
381+
dtype = torch.bfloat16
382+
pipe = FluxPipeline.from_pretrained(
383+
model_id,
384+
torch_dtype=dtype,
385+
)
386+
387+
apply_group_offloading(
388+
pipe.transformer,
389+
offload_type="leaf_level",
390+
offload_device=torch.device("cpu"),
391+
onload_device=torch.device("cuda"),
392+
use_stream=True,
393+
)
394+
apply_group_offloading(
395+
pipe.text_encoder,
396+
offload_device=torch.device("cpu"),
397+
onload_device=torch.device("cuda"),
398+
offload_type="leaf_level",
399+
use_stream=True,
400+
)
401+
apply_group_offloading(
402+
pipe.text_encoder_2,
403+
offload_device=torch.device("cpu"),
404+
onload_device=torch.device("cuda"),
405+
offload_type="leaf_level",
406+
use_stream=True,
407+
)
408+
apply_group_offloading(
409+
pipe.vae,
410+
offload_device=torch.device("cpu"),
411+
onload_device=torch.device("cuda"),
412+
offload_type="leaf_level",
413+
use_stream=True,
414+
)
415+
416+
prompt="A cat wearing sunglasses and working as a lifeguard at pool."
417+
418+
generator = torch.Generator().manual_seed(181201)
419+
image = pipe(
420+
prompt,
421+
width=576,
422+
height=1024,
423+
num_inference_steps=30,
424+
generator=generator
425+
).images[0]
426+
image
427+
```
428+
429+
### Running FP16 inference
364430

365431
Flux can generate high-quality images with FP16 (i.e. to accelerate inference on Turing/Volta GPUs) but produces different outputs compared to FP32/BF16. The issue is that some activations in the text encoders have to be clipped when running in FP16, which affects the overall image. Forcing text encoders to run with FP32 inference thus removes this output difference. See [here](https://github.com/huggingface/diffusers/pull/9097#issuecomment-2272292516) for details.
366432

@@ -389,7 +455,7 @@ out = pipe(
389455
out.save("image.png")
390456
```
391457

392-
## Quantization
458+
### Quantization
393459

394460
Quantization helps reduce the memory requirements of very large models by storing model weights in a lower precision data type. However, quantization may have varying impact on video quality depending on the video model.
395461

0 commit comments

Comments
 (0)