Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
renzhc
diffusers_dcu
Commits
ba59e92f
Unverified
Commit
ba59e92f
authored
Sep 27, 2023
by
Dhruv Nair
Committed by
GitHub
Sep 27, 2023
Browse files
Fix memory issues in tests (#5183)
* fix memory issues * set _offload_gpu_id * set gpu offload id
parent
02247d9c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
36 additions
and
19 deletions
+36
-19
src/diffusers/pipelines/pipeline_utils.py
src/diffusers/pipelines/pipeline_utils.py
+2
-2
tests/pipelines/stable_diffusion_2/test_stable_diffusion_diffedit.py
...ines/stable_diffusion_2/test_stable_diffusion_diffedit.py
+15
-7
tests/pipelines/stable_diffusion_2/test_stable_diffusion_v_pred.py
...elines/stable_diffusion_2/test_stable_diffusion_v_pred.py
+12
-5
tests/pipelines/text_to_video/test_video_to_video.py
tests/pipelines/text_to_video/test_video_to_video.py
+7
-5
No files found.
src/diffusers/pipelines/pipeline_utils.py
View file @
ba59e92f
...
@@ -1360,7 +1360,7 @@ class DiffusionPipeline(ConfigMixin, PushToHubMixin):
...
@@ -1360,7 +1360,7 @@ class DiffusionPipeline(ConfigMixin, PushToHubMixin):
)
)
# _offload_gpu_id should be set to passed gpu_id (or id in passed `device`) or default to previously set id or default to 0
# _offload_gpu_id should be set to passed gpu_id (or id in passed `device`) or default to previously set id or default to 0
self
.
_offload_gpu_id
=
gpu_id
or
torch_device
.
index
or
self
.
_offload_gpu_id
or
0
self
.
_offload_gpu_id
=
gpu_id
or
torch_device
.
index
or
getattr
(
self
,
"
_offload_gpu_id
"
,
0
)
device_type
=
torch_device
.
type
device_type
=
torch_device
.
type
device
=
torch
.
device
(
f
"
{
device_type
}
:
{
self
.
_offload_gpu_id
}
"
)
device
=
torch
.
device
(
f
"
{
device_type
}
:
{
self
.
_offload_gpu_id
}
"
)
...
@@ -1445,7 +1445,7 @@ class DiffusionPipeline(ConfigMixin, PushToHubMixin):
...
@@ -1445,7 +1445,7 @@ class DiffusionPipeline(ConfigMixin, PushToHubMixin):
)
)
# _offload_gpu_id should be set to passed gpu_id (or id in passed `device`) or default to previously set id or default to 0
# _offload_gpu_id should be set to passed gpu_id (or id in passed `device`) or default to previously set id or default to 0
self
.
_offload_gpu_id
=
gpu_id
or
torch_device
.
index
or
self
.
_offload_gpu_id
or
0
self
.
_offload_gpu_id
=
gpu_id
or
torch_device
.
index
or
getattr
(
self
,
"
_offload_gpu_id
"
,
0
)
device_type
=
torch_device
.
type
device_type
=
torch_device
.
type
device
=
torch
.
device
(
f
"
{
device_type
}
:
{
self
.
_offload_gpu_id
}
"
)
device
=
torch
.
device
(
f
"
{
device_type
}
:
{
self
.
_offload_gpu_id
}
"
)
...
...
tests/pipelines/stable_diffusion_2/test_stable_diffusion_diffedit.py
View file @
ba59e92f
...
@@ -37,6 +37,7 @@ from diffusers.utils.testing_utils import (
...
@@ -37,6 +37,7 @@ from diffusers.utils.testing_utils import (
floats_tensor
,
floats_tensor
,
load_image
,
load_image
,
nightly
,
nightly
,
numpy_cosine_similarity_distance
,
require_torch_gpu
,
require_torch_gpu
,
slow
,
slow
,
torch_device
,
torch_device
,
...
@@ -303,8 +304,7 @@ class StableDiffusionDiffEditPipelineIntegrationTests(unittest.TestCase):
...
@@ -303,8 +304,7 @@ class StableDiffusionDiffEditPipelineIntegrationTests(unittest.TestCase):
raw_image
=
load_image
(
raw_image
=
load_image
(
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/diffedit/fruit.png"
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/diffedit/fruit.png"
)
)
raw_image
=
raw_image
.
convert
(
"RGB"
).
resize
((
256
,
256
))
raw_image
=
raw_image
.
convert
(
"RGB"
).
resize
((
768
,
768
))
cls
.
raw_image
=
raw_image
cls
.
raw_image
=
raw_image
...
@@ -312,9 +312,11 @@ class StableDiffusionDiffEditPipelineIntegrationTests(unittest.TestCase):
...
@@ -312,9 +312,11 @@ class StableDiffusionDiffEditPipelineIntegrationTests(unittest.TestCase):
generator
=
torch
.
manual_seed
(
0
)
generator
=
torch
.
manual_seed
(
0
)
pipe
=
StableDiffusionDiffEditPipeline
.
from_pretrained
(
pipe
=
StableDiffusionDiffEditPipeline
.
from_pretrained
(
"stabilityai/stable-diffusion-2-1"
,
safety_checker
=
None
,
torch_dtype
=
torch
.
float16
"stabilityai/stable-diffusion-2-1
-base
"
,
safety_checker
=
None
,
torch_dtype
=
torch
.
float16
)
)
pipe
.
scheduler
=
DDIMScheduler
.
from_config
(
pipe
.
scheduler
.
config
)
pipe
.
scheduler
=
DDIMScheduler
.
from_config
(
pipe
.
scheduler
.
config
)
pipe
.
scheduler
.
clip_sample
=
True
pipe
.
inverse_scheduler
=
DDIMInverseScheduler
.
from_config
(
pipe
.
scheduler
.
config
)
pipe
.
inverse_scheduler
=
DDIMInverseScheduler
.
from_config
(
pipe
.
scheduler
.
config
)
pipe
.
enable_model_cpu_offload
()
pipe
.
enable_model_cpu_offload
()
pipe
.
set_progress_bar_config
(
disable
=
None
)
pipe
.
set_progress_bar_config
(
disable
=
None
)
...
@@ -330,7 +332,11 @@ class StableDiffusionDiffEditPipelineIntegrationTests(unittest.TestCase):
...
@@ -330,7 +332,11 @@ class StableDiffusionDiffEditPipelineIntegrationTests(unittest.TestCase):
)
)
inv_latents
=
pipe
.
invert
(
inv_latents
=
pipe
.
invert
(
prompt
=
source_prompt
,
image
=
self
.
raw_image
,
inpaint_strength
=
0.7
,
generator
=
generator
prompt
=
source_prompt
,
image
=
self
.
raw_image
,
inpaint_strength
=
0.7
,
generator
=
generator
,
num_inference_steps
=
5
,
).
latents
).
latents
image
=
pipe
(
image
=
pipe
(
...
@@ -340,7 +346,8 @@ class StableDiffusionDiffEditPipelineIntegrationTests(unittest.TestCase):
...
@@ -340,7 +346,8 @@ class StableDiffusionDiffEditPipelineIntegrationTests(unittest.TestCase):
generator
=
generator
,
generator
=
generator
,
negative_prompt
=
source_prompt
,
negative_prompt
=
source_prompt
,
inpaint_strength
=
0.7
,
inpaint_strength
=
0.7
,
output_type
=
"numpy"
,
num_inference_steps
=
5
,
output_type
=
"np"
,
).
images
[
0
]
).
images
[
0
]
expected_image
=
(
expected_image
=
(
...
@@ -348,11 +355,12 @@ class StableDiffusionDiffEditPipelineIntegrationTests(unittest.TestCase):
...
@@ -348,11 +355,12 @@ class StableDiffusionDiffEditPipelineIntegrationTests(unittest.TestCase):
load_image
(
load_image
(
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
"/diffedit/pears.png"
"/diffedit/pears.png"
).
resize
((
768
,
768
))
).
resize
((
256
,
256
))
)
)
/
255
/
255
)
)
assert
np
.
abs
((
expected_image
-
image
).
max
())
<
5e-1
assert
numpy_cosine_similarity_distance
(
expected_image
.
flatten
(),
image
.
flatten
())
<
2e-1
@
nightly
@
nightly
...
...
tests/pipelines/stable_diffusion_2/test_stable_diffusion_v_pred.py
View file @
ba59e92f
...
@@ -405,13 +405,20 @@ class StableDiffusion2VPredictionPipelineIntegrationTests(unittest.TestCase):
...
@@ -405,13 +405,20 @@ class StableDiffusion2VPredictionPipelineIntegrationTests(unittest.TestCase):
pipe
.
scheduler
.
config
,
timestep_spacing
=
"trailing"
,
rescale_betas_zero_snr
=
True
pipe
.
scheduler
.
config
,
timestep_spacing
=
"trailing"
,
rescale_betas_zero_snr
=
True
)
)
pipe
.
to
(
torch_device
)
pipe
.
to
(
torch_device
)
pipe
.
enable_
attention_slicing
()
pipe
.
enable_
model_cpu_offload
()
pipe
.
set_progress_bar_config
(
disable
=
None
)
pipe
.
set_progress_bar_config
(
disable
=
None
)
prompt
=
"A lion in galaxies, spirals, nebulae, stars, smoke, iridescent, intricate detail, octane render, 8k"
prompt
=
"A lion in galaxies, spirals, nebulae, stars, smoke, iridescent, intricate detail, octane render, 8k"
generator
=
torch
.
Generator
(
"cpu"
).
manual_seed
(
0
)
generator
=
torch
.
Generator
(
"cpu"
).
manual_seed
(
0
)
output
=
pipe
(
prompt
=
prompt
,
guidance_scale
=
7.5
,
guidance_rescale
=
0.7
,
generator
=
generator
,
output_type
=
"np"
)
output
=
pipe
(
prompt
=
prompt
,
guidance_scale
=
7.5
,
num_inference_steps
=
10
,
guidance_rescale
=
0.7
,
generator
=
generator
,
output_type
=
"np"
,
)
image
=
output
.
images
[
0
]
image
=
output
.
images
[
0
]
assert
image
.
shape
==
(
768
,
768
,
3
)
assert
image
.
shape
==
(
768
,
768
,
3
)
...
@@ -443,7 +450,7 @@ class StableDiffusion2VPredictionPipelineIntegrationTests(unittest.TestCase):
...
@@ -443,7 +450,7 @@ class StableDiffusion2VPredictionPipelineIntegrationTests(unittest.TestCase):
pipe
=
StableDiffusionPipeline
.
from_single_file
(
filename
,
torch_dtype
=
torch
.
float16
)
pipe
=
StableDiffusionPipeline
.
from_single_file
(
filename
,
torch_dtype
=
torch
.
float16
)
pipe
.
scheduler
=
DDIMScheduler
.
from_config
(
pipe
.
scheduler
.
config
)
pipe
.
scheduler
=
DDIMScheduler
.
from_config
(
pipe
.
scheduler
.
config
)
pipe
.
to
(
"cuda"
)
pipe
.
enable_model_cpu_offload
(
)
image_out
=
pipe
(
"test"
,
num_inference_steps
=
1
,
output_type
=
"np"
).
images
[
0
]
image_out
=
pipe
(
"test"
,
num_inference_steps
=
1
,
output_type
=
"np"
).
images
[
0
]
...
@@ -460,7 +467,7 @@ class StableDiffusion2VPredictionPipelineIntegrationTests(unittest.TestCase):
...
@@ -460,7 +467,7 @@ class StableDiffusion2VPredictionPipelineIntegrationTests(unittest.TestCase):
pipe_single
.
enable_model_cpu_offload
()
pipe_single
.
enable_model_cpu_offload
()
generator
=
torch
.
Generator
(
device
=
"cpu"
).
manual_seed
(
0
)
generator
=
torch
.
Generator
(
device
=
"cpu"
).
manual_seed
(
0
)
image_ckpt
=
pipe_single
(
"a turtle"
,
num_inference_steps
=
5
,
generator
=
generator
,
output_type
=
"np"
).
images
[
0
]
image_ckpt
=
pipe_single
(
"a turtle"
,
num_inference_steps
=
2
,
generator
=
generator
,
output_type
=
"np"
).
images
[
0
]
pipe
=
StableDiffusionPipeline
.
from_pretrained
(
"stabilityai/stable-diffusion-2-1"
)
pipe
=
StableDiffusionPipeline
.
from_pretrained
(
"stabilityai/stable-diffusion-2-1"
)
pipe
.
scheduler
=
DDIMScheduler
.
from_config
(
pipe
.
scheduler
.
config
)
pipe
.
scheduler
=
DDIMScheduler
.
from_config
(
pipe
.
scheduler
.
config
)
...
@@ -468,7 +475,7 @@ class StableDiffusion2VPredictionPipelineIntegrationTests(unittest.TestCase):
...
@@ -468,7 +475,7 @@ class StableDiffusion2VPredictionPipelineIntegrationTests(unittest.TestCase):
pipe
.
enable_model_cpu_offload
()
pipe
.
enable_model_cpu_offload
()
generator
=
torch
.
Generator
(
device
=
"cpu"
).
manual_seed
(
0
)
generator
=
torch
.
Generator
(
device
=
"cpu"
).
manual_seed
(
0
)
image
=
pipe
(
"a turtle"
,
num_inference_steps
=
5
,
generator
=
generator
,
output_type
=
"np"
).
images
[
0
]
image
=
pipe
(
"a turtle"
,
num_inference_steps
=
2
,
generator
=
generator
,
output_type
=
"np"
).
images
[
0
]
max_diff
=
numpy_cosine_similarity_distance
(
image
.
flatten
(),
image_ckpt
.
flatten
())
max_diff
=
numpy_cosine_similarity_distance
(
image
.
flatten
(),
image_ckpt
.
flatten
())
assert
max_diff
<
1e-3
assert
max_diff
<
1e-3
...
...
tests/pipelines/text_to_video/test_video_to_video.py
View file @
ba59e92f
...
@@ -31,6 +31,7 @@ from diffusers.utils.testing_utils import (
...
@@ -31,6 +31,7 @@ from diffusers.utils.testing_utils import (
enable_full_determinism
,
enable_full_determinism
,
floats_tensor
,
floats_tensor
,
is_flaky
,
is_flaky
,
numpy_cosine_similarity_distance
,
skip_mps
,
skip_mps
,
slow
,
slow
,
torch_device
,
torch_device
,
...
@@ -198,17 +199,18 @@ class VideoToVideoSDPipelineFastTests(PipelineTesterMixin, unittest.TestCase):
...
@@ -198,17 +199,18 @@ class VideoToVideoSDPipelineFastTests(PipelineTesterMixin, unittest.TestCase):
@
skip_mps
@
skip_mps
class
VideoToVideoSDPipelineSlowTests
(
unittest
.
TestCase
):
class
VideoToVideoSDPipelineSlowTests
(
unittest
.
TestCase
):
def
test_two_step_model
(
self
):
def
test_two_step_model
(
self
):
pipe
=
VideoToVideoSDPipeline
.
from_pretrained
(
"cerspense/zeroscope_v2_
XL
"
,
torch_dtype
=
torch
.
float16
)
pipe
=
VideoToVideoSDPipeline
.
from_pretrained
(
"cerspense/zeroscope_v2_
576w
"
,
torch_dtype
=
torch
.
float16
)
pipe
.
enable_model_cpu_offload
()
pipe
.
enable_model_cpu_offload
()
# 10 frames
# 10 frames
generator
=
torch
.
Generator
(
device
=
"cpu"
).
manual_seed
(
0
)
generator
=
torch
.
Generator
(
device
=
"cpu"
).
manual_seed
(
0
)
video
=
torch
.
randn
((
1
,
10
,
3
,
1024
,
576
),
generator
=
generator
)
video
=
torch
.
randn
((
1
,
10
,
3
,
320
,
576
),
generator
=
generator
)
video
=
video
.
to
(
"cuda"
)
prompt
=
"Spiderman is surfing"
prompt
=
"Spiderman is surfing"
video_frames
=
pipe
(
prompt
,
video
=
video
,
generator
=
generator
,
num_inference_steps
=
3
,
output_type
=
"pt"
).
frames
video_frames
=
pipe
(
prompt
,
video
=
video
,
generator
=
generator
,
num_inference_steps
=
3
,
output_type
=
"pt"
).
frames
expected_array
=
np
.
array
([
-
1.0458984
,
-
1.1279297
,
-
0.9663086
,
-
0.91503906
,
-
0.75097656
])
expected_array
=
np
.
array
([
-
0.9770508
,
-
0.8027344
,
-
0.62646484
,
-
0.8334961
,
-
0.7573242
])
assert
np
.
abs
(
video_frames
.
cpu
().
numpy
()[
0
,
0
,
0
,
0
,
-
5
:]
-
expected_array
).
sum
()
<
1e-2
output_array
=
video_frames
.
cpu
().
numpy
()[
0
,
0
,
0
,
0
,
-
5
:]
assert
numpy_cosine_similarity_distance
(
expected_array
,
output_array
)
<
1e-2
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment