-
Notifications
You must be signed in to change notification settings - Fork 6.5k
[Low CPU memory] + device map #772
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
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
0ea501b
add accelerate to load models with smaller memory footprint
piEsposito 7631dd6
remove low_cpu_mem_usage as it is reduntant
piEsposito 973eb23
Merge branch 'main' of github.com:huggingface/diffusers into main
piEsposito 8592e23
move accelerate init weights context to modelling utils
piEsposito 76b8e4a
add test to ensure results are the same when loading with accelerate
piEsposito dd7f9b9
add tests to ensure ram usage gets lower when using accelerate
piEsposito ec5f7aa
move accelerate logic to single snippet under modelling utils and rem…
piEsposito ae5f56d
Merge branch 'huggingface:main' into main
piEsposito 8392e3f
format code using to pass quality check
piEsposito 615054a
fix imports with isor
piEsposito 75c08a9
add accelerate to test extra deps
piEsposito 7e06f3d
Merge branch 'main' into main
piEsposito 6189b86
only import accelerate if device_map is set to auto
piEsposito 02818b5
Merge branch 'main' of github.com:piEsposito/diffusers into main
piEsposito dc14ace
Merge branch 'main' of github.com:huggingface/diffusers into main
piEsposito bc51061
move accelerate availability check to diffusers import utils
piEsposito ad1b55d
Merge remote-tracking branch 'upstream/main' into main
piEsposito e020d73
format code
piEsposito c3778bb
Merge branch 'main' into main
piEsposito 0e2319d
Merge branch 'main' into main
piEsposito 25e07d8
Merge branch 'main' into main
patrickvonplaten 6206595
add device map to pipeline abstraction
piEsposito 8912f53
lint it to pass PR quality check
piEsposito fd8829f
fix class check to use accelerate when using diffusers ModelMixin sub…
piEsposito 85c2442
Merge branch 'main' of https://github.com/piEsposito/diffusers into main
patrickvonplaten 8132cfd
use low_cpu_mem_usage in transformers if device_map is not available
piEsposito fe251f3
Merge branch 'main' into main
piEsposito 4f7e319
Merge branch 'main' of https://github.com/piEsposito/diffusers into main
patrickvonplaten 06adc23
Merge branch 'main' into main
piEsposito 1fd2ea4
NoModuleLayer
patrickvonplaten c2d9a84
comment out tests
patrickvonplaten a7bb7f8
up
patrickvonplaten 0a9bcd9
uP
patrickvonplaten 6d0bbba
finish
patrickvonplaten e599033
Update src/diffusers/pipelines/stable_diffusion/safety_checker.py
patrickvonplaten dfaabd6
finish
patrickvonplaten 3d406af
Merge branch 'piEspositoMain' of https://github.com/huggingface/diffu…
patrickvonplaten bbc05c5
uP
patrickvonplaten 91f8a59
Merge branch 'main' into piEspositoMain
patrickvonplaten dcf7e0f
make style
patrickvonplaten 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
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
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 |
|---|---|---|
|
|
@@ -17,12 +17,15 @@ | |
| import os | ||
| import random | ||
| import tempfile | ||
| import tracemalloc | ||
| import unittest | ||
|
|
||
| import numpy as np | ||
| import torch | ||
|
|
||
| import accelerate | ||
|
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. Need to sort the imports to make the linter happy, running isort locally gives this order, |
||
| import PIL | ||
| import transformers | ||
|
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. see above comment for isort import grouping and ordering. |
||
| from diffusers import ( | ||
| AutoencoderKL, | ||
| DDIMPipeline, | ||
|
|
@@ -50,6 +53,7 @@ | |
| from diffusers.schedulers.scheduling_utils import SCHEDULER_CONFIG_NAME | ||
| from diffusers.utils import CONFIG_NAME, WEIGHTS_NAME, floats_tensor, load_image, slow, torch_device | ||
| from diffusers.utils.testing_utils import get_tests_dir | ||
| from packaging import version | ||
| from PIL import Image | ||
| from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextConfig, CLIPTextModel, CLIPTokenizer | ||
|
|
||
|
|
@@ -2034,3 +2038,53 @@ def test_callback_fn(step: int, timestep: int, latents: np.ndarray) -> None: | |
| pipe(prompt=prompt, num_inference_steps=5, guidance_scale=7.5, callback=test_callback_fn, callback_steps=1) | ||
| assert test_callback_fn.has_been_called | ||
| assert number_of_steps == 6 | ||
|
|
||
| @slow | ||
| @unittest.skipIf(torch_device == "cpu", "Stable diffusion is supposed to run on GPU") | ||
| def test_stable_diffusion_accelerate_load_works(self): | ||
| if version.parse(version.parse(transformers.__version__).base_version) < version.parse("4.23"): | ||
| return | ||
|
|
||
| if version.parse(version.parse(accelerate.__version__).base_version) < version.parse("0.14"): | ||
| return | ||
|
|
||
| model_id = "CompVis/stable-diffusion-v1-4" | ||
| _ = StableDiffusionPipeline.from_pretrained( | ||
| model_id, revision="fp16", torch_dtype=torch.float16, use_auth_token=True, device_map="auto" | ||
| ).to(torch_device) | ||
|
|
||
| @slow | ||
| @unittest.skipIf(torch_device == "cpu", "This test is supposed to run on GPU") | ||
| def test_stable_diffusion_accelerate_load_reduces_memory_footprint(self): | ||
| if version.parse(version.parse(transformers.__version__).base_version) < version.parse("4.23"): | ||
| return | ||
|
|
||
| if version.parse(version.parse(accelerate.__version__).base_version) < version.parse("0.14"): | ||
| return | ||
|
|
||
| pipeline_id = "CompVis/stable-diffusion-v1-4" | ||
|
|
||
| torch.cuda.empty_cache() | ||
| gc.collect() | ||
|
|
||
| tracemalloc.start() | ||
| pipeline_normal_load = StableDiffusionPipeline.from_pretrained( | ||
| pipeline_id, revision="fp16", torch_dtype=torch.float16, use_auth_token=True | ||
| ) | ||
| pipeline_normal_load.to(torch_device) | ||
| _, peak_normal = tracemalloc.get_traced_memory() | ||
| tracemalloc.stop() | ||
|
|
||
| del pipeline_normal_load | ||
| torch.cuda.empty_cache() | ||
| gc.collect() | ||
|
|
||
| tracemalloc.start() | ||
| _ = StableDiffusionPipeline.from_pretrained( | ||
| pipeline_id, revision="fp16", torch_dtype=torch.float16, use_auth_token=True, device_map="auto" | ||
| ) | ||
| _, peak_accelerate = tracemalloc.get_traced_memory() | ||
|
|
||
| tracemalloc.stop() | ||
|
|
||
| assert peak_accelerate < peak_normal | ||
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.
needed to allow to use
device_map="auto"