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
OpenDAS
diffusers
Commits
6cf72a9b
Unverified
Commit
6cf72a9b
authored
Nov 09, 2022
by
Patrick von Platen
Committed by
GitHub
Nov 09, 2022
Browse files
Fix slow tests (#1210)
* fix tests * Fix more * more
parent
24895a1f
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
40 additions
and
26 deletions
+40
-26
src/diffusers/pipelines/stable_diffusion/pipeline_cycle_diffusion.py
...rs/pipelines/stable_diffusion/pipeline_cycle_diffusion.py
+5
-3
src/diffusers/schedulers/scheduling_ddim.py
src/diffusers/schedulers/scheduling_ddim.py
+1
-1
src/diffusers/schedulers/scheduling_euler_ancestral_discrete.py
...ffusers/schedulers/scheduling_euler_ancestral_discrete.py
+1
-1
src/diffusers/schedulers/scheduling_euler_discrete.py
src/diffusers/schedulers/scheduling_euler_discrete.py
+1
-1
tests/pipelines/stable_diffusion/test_cycle_diffusion.py
tests/pipelines/stable_diffusion/test_cycle_diffusion.py
+5
-3
tests/pipelines/stable_diffusion/test_stable_diffusion.py
tests/pipelines/stable_diffusion/test_stable_diffusion.py
+2
-2
tests/test_pipelines.py
tests/test_pipelines.py
+4
-3
tests/test_scheduler.py
tests/test_scheduler.py
+21
-12
No files found.
src/diffusers/pipelines/stable_diffusion/pipeline_cycle_diffusion.py
View file @
6cf72a9b
...
...
@@ -43,7 +43,7 @@ def preprocess(image):
return
2.0
*
image
-
1.0
def
posterior_sample
(
scheduler
,
latents
,
timestep
,
clean_latents
,
eta
):
def
posterior_sample
(
scheduler
,
latents
,
timestep
,
clean_latents
,
generator
,
eta
):
# 1. get previous step value (=t-1)
prev_timestep
=
timestep
-
scheduler
.
config
.
num_train_timesteps
//
scheduler
.
num_inference_steps
...
...
@@ -62,7 +62,9 @@ def posterior_sample(scheduler, latents, timestep, clean_latents, eta):
# direction pointing to x_t
e_t
=
(
latents
-
alpha_prod_t
**
(
0.5
)
*
clean_latents
)
/
(
1
-
alpha_prod_t
)
**
(
0.5
)
dir_xt
=
(
1.0
-
alpha_prod_t_prev
-
std_dev_t
**
2
)
**
(
0.5
)
*
e_t
noise
=
std_dev_t
*
torch
.
randn
(
clean_latents
.
shape
,
dtype
=
clean_latents
.
dtype
,
device
=
clean_latents
.
device
)
noise
=
std_dev_t
*
torch
.
randn
(
clean_latents
.
shape
,
dtype
=
clean_latents
.
dtype
,
device
=
clean_latents
.
device
,
generator
=
generator
)
prev_latents
=
alpha_prod_t_prev
**
(
0.5
)
*
clean_latents
+
dir_xt
+
noise
return
prev_latents
...
...
@@ -499,7 +501,7 @@ class CycleDiffusionPipeline(DiffusionPipeline):
# Sample source_latents from the posterior distribution.
prev_source_latents
=
posterior_sample
(
self
.
scheduler
,
source_latents
,
t
,
clean_latents
,
**
extra_step_kwargs
self
.
scheduler
,
source_latents
,
t
,
clean_latents
,
generator
=
generator
,
**
extra_step_kwargs
)
# Compute noise.
noise
=
compute_noise
(
...
...
src/diffusers/schedulers/scheduling_ddim.py
View file @
6cf72a9b
...
...
@@ -288,7 +288,7 @@ class DDIMScheduler(SchedulerMixin, ConfigMixin):
if
eta
>
0
:
# randn_like does not support generator https://github.com/pytorch/pytorch/issues/27072
device
=
model_output
.
device
if
torch
.
is_tensor
(
model_output
)
else
torch
.
device
(
"cpu"
)
device
=
model_output
.
device
if
variance_noise
is
not
None
and
generator
is
not
None
:
raise
ValueError
(
"Cannot pass both generator and variance_noise. Please make sure that either `generator` or"
...
...
src/diffusers/schedulers/scheduling_euler_ancestral_discrete.py
View file @
6cf72a9b
...
...
@@ -221,7 +221,7 @@ class EulerAncestralDiscreteScheduler(SchedulerMixin, ConfigMixin):
prev_sample
=
sample
+
derivative
*
dt
device
=
model_output
.
device
if
torch
.
is_tensor
(
model_output
)
else
torch
.
device
(
"cpu"
)
device
=
model_output
.
device
if
device
.
type
==
"mps"
:
# randn does not work reproducibly on mps
noise
=
torch
.
randn
(
model_output
.
shape
,
dtype
=
model_output
.
dtype
,
device
=
"cpu"
,
generator
=
generator
).
to
(
...
...
src/diffusers/schedulers/scheduling_euler_discrete.py
View file @
6cf72a9b
...
...
@@ -218,7 +218,7 @@ class EulerDiscreteScheduler(SchedulerMixin, ConfigMixin):
gamma
=
min
(
s_churn
/
(
len
(
self
.
sigmas
)
-
1
),
2
**
0.5
-
1
)
if
s_tmin
<=
sigma
<=
s_tmax
else
0.0
device
=
model_output
.
device
if
torch
.
is_tensor
(
model_output
)
else
torch
.
device
(
"cpu"
)
device
=
model_output
.
device
if
device
.
type
==
"mps"
:
# randn does not work reproducibly on mps
noise
=
torch
.
randn
(
model_output
.
shape
,
dtype
=
model_output
.
dtype
,
device
=
"cpu"
,
generator
=
generator
).
to
(
...
...
tests/pipelines/stable_diffusion/test_cycle_diffusion.py
View file @
6cf72a9b
...
...
@@ -293,7 +293,7 @@ class CycleDiffusionPipelineIntegrationTests(unittest.TestCase):
source_prompt
=
"A black colored car"
prompt
=
"A blue colored car"
torch
.
manual_seed
(
0
)
generator
=
torch
.
Generator
(
device
=
torch_device
)
.
manual_seed
(
0
)
output
=
pipe
(
prompt
=
prompt
,
source_prompt
=
source_prompt
,
...
...
@@ -303,12 +303,13 @@ class CycleDiffusionPipelineIntegrationTests(unittest.TestCase):
strength
=
0.85
,
guidance_scale
=
3
,
source_guidance_scale
=
1
,
generator
=
generator
,
output_type
=
"np"
,
)
image
=
output
.
images
# the values aren't exactly equal, but the images look the same visually
assert
np
.
abs
(
image
-
expected_image
).
max
()
<
1
e-
2
assert
np
.
abs
(
image
-
expected_image
).
max
()
<
5
e-
1
def
test_cycle_diffusion_pipeline
(
self
):
init_image
=
load_image
(
...
...
@@ -331,7 +332,7 @@ class CycleDiffusionPipelineIntegrationTests(unittest.TestCase):
source_prompt
=
"A black colored car"
prompt
=
"A blue colored car"
torch
.
manual_seed
(
0
)
generator
=
torch
.
Generator
(
device
=
torch_device
)
.
manual_seed
(
0
)
output
=
pipe
(
prompt
=
prompt
,
source_prompt
=
source_prompt
,
...
...
@@ -341,6 +342,7 @@ class CycleDiffusionPipelineIntegrationTests(unittest.TestCase):
strength
=
0.85
,
guidance_scale
=
3
,
source_guidance_scale
=
1
,
generator
=
generator
,
output_type
=
"np"
,
)
image
=
output
.
images
...
...
tests/pipelines/stable_diffusion/test_stable_diffusion.py
View file @
6cf72a9b
...
...
@@ -755,7 +755,7 @@ class StableDiffusionPipelineIntegrationTests(unittest.TestCase):
def
test_stable_diffusion_text2img_pipeline_default
(
self
):
expected_image
=
load_numpy
(
"https://huggingface.co/datasets/
lewington/expected
-images/resolve/main/astronaut_riding_a_horse.npy"
"https://huggingface.co/datasets/
hf-internal-testing/diffusers
-images/resolve/main/
text2img/
astronaut_riding_a_horse.npy"
)
model_id
=
"CompVis/stable-diffusion-v1-4"
...
...
@@ -771,7 +771,7 @@ class StableDiffusionPipelineIntegrationTests(unittest.TestCase):
image
=
output
.
images
[
0
]
assert
image
.
shape
==
(
512
,
512
,
3
)
assert
np
.
abs
(
expected_image
-
image
).
max
()
<
1
e-3
assert
np
.
abs
(
expected_image
-
image
).
max
()
<
5
e-3
def
test_stable_diffusion_text2img_intermediate_state
(
self
):
number_of_steps
=
0
...
...
tests/test_pipelines.py
View file @
6cf72a9b
...
...
@@ -442,7 +442,8 @@ class PipelineSlowTests(unittest.TestCase):
def
test_output_format
(
self
):
model_path
=
"google/ddpm-cifar10-32"
pipe
=
DDIMPipeline
.
from_pretrained
(
model_path
)
scheduler
=
DDIMScheduler
.
from_config
(
model_path
)
pipe
=
DDIMPipeline
.
from_pretrained
(
model_path
,
scheduler
=
scheduler
)
pipe
.
to
(
torch_device
)
pipe
.
set_progress_bar_config
(
disable
=
None
)
...
...
@@ -451,13 +452,13 @@ class PipelineSlowTests(unittest.TestCase):
assert
images
.
shape
==
(
1
,
32
,
32
,
3
)
assert
isinstance
(
images
,
np
.
ndarray
)
images
=
pipe
(
generator
=
generator
,
output_type
=
"pil"
).
images
images
=
pipe
(
generator
=
generator
,
output_type
=
"pil"
,
num_inference_steps
=
4
).
images
assert
isinstance
(
images
,
list
)
assert
len
(
images
)
==
1
assert
isinstance
(
images
[
0
],
PIL
.
Image
.
Image
)
# use PIL by default
images
=
pipe
(
generator
=
generator
).
images
images
=
pipe
(
generator
=
generator
,
num_inference_steps
=
4
).
images
assert
isinstance
(
images
,
list
)
assert
isinstance
(
images
[
0
],
PIL
.
Image
.
Image
)
...
...
tests/test_scheduler.py
View file @
6cf72a9b
...
...
@@ -1281,10 +1281,11 @@ class EulerDiscreteSchedulerTest(SchedulerCommonTest):
scheduler
.
set_timesteps
(
self
.
num_inference_steps
)
generator
=
torch
.
Generator
().
manual_seed
(
0
)
generator
=
torch
.
Generator
(
torch_device
).
manual_seed
(
0
)
model
=
self
.
dummy_model
()
sample
=
self
.
dummy_sample_deter
*
scheduler
.
init_noise_sigma
sample
=
sample
.
to
(
torch_device
)
for
i
,
t
in
enumerate
(
scheduler
.
timesteps
):
sample
=
scheduler
.
scale_model_input
(
sample
,
t
)
...
...
@@ -1296,7 +1297,6 @@ class EulerDiscreteSchedulerTest(SchedulerCommonTest):
result_sum
=
torch
.
sum
(
torch
.
abs
(
sample
))
result_mean
=
torch
.
mean
(
torch
.
abs
(
sample
))
print
(
result_sum
,
result_mean
)
assert
abs
(
result_sum
.
item
()
-
10.0807
)
<
1e-2
assert
abs
(
result_mean
.
item
()
-
0.0131
)
<
1e-3
...
...
@@ -1308,7 +1308,7 @@ class EulerDiscreteSchedulerTest(SchedulerCommonTest):
scheduler
.
set_timesteps
(
self
.
num_inference_steps
,
device
=
torch_device
)
generator
=
torch
.
Generator
().
manual_seed
(
0
)
generator
=
torch
.
Generator
(
torch_device
).
manual_seed
(
0
)
model
=
self
.
dummy_model
()
sample
=
self
.
dummy_sample_deter
*
scheduler
.
init_noise_sigma
...
...
@@ -1324,7 +1324,6 @@ class EulerDiscreteSchedulerTest(SchedulerCommonTest):
result_sum
=
torch
.
sum
(
torch
.
abs
(
sample
))
result_mean
=
torch
.
mean
(
torch
.
abs
(
sample
))
print
(
result_sum
,
result_mean
)
assert
abs
(
result_sum
.
item
()
-
10.0807
)
<
1e-2
assert
abs
(
result_mean
.
item
()
-
0.0131
)
<
1e-3
...
...
@@ -1365,10 +1364,11 @@ class EulerAncestralDiscreteSchedulerTest(SchedulerCommonTest):
scheduler
.
set_timesteps
(
self
.
num_inference_steps
)
generator
=
torch
.
Generator
().
manual_seed
(
0
)
generator
=
torch
.
Generator
(
device
=
torch_device
).
manual_seed
(
0
)
model
=
self
.
dummy_model
()
sample
=
self
.
dummy_sample_deter
*
scheduler
.
init_noise_sigma
sample
=
sample
.
to
(
torch_device
)
for
i
,
t
in
enumerate
(
scheduler
.
timesteps
):
sample
=
scheduler
.
scale_model_input
(
sample
,
t
)
...
...
@@ -1380,9 +1380,14 @@ class EulerAncestralDiscreteSchedulerTest(SchedulerCommonTest):
result_sum
=
torch
.
sum
(
torch
.
abs
(
sample
))
result_mean
=
torch
.
mean
(
torch
.
abs
(
sample
))
print
(
result_sum
,
result_mean
)
if
str
(
torch_device
).
startswith
(
"cpu"
):
assert
abs
(
result_sum
.
item
()
-
152.3192
)
<
1e-2
assert
abs
(
result_mean
.
item
()
-
0.1983
)
<
1e-3
else
:
# CUDA
assert
abs
(
result_sum
.
item
()
-
144.8084
)
<
1e-2
assert
abs
(
result_mean
.
item
()
-
0.18855
)
<
1e-3
def
test_full_loop_device
(
self
):
scheduler_class
=
self
.
scheduler_classes
[
0
]
...
...
@@ -1391,7 +1396,7 @@ class EulerAncestralDiscreteSchedulerTest(SchedulerCommonTest):
scheduler
.
set_timesteps
(
self
.
num_inference_steps
,
device
=
torch_device
)
generator
=
torch
.
Generator
().
manual_seed
(
0
)
generator
=
torch
.
Generator
(
device
=
torch_device
).
manual_seed
(
0
)
model
=
self
.
dummy_model
()
sample
=
self
.
dummy_sample_deter
*
scheduler
.
init_noise_sigma
...
...
@@ -1407,14 +1412,18 @@ class EulerAncestralDiscreteSchedulerTest(SchedulerCommonTest):
result_sum
=
torch
.
sum
(
torch
.
abs
(
sample
))
result_mean
=
torch
.
mean
(
torch
.
abs
(
sample
))
print
(
result_sum
,
result_mean
)
if
not
str
(
torch_device
).
startswith
(
"
mps
"
):
if
str
(
torch_device
).
startswith
(
"
cpu
"
):
# The following sum varies between 148 and 156 on mps. Why?
assert
abs
(
result_sum
.
item
()
-
152.3192
)
<
1e-2
assert
abs
(
result_mean
.
item
()
-
0.1983
)
<
1e-3
el
se
:
el
if
str
(
torch_device
).
startswith
(
"mps"
)
:
# Larger tolerance on mps
assert
abs
(
result_mean
.
item
()
-
0.1983
)
<
1e-2
else
:
# CUDA
assert
abs
(
result_sum
.
item
()
-
144.8084
)
<
1e-2
assert
abs
(
result_mean
.
item
()
-
0.18855
)
<
1e-3
class
IPNDMSchedulerTest
(
SchedulerCommonTest
):
...
...
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