-
Notifications
You must be signed in to change notification settings - Fork 0
/
attn.py
1703 lines (1284 loc) · 62.3 KB
/
attn.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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import torch
from torch import nn
from exllamav2.module import ExLlamaV2Module
from exllamav2.rmsnorm import ExLlamaV2RMSNorm
from exllamav2.layernorm import ExLlamaV2LayerNorm
from exllamav2.headnorm import ExLlamaV2HeadNorm
from exllamav2.linear import ExLlamaV2Linear
from exllamav2.cache import ExLlamaV2CacheBase,ExLlamaV2Cache,ExLlamaV2Cache_CPU
from exllamav2.ext import exllamav2_ext as ext_c, none_tensor
from exllamav2.compat import safe_move_tensor
from exllamav2.lora import ExLlamaV2Lora
import math
# import xformers.ops as xops
# from exllamav2.util import list_live_tensors, set_snapshot, diff_snapshot, print_vram_usage_peak
# import torch.nn.functional as F
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from exllamav2.model import ExLlamaV2
# Detect flash-attn
k_need_index = []
has_flash_attn = False
try:
import flash_attn
flash_attn_ver = [int(t) for t in flash_attn.__version__.split(".") if t.isdigit()]
is_ampere_or_newer_gpu = any(torch.cuda.get_device_properties(i).major >= 8 for i in range(torch.cuda.device_count()))
if flash_attn_ver >= [2, 2, 1] and is_ampere_or_newer_gpu:
from flash_attn import flash_attn_func
has_flash_attn = True
except ModuleNotFoundError:
pass
class ExLlamaV2Attention(ExLlamaV2Module):
name: str = "Attention"
layer_idx: int
input_layernorm: ExLlamaV2RMSNorm | ExLlamaV2LayerNorm | None
q_proj: ExLlamaV2Linear | None
k_proj: ExLlamaV2Linear | None
v_proj: ExLlamaV2Linear | None
o_proj: ExLlamaV2Linear | None
q_norm: ExLlamaV2HeadNorm | None
k_norm: ExLlamaV2HeadNorm | None
q_handle: int | None
temp_state: torch.tensor
temp_q: torch.tensor
temp_k: torch.tensor
temp_v: torch.tensor
temp_o: torch.tensor
temp_dq: torch.tensor
# temp_kv: torch.tensor
temp_lora_size: int
has_norm: bool
has_residual: bool
class Params:
batch_size: int
seq_len: int
past_len: int | None
past_lens: list[int] | None
input_mask: torch.Tensor | None
additional_attn_mask: torch.Tensor | None
multi_cache: bool
attn_mask: torch.Tensor | None
attn_masks: torch.Tensor | None
position_offsets: torch.Tensor | None
past_lens_tensor: torch.Tensor | None
def __init__(self,
batch_size: int,
seq_len: int,
past_len: int | list[int],
input_mask: torch.Tensor,
additional_attn_mask: torch.Tensor,
position_offsets: torch.Tensor):
self.batch_size = batch_size
self.seq_len = seq_len
if isinstance(past_len, list):
self.past_len = None
self.past_lens = past_len
self.multi_cache = True
else:
self.past_len = past_len
self.past_lens = None
self.multi_cache = False
self.input_mask = input_mask
self.additional_attn_mask = additional_attn_mask
self.attn_mask = None
self.attn_masks = None
self.position_offsets = position_offsets
self.past_lens_tensor = None
def is_causal(self) -> bool:
return self.input_mask is None and self.additional_attn_mask is None
def get_position_offsets(self, device) -> torch.Tensor | None:
assert self.position_offsets is not None
if self.position_offsets.device != device:
self.position_offsets = safe_move_tensor(self.position_offsets, device)
return self.position_offsets
def get_past_lens(self, device) -> torch.Tensor | None:
assert self.past_lens is not None
if self.past_lens_tensor is None:
self.past_lens_tensor = torch.tensor(self.past_lens, dtype = torch.int, device = device)
elif self.past_lens_tensor.device != device:
self.past_lens_tensor = safe_move_tensor(self.past_lens_tensor, device)
return self.past_lens_tensor
def get_attn_mask(self, device) -> torch.Tensor | None:
if self.attn_mask is None:
self.attn_mask = self.build_attn_mask(device)
elif self.attn_mask.device != device:
self.attn_mask = safe_move_tensor(self.attn_mask, device)
return self.attn_mask
def get_attn_masks(self, device) -> torch.Tensor | None:
if self.attn_masks is None:
self.attn_masks = self.build_attn_masks(device)
elif self.attn_masks[0] is not None and self.attn_masks[0].device != device:
self.attn_masks = [(safe_move_tensor(m, device) if m is not None else None) for m in self.attn_masks]
return self.attn_masks
def build_single_attn_mask(self, batch_size, seq_len, past_len, device, input_mask, additional_attn_mask):
attn_mask = torch.zeros((batch_size, 1, seq_len, past_len + seq_len), dtype = torch.float16, device = device)
attn_mask_triu = torch.triu(torch.full((seq_len - 1, seq_len - 1), -65504.0))
attn_mask[:, :, : seq_len - 1, past_len + 1: past_len + seq_len] = attn_mask_triu
if input_mask is not None:
min_mask_width = min(input_mask.shape[-1], seq_len + past_len)
input_mask_part = safe_move_tensor(input_mask[:, :min_mask_width], attn_mask.device)
input_mask_part = input_mask_part.unsqueeze(1).unsqueeze(2)
attn_mask[:, :, :, :min_mask_width] = torch.minimum(attn_mask[:, :, :, :min_mask_width], input_mask_part)
if additional_attn_mask is not None:
attn_mask = torch.zeros((batch_size, 1, seq_len, past_len + seq_len), dtype = torch.float16, device = device)
attn_mask_triu = torch.triu(torch.full((seq_len - 1, seq_len - 1), -65504.0))
attn_mask[:, :, : seq_len - 1, past_len + 1: past_len + seq_len] = attn_mask_triu
min_mask_width = min(additional_attn_mask.shape[-1], seq_len + past_len)
additional_attn_mask_part = safe_move_tensor(additional_attn_mask[:, past_len:min_mask_width, :min_mask_width], attn_mask.device)
additional_attn_mask_part = additional_attn_mask_part.unsqueeze(1)
attn_mask = torch.minimum(attn_mask[:, :, :, :min_mask_width], additional_attn_mask_part)
# if additional_attn_mask is not None:
# min_mask_width_now_seq=min(additional_attn_mask.shape[-1]-past_len, seq_len)
# if min_mask_width_now_seq>0:
# min_mask_width = min(additional_attn_mask.shape[-1], seq_len + past_len)
# additional_attn_mask_part = safe_move_tensor(additional_attn_mask[:, past_len:past_len+min_mask_width_now_seq, :min_mask_width], attn_mask.device)
# additional_attn_mask_part = additional_attn_mask_part.unsqueeze(1)
# attn_mask[:, :, :min_mask_width_now_seq, :min_mask_width] = torch.minimum(attn_mask[:, :, :min_mask_width_now_seq, :min_mask_width], additional_attn_mask_part)
return attn_mask
def build_attn_mask(self, device) -> torch.Tensor | None:
assert not self.multi_cache, "Building single mask for multiple caches"
if self.input_mask is None and self.seq_len == 1: return None
return self.build_single_attn_mask(self.batch_size, self.seq_len, self.past_len, device, self.input_mask, self.additional_attn_mask)
def build_attn_masks(self, device) -> torch.Tensor | None:
assert self.multi_cache, "Building multiple masks for single cache"
attn_masks = []
for i, past_len in enumerate(self.past_lens):
if self.input_mask is None and self.seq_len == 1:
attn_masks.append(None)
else:
attn_masks.append(self.build_single_attn_mask(1, self.seq_len, past_len, device, self.input_mask[i], self.additional_attn_mask[i]))
return attn_masks
def __init__(self,
model: ExLlamaV2,
key: str,
layer_idx: int,
has_norm: bool = True,
has_residual: bool = True):
super().__init__(model, key)
cfg = self.model.config
self.layer_idx = layer_idx
self.has_norm = has_norm
self.has_residual = has_residual
self.q_handle = None
self.temp_lora_size = 0
hidden_size = cfg.hidden_size
if self.has_norm:
if cfg.arch.norm == "layernorm":
self.input_layernorm = ExLlamaV2LayerNorm(model, key + cfg.arch.norm_key_1)
elif cfg.arch.norm == "rmsnorm":
self.input_layernorm = ExLlamaV2RMSNorm(model, key + cfg.arch.norm_key_1)
else:
self.input_layernorm = None
f_a = 0
f_b = cfg.num_attention_heads * cfg.head_dim
f_c = f_b + cfg.num_key_value_heads * cfg.head_dim
f_d = f_c + cfg.num_key_value_heads * cfg.head_dim
f_key = (key + ".self_attn." + cfg.arch.fused_qkv_key) if cfg.arch.fused_qkv_key else None
self.q_proj = ExLlamaV2Linear(model, key + ".self_attn.q_proj", hidden_size, cfg.num_attention_heads * cfg.head_dim, cfg.arch.attention_bias_qkv, f_key = f_key, f_beg = f_a, f_end = f_b)
self.k_proj = ExLlamaV2Linear(model, key + ".self_attn.k_proj", hidden_size, cfg.num_key_value_heads * cfg.head_dim, cfg.arch.attention_bias_qkv, f_key = f_key, f_beg = f_b, f_end = f_c)
self.v_proj = ExLlamaV2Linear(model, key + ".self_attn.v_proj", hidden_size, cfg.num_key_value_heads * cfg.head_dim, cfg.arch.attention_bias_qkv, f_key = f_key, f_beg = f_c, f_end = f_d)
self.o_proj = ExLlamaV2Linear(model, key + ".self_attn.o_proj", cfg.num_attention_heads * cfg.head_dim, hidden_size, cfg.arch.attention_bias_o)
if cfg.use_qk_norm:
self.q_norm = ExLlamaV2HeadNorm(model, key + ".self_attn.q_norm", cfg.num_attention_heads, cfg.head_dim)
self.k_norm = ExLlamaV2HeadNorm(model, key + ".self_attn.k_norm", cfg.num_key_value_heads, cfg.head_dim)
else:
self.q_norm = None
self.k_norm = None
self.submodules = [self.q_proj,
self.k_proj,
self.v_proj,
self.o_proj]
if self.has_norm:
self.submodules += [self.input_layernorm]
if cfg.use_qk_norm:
self.submodules += [self.q_norm,
self.k_norm]
def numel(self) -> int:
numel = self.q_proj.numel() + \
self.k_proj.numel() + \
self.v_proj.numel() + \
self.o_proj.numel()
if self.input_layernorm is not None: numel += self.input_layernorm.numel()
if self.q_norm is not None: numel += self.q_norm.numel()
if self.k_norm is not None: numel += self.k_norm.numel()
return numel
def load(self):
if self.input_layernorm is not None: self.input_layernorm.load()
self.q_proj.load()
self.k_proj.load()
self.v_proj.load()
self.o_proj.load()
if self.q_norm is not None: self.q_norm.load()
if self.k_norm is not None: self.k_norm.load()
if self.q_proj.is_quant():
assert self.k_proj.is_quant() and self.v_proj.is_quant() and self.o_proj.is_quant(), "Partially quantized attention layer"
device_tensors = self.model.get_device_tensors(self.device_idx)
device_tensors.begin_scratch_alloc()
self.temp_state = device_tensors.get_scratch_slice(self.temp_state_size())
# self.temp_q = device_tensors.get_scratch_slice(self.temp_q_size())
# self.temp_k = device_tensors.get_scratch_slice(self.temp_k_size())
# self.temp_v = device_tensors.get_scratch_slice(self.temp_v_size())
self.temp_dq = device_tensors.get_scratch_slice(self.temp_dq_size())
# self.temp_kv = device_tensors.get_scratch_slice(self.temp_kv_size()) if self.model.config.num_attention_heads != self.model.config.num_key_value_heads else None
if self.has_norm:
norm_weight = self.input_layernorm.weight if self.input_layernorm.weight is not None else none_tensor
norm_bias = self.input_layernorm.bias if self.input_layernorm.bias is not None else none_tensor
is_rms = isinstance(self.input_layernorm, ExLlamaV2RMSNorm)
eps = self.input_layernorm.variance_epsilon
else:
norm_weight = none_tensor
norm_bias = none_tensor
is_rms = False
eps = 0
if self.q_norm is None:
q_norm = none_tensor
else:
q_norm = self.q_norm.weight
if self.k_norm is None:
k_norm = none_tensor
else:
k_norm = self.k_norm.weight
self.q_handle = ext_c.make_q_attn(norm_weight,
norm_bias,
is_rms,
eps,
self.q_proj.q_handle,
self.k_proj.q_handle,
self.v_proj.q_handle,
self.o_proj.q_handle,
self.temp_state,
# self.temp_q,
# self.temp_k,
# self.temp_v,
self.temp_dq,
self.model.config.max_input_len * self.model.config.max_batch_size,
self.model.config.hidden_size,
self.model.config.num_attention_heads,
self.model.config.num_key_value_heads,
self.model.config.head_dim,
self.model.config.max_seq_len,
self.has_residual,
self.model.config.arch.rope_neox_style,
q_norm,
k_norm)
def unload(self):
if self.q_handle is not None:
ext_c.free_q_attn(self.q_handle)
self.q_handle = None
if self.input_layernorm is not None: self.input_layernorm.unload()
if self.q_proj is not None: self.q_proj.unload()
if self.k_proj is not None: self.k_proj.unload()
if self.v_proj is not None: self.v_proj.unload()
self.o_proj.unload()
self.temp_state = None
self.temp_dq = None
if self.q_norm is not None: self.q_norm.unload()
if self.k_norm is not None: self.k_norm.unload()
def weight_footprint(self):
fp = self.q_proj.weight_footprint() + \
self.k_proj.weight_footprint() + \
self.v_proj.weight_footprint() + \
self.o_proj.weight_footprint()
if self.input_layernorm is not None:
fp += self.input_layernorm.weight_footprint()
if self.q_norm is not None:
fp += self.q_norm.weight_footprint()
if self.k_norm is not None:
fp += self.k_norm.weight_footprint()
return fp
def scratch_space_fixed(self):
return self.temp_state_size() + \
self.temp_dq_size()
def scratch_space(self):
return self.temp_state_size() + \
self.temp_q_size() + \
self.temp_k_size() + \
self.temp_v_size() + \
self.temp_dq_size() + \
self.temp_kv_size()
# self.temp_attn_size() + # Accounted for separately in model.set_device_map()
def temp_state_size(self):
return self.model.config.max_input_len * self.model.config.max_batch_size * self.model.config.num_attention_heads * self.model.config.head_dim * 2 + 128
def temp_q_size(self):
return self.model.config.max_input_len * self.model.config.max_batch_size * self.model.config.num_attention_heads * self.model.config.head_dim * 2 + 128
def temp_k_size(self):
return self.model.config.max_input_len * self.model.config.max_batch_size * self.model.config.num_key_value_heads * self.model.config.head_dim * 2 + 128
def temp_v_size(self):
return self.model.config.max_input_len * self.model.config.max_batch_size * self.model.config.num_key_value_heads * self.model.config.head_dim * 2 + 128
def temp_dq_size(self):
return max(self.q_proj.temp_dq_size(),
self.k_proj.temp_dq_size(),
self.v_proj.temp_dq_size(),
self.o_proj.temp_dq_size())
def temp_kv_size(self):
if self.model.config.num_key_value_heads == self.model.config.num_attention_heads: return 0
return 2 * self.model.config.max_seq_len * self.model.config.max_batch_size * self.model.config.num_attention_heads * self.model.config.head_dim * 2 + 128
def temp_attn_size(self):
global has_flash_attn
att_max = min(self.model.config.max_attention_size, self.model.config.max_seq_len ** 2)
if has_flash_attn and not self.model.config.no_flash_attn:
eff = self.model.config.max_attention_size ** 0.5 / 190 # based on supposed memory savings listed in flash-attn repo + some fudging
att_max //= eff
return 2 * att_max * self.model.config.num_attention_heads * 2 + 128
def set_device_idx(self, idx):
super().set_device_idx(idx)
if self.input_layernorm is not None: self.input_layernorm.set_device_idx(idx)
self.q_proj.set_device_idx(idx)
self.k_proj.set_device_idx(idx)
self.v_proj.set_device_idx(idx)
self.o_proj.set_device_idx(idx)
if self.q_norm is not None: self.q_norm.set_device_idx(idx)
if self.k_norm is not None: self.k_norm.set_device_idx(idx)
def repeat_kv(self, hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
if n_rep == 1: return hidden_states
batch, num_key_value_heads, slen, head_dim = hidden_states.shape
hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
hidden_states = hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
return hidden_states
def forward(self,
hidden_states: torch.Tensor,
cache: ExLlamaV2CacheBase | None = None,
attn_params: ExLlamaV2Attention.Params | None = None,
past_len: int | None = None,
intermediates: bool = False,
loras: list[ExLlamaV2Lora] | None = None,
**kwargs) -> torch.Tensor | dict[str: torch.Tensor]:
global has_flash_attn
if 'importance' in kwargs.keys() and kwargs['importance'] is not None:
return self.forward_query_select_token(hidden_states,
cache,
attn_params,
past_len,
intermediates,
loras = loras,
**kwargs)
if 'rate' in kwargs.keys() and kwargs['rate'] is not None:
return self.forward_rate(hidden_states,
cache,
attn_params,
past_len,
intermediates,
loras = loras,
**kwargs)
if 'k_need_index' in kwargs.keys():
return self.forward_retrival(hidden_states,
cache,
attn_params,
past_len,
intermediates,
loras = loras,
**kwargs)
elif 'dump_forward' in kwargs.keys():
return self.forward_dump(hidden_states,
cache,
attn_params,
past_len,
intermediates,
loras = loras,
**kwargs)
elif 'dump_forward_importance' in kwargs.keys():
return self.forward_dump_importance(hidden_states,
cache,
attn_params,
past_len,
intermediates,
loras = loras,
**kwargs)
if self.q_handle is None or intermediates:
return self.forward_torch(hidden_states,
cache,
attn_params,
past_len,
intermediates,
loras = loras,
**kwargs)
batch_size = hidden_states.shape[0]
q_len = hidden_states.shape[1]
cfg = self.model.config
direct = (batch_size == 1 and cache is not None and isinstance(cache, ExLlamaV2CacheBase))
# past_len = 0
# if cache is not None:
# if isinstance(cache, ExLlamaV2Cache):
# past_len = cache.current_seq_len
# if isinstance(cache, list):
# past_len = [c.current_seq_len for c in cache]
num_attention_heads = cfg.num_attention_heads
num_key_value_heads = cfg.num_key_value_heads
num_key_value_groups = cfg.num_key_value_groups
head_dim = cfg.head_dim
hidden_size = cfg.hidden_size
constants = self.model.get_device_tensors(self.device_idx)
q_shape = hidden_states.shape[:-1] + (self.q_proj.out_features,)
k_shape = hidden_states.shape[:-1] + (self.k_proj.out_features,)
v_shape = hidden_states.shape[:-1] + (self.v_proj.out_features,)
q_states = torch.empty(q_shape, device = hidden_states.device, dtype = torch.half)
# If conditions are right we can write the K/V projections directly into the cache
if direct:
batch_keys, batch_values = cache.get_kv_state(self.layer_idx, batch_size, 0, past_len)
k_states = batch_keys.narrow(0, 0, batch_size).narrow(1, past_len, q_len)
v_states = batch_values.narrow(0, 0, batch_size).narrow(1, past_len, q_len)
else:
k_states = torch.empty(k_shape, device = hidden_states.device, dtype = torch.half)
v_states = torch.empty(v_shape, device = hidden_states.device, dtype = torch.half)
# RMS norm, Q/K/V projections, position embeddings
if loras is None or self.temp_lora_size == 0:
pass_loras = []
pass_lora_temp = none_tensor
else:
pass_loras = [id(x) for x in loras]
pass_lora_temp = torch.empty((self.temp_lora_size,), dtype = torch.half, device = hidden_states.device)
if attn_params.multi_cache:
pass_past_len_1 = -1
pass_past_len_2 = attn_params.get_past_lens(hidden_states.device)
elif attn_params.position_offsets is not None:
pass_past_len_1 = past_len
pass_past_len_2 = attn_params.get_position_offsets(hidden_states.device)
else:
pass_past_len_1 = past_len
pass_past_len_2 = none_tensor
ext_c.q_attn_forward_1(self.q_handle,
hidden_states,
batch_size,
q_len,
pass_past_len_1,
pass_past_len_2,
q_states,
k_states,
v_states,
constants.sin,
constants.cos,
pass_loras,
pass_lora_temp)
# Shape for attention
q_states = q_states.view(batch_size, q_len, num_attention_heads, head_dim)
k_states = k_states.view(batch_size, q_len, num_key_value_heads, head_dim)
v_states = v_states.view(batch_size, q_len, num_key_value_heads, head_dim)
# Regular (batched) attention with optional padding mask
if cache is None or isinstance(cache, ExLlamaV2CacheBase):
# Add keys and values to cache
if cache is not None:
if direct:
k_states = batch_keys.narrow(0, 0, batch_size).narrow(1, 0, past_len + q_len)
v_states = batch_values.narrow(0, 0, batch_size).narrow(1, 0, past_len + q_len)
else:
batch_keys, batch_values = cache.get_kv_state(self.layer_idx, batch_size, 0, past_len)
new_keys = batch_keys.narrow(0, 0, batch_size).narrow(1, past_len, q_len)
new_values = batch_values.narrow(0, 0, batch_size).narrow(1, past_len, q_len)
new_keys.copy_(k_states)
new_values.copy_(v_states)
# Key/value tensors with past
k_states = batch_keys.narrow(0, 0, batch_size).narrow(1, 0, past_len + q_len)
v_states = batch_values.narrow(0, 0, batch_size).narrow(1, 0, past_len + q_len)
# Torch matmul attention
if cfg.no_flash_attn or not has_flash_attn or not attn_params.is_causal():
q_states = q_states.transpose(1, 2)
k_states = k_states.transpose(1, 2)
v_states = v_states.transpose(1, 2)
k_states = self.repeat_kv(k_states, num_key_value_groups)
k_states = k_states.transpose(-1, -2)
attn_weights = torch.matmul(q_states, k_states)
k_states = None
q_states = None
attn_weights /= math.sqrt(head_dim)
attn_mask = attn_params.get_attn_mask(hidden_states.device)
if attn_mask is not None: attn_weights = attn_weights + attn_mask
attn_weights = nn.functional.softmax(attn_weights, dim = -1, dtype = torch.float16)
v_states = self.repeat_kv(v_states, num_key_value_groups)
attn_output = torch.matmul(attn_weights, v_states)
v_states = None
attn_output = attn_output.transpose(1, 2)
attn_output = attn_output.reshape((batch_size, q_len, cfg.num_attention_heads * cfg.head_dim))
# Flash Attention 2
else:
# TODO: Enable flash-attn with input mask
attn_output = flash_attn_func(q_states, k_states, v_states, causal = True)
attn_output = attn_output.reshape((batch_size, q_len, cfg.num_attention_heads * cfg.head_dim))
# xformers memory_efficient_attention
# attn_output = xops.memory_efficient_attention(q_states, k_states, v_states, attn_bias = xops.LowerTriangularMask())
# attn_output = attn_output.reshape((batch_size, q_len, hidden_size));
# Torch SDP attention:
# q_states = q_states.transpose(1, 2)
# k_states = k_states.transpose(1, 2)
# v_states = v_states.transpose(1, 2)
#
# # k_states = self.repeat_kv(k_states, num_key_value_groups)
# # v_states = self.repeat_kv(v_states, num_key_value_groups)
#
# attn_output = F.scaled_dot_product_attention(q_states, k_states, v_states, attn_mask = attn_mask, is_causal = False)
# attn_output = attn_output.transpose(1, 2)
# attn_output = attn_output.reshape((batch_size, q_len, hidden_size))
# Update 8-bit/Q4 cache
if cache is not None:
cache.store_kv_state(self.layer_idx, batch_size, past_len, q_len)
# Multiple caches
elif isinstance(cache, list):
assert attn_params.multi_cache
attn_masks = attn_params.get_attn_masks(hidden_states.device)
attn_outputs = []
for i in range(len(cache)):
# TODO: Once nested tensors are finalized in Torch, this could all be batched, probably
# Add keys and values to cache
batch_keys, batch_values = cache[i].get_kv_state(self.layer_idx, 1, 0, past_len[i])
new_keys = batch_keys.narrow(1, past_len[i], q_len)
new_values = batch_values.narrow(1, past_len[i], q_len)
new_keys.copy_(k_states.narrow(0, i, 1))
new_values.copy_(v_states.narrow(0, i, 1))
# Store updated cache values
cache[i].store_kv_state(self.layer_idx, 1, past_len[i], q_len)
# Key/value tensors with past
k_states_b = batch_keys.narrow(1, 0, past_len[i] + q_len)
v_states_b = batch_values.narrow(1, 0, past_len[i] + q_len)
# Torch matmul attention
# TODO: enable flash-attn
q_states_b = q_states.transpose(1, 2).narrow(0, i, 1)
k_states_b = k_states_b.transpose(1, 2)
v_states_b = v_states_b.transpose(1, 2)
k_states_b = self.repeat_kv(k_states_b, num_key_value_groups)
k_states_b = k_states_b.transpose(-1, -2)
attn_weights = torch.matmul(q_states_b, k_states_b)
q_states_b = None
k_states_b = None
attn_weights /= math.sqrt(head_dim)
if attn_masks[i] is not None: attn_weights = attn_weights + attn_masks[i]
attn_weights = nn.functional.softmax(attn_weights, dim = -1, dtype = torch.float16)
v_states_b = self.repeat_kv(v_states_b, num_key_value_groups)
attn_output_b = torch.matmul(attn_weights, v_states_b)
v_states_b = None
attn_outputs.append(attn_output_b)
q_states = None
k_states = None
v_states = None
attn_output = torch.cat(attn_outputs, dim = 0)
attn_output = attn_output.transpose(1, 2)
attn_output = attn_output.reshape((batch_size, q_len, hidden_size))
# Output projection
ext_c.q_attn_forward_2(self.q_handle,
hidden_states,
attn_output,
batch_size,
q_len,
pass_loras,
pass_lora_temp)
attn_output = None
attn_weights = None
return hidden_states
def forward_torch(self,
hidden_states: torch.Tensor,
cache: ExLlamaV2CacheBase | None = None,
attn_params: ExLlamaV2Attention.Params | None = None,
past_len: int | None = None,
intermediates: bool = False,
loras: list[ExLlamaV2Lora] | None = None,
**kwargs) -> torch.Tensor | dict:
cfg = self.model.config
num_attention_heads = cfg.num_attention_heads
num_key_value_heads = cfg.num_key_value_heads
num_key_value_groups = cfg.num_key_value_groups
head_dim = cfg.head_dim
hidden_size = cfg.hidden_size
batch_size, q_len, _ = hidden_states.size()
past_len = 0 if cache is None else cache.current_seq_len
# Project q, k, v
residual = hidden_states
post_norm = self.input_layernorm.forward(hidden_states) if self.has_norm else hidden_states
query_states = self.q_proj.forward(post_norm, loras = loras)
key_states = self.k_proj.forward(post_norm, loras = loras)
value_states = self.v_proj.forward(post_norm, loras = loras)
# Shape for attention
query_states = query_states.view(batch_size, q_len, num_attention_heads, head_dim)
key_states = key_states.view(batch_size, q_len, num_key_value_heads, head_dim)
value_states = value_states.view(batch_size, q_len, num_key_value_heads, head_dim)
# Apply Q/K norms
if cfg.use_qk_norm:
query_states = self.q_norm.forward(query_states)
key_states = self.k_norm.forward(key_states)
# Apply position embeddings
constants = self.model.get_device_tensors(self.device_idx, scratch = False)
if attn_params.position_offsets is not None:
position_offsets = attn_params.get_position_offsets(hidden_states.device)
else:
position_offsets = none_tensor
ext_c.rope_(query_states, constants.sin, constants.cos, past_len, num_attention_heads, head_dim, position_offsets, self.model.config.arch.rope_neox_style)
ext_c.rope_(key_states, constants.sin, constants.cos, past_len, num_key_value_heads, head_dim, position_offsets, self.model.config.arch.rope_neox_style)
# Add keys and values to cache
if cache is not None:
batch_keys, batch_values = cache.get_kv_state(self.layer_idx, batch_size, 0, past_len)
new_keys = batch_keys.narrow(1, past_len, q_len).narrow(0, 0, batch_size)
new_values = batch_values.narrow(1, past_len, q_len).narrow(0, 0, batch_size)
new_keys.copy_(key_states)
new_values.copy_(value_states)
# Key/value tensors with past
key_states = batch_keys.narrow(1, 0, past_len + q_len).narrow(0, 0, batch_size)
value_states = batch_values.narrow(1, 0, past_len + q_len).narrow(0, 0, batch_size)
if isinstance(cache,ExLlamaV2Cache_CPU):
cache.store_kv_state(self.layer_idx, batch_size, past_len+q_len,key_states,value_states)
torch.cuda.empty_cache()
# Torch matmul attention
if cfg.no_flash_attn or not has_flash_attn or not attn_params.is_causal():
query_states = query_states.transpose(1, 2)
key_states = key_states.transpose(1, 2)
value_states = value_states.transpose(1, 2)
key_states = self.repeat_kv(key_states, cfg.num_key_value_groups)
key_states = key_states.transpose(-1, -2)
attn_weights = torch.matmul(query_states, key_states)
# weight = attn_weights.squeeze(0)
# weight = weight[:,-62:,:-62]
# torch.save(weight,f'weight_{self.layer_idx}.pt')
attn_weights /= math.sqrt(head_dim)
attn_mask = attn_params.get_attn_mask(hidden_states.device)
if attn_mask is not None: attn_weights = attn_weights + attn_mask
attn_weights = nn.functional.softmax(attn_weights, dim = -1, dtype = torch.float16)
value_states = self.repeat_kv(value_states, cfg.num_key_value_groups)
attn_output = torch.matmul(attn_weights, value_states)
attn_output = attn_output.transpose(1, 2)
attn_output = attn_output.reshape((batch_size, q_len, cfg.num_attention_heads * cfg.head_dim))
# Flash Attention 2
else:
attn_output = flash_attn_func(query_states, key_states, value_states, causal = True)
attn_output = attn_output.reshape((batch_size, q_len, cfg.num_attention_heads * cfg.head_dim))
# Update 8-bit/Q4 cache
if cache is not None and not isinstance(cache, ExLlamaV2Cache_CPU):
cache.store_kv_state(self.layer_idx, batch_size, past_len, q_len)
# Output projection
attn_proj = self.o_proj.forward(attn_output, loras = loras)
# Add residual connection
hidden_states = (attn_proj + residual) if self.has_residual else attn_proj
if intermediates:
return {"post_norm": post_norm,
"attn_output": attn_output,
"hidden_states": hidden_states}
else:
return hidden_states
def forward_query_select_token(self,
hidden_states: torch.Tensor,
cache: ExLlamaV2CacheBase | None = None,
attn_params: ExLlamaV2Attention.Params | None = None,
past_len: int | None = None,
intermediates: bool = False,
loras: list[ExLlamaV2Lora] | None = None,
**kwargs) -> torch.Tensor | dict:
importance = kwargs['importance']
tokens_len = kwargs['tokens_len']
cfg = self.model.config
num_attention_heads = cfg.num_attention_heads
num_key_value_heads = cfg.num_key_value_heads
num_key_value_groups = cfg.num_key_value_groups
head_dim = cfg.head_dim
hidden_size = cfg.hidden_size
batch_size, q_len, _ = hidden_states.size()
past_len = 0 if cache is None else cache.current_seq_len
# Project q, k, v
residual = hidden_states
post_norm = self.input_layernorm.forward(hidden_states) if self.has_norm else hidden_states
query_states = self.q_proj.forward(post_norm, loras = loras)
key_states = self.k_proj.forward(post_norm, loras = loras)
value_states = self.v_proj.forward(post_norm, loras = loras)
# Shape for attention
query_states = query_states.view(batch_size, q_len, num_attention_heads, head_dim)
key_states = key_states.view(batch_size, q_len, num_key_value_heads, head_dim)
value_states = value_states.view(batch_size, q_len, num_key_value_heads, head_dim)
# Apply Q/K norms
if cfg.use_qk_norm:
query_states = self.q_norm.forward(query_states)
key_states = self.k_norm.forward(key_states)
# Apply position embeddings
constants = self.model.get_device_tensors(self.device_idx, scratch = False)
if attn_params.position_offsets is not None:
position_offsets = attn_params.get_position_offsets(hidden_states.device)
else:
position_offsets = none_tensor
ext_c.rope_(query_states, constants.sin, constants.cos, past_len, num_attention_heads, head_dim, position_offsets, self.model.config.arch.rope_neox_style)
ext_c.rope_(key_states, constants.sin, constants.cos, past_len, num_key_value_heads, head_dim, position_offsets, self.model.config.arch.rope_neox_style)
# Add keys and values to cache
if cache is not None:
batch_keys, batch_values = cache.get_kv_state(self.layer_idx, batch_size, 0, past_len)
new_keys = batch_keys.narrow(1, past_len, q_len).narrow(0, 0, batch_size)
new_values = batch_values.narrow(1, past_len, q_len).narrow(0, 0, batch_size)
new_keys.copy_(key_states)
new_values.copy_(value_states)
# Key/value tensors with past
key_states = batch_keys.narrow(1, 0, past_len + q_len).narrow(0, 0, batch_size)
value_states = batch_values.narrow(1, 0, past_len + q_len).narrow(0, 0, batch_size)
if isinstance(cache,ExLlamaV2Cache_CPU):
cache.store_kv_state(self.layer_idx, batch_size, past_len+q_len,key_states,value_states)
torch.cuda.empty_cache()
# Torch matmul attention
if cfg.no_flash_attn or not has_flash_attn or not attn_params.is_causal():
query_states = query_states.transpose(1, 2)
key_states = key_states.transpose(1, 2)
value_states = value_states.transpose(1, 2)
key_states = self.repeat_kv(key_states, cfg.num_key_value_groups)
key_states = key_states.transpose(-1, -2)
attn_weights = torch.matmul(query_states, key_states)
# weight = attn_weights.squeeze(0)
# weight = weight[:,-62:,:-62]
# torch.save(weight,f'weight_{self.layer_idx}.pt')
attn_weights /= math.sqrt(head_dim)
attn_mask = attn_params.get_attn_mask(hidden_states.device)
if attn_mask is not None: attn_weights = attn_weights + attn_mask
attn_weights = nn.functional.softmax(attn_weights, dim = -1, dtype = torch.float16)
value_states = self.repeat_kv(value_states, cfg.num_key_value_groups)
attn_output = torch.matmul(attn_weights, value_states)
attn_output = attn_output.transpose(1, 2)
attn_output = attn_output.reshape((batch_size, q_len, cfg.num_attention_heads * cfg.head_dim))
# Flash Attention 2
else:
attn_output = flash_attn_func(query_states, key_states, value_states, causal = True)
attn_output = attn_output.reshape((batch_size, q_len, cfg.num_attention_heads * cfg.head_dim))
# Update 8-bit/Q4 cache
if cache is not None and not isinstance(cache, ExLlamaV2Cache_CPU):
cache.store_kv_state(self.layer_idx, batch_size, past_len, q_len)
# Output projection
attn_proj = self.o_proj.forward(attn_output, loras = loras)
# Add residual connection