Skip to content

Conversation

@patrickvonplaten
Copy link
Contributor

@patrickvonplaten patrickvonplaten commented Jul 15, 2023

Refactor enable_sequential_cpu_offload and _execution_device

Every pipeline should be able to use cpu_offload in a very non-customized way as essentially we just need to wrap all torch.nn.Module componets into the cpu_offload function. Similarly can we determine the _execution_device by looping over all torch.nn.Module components.

Therefore this PR:

  • adds enable_sequantial_cpu_offload and _execution_device to DiffusionPipeline
  • adds a _exclude_from_cpu_offload class list to classes where certain specific components cannot be offloaded because of weird parameter structure
  • makes sure that every pipeline can run cpu offload
  • partly fixes Add gpu option to cpu offload #3990 as this way we just need to apply the fix to a single funtion
  • Remove >2000 LOC trying to make @williamberman happy

I tested this PR on GPU by running:

pytest tests -k "cpu_offload"

return torch.device(module._hf_hook.execution_device)
return self.device

def enable_sequential_cpu_offload(self, gpu_id: int = 0, device: Union[torch.device, str] = "cuda"):
Copy link
Contributor Author

@patrickvonplaten patrickvonplaten Jul 15, 2023

Choose a reason for hiding this comment

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

Is general enough to work with all models. Also applies suggestion from #3990

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Jul 15, 2023

The documentation is not available anymore as the PR was closed or merged.

Copy link
Contributor

@williamberman williamberman left a comment

Choose a reason for hiding this comment

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

Nice, this makes a ton of sense! We should be sure to monitor the integration tests

def _execution_device(self):
r"""
Returns the device on which the pipeline's models will be executed. After calling
`pipeline.enable_sequential_cpu_offload()` the execution device can only be inferred from Accelerate's module
Copy link
Member

Choose a reason for hiding this comment

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

Can we format pipeline.enable_sequential_cpu_offload() such that the hyperlink generates properly?


# set these parameters to False in the child class if the pipeline does not support the corresponding functionality
test_attention_slicing = True
test_cpu_offload = True
Copy link
Member

Choose a reason for hiding this comment

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

Why are we discarding this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We always test it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We completely remove the test_cpu_offload flag because every pipeline is now tested

Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

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

Thanks for cleaning up!

Copy link
Collaborator

@yiyixuxu yiyixuxu left a comment

Choose a reason for hiding this comment

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

Thanks!

@patrickvonplaten patrickvonplaten merged commit 27062c3 into main Jul 18, 2023
@patrickvonplaten patrickvonplaten deleted the refactor_execution_device branch July 18, 2023 09:04
orpatashnik pushed a commit to orpatashnik/diffusers that referenced this pull request Aug 1, 2023
* create general cpu offload & execution device

* Remove boiler plate

* finish

* kp

* Correct offload more pipelines

* up

* Update src/diffusers/pipelines/pipeline_utils.py

* make style

* up
orpatashnik pushed a commit to orpatashnik/diffusers that referenced this pull request Aug 1, 2023
* create general cpu offload & execution device

* Remove boiler plate

* finish

* kp

* Correct offload more pipelines

* up

* Update src/diffusers/pipelines/pipeline_utils.py

* make style

* up
orpatashnik pushed a commit to orpatashnik/diffusers that referenced this pull request Aug 1, 2023
* create general cpu offload & execution device

* Remove boiler plate

* finish

* kp

* Correct offload more pipelines

* up

* Update src/diffusers/pipelines/pipeline_utils.py

* make style

* up
yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
* create general cpu offload & execution device

* Remove boiler plate

* finish

* kp

* Correct offload more pipelines

* up

* Update src/diffusers/pipelines/pipeline_utils.py

* make style

* up
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
* create general cpu offload & execution device

* Remove boiler plate

* finish

* kp

* Correct offload more pipelines

* up

* Update src/diffusers/pipelines/pipeline_utils.py

* make style

* up
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.

6 participants