-
Notifications
You must be signed in to change notification settings - Fork 6.5k
[docs] Flux group offload #10847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[docs] Flux group offload #10847
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -355,8 +355,65 @@ image.save('flux_ip_adapter_output.jpg') | |
| <figcaption class="mt-2 text-sm text-center text-gray-500">IP-Adapter examples with prompt "wearing sunglasses"</figcaption> | ||
| </div> | ||
|
|
||
| ## Optimize | ||
|
|
||
| ## Running FP16 inference | ||
| Flux is a very large model and requires ~50GB of RAM. Enable some of the optimizations below to lower the memory requirements. | ||
stevhliu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ### Group offloading | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stevhliu it might be a good idea to consider making it more generally available to all major pipelines with high usage. |
||
|
|
||
| [Group offloading](../../optimization/memory#group-offloading) saves memory by offloading groups of internal layers rather than the whole model or weights. Use [`~hooks.apply_group_offloading`] on a model and you can optionally specify the `offload_type`. Setting it to `leaf_level` offloads the lowest leaf-level parameters to the CPU instead of offloading at the module-level. | ||
stevhliu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```py | ||
| import torch | ||
| from diffusers import FluxPipeline | ||
| from diffusers.hooks import apply_group_offloading | ||
|
|
||
| model_id = "black-forest-labs/FLUX.1-dev" | ||
| dtype = torch.bfloat16 | ||
| pipe = FluxPipeline.from_pretrained( | ||
| model_id, | ||
| torch_dtype=dtype, | ||
| ) | ||
|
|
||
| apply_group_offloading( | ||
stevhliu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pipe.transformer, | ||
| offload_type="leaf_level", | ||
| offload_device=torch.device("cpu"), | ||
| onload_device=torch.device("cuda"), | ||
| ) | ||
| apply_group_offloading( | ||
| pipe.text_encoder, | ||
| offload_device=torch.device("cpu"), | ||
| onload_device=torch.device("cuda"), | ||
| offload_type="leaf_level" | ||
| ) | ||
| apply_group_offloading( | ||
| pipe.text_encoder_2, | ||
| offload_device=torch.device("cpu"), | ||
| onload_device=torch.device("cuda"), | ||
| offload_type="leaf_level" | ||
| ) | ||
| apply_group_offloading( | ||
| pipe.vae, | ||
| offload_device=torch.device("cpu"), | ||
| onload_device=torch.device("cuda"), | ||
| offload_type="leaf_level" | ||
| ) | ||
|
|
||
| prompt="A cat wearing sunglasses and working as a lifeguard at pool." | ||
|
|
||
| generator = torch.Generator().manual_seed(181201) | ||
| image = pipe( | ||
| prompt, | ||
| width=576, | ||
| height=1024, | ||
| num_inference_steps=30, | ||
| generator=generator | ||
a-r-r-o-w marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ).images[0] | ||
| image | ||
| ``` | ||
|
|
||
| ### Running FP16 inference | ||
|
|
||
| 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. | ||
|
|
||
|
|
@@ -385,7 +442,7 @@ out = pipe( | |
| out.save("image.png") | ||
| ``` | ||
|
|
||
| ## Quantization | ||
| ### Quantization | ||
|
|
||
| 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. | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!, but the 50 GB of RAM are used when using group offloading not before, also @a-r-r-o-w was going to check if this is the real number or not, I get this but maybe there's something in my env that makes it go that high. It should use around 20GB for the transformer model in theory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, I'll update this number once we get a clearer value from @a-r-r-o-w!