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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn.functional as F
|
| 3 |
+
from diffusers.models.attention_processor import (
|
| 4 |
+
Attention,
|
| 5 |
+
AttnProcessor2_0,
|
| 6 |
+
SlicedAttnProcessor,
|
| 7 |
+
XFormersAttnProcessor
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
try:
|
| 11 |
+
import xformers.ops
|
| 12 |
+
except:
|
| 13 |
+
xformers = None
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
loaded_networks = []
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def apply_single_hypernetwork(
|
| 20 |
+
hypernetwork, hidden_states, encoder_hidden_states
|
| 21 |
+
):
|
| 22 |
+
context_k, context_v = hypernetwork.forward(hidden_states, encoder_hidden_states)
|
| 23 |
+
return context_k, context_v
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def apply_hypernetworks(context_k, context_v, layer=None):
|
| 27 |
+
if len(loaded_networks) == 0:
|
| 28 |
+
return context_v, context_v
|
| 29 |
+
for hypernetwork in loaded_networks:
|
| 30 |
+
context_k, context_v = hypernetwork.forward(context_k, context_v)
|
| 31 |
+
|
| 32 |
+
context_k = context_k.to(dtype=context_k.dtype)
|
| 33 |
+
context_v = context_v.to(dtype=context_k.dtype)
|
| 34 |
+
|
| 35 |
+
return context_k, context_v
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def xformers_forward(
|
| 40 |
+
self: XFormersAttnProcessor,
|
| 41 |
+
attn: Attention,
|
| 42 |
+
hidden_states: torch.Tensor,
|
| 43 |
+
encoder_hidden_states: torch.Tensor = None,
|
| 44 |
+
attention_mask: torch.Tensor = None,
|
| 45 |
+
):
|
| 46 |
+
batch_size, sequence_length, _ = (
|
| 47 |
+
hidden_states.shape
|
| 48 |
+
if encoder_hidden_states is None
|
| 49 |
+
else encoder_hidden_states.shape
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
attention_mask = attn.prepare_attention_mask(
|
| 53 |
+
attention_mask, sequence_length, batch_size
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
query = attn.to_q(hidden_states)
|
| 57 |
+
|
| 58 |
+
if encoder_hidden_states is None:
|
| 59 |
+
encoder_hidden_states = hidden_states
|
| 60 |
+
elif attn.norm_cross:
|
| 61 |
+
encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)
|
| 62 |
+
|
| 63 |
+
context_k, context_v = apply_hypernetworks(hidden_states, encoder_hidden_states)
|
| 64 |
+
|
| 65 |
+
key = attn.to_k(context_k)
|
| 66 |
+
value = attn.to_v(context_v)
|
| 67 |
+
|
| 68 |
+
query = attn.head_to_batch_dim(query).contiguous()
|
| 69 |
+
key = attn.head_to_batch_dim(key).contiguous()
|
| 70 |
+
value = attn.head_to_batch_dim(value).contiguous()
|
| 71 |
+
|
| 72 |
+
hidden_states = xformers.ops.memory_efficient_attention(
|
| 73 |
+
query,
|
| 74 |
+
key,
|
| 75 |
+
value,
|
| 76 |
+
attn_bias=attention_mask,
|
| 77 |
+
op=self.attention_op,
|
| 78 |
+
scale=attn.scale,
|
| 79 |
+
)
|
| 80 |
+
hidden_states = hidden_states.to(query.dtype)
|
| 81 |
+
hidden_states = attn.batch_to_head_dim(hidden_states)
|
| 82 |
+
|
| 83 |
+
# linear proj
|
| 84 |
+
hidden_states = attn.to_out[0](hidden_states)
|
| 85 |
+
# dropout
|
| 86 |
+
hidden_states = attn.to_out[1](hidden_states)
|
| 87 |
+
return hidden_states
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def sliced_attn_forward(
|
| 91 |
+
self: SlicedAttnProcessor,
|
| 92 |
+
attn: Attention,
|
| 93 |
+
hidden_states: torch.Tensor,
|
| 94 |
+
encoder_hidden_states: torch.Tensor = None,
|
| 95 |
+
attention_mask: torch.Tensor = None,
|
| 96 |
+
):
|
| 97 |
+
batch_size, sequence_length, _ = (
|
| 98 |
+
hidden_states.shape
|
| 99 |
+
if encoder_hidden_states is None
|
| 100 |
+
else encoder_hidden_states.shape
|
| 101 |
+
)
|
| 102 |
+
attention_mask = attn.prepare_attention_mask(
|
| 103 |
+
attention_mask, sequence_length, batch_size
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
query = attn.to_q(hidden_states)
|
| 107 |
+
dim = query.shape[-1]
|
| 108 |
+
query = attn.head_to_batch_dim(query)
|
| 109 |
+
|
| 110 |
+
if encoder_hidden_states is None:
|
| 111 |
+
encoder_hidden_states = hidden_states
|
| 112 |
+
elif attn.norm_cross:
|
| 113 |
+
encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)
|
| 114 |
+
|
| 115 |
+
context_k, context_v = apply_hypernetworks(hidden_states, encoder_hidden_states)
|
| 116 |
+
|
| 117 |
+
key = attn.to_k(context_k)
|
| 118 |
+
value = attn.to_v(context_v)
|
| 119 |
+
key = attn.head_to_batch_dim(key)
|
| 120 |
+
value = attn.head_to_batch_dim(value)
|
| 121 |
+
|
| 122 |
+
batch_size_attention, query_tokens, _ = query.shape
|
| 123 |
+
hidden_states = torch.zeros(
|
| 124 |
+
(batch_size_attention, query_tokens, dim // attn.heads),
|
| 125 |
+
device=query.device,
|
| 126 |
+
dtype=query.dtype,
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
for i in range(batch_size_attention // self.slice_size):
|
| 130 |
+
start_idx = i * self.slice_size
|
| 131 |
+
end_idx = (i + 1) * self.slice_size
|
| 132 |
+
|
| 133 |
+
query_slice = query[start_idx:end_idx]
|
| 134 |
+
key_slice = key[start_idx:end_idx]
|
| 135 |
+
attn_mask_slice = (
|
| 136 |
+
attention_mask[start_idx:end_idx] if attention_mask is not None else None
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
attn_slice = attn.get_attention_scores(query_slice, key_slice, attn_mask_slice)
|
| 140 |
+
|
| 141 |
+
attn_slice = torch.bmm(attn_slice, value[start_idx:end_idx])
|
| 142 |
+
|
| 143 |
+
hidden_states[start_idx:end_idx] = attn_slice
|
| 144 |
+
|
| 145 |
+
hidden_states = attn.batch_to_head_dim(hidden_states)
|
| 146 |
+
|
| 147 |
+
# linear proj
|
| 148 |
+
hidden_states = attn.to_out[0](hidden_states)
|
| 149 |
+
# dropout
|
| 150 |
+
hidden_states = attn.to_out[1](hidden_states)
|
| 151 |
+
|
| 152 |
+
return hidden_states
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def v2_0_forward(
|
| 156 |
+
self: AttnProcessor2_0,
|
| 157 |
+
attn: Attention,
|
| 158 |
+
hidden_states,
|
| 159 |
+
encoder_hidden_states=None,
|
| 160 |
+
attention_mask=None,
|
| 161 |
+
):
|
| 162 |
+
batch_size, sequence_length, _ = (
|
| 163 |
+
hidden_states.shape
|
| 164 |
+
if encoder_hidden_states is None
|
| 165 |
+
else encoder_hidden_states.shape
|
| 166 |
+
)
|
| 167 |
+
inner_dim = hidden_states.shape[-1]
|
| 168 |
+
|
| 169 |
+
if attention_mask is not None:
|
| 170 |
+
attention_mask = attn.prepare_attention_mask(
|
| 171 |
+
attention_mask, sequence_length, batch_size
|
| 172 |
+
)
|
| 173 |
+
# scaled_dot_product_attention expects attention_mask shape to be
|
| 174 |
+
# (batch, heads, source_length, target_length)
|
| 175 |
+
attention_mask = attention_mask.view(
|
| 176 |
+
batch_size, attn.heads, -1, attention_mask.shape[-1]
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
query = attn.to_q(hidden_states)
|
| 180 |
+
|
| 181 |
+
if encoder_hidden_states is None:
|
| 182 |
+
encoder_hidden_states = hidden_states
|
| 183 |
+
elif attn.norm_cross:
|
| 184 |
+
encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)
|
| 185 |
+
|
| 186 |
+
context_k, context_v = apply_hypernetworks(hidden_states, encoder_hidden_states)
|
| 187 |
+
|
| 188 |
+
key = attn.to_k(context_k)
|
| 189 |
+
value = attn.to_v(context_v)
|
| 190 |
+
|
| 191 |
+
head_dim = inner_dim // attn.heads
|
| 192 |
+
query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
|
| 193 |
+
key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
|
| 194 |
+
value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
|
| 195 |
+
|
| 196 |
+
# the output of sdp = (batch, num_heads, seq_len, head_dim)
|
| 197 |
+
# TODO: add support for attn.scale when we move to Torch 2.1
|
| 198 |
+
hidden_states = F.scaled_dot_product_attention(
|
| 199 |
+
query, key, value, attn_mask=attention_mask, dropout_p=0.0, is_causal=False
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
hidden_states = hidden_states.transpose(1, 2).reshape(
|
| 203 |
+
batch_size, -1, attn.heads * head_dim
|
| 204 |
+
)
|
| 205 |
+
hidden_states = hidden_states.to(query.dtype)
|
| 206 |
+
|
| 207 |
+
# linear proj
|
| 208 |
+
hidden_states = attn.to_out[0](hidden_states)
|
| 209 |
+
# dropout
|
| 210 |
+
hidden_states = attn.to_out[1](hidden_states)
|
| 211 |
+
return hidden_states
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
def replace_attentions_for_hypernetwork():
|
| 215 |
+
import diffusers.models.attention_processor
|
| 216 |
+
|
| 217 |
+
diffusers.models.attention_processor.XFormersAttnProcessor.__call__ = (
|
| 218 |
+
xformers_forward
|
| 219 |
+
)
|
| 220 |
+
diffusers.models.attention_processor.SlicedAttnProcessor.__call__ = (
|
| 221 |
+
sliced_attn_forward
|
| 222 |
+
)
|
| 223 |
+
diffusers.models.attention_processor.AttnProcessor2_0.__call__ = v2_0_forward
|