-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
[Accelerate model loading] Fix meta device and super low memory usage #1016
Merged
Merged
Changes from all commits
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 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 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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
|
||
import gc | ||
import random | ||
import time | ||
import unittest | ||
|
||
import numpy as np | ||
|
@@ -730,3 +731,39 @@ def test_callback_fn(step: int, timestep: int, latents: torch.FloatTensor) -> No | |
) | ||
assert test_callback_fn.has_been_called | ||
assert number_of_steps == 51 | ||
|
||
def test_stable_diffusion_accelerate_auto_device(self): | ||
patrickvonplaten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pipeline_id = "CompVis/stable-diffusion-v1-4" | ||
|
||
start_time = time.time() | ||
pipeline_normal_load = StableDiffusionPipeline.from_pretrained( | ||
pipeline_id, revision="fp16", torch_dtype=torch.float16, use_auth_token=True | ||
) | ||
pipeline_normal_load.to(torch_device) | ||
normal_load_time = time.time() - start_time | ||
|
||
start_time = time.time() | ||
_ = StableDiffusionPipeline.from_pretrained( | ||
pipeline_id, revision="fp16", torch_dtype=torch.float16, use_auth_token=True, device_map="auto" | ||
) | ||
meta_device_load_time = time.time() - start_time | ||
|
||
assert 2 * meta_device_load_time < normal_load_time | ||
Comment on lines
+736
to
+751
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. Very cool! |
||
|
||
@unittest.skipIf(torch_device == "cpu", "This test is supposed to run on GPU") | ||
def test_stable_diffusion_pipeline_with_unet_on_gpu_only(self): | ||
torch.cuda.empty_cache() | ||
torch.cuda.reset_max_memory_allocated() | ||
|
||
pipeline_id = "CompVis/stable-diffusion-v1-4" | ||
prompt = "Andromeda galaxy in a bottle" | ||
|
||
pipeline = StableDiffusionPipeline.from_pretrained(pipeline_id, revision="fp16", torch_dtype=torch.float16) | ||
pipeline.enable_attention_slicing(1) | ||
pipeline.enable_sequential_cpu_offload() | ||
|
||
_ = pipeline(prompt, num_inference_steps=5) | ||
|
||
mem_bytes = torch.cuda.max_memory_allocated() | ||
# make sure that less than 1.5 GB is allocated | ||
assert mem_bytes < 1.5 * 10**9 |
This file contains 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,15 +17,12 @@ | |
import os | ||
import random | ||
import tempfile | ||
import tracemalloc | ||
import unittest | ||
|
||
import numpy as np | ||
import torch | ||
|
||
import accelerate | ||
import PIL | ||
import transformers | ||
from diffusers import ( | ||
AutoencoderKL, | ||
DDIMPipeline, | ||
|
@@ -44,8 +41,7 @@ | |
from diffusers.pipeline_utils import DiffusionPipeline | ||
from diffusers.schedulers.scheduling_utils import SCHEDULER_CONFIG_NAME | ||
from diffusers.utils import CONFIG_NAME, WEIGHTS_NAME, floats_tensor, slow, torch_device | ||
from diffusers.utils.testing_utils import CaptureLogger, get_tests_dir, require_torch_gpu | ||
from packaging import version | ||
from diffusers.utils.testing_utils import CaptureLogger, get_tests_dir | ||
from PIL import Image | ||
from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextConfig, CLIPTextModel, CLIPTokenizer | ||
|
||
|
@@ -487,71 +483,3 @@ def test_ddpm_ddim_equality_batched(self): | |
|
||
# the values aren't exactly equal, but the images look the same visually | ||
assert np.abs(ddpm_images - ddim_images).max() < 1e-1 | ||
|
||
@require_torch_gpu | ||
def test_stable_diffusion_accelerate_load_works(self): | ||
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. this test doesn't do anything so let's delete it |
||
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) | ||
|
||
@require_torch_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 | ||
|
||
@slow | ||
@unittest.skipIf(torch_device == "cpu", "This test is supposed to run on GPU") | ||
def test_stable_diffusion_pipeline_with_unet_on_gpu_only(self): | ||
torch.cuda.empty_cache() | ||
torch.cuda.reset_max_memory_allocated() | ||
|
||
pipeline_id = "CompVis/stable-diffusion-v1-4" | ||
prompt = "Andromeda galaxy in a bottle" | ||
|
||
pipeline = StableDiffusionPipeline.from_pretrained( | ||
pipeline_id, revision="fp16", torch_dtype=torch.float32, use_auth_token=True | ||
) | ||
pipeline.cuda_with_minimal_gpu_usage() | ||
|
||
_ = pipeline(prompt) | ||
|
||
mem_bytes = torch.cuda.max_memory_allocated() | ||
# make sure that less than 0.8 GB is allocated | ||
assert mem_bytes < 0.8 * 10**9 |
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.
Great name choice!