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
ced7c960
Unverified
Commit
ced7c960
authored
Dec 07, 2022
by
Suraj Patil
Committed by
GitHub
Dec 07, 2022
Browse files
fix upcast in slice attention (#1591)
* fix upcast in slice attention * fix dtype * add test * fix test
parent
8e74efad
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
3 deletions
+22
-3
src/diffusers/models/attention.py
src/diffusers/models/attention.py
+3
-3
tests/pipelines/stable_diffusion_2/test_stable_diffusion_v_pred.py
...elines/stable_diffusion_2/test_stable_diffusion_v_pred.py
+19
-0
No files found.
src/diffusers/models/attention.py
View file @
ced7c960
...
...
@@ -649,9 +649,9 @@ class CrossAttention(nn.Module):
key_slice
=
key_slice
.
float
()
attn_slice
=
torch
.
baddbmm
(
torch
.
empty
(
slice_size
,
query
.
shape
[
1
],
key
.
shape
[
1
],
dtype
=
query
.
dtype
,
device
=
query
.
device
),
query
[
start_idx
:
end_idx
]
,
key
[
start_idx
:
end_idx
]
.
transpose
(
-
1
,
-
2
),
torch
.
empty
(
slice_size
,
query
.
shape
[
1
],
key
.
shape
[
1
],
dtype
=
query
_slice
.
dtype
,
device
=
query
.
device
),
query
_slice
,
key
_slice
.
transpose
(
-
1
,
-
2
),
beta
=
0
,
alpha
=
self
.
scale
,
)
...
...
tests/pipelines/stable_diffusion_2/test_stable_diffusion_v_pred.py
View file @
ced7c960
...
...
@@ -265,6 +265,25 @@ class StableDiffusion2VPredictionPipelineIntegrationTests(unittest.TestCase):
expected_slice
=
np
.
array
([
0.0567
,
0.057
,
0.0416
,
0.0463
,
0.0433
,
0.06
,
0.0517
,
0.0526
,
0.0866
])
assert
np
.
abs
(
image_slice
.
flatten
()
-
expected_slice
).
max
()
<
1e-2
def
test_stable_diffusion_v_pred_upcast_attention
(
self
):
sd_pipe
=
StableDiffusionPipeline
.
from_pretrained
(
"stabilityai/stable-diffusion-2-1"
,
torch_dtype
=
torch
.
float16
)
sd_pipe
=
sd_pipe
.
to
(
torch_device
)
sd_pipe
.
enable_attention_slicing
()
sd_pipe
.
set_progress_bar_config
(
disable
=
None
)
prompt
=
"A painting of a squirrel eating a burger"
generator
=
torch
.
Generator
(
device
=
torch_device
).
manual_seed
(
0
)
output
=
sd_pipe
([
prompt
],
generator
=
generator
,
guidance_scale
=
7.5
,
num_inference_steps
=
20
,
output_type
=
"np"
)
image
=
output
.
images
image_slice
=
image
[
0
,
253
:
256
,
253
:
256
,
-
1
]
assert
image
.
shape
==
(
1
,
768
,
768
,
3
)
expected_slice
=
np
.
array
([
0.0461
,
0.0483
,
0.0566
,
0.0512
,
0.0446
,
0.0751
,
0.0664
,
0.0551
,
0.0488
])
assert
np
.
abs
(
image_slice
.
flatten
()
-
expected_slice
).
max
()
<
1e-2
def
test_stable_diffusion_v_pred_euler
(
self
):
scheduler
=
EulerDiscreteScheduler
.
from_pretrained
(
"stabilityai/stable-diffusion-2"
,
subfolder
=
"scheduler"
)
sd_pipe
=
StableDiffusionPipeline
.
from_pretrained
(
"stabilityai/stable-diffusion-2"
,
scheduler
=
scheduler
)
...
...
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