Skip to content

Conversation

@JoeGaffney
Copy link
Contributor

@JoeGaffney JoeGaffney commented Jul 26, 2025

What does this PR do?

Added optimisation to not attempt to move devices if already on that the device. This is more noticeable in large step iterations on diffusion loops when the pre_forward can get called many times

Possibly the there is a better way than using next() and noticed it's possibly to set the prev_module_hook to invalid object so added an additional check. Glad for any feedback to improve. Or perhaps there's an even better way.

Fixes # (issue)

Issue was not as bad as I initially thought. But from speaking with maintainers there was some smaller gains found. Mainly noticeable in large loops

huggingface/diffusers#11872

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

…y on that the device. This is more noticiable in large step itterations on diffusion loops when the pre_froward can get called many times
@JoeGaffney JoeGaffney marked this pull request as ready for review July 26, 2025 14:29
@S1ro1
Copy link
Contributor

S1ro1 commented Aug 2, 2025

cc @SunMarc

Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ! I'll merge this now for the release

if self.prev_module_hook is not None:
self.prev_module_hook.offload()
clear_device_cache()
if self.prev_module_hook is not None and isinstance(self.prev_module_hook, UserCpuOffloadHook):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be fine to not check for isinstance(self.prev_module_hook, UserCpuOffloadHook) as we expect it to be UserCpuOffloadHook in the docstring.

@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.

@SunMarc
Copy link
Member

SunMarc commented Aug 6, 2025

@bot /style

@github-actions
Copy link
Contributor

github-actions bot commented Aug 6, 2025

Style bot fixed some files and pushed the changes.

@SunMarc SunMarc merged commit c0a3aef into huggingface:main Aug 6, 2025
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.

5 participants