File size: 57,330 Bytes
344e159 |
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 |
"""
ComplexNet model with Dummy Complex Semantic
backpropogation with simple autograd
"""
from typing import Optional, Tuple, Callable, Any, Dict, List
import math
from functools import partial
import torch
import torch.nn as nn
from torch.nn import init
import torch.nn.functional as F
from transformers.cache_utils import Cache, StaticCache
from transformers.modeling_attn_mask_utils import AttentionMaskConverter
from transformers.modeling_utils import PreTrainedModel
try:
from transformers.generation.utils import GenerationMixin
except ImportError:
from transformers.modeling_utils import GenerationMixin
from transformers.pytorch_utils import ALL_LAYERNORM_LAYERS
from transformers.modeling_outputs import CausalLMOutputWithPast
from transformers.utils import (
is_flash_attn_2_available,
is_flash_attn_greater_or_equal_2_10,
logging,
)
from .configuration_complexnet import ComplexNetConfig
from transformers.cache_utils import Cache
import torch
from packaging import version
from transformers.configuration_utils import PretrainedConfig
from transformers.utils import (
is_hqq_available,
is_optimum_quanto_available,
is_torchdynamo_compiling,
logging,
)
from transformers.utils.deprecation import deprecate_kwarg
logger = logging.get_logger(__name__)
class ComplexDynamicCache(Cache):
"""
A cache that grows dynamically as more tokens are generated. This is the default for generative models.
It stores the Key and Value states as a list of tensors, one for each layer. The expected shape for each tensor is
`[batch_size, num_heads, seq_len, head_dim]`.
Example:
```python
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, DynamicCache
>>> model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-0.5B-Instruct")
>>> tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-0.5B-Instruct")
>>> inputs = tokenizer(text="My name is Qwen2", return_tensors="pt")
>>> # Prepare a cache class and pass it to model's forward
>>> past_key_values = DynamicCache()
>>> outputs = model(**inputs, past_key_values=past_key_values, use_cache=True)
>>> outputs.past_key_values # access cache filled with key/values from generation
DynamicCache()
```
"""
@deprecate_kwarg("num_hidden_layers", version="4.47.0")
def __init__(self, num_hidden_layers: Optional[int] = None) -> None:
super().__init__()
self._seen_tokens = (
0 # Used in `generate` to keep tally of how many tokens the cache has seen
)
self.key_real_cache: List[torch.Tensor] = []
self.key_imag_cache: List[torch.Tensor] = []
self.value_real_cache: List[torch.Tensor] = []
self.value_imag_cache: List[torch.Tensor] = []
def __getitem__(self, layer_idx: int) -> List[Tuple[torch.Tensor]]:
"""
Support for backwards-compatible `past_key_value` indexing, e.g. `past_key_value[0][0].shape[2]` to get the
sequence length.
"""
if layer_idx < len(self):
return (
self.key_real_cache[layer_idx],
self.key_imag_cache[layer_idx],
self.value_real_cache[layer_idx],
self.value_imag_cache[layer_idx],
)
else:
raise KeyError(
f"Cache only has {len(self)} layers, attempted to access layer with index {layer_idx}"
)
def __iter__(self):
"""
Support for backwards-compatible `past_key_value` iteration, e.g. `for x in past_key_value:` to iterate over
keys and values
"""
for layer_idx in range(len(self)):
yield (
self.key_real_cache[layer_idx],
self.key_imag_cache[layer_idx],
self.value_real_cache[layer_idx],
self.value_imag_cache[layer_idx],
)
def __len__(self):
"""
Support for backwards-compatible `past_key_value` length, e.g. `len(past_key_value)`. This value corresponds
to the number of layers in the model.
"""
return len(self.key_real_cache)
def update(
self,
key_real_states: torch.Tensor,
key_imag_states: torch.Tensor,
value_real_states: torch.Tensor,
value_imag_states: torch.Tensor,
layer_idx: int,
cache_kwargs: Optional[Dict[str, Any]] = None,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
"""
Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`.
Parameters:
key_states (`torch.Tensor`):
The new key states to cache.
value_states (`torch.Tensor`):
The new value states to cache.
layer_idx (`int`):
The index of the layer to cache the states for.
cache_kwargs (`Dict[str, Any]`, `optional`):
Additional arguments for the cache subclass. No additional arguments are used in `DynamicCache`.
Return:
A tuple containing the updated key and value states.
"""
# Update the number of seen tokens
if layer_idx == 0:
self._seen_tokens += key_real_states.shape[-2]
# Update the cache
if key_real_states is not None:
if len(self.key_real_cache) <= layer_idx:
# There may be skipped layers, fill them with empty lists
for _ in range(len(self.key_real_cache), layer_idx):
self.key_real_cache.append([])
# self.key_imag_cache.append([])
self.value_real_cache.append([])
self.value_imag_cache.append([])
self.key_real_cache.append(key_real_states)
# self.key_imag_cache.append(key_imag_states)
self.value_real_cache.append(value_real_states)
self.value_imag_cache.append(value_imag_states)
elif (
len(self.key_real_cache[layer_idx]) == 0
): # fills previously skipped layers; checking for tensor causes errors
self.key_real_cache[layer_idx] = key_real_states
# self.key_imag_cache[layer_idx] = key_imag_states
self.value_real_cache[layer_idx] = value_real_states
self.value_imag_cache[layer_idx] = value_imag_states
else:
self.key_real_cache[layer_idx] = torch.cat(
[self.key_real_cache[layer_idx], key_real_states], dim=-2
)
# self.key_imag_cache[layer_idx] = torch.cat([self.key_imag_cache[layer_idx], key_imag_states], dim=-2)
self.value_real_cache[layer_idx] = torch.cat(
[self.value_real_cache[layer_idx], value_real_states], dim=-2
)
self.value_imag_cache[layer_idx] = torch.cat(
[self.value_imag_cache[layer_idx], value_imag_states], dim=-2
)
return (
self.key_real_cache[layer_idx],
# self.key_imag_cache[layer_idx],
None,
self.value_real_cache[layer_idx],
self.value_imag_cache[layer_idx],
)
def get_seq_length(self, layer_idx: Optional[int] = 0) -> int:
"""Returns the sequence length of the cached states. A layer index can be optionally passed."""
# TODO: deprecate this function in favor of `cache_position`
is_empty_layer = (
len(self.key_real_cache) == 0 # no cache in any layer
or len(self.key_real_cache)
<= layer_idx # skipped `layer_idx` and hasn't run a layer with cache after it
or len(self.key_real_cache[layer_idx]) == 0 # the layer has no cache
)
layer_seq_length = (
self.key_real_cache[layer_idx].shape[-2] if not is_empty_layer else 0
)
return layer_seq_length
def get_max_cache_shape(self) -> Optional[int]:
"""Returns the maximum sequence length of the cache object. DynamicCache does not have a maximum length."""
return None
def to_legacy_cache(
self,
) -> Tuple[
Tuple[torch.Tensor],
Tuple[torch.Tensor],
Tuple[torch.Tensor],
Tuple[torch.Tensor],
]:
"""Converts the `DynamicCache` instance into the its equivalent in the legacy cache format. Used for
backward compatibility."""
legacy_cache = ()
for layer_idx in range(len(self)):
legacy_cache += (
(
self.key_real_cache[layer_idx],
self.key_imag_cache[layer_idx],
self.value_real_cache[layer_idx],
self.value_imag_cache[layer_idx],
),
)
return legacy_cache
@classmethod
@deprecate_kwarg("num_hidden_layers", version="4.47.0")
def from_legacy_cache(
cls,
past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
num_hidden_layers: int = None,
) -> "ComplexDynamicCache":
"""Converts a cache in the legacy cache format into an equivalent `DynamicCache`. Used for
backward compatibility."""
cache = cls()
if past_key_values is not None:
for layer_idx in range(len(past_key_values)):
(
key_real_states,
key_imag_states,
value_real_states,
value_imag_states,
) = past_key_values[layer_idx]
cache.update(
key_real_states,
key_imag_states,
value_real_states,
value_imag_states,
layer_idx,
)
return cache
def crop(self, max_length: int):
"""Crop the past key values up to a new `max_length` in terms of tokens. `max_length` can also be
negative to remove `max_length` tokens. This is used in assisted decoding and contrastive search.
"""
# In case it is negative
if max_length < 0:
max_length = self.get_seq_length() - abs(max_length)
if self.get_seq_length() <= max_length:
return
self._seen_tokens = max_length
for idx in range(len(self.key_real_cache)):
if self.key_real_cache[idx] != []:
self.key_real_cache[idx] = self.key_real_cache[idx][..., :max_length, :]
self.key_imag_cache[idx] = self.key_imag_cache[idx][..., :max_length, :]
self.value_real_cache[idx] = self.value_real_cache[idx][
..., :max_length, :
]
self.value_imag_cache[idx] = self.value_imag_cache[idx][
..., :max_length, :
]
@deprecate_kwarg("num_hidden_layers", version="4.47.0")
def batch_split(
self, full_batch_size: int, split_size: int, num_hidden_layers: int = None
) -> List["ComplexDynamicCache"]:
"""Split the current instance into a list of `DynamicCache` by the batch size. This will be used by
`_split_model_inputs()` in `generation.utils`"""
out = []
for i in range(0, full_batch_size, split_size):
current_split = ComplexDynamicCache()
current_split._seen_tokens = self._seen_tokens
current_split.key_real_cache = [
tensor[i : i + split_size] for tensor in self.key_real_cache
]
current_split.key_imag_cache = [
tensor[i : i + split_size] for tensor in self.key_imag_cache
]
current_split.value_real_cache = [
tensor[i : i + split_size] for tensor in self.value_real_cache
]
current_split.value_imag_cache = [
tensor[i : i + split_size] for tensor in self.value_imag_cache
]
out.append(current_split)
return out
@classmethod
@deprecate_kwarg("num_hidden_layers", version="4.47.0")
def from_batch_splits(
cls, splits: List["ComplexDynamicCache"], num_hidden_layers: int = None
) -> "ComplexDynamicCache":
"""This is the opposite of the above `batch_split()` method. This will be used by `stack_model_outputs` in
`generation.utils`"""
cache = cls()
for idx in range(len(splits[0])):
key_real_cache = [
current.key_real_cache[idx]
for current in splits
if current.key_real_cache[idx] != []
]
key_imag_cache = [
current.key_imag_cache[idx]
for current in splits
if current.key_imag_cache[idx] != []
]
value_real_cache = [
current.value_real_cache[idx]
for current in splits
if current.value_real_cache[idx] != []
]
value_imag_cache = [
current.value_imag_cache[idx]
for current in splits
if current.value_imag_cache[idx] != []
]
# value_cache = [current.value_cache[idx] for current in splits if current.value_cache[idx] != []]
if key_real_cache != []:
layer_keys_real = torch.cat(key_real_cache, dim=0)
layer_keys_imag = torch.cat(key_imag_cache, dim=0)
layer_values_real = torch.cat(value_real_cache, dim=0)
layer_values_imag = torch.cat(value_imag_cache, dim=0)
# layer_values = torch.cat(value_cache, dim=0)
cache.update(
layer_keys_real,
layer_keys_imag,
layer_values_real,
layer_values_imag,
idx,
)
return cache
def batch_repeat_interleave(self, repeats: int):
"""Repeat the cache `repeats` times in the batch dimension. Used in contrastive search."""
for layer_idx in range(len(self)):
self.key_real_cache[layer_idx] = self.key_real_cache[
layer_idx
].repeat_interleave(repeats, dim=0)
self.key_imag_cache[layer_idx] = self.key_imag_cache[
layer_idx
].repeat_interleave(repeats, dim=0)
self.value_real_cache[layer_idx] = self.value_real_cache[
layer_idx
].repeat_interleave(repeats, dim=0)
self.value_imag_cache[layer_idx] = self.value_imag_cache[
layer_idx
].repeat_interleave(repeats, dim=0)
# self.value_cache[layer_idx] = self.value_cache[layer_idx].repeat_interleave(repeats, dim=0)
def batch_select_indices(self, indices: torch.Tensor):
"""Only keep the `indices` in the batch dimension of the cache. Used in contrastive search."""
for layer_idx in range(len(self)):
self.key_real_cache[layer_idx] = self.key_real_cache[layer_idx][
indices, ...
]
self.key_imag_cache[layer_idx] = self.key_imag_cache[layer_idx][
indices, ...
]
self.value_real_cache[layer_idx] = self.value_real_cache[layer_idx][
indices, ...
]
self.value_imag_cache[layer_idx] = self.value_imag_cache[layer_idx][
indices, ...
]
if is_flash_attn_2_available():
from flash_attn import flash_attn_func, flash_attn_varlen_func
from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input
_CONFIG_FOR_DOC = "ComplexNetConfig"
class DirectionQuantSTE(torch.autograd.Function):
@staticmethod
def forward(ctx, w_real: torch.Tensor, w_imag: torch.Tensor):
phase = torch.angle(w_real + 1j * w_imag)
real_scale = 1.0 / torch.clamp(w_real.abs().mean(), min=1e-5)
imag_scale = 1.0 / torch.clamp(w_imag.abs().mean(), min=1e-5)
w_real_scaled = w_real * real_scale
w_imag_scaled = w_imag * imag_scale
qw_real = torch.zeros_like(w_real_scaled)
qw_imag = torch.zeros_like(w_imag_scaled)
qw_real[(phase >= -torch.pi / 4) & (phase < torch.pi / 4)] = 1.0
qw_imag[(phase >= torch.pi / 4) & (phase < 3 * torch.pi / 4)] = 1.0
qw_real[(phase >= 3 * torch.pi / 4) | (phase < -3 * torch.pi / 4)] = -1.0
qw_imag[(phase >= -3 * torch.pi / 4) & (phase < -torch.pi / 4)] = -1.0
qw_real = qw_real / real_scale
qw_imag = qw_imag / imag_scale
return qw_real, qw_imag
@staticmethod
def backward(ctx, grad_real, grad_imag):
return grad_real, grad_imag
def weight_quant_qat(w_real: torch.Tensor, w_imag: torch.Tensor):
return DirectionQuantSTE.apply(w_real, w_imag)
class ComplexWeightQuantizer(nn.Module):
def __init__(self):
super().__init__()
def forward(self, w_real, w_imag):
return weight_quant_qat(w_real, w_imag)
class ActivationQuantSTE(torch.autograd.Function):
@staticmethod
def forward(ctx, x_real: torch.Tensor, x_imag: torch.Tensor):
real_scale = 127.0 / x_real.abs().max(dim=-1, keepdim=True).values.clamp_(
min=1e-5
)
imag_scale = 127.0 / x_imag.abs().max(dim=-1, keepdim=True).values.clamp_(
min=1e-5
)
qx_real = x_real * real_scale
qx_real = qx_real.contiguous()
qx_real.round_()
qx_real.clamp_(-128, 127)
qx_real.div_(real_scale)
qx_imag = x_imag * imag_scale
qx_imag = qx_imag.contiguous()
qx_imag.round_()
qx_imag.clamp_(-128, 127)
qx_imag.div_(imag_scale)
return qx_real, qx_imag
@staticmethod
def backward(ctx, grad_real, grad_imag):
# STE: 直接通过梯度
return grad_real, grad_imag
def activation_quant_qat(x_real: torch.Tensor, x_imag: torch.Tensor):
return ActivationQuantSTE.apply(x_real, x_imag)
class ComplexActivationQuantizer(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x_real, x_imag):
return activation_quant_qat(x_real, x_imag)
class HalfComplexLinear(nn.Module):
"""
HalfComplexLinear is a linear layer that only outputs real_output.
"""
def __init__(self, in_features: int, out_features: int):
super(HalfComplexLinear, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight_real = nn.Parameter(
torch.empty(self.out_features, self.in_features)
)
self.weight_imag = nn.Parameter(
torch.empty(self.out_features, self.in_features)
)
self.act_quantizer = ComplexActivationQuantizer()
self.weight_quantizer = ComplexWeightQuantizer()
self.reset_parameters()
def reset_parameters(self):
init.kaiming_uniform_(self.weight_real, a=math.sqrt(5))
init.kaiming_uniform_(self.weight_imag, a=math.sqrt(5))
def forward(self, x_real: torch.Tensor, x_imag: torch.Tensor) -> torch.Tensor:
# qw_real, qw_imag = self.weight_quantizer(self.weight_real, self.weight_imag)
# qx_real, qx_imag = self.act_quantizer(x_real, x_imag)
# out_real = F.linear(qx_real, qw_real) + F.linear(qx_imag, qw_imag)
out_real = F.linear(x_real, self.weight_real) + F.linear(x_imag, self.weight_imag)
return out_real
class ComplexLinear(nn.Module):
def __init__(self, in_features: int, out_features: int):
super(ComplexLinear, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight_real = nn.Parameter(
torch.empty(self.out_features, self.in_features)
)
self.weight_imag = nn.Parameter(
torch.empty(self.out_features, self.in_features)
)
self.act_quantizer = ComplexActivationQuantizer()
self.weight_quantizer = ComplexWeightQuantizer()
self.reset_parameters()
def reset_parameters(self):
init.kaiming_uniform_(self.weight_real, a=math.sqrt(5))
init.kaiming_uniform_(self.weight_imag, a=math.sqrt(5))
def forward(self, x_real: torch.Tensor, x_imag: torch.Tensor) -> torch.Tensor:
# qw_real, qw_imag = self.weight_quantizer(self.weight_real, self.weight_imag)
# qx_real, qx_imag = self.act_quantizer(x_real, x_imag)
# out_real = F.linear(qx_real, qw_real) + F.linear(qx_imag, qw_imag)
# out_imag = F.linear(qx_real, qw_imag) - F.linear(qx_imag, qw_real)
out_real = F.linear(x_real, self.weight_real) + F.linear(x_imag, self.weight_imag)
out_imag = F.linear(x_real, self.weight_imag) - F.linear(x_imag, self.weight_real)
return out_real, out_imag
class ComplexNetRMSNorm(nn.Module):
def __init__(self, hidden_size: int, eps: float = 1e-6):
super().__init__()
self.weight_real = nn.Parameter(torch.ones(hidden_size))
self.weight_imag = nn.Parameter(torch.ones(hidden_size))
self.variance_epsilon = eps
def forward(
self, hidden_states_real: torch.Tensor, hidden_states_imag: torch.Tensor
):
input_dtype = hidden_states_real.dtype
hidden_states_real.to(torch.float32)
hidden_states_imag.to(torch.float32)
magnitude = torch.mean(
hidden_states_real**2 + hidden_states_imag**2, dim=-1, keepdim=True
)
variance = torch.rsqrt(magnitude + self.variance_epsilon)
hidden_states_real = hidden_states_real * variance
hidden_states_imag = hidden_states_imag * variance
rmsnorm_out_real = self.weight_real * hidden_states_real
rmsnorm_out_imag = self.weight_imag * hidden_states_imag
return rmsnorm_out_real.to(input_dtype), rmsnorm_out_imag.to(input_dtype)
ALL_LAYERNORM_LAYERS.append(ComplexNetRMSNorm)
class ComplexNetMLP(nn.Module):
def __init__(self, config: ComplexNetConfig):
super().__init__()
self.config = config
self.hidden_size = self.config.hidden_size
self.im_size = self.config.intermediate_size
self.gate_proj = ComplexLinear(self.hidden_size, self.im_size)
self.up_proj = ComplexLinear(self.hidden_size, self.im_size)
self.down_proj = ComplexLinear(self.im_size, self.hidden_size)
self.ffn_layernorm = ComplexNetRMSNorm(self.im_size, eps=config.rms_norm_eps)
def complex_relu2(self, x_real: torch.Tensor, x_imag: torch.Tensor) -> torch.Tensor:
mask = torch.logical_and(x_real < 0, x_imag < 0)
x_real[mask] = 0
x_imag[mask] = 0
x_real = x_real**2
x_imag = x_imag**2
return x_real, x_imag
def forward(self, x_real: torch.Tensor, x_imag: torch.Tensor) -> torch.Tensor:
gate_proj_real, gate_proj_imag = self.gate_proj(x_real, x_imag)
activated_real, activated_imag = self.complex_relu2(
gate_proj_real, gate_proj_imag
)
up_proj_real, up_proj_imag = self.up_proj(x_real, x_imag)
up_proj_activated_real = (
activated_real * up_proj_real + activated_imag * up_proj_imag
)
up_proj_activated_imag = (
activated_real * up_proj_imag - activated_imag * up_proj_real
)
ln_real, ln_imag = self.ffn_layernorm(
up_proj_activated_real, up_proj_activated_imag
)
out_real, out_imag = self.down_proj(ln_real, ln_imag)
return out_real, out_imag
class ComplexNetRotaryEmbedding(nn.Module):
def __init__(self, config: ComplexNetConfig):
super().__init__()
self.config = config
self.base = self.config.rope_theta
self.hidden_size = self.config.hidden_size
self.num_attention_heads = self.config.num_attention_heads
self.max_seq_len_cached = self.config.max_position_embeddings
inv_freq = self._compute_inv_freq()
self.register_buffer("inv_freq", inv_freq, persistent=False)
def _compute_inv_freq(self):
base = self.base
head_dim = self.hidden_size // self.num_attention_heads
inv_freq = 1.0 / (
base ** (torch.arange(0, head_dim, dtype=torch.int64) / head_dim)
)
return inv_freq
@torch.no_grad()
def forward(
self, position_ids: torch.Tensor, hidden_states_type: torch.dtype
) -> tuple:
batch_size = position_ids.shape[0]
position_ids = position_ids[:, None, :].to(torch.float32)
if self.inv_freq.dim() == 1:
self.inv_freq = (
self.inv_freq[None, :, None]
.expand(batch_size, -1, 1)
.to(position_ids.device)
)
if position_ids.shape[0] > self.max_seq_len_cached:
print(f"Truncate position_ids within max_seq_len_cached.")
position_ids = position_ids[: self.max_seq_len_cached]
theta = (self.inv_freq.to(position_ids.dtype) @ position_ids).transpose(1, 2)
cos_emb = torch.cos(theta).to(hidden_states_type)
sin_emb = torch.sin(theta).to(hidden_states_type)
return cos_emb, sin_emb
def _apply_rotary_pos_emb(
q_real: torch.Tensor,
q_imag: torch.Tensor,
k_real: torch.Tensor,
k_imag: torch.Tensor,
cos_emb: torch.Tensor,
sin_emb: torch.Tensor,
) -> tuple:
def _apply_rotation(
x_real: torch.Tensor,
x_imag: torch.Tensor,
cos_emb: torch.Tensor,
sin_emb: torch.Tensor,
) -> torch.Tensor:
cos_emb = cos_emb.unsqueeze(1)
sin_emb = sin_emb.unsqueeze(1)
rotated_x_real = x_real * cos_emb - x_imag * sin_emb
rotated_x_imag = x_real * sin_emb + x_imag * cos_emb
return rotated_x_real, rotated_x_imag
rotated_q_real, rotated_q_imag = _apply_rotation(q_real, q_imag, cos_emb, sin_emb)
rotated_k_real, rotated_k_imag = _apply_rotation(k_real, k_imag, cos_emb, sin_emb)
return rotated_q_real, rotated_q_imag, rotated_k_real, rotated_k_imag
def repeat_kv(
hidden_states_real: torch.Tensor,
hidden_states_imag: torch.Tensor,
num_key_value_groups: int,
) -> torch.Tensor:
batch_size, num_key_value_heads, seq_length, head_dim = hidden_states_real.shape
if num_key_value_groups == 1:
return hidden_states_real, hidden_states_imag
hidden_states_real = hidden_states_real[:, :, None, :, :].expand(
batch_size, num_key_value_heads, num_key_value_groups, seq_length, head_dim
)
hidden_states_imag = hidden_states_imag[:, :, None, :, :].expand(
batch_size, num_key_value_heads, num_key_value_groups, seq_length, head_dim
)
hidden_states_real = hidden_states_real.reshape(
batch_size, num_key_value_heads * num_key_value_groups, seq_length, head_dim
)
hidden_states_imag = hidden_states_imag.reshape(
batch_size, num_key_value_heads * num_key_value_groups, seq_length, head_dim
)
return hidden_states_real, hidden_states_imag
def repeat_kv_for_real(
hidden_states_real: torch.Tensor,
num_key_value_groups: int,
) -> torch.Tensor:
batch_size, num_key_value_heads, seq_length, head_dim = hidden_states_real.shape
if num_key_value_groups == 1:
return hidden_states_real
hidden_states_real = hidden_states_real[:, :, None, :, :].expand(
batch_size, num_key_value_heads, num_key_value_groups, seq_length, head_dim
)
hidden_states_real = hidden_states_real.reshape(
batch_size, num_key_value_heads * num_key_value_groups, seq_length, head_dim
)
return hidden_states_real
def _rotate_half(x):
"""Rotates half the hidden dims of the input."""
x1 = x[..., : x.shape[-1] // 2]
x2 = x[..., x.shape[-1] // 2 :]
return torch.cat((-x2, x1), dim=-1)
def _apply_rotary_pos_emb_only_for_real(
q_real: torch.Tensor,
k_real: torch.Tensor,
cos_emb: torch.Tensor,
sin_emb: torch.Tensor,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
cos_emb = cos_emb.unsqueeze(1)
sin_emb = sin_emb.unsqueeze(1)
q_embed = (q_real * cos_emb) + (_rotate_half(q_real) * sin_emb)
k_embed = (k_real * cos_emb) + (_rotate_half(k_real) * sin_emb)
return q_embed, k_embed
class ComplexNetAttentionBase(nn.Module):
def __init__(self, config: ComplexNetConfig, layer_idx: int):
super().__init__()
self.config = config
self.layer_idx = layer_idx
self.attn_dropout = self.config.attention_dropout
self.hidden_size = self.config.hidden_size
self.num_attn_heads = self.config.num_attention_heads
self.head_dim = self.hidden_size // self.num_attn_heads
self.num_key_value_heads = self.config.num_key_value_heads
self.num_key_value_groups = (
self.num_attn_heads // self.config.num_key_value_heads
)
self.max_position_embeddings = self.config.max_position_embeddings
self.rope_theta = self.config.rope_theta
self.scaling = self.head_dim**-0.5
self.is_causal = True
self.rms_norm_eps = self.config.rms_norm_eps
self.q_proj = ComplexLinear(
self.hidden_size, self.num_attn_heads * self.head_dim
)
self.k_proj = ComplexLinear(
self.hidden_size, self.num_key_value_heads * self.head_dim
)
self.v_proj = ComplexLinear(
self.hidden_size, self.num_key_value_heads * self.head_dim
)
self.o_proj = ComplexLinear(self.hidden_size, self.hidden_size)
self.rotary_emb = ComplexNetRotaryEmbedding(self.config)
self.attn_layernorm = ComplexNetRMSNorm(self.hidden_size, eps=self.rms_norm_eps)
def forward(
self,
hidden_states_real: torch.Tensor,
hidden_states_imag: torch.Tensor,
position_embeddings: Tuple[torch.Tensor, torch.Tensor],
attn_mask_real: Optional[torch.Tensor] = None,
attn_mask_imag: Optional[torch.Tensor] = None,
past_key_value: Optional[ComplexDynamicCache] = None,
cache_position: Optional[torch.LongTensor] = None,
**kwargs,
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
input_shape = hidden_states_real.shape[:-1]
q_shape = (*input_shape, self.num_attn_heads, self.head_dim)
kv_shape = (*input_shape, self.num_key_value_heads, self.head_dim)
q_real = self.q_proj(hidden_states_real, hidden_states_imag)
q_real = q_real.view(q_shape).transpose(1, 2)
k_real = self.k_proj(hidden_states_real, hidden_states_imag)
k_imag = None # for compatibility of ComplexCache
k_real = k_real.view(kv_shape).transpose(1, 2)
v_real, v_imag = self.v_proj(hidden_states_real, hidden_states_imag)
v_real = v_real.view(kv_shape).transpose(1, 2)
v_imag = v_imag.view(kv_shape).transpose(1, 2)
cos_emb, sin_emb = position_embeddings
q_real, k_real = _apply_rotary_pos_emb_only_for_real(
q_real, k_real, cos_emb, sin_emb
)
if past_key_value is not None:
cache_kwargs = {
"sin": sin_emb,
"cos": cos_emb,
"cache_position": cache_position,
}
k_real, k_imag, v_real, v_imag = past_key_value.update(
k_real, k_imag, v_real, v_imag, self.layer_idx, cache_kwargs
)
k_real = repeat_kv_for_real(k_real, self.num_key_value_groups)
v_real, v_imag = repeat_kv(v_real, v_imag, self.num_key_value_groups)
attn_weights_real = (q_real @ k_real.transpose(2, 3)) * self.scaling
if attn_mask_real is not None:
causal_mask_real = attn_mask_real[:, :, :, : k_real.shape[-2]]
attn_weights_real = attn_weights_real + causal_mask_real
attn_weights = attn_weights_real
attn_weights = F.softmax(attn_weights, dim=-1, dtype=torch.float32).to(
q_real.dtype
)
attn_weights = F.dropout(
attn_weights, p=self.attn_dropout, training=self.training
)
attn_output_real = (
torch.matmul(attn_weights, v_real)
.transpose(1, 2)
.contiguous()
.reshape(input_shape[0], input_shape[1], self.hidden_size)
)
attn_output_imag = (
torch.matmul(attn_weights, v_imag)
.transpose(1, 2)
.contiguous()
.reshape(input_shape[0], input_shape[1], self.hidden_size)
)
attn_output_real, attn_output_imag = self.attn_layernorm(
attn_output_real, attn_output_imag
)
attn_output_real, attn_output_imag = self.o_proj(
attn_output_real, attn_output_imag
)
return (
attn_output_real,
attn_output_imag,
attn_weights_real,
None, # attn_weights_imag
past_key_value,
)
def _get_unpad_data(attention_mask):
seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
max_seqlen_in_batch = seqlens_in_batch.max().item()
cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.int32), (1, 0))
return (
indices,
cu_seqlens,
max_seqlen_in_batch,
)
def _upad_input(
module,
query_layer,
key_layer,
value_layer,
attention_mask,
query_length,
):
indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
batch_size, kv_seq_len, num_key_value_heads, head_dim = key_layer.shape
# print(indices_k)
key_layer = index_first_axis(
key_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim),
indices_k,
)
value_layer = index_first_axis(
value_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim),
indices_k,
)
if query_length == kv_seq_len:
query_layer = index_first_axis(
query_layer.reshape(
batch_size * kv_seq_len, module.num_attn_heads, head_dim
),
indices_k,
)
cu_seqlens_q = cu_seqlens_k
max_seqlen_in_batch_q = max_seqlen_in_batch_k
indices_q = indices_k
elif query_length == 1:
max_seqlen_in_batch_q = 1
cu_seqlens_q = torch.arange(
batch_size + 1, dtype=torch.int32, device=query_layer.device
)
indices_q = cu_seqlens_q[:-1]
query_layer = query_layer.squeeze(1)
else:
attention_mask = attention_mask[:, -query_length:]
query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(
query_layer, attention_mask
)
return (
query_layer,
key_layer,
value_layer,
indices_q,
(cu_seqlens_q, cu_seqlens_k),
(max_seqlen_in_batch_q, max_seqlen_in_batch_k),
)
def eager_attention_forward(
module: nn.Module,
q_cat: torch.Tensor,
k_cat: torch.Tensor,
v_cat: torch.Tensor,
attn_mask: Optional[torch.Tensor],
scaling: float,
dropout: float = 0.0,
**kwargs,
):
# for_real func only handle one input
k_cat = repeat_kv_for_real(k_cat, module.num_key_value_groups)
v_cat = repeat_kv_for_real(v_cat, module.num_key_value_groups)
attn_weights_real = (q_cat @ k_cat.transpose(2, 3)) * scaling
if attn_mask is not None:
causal_mask_real = attn_mask[:, :, :, : k_cat.shape[-2]]
attn_weights_real = attn_weights_real + causal_mask_real
attn_weights = attn_weights_real
attn_weights = F.softmax(attn_weights, dim=-1, dtype=torch.float32).to(
q_cat.dtype
)
# print(f"attn_weights{attn_weights}")
attn_weights = F.dropout(attn_weights, p=dropout, training=module.training)
attn_output = torch.matmul(attn_weights, v_cat).transpose(1, 2).contiguous()
return attn_output, None, None
def flash_attention_forward(
module: nn.Module,
q_cat: torch.Tensor,
k_cat: torch.Tensor,
v_cat: torch.Tensor,
attn_mask: Optional[torch.Tensor],
scaling: float,
dropout: float = 0.0,
softmax_scale: Optional[float] = None,
**kwargs,
):
def transpose_hidden_states(*hidden_states: torch.Tensor):
return [tensor.transpose(1, 2) for tensor in hidden_states]
(q_cat, k_cat, v_cat) = transpose_hidden_states(q_cat, k_cat, v_cat)
query_len = 1
query_len = q_cat.shape[1]
input_dtype = q_cat.dtype
if input_dtype == torch.float32:
if torch.is_autocast_enabled():
target_dtype = torch.get_autocast_gpu_dtype()
elif hasattr(module.config, "_pre_quantization_dtype"):
target_dtype = module.config._pre_quantization_dtype
else:
target_dtype = module.q_proj.weight_real.dtype
def dtype_cast(*tensors: torch.Tensor):
return [tensor.to(target_dtype) for tensor in tensors]
(q_cat, k_cat, v_cat) = dtype_cast(q_cat, k_cat, v_cat)
if not module._flash_attn_uses_top_left_mask:
causal = module.is_causal
else:
causal = module.is_causal and query_len != 1
if attn_mask is not None:
batch_size = q_cat.shape[0]
(
q_cat,
k_cat,
v_cat,
indices_q,
cu_seq_lens,
max_seq_lens,
) = _upad_input(
module,
q_cat,
k_cat,
v_cat,
attn_mask,
query_len,
)
cu_seqlens_q, cu_seqlens_k = cu_seq_lens
max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
def mask_complex_flash_attn(q, k, v):
return flash_attn_varlen_func(
q,
k,
v,
cu_seqlens_q=cu_seqlens_q,
cu_seqlens_k=cu_seqlens_k,
max_seqlen_q=max_seqlen_in_batch_q,
max_seqlen_k=max_seqlen_in_batch_k,
dropout_p=dropout,
softmax_scale=softmax_scale,
causal=causal,
)
# print(f"causal{causal}")
attn_output_unpad = mask_complex_flash_attn(q_cat, k_cat, v_cat)
attn_output = pad_input(attn_output_unpad, indices_q, batch_size, query_len)
else:
def unmask_complex_flash_attn(q, k, v):
return flash_attn_func(
q,
k,
v,
dropout_p=dropout,
softmax_scale=softmax_scale,
causal=causal,
)
attn_output = unmask_complex_flash_attn(q_cat, k_cat, v_cat)
return attn_output, None, None
ALL_ATTENTION_FUNCTIONS = {
"eager": eager_attention_forward,
"flash_attention_2": flash_attention_forward,
}
class ComplexNetAttention(ComplexNetAttentionBase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10()
def forward(
self,
hidden_states_real: torch.Tensor,
hidden_states_imag: torch.Tensor,
position_embeddings: Tuple[torch.Tensor, torch.Tensor],
attn_mask_real: Optional[torch.Tensor] = None,
attn_mask_imag: Optional[torch.Tensor] = None,
past_key_value: Optional[Cache] = None,
output_attentions: bool = False,
use_cache: bool = False,
cache_position: Optional[torch.LongTensor] = None,
**kwargs,
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
input_shape = hidden_states_real.shape[:-1]
q_shape = (*input_shape, self.num_attn_heads, self.head_dim)
kv_shape = (*input_shape, self.num_key_value_heads, self.head_dim)
def transpose_hidden_states(*hidden_states: torch.Tensor):
return [tensor.transpose(1, 2) for tensor in hidden_states]
q_real, q_imag = self.q_proj(hidden_states_real, hidden_states_imag)
k_real, k_imag = self.k_proj(hidden_states_real, hidden_states_imag)
v_real, v_imag = self.v_proj(hidden_states_real, hidden_states_imag)
(q_real, q_imag, k_real, k_imag, v_real, v_imag) = transpose_hidden_states(
q_real.view(q_shape),
q_imag.view(q_shape),
k_real.view(kv_shape),
k_imag.view(kv_shape),
v_real.view(kv_shape),
v_imag.view(kv_shape),
)
# k_imag = None # for compatibility of ComplexCache
# cos_emb, sin_emb = self.rotary_emb(position_ids, input_dtype)
cos_emb, sin_emb = position_embeddings
# q_real, k_real = _apply_rotary_pos_emb_only_for_real(
# q_real, k_real, cos_emb, sin_emb
# )
q_real, q_imag, k_real, k_imag = _apply_rotary_pos_emb(
q_real, q_imag, k_real, k_imag, cos_emb, sin_emb
)
past_key_value = getattr(self, "past_key_value", past_key_value)
if past_key_value is not None:
cache_kwargs = {
"sin": sin_emb,
"cos": cos_emb,
"cache_position": cache_position,
}
k_real, k_imag, v_real, v_imag = past_key_value.update(
k_real, k_imag, v_real, v_imag, self.layer_idx, cache_kwargs
)
attention_interface: Callable = eager_attention_forward
if self.config._attn_implementation != "eager":
if self.config._attn_implementation == "sdpa" and kwargs.get(
"output_attentions", False
):
logger.warning_once(
"`torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to "
'eager attention. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.'
)
raise ValueError(
f"Unsupported attention implementation: {self.config._attn_implementation}. Supported implementations are: {list(ALL_ATTENTION_FUNCTIONS.keys())}."
)
elif self.config._attn_implementation == "flash_attention_2":
attention_interface = ALL_ATTENTION_FUNCTIONS[
self.config._attn_implementation
]
else:
raise ValueError(
f"Unsupported attention implementation: {self.config._attn_implementation}. Supported implementations are: {list(ALL_ATTENTION_FUNCTIONS.keys())}."
)
cat_q = torch.cat([q_real, q_imag], dim=-1).reshape(
input_shape[0], self.num_attn_heads, input_shape[1], 2 * self.head_dim
)
cat_k = torch.cat([k_real, k_imag], dim=-1).reshape(
input_shape[0], self.num_key_value_heads, input_shape[1], 2 * self.head_dim
)
cat_v = torch.cat([v_real, v_imag], dim=-1).reshape(
input_shape[0], self.num_key_value_heads, input_shape[1], 2 * self.head_dim
)
attn_output, attn_weights_real, attn_weights_imag = attention_interface(
self,
cat_q,
cat_k,
cat_v,
attn_mask_real,
scaling=self.scaling,
dropout=self.attn_dropout if self.training else 0.0,
**kwargs,
)
attn_output_real, attn_output_imag = torch.chunk(attn_output, 2, dim=-1)
attn_output_real = attn_output_real.reshape(
input_shape[0], input_shape[1], self.hidden_size
).contiguous()
attn_output_imag = attn_output_imag.reshape(
input_shape[0], input_shape[1], self.hidden_size
).contiguous()
attn_output_real, attn_output_imag = self.attn_layernorm(
attn_output_real, attn_output_imag
)
attn_output_real, attn_output_imag = self.o_proj(
attn_output_real, attn_output_imag
)
if not output_attentions:
attn_weights_real = None
attn_weights_imag = None
return (
attn_output_real,
attn_output_imag,
attn_weights_real,
attn_weights_imag,
past_key_value,
)
class ComplexNetDecoderLayer(nn.Module):
def __init__(self, config: ComplexNetConfig, layer_idx: int):
super().__init__()
self.config = config
self.hidden_size = self.config.hidden_size
self.self_attn = ComplexNetAttention(config=config, layer_idx=layer_idx)
self.mlp = ComplexNetMLP(config)
self.pre_layernorm = ComplexNetRMSNorm(
config.hidden_size, eps=config.rms_norm_eps
)
self.post_layernorm = ComplexNetRMSNorm(
config.hidden_size, eps=config.rms_norm_eps
)
def forward(
self,
hidden_states_real: torch.Tensor,
hidden_states_imag: torch.Tensor,
attention_mask_real: Optional[torch.Tensor] = None,
attention_mask_imag: Optional[torch.Tensor] = None,
past_key_value: Optional[Cache] = None,
output_attentions: Optional[bool] = False,
use_cache: Optional[bool] = False,
cache_position: Optional[torch.LongTensor] = None,
position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
**kwargs,
) -> Tuple[
torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]
]:
residual_real = hidden_states_real
residual_imag = hidden_states_imag
hidden_states_real, hidden_states_imag = self.pre_layernorm(
hidden_states_real, hidden_states_imag
)
(
hidden_states_real,
hidden_states_imag,
attn_weights_real,
attn_weights_imag,
present_key_value,
) = self.self_attn(
hidden_states_real=hidden_states_real,
hidden_states_imag=hidden_states_imag,
position_embeddings=position_embeddings,
attn_mask_real=attention_mask_real,
attn_mask_imag=attention_mask_imag,
past_key_value=past_key_value,
output_attentions=output_attentions,
use_cache=use_cache,
cache_position=cache_position,
**kwargs,
)
hidden_states_real = residual_real + hidden_states_real
hidden_states_imag = residual_imag + hidden_states_imag
residual_real = hidden_states_real
residual_imag = hidden_states_imag
hidden_states_real, hidden_states_imag = self.post_layernorm(
hidden_states_real, hidden_states_imag
)
hidden_states_real, hidden_states_imag = self.mlp(
hidden_states_real, hidden_states_imag
)
hidden_states_real = residual_real + hidden_states_real
hidden_states_imag = residual_imag + hidden_states_imag
outputs = (
hidden_states_real,
hidden_states_imag,
)
if output_attentions:
outputs += (
attn_weights_real,
attn_weights_imag,
)
if use_cache:
outputs += (present_key_value,)
return outputs
logger = logging.get_logger(__name__)
class ComplexNetLM(PreTrainedModel, GenerationMixin):
config_class = ComplexNetConfig
supports_gradient_checkpointing = True
_supports_flash_attn_2 = True
def __init__(self, config: ComplexNetConfig):
super().__init__(config=config)
self.config = config
self.n_vocab = self.config.vocab_size
self.max_position_embeddings = self.config.max_position_embeddings
self.hidden_size = self.config.hidden_size
self.num_hidden_layers = self.config.num_hidden_layers
self.use_cache = self.config.use_cache
self.token_embeddings_real = nn.Embedding(self.n_vocab, self.hidden_size)
self.token_embeddings_imag = nn.Embedding(self.n_vocab, self.hidden_size)
self.final_norm = ComplexNetRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.layer = nn.ModuleList(
[
ComplexNetDecoderLayer(config, layer_idx)
for layer_idx in range(self.num_hidden_layers)
]
)
self.gradient_checkpointing = False
self.rotary_emb = ComplexNetRotaryEmbedding(self.config)
self.lm_head = nn.Linear(self.hidden_size * 2, self.n_vocab, bias=False)
self.apply(self._init_weights)
def _init_weights(self, module):
std = self.config.initializer_range
if isinstance(module, nn.Linear):
torch.nn.init.normal_(module.weight, mean=0.0, std=std)
if module.bias is not None:
torch.nn.init.zeros_(module.bias)
elif isinstance(module, nn.Embedding):
torch.nn.init.normal_(module.weight, mean=0.0, std=std)
elif isinstance(module, ComplexLinear):
std = std / math.sqrt(2)
torch.nn.init.normal_(module.weight_real, mean=0.0, std=std)
torch.nn.init.normal_(module.weight_imag, mean=0.0, std=std)
def embed(
self,
input_ids: torch.LongTensor,
attention_mask: Optional[torch.Tensor] = None,
) -> Tuple[torch.FloatTensor, torch.FloatTensor]:
token_embeddings_real = self.token_embeddings_real(input_ids)
token_embeddings_imag = self.token_embeddings_imag(input_ids)
return token_embeddings_real, token_embeddings_imag
def token_logits(
self,
x_real: torch.FloatTensor,
x_imag: torch.FloatTensor,
) -> torch.FloatTensor:
# catenate the real and imaginary parts
x_cat = torch.cat([x_real, x_imag], dim=-1)
logits = self.lm_head(x_cat)
return logits
def forward(
self,
input_ids: torch.LongTensor,
attention_mask: Optional[torch.Tensor] = None,
labels=None,
position_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[Cache] = None,
output_attentions: Optional[bool] = False,
use_cache: Optional[bool] = False,
cache_position: Optional[torch.LongTensor] = None,
**kwargs,
) -> dict:
output_attentions = (
output_attentions
if output_attentions is not None
else self.config.output_attentions
)
use_cache = use_cache if use_cache is not None else self.config.use_cache
if self.gradient_checkpointing and self.training and use_cache:
logger.warning_once(
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`."
)
use_cache = False
if not isinstance(past_key_values, (type(None), Cache)):
raise ValueError(
"The `past_key_values` should be either a `Cache` object or `None`."
)
batch_size, seq_len = input_ids.shape
device = input_ids.device
x_real, x_imag = self.embed(input_ids, attention_mask)
if use_cache and past_key_values is None:
past_key_values = ComplexDynamicCache()
if cache_position is None:
past_seen_tokens = (
past_key_values.get_seq_length() if past_key_values is not None else 0
)
cache_position = torch.arange(
past_seen_tokens,
past_seen_tokens + x_real.shape[1],
device=x_real.device,
)
if position_ids is None:
position_ids = cache_position.unsqueeze(0)
causal_mask = self._update_causal_mask(
attention_mask, x_real, cache_position, past_key_values, output_attentions
)
position_embeddings = self.rotary_emb(position_ids, x_real.dtype)
all_hidden_states_real = []
all_hidden_states_imag = []
for i, layer_module in enumerate(self.layer):
if self.gradient_checkpointing and self.training:
layer_outputs = self._gradient_checkpointing_func(
partial(layer_module.__call__, **kwargs),
x_real,
x_imag,
causal_mask,
causal_mask,
past_key_values,
output_attentions,
use_cache,
cache_position,
position_embeddings,
)
else:
layer_outputs = layer_module(
hidden_states_real=x_real,
hidden_states_imag=x_imag,
attention_mask_real=causal_mask,
attention_mask_imag=causal_mask,
past_key_value=past_key_values,
output_attentions=output_attentions,
use_cache=use_cache,
cache_position=cache_position,
position_embeddings=position_embeddings,
**kwargs,
)
x_real, x_imag = layer_outputs[:2]
if output_attentions:
all_hidden_states_real.append(layer_outputs[2])
all_hidden_states_imag.append(layer_outputs[3])
x_real, x_imag = self.final_norm(x_real, x_imag)
logits = self.token_logits(x_real, x_imag)
loss = None
if labels is not None:
loss = self.loss_function(
logits=logits,
labels=labels,
vocab_size=self.config.vocab_size,
**kwargs,
)
return CausalLMOutputWithPast(
loss=loss,
logits=logits,
past_key_values=past_key_values,
)
def _update_causal_mask(
self,
attention_mask: torch.Tensor,
input_tensor: torch.Tensor,
cache_position: torch.Tensor,
past_key_values: Cache,
output_attentions: bool = False,
):
if self.config._attn_implementation == "flash_attention_2":
# print("using flash_attention_2")
if attention_mask is not None and (attention_mask == 0.0).any():
return attention_mask
return None
past_seen_tokens = (
past_key_values.get_seq_length() if past_key_values is not None else 0
)
using_static_cache = isinstance(past_key_values, StaticCache)
dtype, device = input_tensor.dtype, input_tensor.device
sequence_length = input_tensor.shape[1]
if using_static_cache:
target_length = past_key_values.get_max_cache_shape()
else:
target_length = (
attention_mask.shape[-1]
if isinstance(attention_mask, torch.Tensor)
else past_seen_tokens + sequence_length + 1
)
causal_mask = self._prepare_4d_causal_attention_mask_with_cache_position(
attention_mask=attention_mask,
sequence_length=sequence_length,
target_length=target_length,
dtype=dtype,
device=device,
cache_position=cache_position,
batch_size=input_tensor.shape[0],
)
return causal_mask
def _prepare_4d_causal_attention_mask_with_cache_position(
self,
attention_mask: torch.Tensor,
sequence_length: int,
target_length: int,
dtype: torch.dtype,
device: torch.device,
cache_position: torch.Tensor,
batch_size: int,
**kwargs,
):
if attention_mask is not None and attention_mask.dim() == 4:
# In this case we assume that the mask comes already in inverted form and requires no inversion or slicing.
causal_mask = attention_mask
else:
min_dtype = torch.finfo(dtype).min
causal_mask = torch.full(
(sequence_length, target_length),
fill_value=min_dtype,
dtype=dtype,
device=device,
)
if sequence_length != 1:
causal_mask = torch.triu(causal_mask, diagonal=1)
causal_mask *= torch.arange(
target_length, device=device
) > cache_position.reshape(-1, 1)
causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1)
if attention_mask is not None:
causal_mask = (
causal_mask.clone()
) # copy to contiguous memory for in-place edit
mask_length = attention_mask.shape[-1]
padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[
:, None, None, :
].to(causal_mask.device)
padding_mask = padding_mask == 0
causal_mask[:, :, :, :mask_length] = causal_mask[
:, :, :, :mask_length
].masked_fill(padding_mask, min_dtype)
return causal_mask
|