Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
Stable Diffusion Webui
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
novelai-storage
Stable Diffusion Webui
Commits
f174fb29
Commit
f174fb29
authored
Oct 07, 2022
by
C43H66N12O12S2
Committed by
GitHub
Oct 07, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add xformers attention
parent
2995107f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
1 deletion
+38
-1
modules/sd_hijack_optimizations.py
modules/sd_hijack_optimizations.py
+38
-1
No files found.
modules/sd_hijack_optimizations.py
View file @
f174fb29
import
math
import
torch
from
torch
import
einsum
import
xformers.ops
import
functorch
xformers
.
_is_functorch_available
=
True
from
ldm.util
import
default
from
einops
import
rearrange
...
...
@@ -92,6 +94,41 @@ def split_cross_attention_forward(self, x, context=None, mask=None):
return
self
.
to_out
(
r2
)
def
_maybe_init
(
self
,
x
):
"""
Initialize the attention operator, if required We expect the head dimension to be exposed here, meaning that x
: B, Head, Length
"""
if
self
.
attention_op
is
not
None
:
return
_
,
M
,
K
=
x
.
shape
try
:
self
.
attention_op
=
xformers
.
ops
.
AttentionOpDispatch
(
dtype
=
x
.
dtype
,
device
=
x
.
device
,
k
=
K
,
attn_bias_type
=
type
(
None
),
has_dropout
=
False
,
kv_len
=
M
,
q_len
=
M
,
)
.
op
except
NotImplementedError
as
err
:
raise
NotImplementedError
(
f
"Please install xformers with the flash attention / cutlass components.
\n
{err}"
)
def
xformers_attention_forward
(
self
,
x
,
context
=
None
,
mask
=
None
):
h
=
self
.
heads
q_in
=
self
.
to_q
(
x
)
context
=
default
(
context
,
x
)
k_in
=
self
.
to_k
(
context
)
v_in
=
self
.
to_v
(
context
)
q
,
k
,
v
=
map
(
lambda
t
:
rearrange
(
t
,
'b n (h d) -> (b h) n d'
,
h
=
h
),
(
q_in
,
k_in
,
v_in
))
del
q_in
,
k_in
,
v_in
self
.
_maybe_init
(
q
)
out
=
xformers
.
ops
.
memory_efficient_attention
(
q
,
k
,
v
,
attn_bias
=
None
,
op
=
self
.
attention_op
)
out
=
rearrange
(
out
,
'(b h) n d -> b n (h d)'
,
h
=
h
)
return
self
.
to_out
(
out
)
def
cross_attention_attnblock_forward
(
self
,
x
):
h_
=
x
h_
=
self
.
norm
(
h_
)
...
...
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