Skip to content

[training] add an offload utility that can be used as a context manager. #11775

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 10 commits into from
Jul 16, 2025

Conversation

sayakpaul
Copy link
Member

@sayakpaul sayakpaul commented Jun 23, 2025

What does this PR do?

Useful for handling offloading in a simple and easy manner. Currently, in some of our training scripts, we have code patterns like so:

if args.offload:
     vae.to(accelerator.device)

# perform computation
....

if args.offload:
    vae.cpu()

Instead of having those conditionals, this PR introduces an offload_models() method that turns this into a simple block of code:

with offload_models(vae, device=accelerator.device, offload=args.offload):
    pixel_values = batch["pixel_values"].to(dtype=vae.dtype)

@DN6 I am including changes to examples/**/*.py to also trigger the fast PR tests on GPU. Hope, that's okay?

Cc: @a-r-r-o-w for visibility.

@sayakpaul sayakpaul requested a review from linoytsaban June 23, 2025 08:03
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@linoytsaban
Copy link
Collaborator

nice @sayakpaul, thanks! should we also add a test to [test_dreambooth_lora_hidream.py]?(https://github.com/huggingface/diffusers/blob/main/examples/dreambooth/test_dreambooth_lora_hidream.py)

@sayakpaul
Copy link
Member Author

should we also add a test to [test_dreambooth_lora_hidream.py]?

I think it should be added as a utility test under tests/others. Currently, it's a bit non-trivial to test in terms of how the testing suite is designed for the scripts.

@sayakpaul sayakpaul requested a review from DN6 June 23, 2025 10:38
@sayakpaul
Copy link
Member Author

@DN6 okay regarding?

I am including changes to examples/**/*.py to also trigger the fast PR tests on GPU. Hope, that's okay?

@sayakpaul sayakpaul merged commit 9c13f86 into main Jul 16, 2025
30 of 32 checks passed
@sayakpaul sayakpaul deleted the offload-model-utility branch July 16, 2025 08:09
@sayakpaul
Copy link
Member Author

@linoytsaban two things:

tolgacangoz pushed a commit to tolgacangoz/diffusers that referenced this pull request Jul 17, 2025
…er. (huggingface#11775)

* add an offload utility that can be used as a context manager.

* update

---------

Co-authored-by: Linoy Tsaban <57615435+linoytsaban@users.noreply.github.com>
tolgacangoz pushed a commit to tolgacangoz/diffusers that referenced this pull request Jul 18, 2025
…er. (huggingface#11775)

* add an offload utility that can be used as a context manager.

* update

---------

Co-authored-by: Linoy Tsaban <57615435+linoytsaban@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants