-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattn_processor.py
635 lines (500 loc) · 26 KB
/
attn_processor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
from typing import Optional
from abc import ABCMeta, abstractmethod
import torch
import torch.nn.functional as F
import numpy as np
from collections import defaultdict
from diffusers.utils import deprecate
from diffusers.models.attention_processor import Attention
from diffusers.models.embeddings import apply_rotary_emb
class AttnProcessorExperimentBase(metaclass=ABCMeta):
def __init__(self):
if not hasattr(F, "scaled_dot_product_attention"):
raise ImportError("AttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0.")
self.timestep = 0
self.kv_cache = defaultdict(lambda: defaultdict(list))
self.activation_cache = defaultdict(lambda: defaultdict(list))
self.previous_step_cache = {
'k': None, 'v': None, 'a': None, 'ek': None, 'ev': None, 'ea': None
}
self.info = {
'means': {
'k': [], 'v': [], 'a': [], 'ek': [], 'ev': [], 'ea': []
},
'vars': {
'k': [], 'v': [], 'a': [], 'ek': [], 'ev': [], 'ea': []
}
}
@abstractmethod
def __call__():
pass
def update_cache(self, key, value):
if self.previous_step_cache[key] is None:
self.previous_step_cache[key] = value
else:
diff = torch.abs(value - self.previous_step_cache[key])
means = torch.mean(diff).item()
vars = torch.var(diff).item()
self.info['means'][key].append(means)
self.info['vars'][key].append(vars)
self.previous_step_cache[key] = value
def reset_cache(self):
self.previous_step_cache = {
'k': None, 'v': None, 'a': None, 'ek': None, 'ev': None, 'ea': None
}
self.info = {
'means': {
'k': [], 'v': [], 'a': [], 'ek': [], 'ev': [], 'ea': []
},
'vars': {
'k': [], 'v': [], 'a': [], 'ek': [], 'ev': [], 'ea': []
}
}
def save_activation_cache(self, activation, attn):
layer_name = attn.__class__.__name__
self.activation_cache[layer_name]['activation'].append(activation.detach().cpu().numpy())
def plot_kv_diff(self, layer_num: int, ax1, num_columns: int, relative: bool = False):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
for layer_name, kv_data in self.kv_cache.items():
if len(kv_data['key']) < 2:
print(f"Not enough timesteps for layer {layer_name}. Skipping.")
continue
key_diff_means, key_diff_vars = [], []
value_diff_means, value_diff_vars = [], []
pre_key_gpu, cur_key_gpu = None, None
pre_value_gpu, cur_value_gpu = None, None
for i in range(len(kv_data['key'])):
cur_key = kv_data['key'][i]
cur_value = kv_data['value'][i]
cur_key_gpu = torch.abs(torch.tensor(cur_key, device=device))
cur_value_gpu = torch.abs(torch.tensor(cur_value, device=device))
cur_key_gpu[cur_key_gpu == 0] = 1e-4
cur_value_gpu[cur_value_gpu == 0] = 1e-4
if i == 0:
if relative:
key_diff_gpu = torch.ones_like(cur_key_gpu, device=device)
value_diff_gpu = torch.ones_like(cur_value_gpu, device=device)
else:
key_diff_gpu = cur_key_gpu
value_diff_gpu = cur_value_gpu
else:
if relative:
key_diff_gpu = torch.abs(cur_key_gpu - pre_key_gpu) / (cur_key_gpu + pre_key_gpu)
value_diff_gpu = torch.abs(cur_value_gpu - pre_value_gpu) / (cur_value_gpu + pre_value_gpu)
else:
key_diff_gpu = cur_key_gpu - pre_key_gpu
value_diff_gpu = cur_value_gpu - pre_value_gpu
pre_key_gpu, cur_key_gpu = cur_key_gpu, None
pre_value_gpu, cur_value_gpu = cur_value_gpu, None
key_diff_means.append(torch.mean(torch.abs(key_diff_gpu)).item())
key_diff_vars.append(torch.var(key_diff_gpu).item())
value_diff_means.append(torch.mean(torch.abs(value_diff_gpu)).item())
value_diff_vars.append(torch.var(value_diff_gpu).item())
timesteps = range(len(kv_data['key']))
# Plot differences with error bars
row, column = layer_num//num_columns, layer_num%num_columns
ax1[row, column].errorbar(timesteps, key_diff_means, yerr=np.sqrt(key_diff_vars),
label='Key Diff', color='blue', capsize=5)
ax1[row, column].errorbar(timesteps, value_diff_means, yerr=np.sqrt(value_diff_vars),
label='Value Diff', color='red', capsize=5)
ax1[row, column].set_xticks(range(0, len(kv_data['key']), len(kv_data['key']) // 10))
ax1[row, column].set_xlabel('Timestep')
if relative:
ax1[row, column].set_ylabel('Mean of Relative Differences')
else:
ax1[row, column].set_ylabel('Mean of Absolute Differences')
ax1[row, column].legend()
ax1[row, column].set_title(f'{layer_name} {layer_num} KV Diff')
self.kv_cache.clear()
def plot_activation_diff(self, layer_num: int, ax1, num_columns: int, relative: bool = False):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
for layer_name, activaton_data in self.activation_cache.items():
if len(activaton_data['activation']) < 2:
print(f"Not enough timesteps for layer {layer_name}. Skipping.")
continue
activation_diff_means, activation_diff_vars = [], []
pre_activation_gpu, cur_activation_gpu = None, None
for i in range(len(activaton_data['activation'])):
cur_activation = activaton_data['activation'][i]
cur_activation_gpu = torch.abs(torch.tensor(cur_activation, device=device))
cur_activation_gpu[cur_activation_gpu == 0] = 1e-4
if i == 0:
if relative:
activation_diff_gpu = torch.ones_like(cur_activation_gpu, device=device)
else:
activation_diff_gpu = cur_activation_gpu
else:
if relative:
activation_diff_gpu = torch.abs(cur_activation_gpu - pre_activation_gpu) / (cur_activation_gpu + pre_activation_gpu)
else:
activation_diff_gpu = cur_activation_gpu - pre_activation_gpu
pre_activation_gpu, cur_activation_gpu = cur_activation_gpu, None
activation_diff_means.append(torch.mean(torch.abs(activation_diff_gpu)).item())
activation_diff_vars.append(torch.var(activation_diff_gpu).item())
timesteps = range(len(activaton_data['activation']))
# Plot differences with error bars
row, column = layer_num//num_columns, layer_num%num_columns
ax1[row, column].errorbar(timesteps, activation_diff_means, yerr=np.sqrt(activation_diff_vars),
label='Activation Diff', color='blue', capsize=5)
ax1[row, column].set_xticks(range(0, len(activaton_data['activation']), len(activaton_data['activation']) // 10))
ax1[row, column].set_xlabel('Timestep')
if relative:
ax1[row, column].set_ylabel('Mean of Relative Differences')
else:
ax1[row, column].set_ylabel('Mean of Absolute Differences')
ax1[row, column].legend()
ax1[row, column].set_title(f'{layer_name} {layer_num} Activation Diff')
self.activation_cache.clear()
class LatteAttnProcessor2_0(AttnProcessorExperimentBase):
def __init__(self):
self.record = False
super().__init__()
def __call__(
self,
attn: Attention,
hidden_states: torch.Tensor,
encoder_hidden_states: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None,
temb: Optional[torch.Tensor] = None,
*args,
**kwargs,
) -> torch.Tensor:
if len(args) > 0 or kwargs.get("scale", None) is not None:
deprecation_message = "The `scale` argument is deprecated and will be ignored. Please remove it, as passing it will raise an error in the future. `scale` should directly be passed while calling the underlying pipeline component i.e., via `cross_attention_kwargs`."
deprecate("scale", "1.0.0", deprecation_message)
residual = hidden_states
if attn.spatial_norm is not None:
hidden_states = attn.spatial_norm(hidden_states, temb)
input_ndim = hidden_states.ndim
if input_ndim == 4:
batch_size, channel, height, width = hidden_states.shape
hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
batch_size, sequence_length, _ = (
hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape
)
if attention_mask is not None:
attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)
# scaled_dot_product_attention expects attention_mask shape to be
# (batch, heads, source_length, target_length)
attention_mask = attention_mask.view(batch_size, attn.heads, -1, attention_mask.shape[-1])
if attn.group_norm is not None:
hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2)
query = attn.to_q(hidden_states)
if encoder_hidden_states is None:
encoder_hidden_states = hidden_states
elif attn.norm_cross:
encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)
key = attn.to_k(encoder_hidden_states)
value = attn.to_v(encoder_hidden_states)
if self.record:
self.update_cache('k', key)
self.update_cache('v', value)
inner_dim = key.shape[-1]
head_dim = inner_dim // attn.heads
query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
if attn.norm_q is not None:
query = attn.norm_q(query)
if attn.norm_k is not None:
key = attn.norm_k(key)
# the output of sdp = (batch, num_heads, seq_len, head_dim)
# TODO: add support for attn.scale when we move to Torch 2.1
hidden_states = F.scaled_dot_product_attention(
query, key, value, attn_mask=attention_mask, dropout_p=0.0, is_causal=False
)
hidden_states = hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim)
hidden_states = hidden_states.to(query.dtype)
# linear proj
hidden_states = attn.to_out[0](hidden_states)
# dropout
hidden_states = attn.to_out[1](hidden_states)
if input_ndim == 4:
hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width)
if attn.residual_connection:
hidden_states = hidden_states + residual
hidden_states = hidden_states / attn.rescale_output_factor
if self.record:
self.update_cache('a', hidden_states)
self.timestep += 1
return hidden_states
class AttnProcessor2_0(AttnProcessorExperimentBase):
def __init__(self):
super().__init__()
def __call__(
self,
attn: Attention,
hidden_states: torch.Tensor,
encoder_hidden_states: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None,
temb: Optional[torch.Tensor] = None,
*args,
**kwargs,
) -> torch.Tensor:
if len(args) > 0 or kwargs.get("scale", None) is not None:
deprecation_message = "The `scale` argument is deprecated and will be ignored. Please remove it, as passing it will raise an error in the future. `scale` should directly be passed while calling the underlying pipeline component i.e., via `cross_attention_kwargs`."
deprecate("scale", "1.0.0", deprecation_message)
residual = hidden_states
if attn.spatial_norm is not None:
hidden_states = attn.spatial_norm(hidden_states, temb)
input_ndim = hidden_states.ndim
if input_ndim == 4:
batch_size, channel, height, width = hidden_states.shape
hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
batch_size, sequence_length, _ = (
hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape
)
if attention_mask is not None:
attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)
# scaled_dot_product_attention expects attention_mask shape to be
# (batch, heads, source_length, target_length)
attention_mask = attention_mask.view(batch_size, attn.heads, -1, attention_mask.shape[-1])
if attn.group_norm is not None:
hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2)
query = attn.to_q(hidden_states)
if encoder_hidden_states is None:
encoder_hidden_states = hidden_states
elif attn.norm_cross:
encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)
key = attn.to_k(encoder_hidden_states)
value = attn.to_v(encoder_hidden_states)
self.update_cache('k', key)
self.update_cache('v', value)
inner_dim = key.shape[-1]
head_dim = inner_dim // attn.heads
query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
if attn.norm_q is not None:
query = attn.norm_q(query)
if attn.norm_k is not None:
key = attn.norm_k(key)
# the output of sdp = (batch, num_heads, seq_len, head_dim)
# TODO: add support for attn.scale when we move to Torch 2.1
hidden_states = F.scaled_dot_product_attention(
query, key, value, attn_mask=attention_mask, dropout_p=0.0, is_causal=False
)
hidden_states = hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim)
hidden_states = hidden_states.to(query.dtype)
# linear proj
hidden_states = attn.to_out[0](hidden_states)
# dropout
hidden_states = attn.to_out[1](hidden_states)
if input_ndim == 4:
hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width)
if attn.residual_connection:
hidden_states = hidden_states + residual
hidden_states = hidden_states / attn.rescale_output_factor
self.update_cache('a', hidden_states)
self.timestep += 1
return hidden_states
class xFuserCogVideoXAttnProcessor2_0(AttnProcessorExperimentBase):
def __init__(self):
super().__init__()
def __call__(
self,
attn: Attention,
hidden_states: torch.Tensor,
encoder_hidden_states: torch.Tensor,
attention_mask: Optional[torch.Tensor] = None,
image_rotary_emb: Optional[torch.Tensor] = None,
*args,
**kwargs,
) -> torch.Tensor:
text_seq_length = encoder_hidden_states.size(1)
latent_seq_length = hidden_states.size(1)
hidden_states = torch.cat([encoder_hidden_states, hidden_states], dim=1)
batch_size, sequence_length, _ = (
hidden_states.shape
if encoder_hidden_states is None
else encoder_hidden_states.shape
)
if attention_mask is not None:
attention_mask = attn.prepare_attention_mask(
attention_mask, sequence_length, batch_size
)
attention_mask = attention_mask.view(
batch_size, attn.heads, -1, attention_mask.shape[-1]
)
query = attn.to_q(hidden_states)
key = attn.to_k(hidden_states)
value = attn.to_v(hidden_states)
inner_dim = key.shape[-1]
head_dim = inner_dim // attn.heads
query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
if attn.norm_q is not None:
query = attn.norm_q(query)
if attn.norm_k is not None:
key = attn.norm_k(key)
# Apply RoPE if needed
if image_rotary_emb is not None:
query[:, :, text_seq_length:] = apply_rotary_emb(query[:, :, text_seq_length:], image_rotary_emb)
if not attn.is_cross_attention:
key[:, :, text_seq_length:] = apply_rotary_emb(key[:, :, text_seq_length:], image_rotary_emb)
self.update_cache('k', key[:,:,text_seq_length:])
self.update_cache('v', value[:,:,text_seq_length:])
self.update_cache('ek', key[:,:,:text_seq_length])
self.update_cache('ev', value[:,:,:text_seq_length])
# TODO: add support for attn.scale when we move to Torch 2.1
hidden_states = F.scaled_dot_product_attention(
query, key, value, dropout_p=0.0, is_causal=False
)
hidden_states = hidden_states.transpose(1, 2).reshape(
batch_size, -1, attn.heads * head_dim
)
assert text_seq_length + latent_seq_length == hidden_states.shape[1]
# linear proj
hidden_states = attn.to_out[0](hidden_states)
# dropout
hidden_states = attn.to_out[1](hidden_states)
encoder_hidden_states, hidden_states = hidden_states.split(
[text_seq_length, latent_seq_length], dim=1
)
self.update_cache('a', hidden_states)
self.update_cache('ea', encoder_hidden_states)
return hidden_states, encoder_hidden_states
class xFuserJointAttnProcessor2_0(AttnProcessorExperimentBase):
def __init__(self):
super().__init__()
def __call__(
self,
attn: Attention,
hidden_states: torch.FloatTensor,
encoder_hidden_states: torch.FloatTensor = None,
attention_mask: Optional[torch.FloatTensor] = None,
*args,
**kwargs,
) -> torch.FloatTensor:
residual = hidden_states
input_ndim = hidden_states.ndim
if input_ndim == 4:
batch_size, channel, height, width = hidden_states.shape
hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
context_input_ndim = encoder_hidden_states.ndim
if context_input_ndim == 4:
batch_size, channel, height, width = encoder_hidden_states.shape
encoder_hidden_states = encoder_hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
batch_size = encoder_hidden_states.shape[0]
# `sample` projections.
query = attn.to_q(hidden_states)
key = attn.to_k(hidden_states)
value = attn.to_v(hidden_states)
# `context` projections.
encoder_hidden_states_query_proj = attn.add_q_proj(encoder_hidden_states)
encoder_hidden_states_key_proj = attn.add_k_proj(encoder_hidden_states)
encoder_hidden_states_value_proj = attn.add_v_proj(encoder_hidden_states)
self.update_cache('k', key)
self.update_cache('v', value)
self.update_cache('ek', encoder_hidden_states_key_proj)
self.update_cache('ev', encoder_hidden_states_value_proj)
# attention
query = torch.cat([query, encoder_hidden_states_query_proj], dim=1)
key = torch.cat([key, encoder_hidden_states_key_proj], dim=1)
value = torch.cat([value, encoder_hidden_states_value_proj], dim=1)
inner_dim = key.shape[-1]
head_dim = inner_dim // attn.heads
query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
hidden_states = F.scaled_dot_product_attention(query, key, value, dropout_p=0.0, is_causal=False)
hidden_states = hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim)
hidden_states = hidden_states.to(query.dtype)
# Split the attention outputs.
hidden_states, encoder_hidden_states = (
hidden_states[:, : residual.shape[1]],
hidden_states[:, residual.shape[1] :],
)
# linear proj
hidden_states = attn.to_out[0](hidden_states)
# dropout
hidden_states = attn.to_out[1](hidden_states)
if not attn.context_pre_only:
encoder_hidden_states = attn.to_add_out(encoder_hidden_states)
self.update_cache('a', hidden_states)
self.update_cache('ea', encoder_hidden_states)
if input_ndim == 4:
hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width)
if context_input_ndim == 4:
encoder_hidden_states = encoder_hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width)
return hidden_states, encoder_hidden_states
class FluxAttnProcessor2_0(AttnProcessorExperimentBase):
def __init__(self):
super().__init__()
def __call__(
self,
attn: Attention,
hidden_states: torch.FloatTensor,
encoder_hidden_states: torch.FloatTensor = None,
attention_mask: Optional[torch.FloatTensor] = None,
image_rotary_emb: Optional[torch.Tensor] = None,
) -> torch.FloatTensor:
batch_size, _, _ = hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape
# `sample` projections.
query = attn.to_q(hidden_states)
key = attn.to_k(hidden_states)
value = attn.to_v(hidden_states)
inner_dim = key.shape[-1]
head_dim = inner_dim // attn.heads
query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
if attn.norm_q is not None:
query = attn.norm_q(query)
if attn.norm_k is not None:
key = attn.norm_k(key)
# the attention in FluxSingleTransformerBlock does not use `encoder_hidden_states`
if encoder_hidden_states is not None:
# `context` projections.
encoder_hidden_states_query_proj = attn.add_q_proj(encoder_hidden_states)
encoder_hidden_states_key_proj = attn.add_k_proj(encoder_hidden_states)
encoder_hidden_states_value_proj = attn.add_v_proj(encoder_hidden_states)
encoder_hidden_states_query_proj = encoder_hidden_states_query_proj.view(
batch_size, -1, attn.heads, head_dim
).transpose(1, 2)
encoder_hidden_states_key_proj = encoder_hidden_states_key_proj.view(
batch_size, -1, attn.heads, head_dim
).transpose(1, 2)
encoder_hidden_states_value_proj = encoder_hidden_states_value_proj.view(
batch_size, -1, attn.heads, head_dim
).transpose(1, 2)
if attn.norm_added_q is not None:
encoder_hidden_states_query_proj = attn.norm_added_q(encoder_hidden_states_query_proj)
if attn.norm_added_k is not None:
encoder_hidden_states_key_proj = attn.norm_added_k(encoder_hidden_states_key_proj)
encoder_size = encoder_hidden_states_query_proj.shape[2]
# attention
query = torch.cat([encoder_hidden_states_query_proj, query], dim=2)
key = torch.cat([encoder_hidden_states_key_proj, key], dim=2)
value = torch.cat([encoder_hidden_states_value_proj, value], dim=2)
if image_rotary_emb is not None:
query = apply_rotary_emb(query, image_rotary_emb)
key = apply_rotary_emb(key, image_rotary_emb)
if encoder_hidden_states is not None:
self.update_cache('k', key[:,:,encoder_size:])
self.update_cache('v', value[:,:,encoder_size:])
self.update_cache('ek', key[:,:,:encoder_size])
self.update_cache('ev', value[:,:,:encoder_size])
else:
self.update_cache('k', key)
self.update_cache('v', value)
hidden_states = F.scaled_dot_product_attention(query, key, value, dropout_p=0.0, is_causal=False)
hidden_states = hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim)
hidden_states = hidden_states.to(query.dtype)
if encoder_hidden_states is not None:
encoder_hidden_states, hidden_states = (
hidden_states[:, : encoder_hidden_states.shape[1]],
hidden_states[:, encoder_hidden_states.shape[1] :],
)
# linear proj
hidden_states = attn.to_out[0](hidden_states)
# dropout
hidden_states = attn.to_out[1](hidden_states)
encoder_hidden_states = attn.to_add_out(encoder_hidden_states)
self.update_cache('a', hidden_states)
self.update_cache('ea', encoder_hidden_states)
return hidden_states, encoder_hidden_states
else:
self.update_cache('a', hidden_states)
return hidden_states