Documentation for the model
model description
scprint.model.model
scPrint
Bases: LightningModule
, PyTorchModelHubMixin
scPRINT transformer for single cell biology and the inference of Gene Regulatory networks
Parameters: |
|
---|
Notes
for other parameters of the model that are not part of its class definition, see @trainer.trainer.py
Raises: |
|
---|
Source code in scprint/model/model.py
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 |
|
configure_optimizers
@see pl.LightningModule
Source code in scprint/model/model.py
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 |
|
forward
forward also called on self(), a full forward pass on the model
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/model.py
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 |
|
get_cell_embs
get_cell_embs
Parameters: |
|
---|
Raises: |
|
---|
Returns: |
|
---|
Source code in scprint/model/model.py
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 |
|
log_adata
log_adata will log an adata from predictions. It will log to tensorboard and wandb if available
see @utils.log_adata
Source code in scprint/model/model.py
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 |
|
on_fit_start
@see pl.LightningModule
Source code in scprint/model/model.py
637 638 639 640 641 642 643 |
|
on_predict_epoch_end
@see pl.LightningModule will
Source code in scprint/model/model.py
1321 1322 1323 1324 1325 1326 1327 |
|
on_predict_epoch_start
@see pl.LightningModule
Source code in scprint/model/model.py
1122 1123 1124 1125 1126 1127 1128 1129 1130 |
|
on_validation_epoch_end
@see pl.LightningModule
Source code in scprint/model/model.py
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 |
|
optimizer_step
@see pl.LightningModule
Source code in scprint/model/model.py
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 |
|
predict_step
embed given gene expression, encode the gene embedding and cell embedding.
Returns: |
|
---|
Source code in scprint/model/model.py
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 |
|
training_step
training_step defines the train loop. It is independent of forward
@see pl.LightningModule
Returns: |
|
---|
Source code in scprint/model/model.py
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 |
|
validation_step
validation_step defines the validation loop. It is independent of forward @see pl.LightningModule
Parameters: |
|
---|
Source code in scprint/model/model.py
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 |
|
losses
scprint.model.loss
AdversarialDiscriminatorLoss
Bases: Module
Discriminator for the adversarial training for batch correction.
Parameters: |
|
---|
Source code in scprint/model/loss.py
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 |
|
forward
Parameters: |
|
---|
Source code in scprint/model/loss.py
303 304 305 306 307 308 309 310 311 312 313 |
|
classification
Computes the classification loss for a given batch of predictions and ground truth labels.
Parameters: |
|
---|
Raises: |
|
---|
Returns: |
|
---|
Source code in scprint/model/loss.py
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 |
|
criterion_neg_log_bernoulli
Compute the negative log-likelihood of Bernoulli distribution
Source code in scprint/model/loss.py
144 145 146 147 148 149 150 151 |
|
ecs
ecs Computes the similarity of cell embeddings based on a threshold.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/loss.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
grad_reverse
grad_reverse Reverses the gradient of the input tensor.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/loss.py
327 328 329 330 331 332 333 334 335 336 337 338 |
|
masked_mae
Compute the masked MAE loss between input and target. MAE = mean absolute error
Source code in scprint/model/loss.py
30 31 32 33 34 35 36 37 |
|
masked_mse
Compute the masked MSE loss between input and target.
Source code in scprint/model/loss.py
21 22 23 24 25 26 27 |
|
masked_nb
Compute the masked negative binomial loss between input and target.
Source code in scprint/model/loss.py
40 41 42 43 44 45 46 47 |
|
masked_relative_error
Compute the masked relative error between input and target.
Source code in scprint/model/loss.py
154 155 156 157 158 159 160 161 162 |
|
mse
Compute the MSE loss between input and target.
Source code in scprint/model/loss.py
10 11 12 13 14 15 16 17 18 |
|
nb
Computes the negative binomial (NB) loss.
This function was adapted from scvi-tools.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/loss.py
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 |
|
nb_dist
nb_dist Computes the negative binomial distribution.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/loss.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
similarity
Dot product or cosine similarity
Source code in scprint/model/loss.py
165 166 167 168 169 170 171 |
|
zinb
Computes zero-inflated negative binomial (ZINB) loss.
This function was modified from scvi-tools.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/loss.py
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 |
|
utils
scprint.model.utils
Attention
Initialize the Attention class.
Parameters: |
|
---|
Source code in scprint/model/utils.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
|
add_attn
Aggregate the attention or data based on the comp_attn flag.
Parameters: |
|
---|
Source code in scprint/model/utils.py
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 |
|
add_qk
Add data to the internal storage.
Parameters: |
|
---|
Source code in scprint/model/utils.py
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
|
get
Get the aggregated attention or data.
Returns: |
|
---|
Source code in scprint/model/utils.py
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
|
downsample_profile
This function downsamples the expression profile of a given single cell RNA matrix.
The noise is applied based on the renoise parameter, the total counts of the matrix, and the number of genes. The function first calculates the noise threshold (scaler) based on the renoise parameter. It then generates an initial matrix count by applying a Poisson distribution to a random tensor scaled by the total counts and the number of genes. The function then models the sampling zeros by applying a Poisson distribution to a random tensor scaled by the noise threshold, the total counts, and the number of genes. The function also models the technical zeros by generating a random tensor and comparing it to the noise threshold. The final matrix count is calculated by subtracting the sampling zeros from the initial matrix count and multiplying by the technical zeros. The function ensures that the final matrix count is not less than zero by taking the maximum of the final matrix count and a tensor of zeros. The function returns the final matrix count.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/utils.py
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 |
|
make_adata
This function creates an AnnData object from the given input parameters.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/utils.py
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 |
|
simple_masker
Randomly mask a batch of data.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/utils.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
|
test
Test the given model on the full set of benchmarks and save the results to JSON files.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/utils.py
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 |
|
translate
translate This function translates the given value based on the specified type.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/utils.py
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 |
|
weighted_masker
Randomly mask a batch of data.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/utils.py
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 |
|
zinb_sample
zinb_sample This function generates a sample from a Zero-Inflated Negative Binomial (ZINB) distribution.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/utils.py
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 |
|
encoder and decoder modules
scprint.model.encoders
CategoryValueEncoder
Bases: Module
Encodes categorical values into a vector using an embedding layer and layer normalization.
Parameters: |
|
---|
Returns: |
|
---|
Note: not used in the current version of scprint.
Source code in scprint/model/encoders.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
|
ContinuousValueEncoder
Bases: Module
Encode real number values to a vector using neural nets projection.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/encoders.py
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 |
|
forward
Parameters: |
|
---|
Source code in scprint/model/encoders.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
DPositionalEncoding
Bases: Module
The PositionalEncoding module applies a positional encoding to a sequence of vectors. This is necessary for the Transformer model, which does not have any inherent notion of position in a sequence. The positional encoding is added to the input embeddings and allows the model to attend to positions in the sequence.
Parameters: |
|
---|
Note: not used in the current version of scprint.
Source code in scprint/model/encoders.py
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 |
|
forward
Parameters: |
|
---|
Source code in scprint/model/encoders.py
149 150 151 152 153 154 155 156 |
|
GeneEncoder
Bases: Module
Encodes gene sequences into a continuous vector space using an embedding layer.
The output is then normalized using a LayerNorm.
Parameters: |
|
---|
Note: not used in the current version of scprint.
Source code in scprint/model/encoders.py
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 |
|
PositionalEncoding
Bases: Module
The PositionalEncoding module applies a positional encoding to a sequence of vectors. This is necessary for the Transformer model, which does not have any inherent notion of position in a sequence. The positional encoding is added to the input embeddings and allows the model to attend to positions in the sequence.
Parameters: |
|
---|
Note: not used in the current version of scprint.
Source code in scprint/model/encoders.py
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 |
|
forward
Parameters: |
|
---|
Source code in scprint/model/encoders.py
89 90 91 92 93 94 95 96 |
|
scprint.model.decoders
ClsDecoder
Bases: Module
ClsDecoder Decoder for classification task.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/decoders.py
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 |
|
forward
Parameters: |
|
---|
Source code in scprint/model/decoders.py
207 208 209 210 211 212 213 |
|
ExprDecoder
Bases: Module
ExprDecoder Decoder for the gene expression prediction.
Will output the mean, variance and zero logits, parameters of a zero inflated negative binomial distribution.
Parameters: |
|
---|
Source code in scprint/model/decoders.py
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 |
|
forward
x is the output of the transformer, (batch, seq_len, d_model)
Source code in scprint/model/decoders.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
GraphSDEExprDecoder
Bases: Module
Initialize the ExprNeuralSDEDecoder module.
Parameters: |
|
---|
Source code in scprint/model/decoders.py
9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
MVCDecoder
Bases: Module
MVCDecoder Decoder for the masked value prediction for cell embeddings.
Will use the gene embeddings with the cell embeddings to predict the mean, variance and zero logits
Parameters: |
|
---|
Source code in scprint/model/decoders.py
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 |
|
forward
Parameters: |
|
---|
Source code in scprint/model/decoders.py
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 |
|
flashattention
scprint.model.flash_attn.flashformer
FlashTransformerEncoder
Bases: Module
FlashTransformerEncoder a transformer encoder with flash attention.
Parameters: |
|
---|
Raises: |
|
---|
Source code in scprint/model/flash_attn/flashformer.py
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 |
|
scprint.model.flash_attn.mha
CrossAttention
Bases: Module
Implement the scaled dot product attention with softmax.
Args softmax_scale: The temperature to use for the softmax attention. Default to 1/sqrt(d_keys) where d_keys is computed at runtime attention_dropout: The dropout rate to apply to the attention. default to 0.0.
Source code in scprint/model/flash_attn/mha.py
244 245 246 247 248 |
|
forward
Implements the multihead softmax attention.
Args q: The tensor containing the query. (B, Sq, H, D) kv: The tensor containing the key and value. (B, Sk, 2, H_k, D) causal: if passed, will override self.causal key_padding_mask: boolean mask to apply to the attention weights. True means to keep, False means to mask out. (B, Sk)
Source code in scprint/model/flash_attn/mha.py
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 |
|
FlashCrossAttention
Bases: Module
Implement the scaled dot product attention with softmax.
Args softmax_scale: The temperature to use for the softmax attention. (default: 1/sqrt(d_keys) where d_keys is computed at runtime) attention_dropout: The dropout rate to apply to the attention (default: 0.0)
Source code in scprint/model/flash_attn/mha.py
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 |
|
forward
Implements the multihead softmax attention.
Args q: The tensor containing the query. (B, Sq, H, D) kv: The tensor containing the key and value. (B, Sk, 2, H_k, D) causal: if passed, will override self.causal cu_seqlens: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths of the sequences in the batch, used to index into q. max_seqlen: int. Maximum sequence length in the batch of q. cu_seqlens_k: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths of the sequences in the batch, used to index into kv. max_seqlen_k: int. Maximum sequence length in the batch of k and v.
Source code in scprint/model/flash_attn/mha.py
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 |
|
FlashSelfAttention
Bases: Module
Implement the scaled dot product attention with softmax.
Parameters: |
|
---|
Source code in scprint/model/flash_attn/mha.py
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 |
|
forward
Implements the multihead softmax attention.
Args qkv (Tensor): The tensor containing the query, key, and value. If cu_seqlens is None and max_seqlen is None, then qkv has shape (B, S, 3, H, D). If cu_seqlens is not None and max_seqlen is not None, then qkv has shape (total, 3, H, D), where total is the sum of the sequence lengths in the batch. causal (bool): if passed, will override self.causal cu_seqlens (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths of the sequences in the batch, used to index into qkv. max_seqlen (int). Maximum sequence length in the batch. Returns: out: (total, H, D) if cu_seqlens is not None and max_seqlen is not None, else (B, S, H, D).
Source code in scprint/model/flash_attn/mha.py
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 |
|
LinearResidual
Bases: Linear
Wrap nn.Linear to return the residual as well. For compatibility with FusedDense.
MHA
Bases: Module
MHA Multi-head self-attention and cross-attention
Parameters: |
|
---|
Source code in scprint/model/flash_attn/mha.py
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 |
|
forward
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/flash_attn/mha.py
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 |
|
SelfAttention
Bases: Module
Implement the scaled dot product attention with softmax.
Parameters: |
|
---|
Source code in scprint/model/flash_attn/mha.py
194 195 196 197 198 |
|
forward
Implements the multihead softmax attention.
Parameters: |
|
---|
Source code in scprint/model/flash_attn/mha.py
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 |
|
scprint.model.flash_attn.mlp
Mlp
Bases: Module
Multi-layer perceptron (MLP) module.
Parameters: |
|
---|
Source code in scprint/model/flash_attn/mlp.py
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 |
|
forward
Forward pass of the MLP.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/flash_attn/mlp.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
scprint.model.flash_attn.block
Block
Bases: Module
For prenorm=True, this Block has a slightly different structure compared to a regular prenorm Transformer block. The standard block is: LN -> MHA -> Dropout -> Add -> LN -> MLP -> Dropout -> Add. [Ref: https://arxiv.org/abs/2002.04745] Here we have: Dropout -> Add -> LN -> MHA -> Dropout -> Add -> LN -> MLP, returning both the hidden_states (output of the MLP) and the residual. This is for performance reasons, as we can fuse the dropout, add and LayerNorm. The residual needs to be provided (except for the very first block).
For prenorm=False, this Block has the same structure as a regular postnorm Transformer block: MHA -> Dropout -> Add -> LN -> MLP -> Dropout -> Add -> LN.
Parameters: |
|
---|
Source code in scprint/model/flash_attn/block.py
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 |
|
forward
Pass the input through the encoder layer.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/flash_attn/block.py
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 |
|
scprint.model.flash_attn.flashattention
Experimental implementation of FlashAttention in Triton. Tested with triton==2.0.0.dev20221202. Triton 2.0 has a new backend (MLIR) but seems like it doesn't yet work for head dimensions other than 64: https://github.com/openai/triton/blob/d376020f90002757eea3ea9475d4f7cfc2ec5ead/python/triton/ops/flash_attention.py#L207 We'll update this implementation with the new Triton backend once this is fixed.
We use the FlashAttention implementation from Phil Tillet a starting point. https://github.com/openai/triton/blob/master/python/tutorials/06-fused-attention.py
Changes: - Implement both causal and non-causal attention. - Implement both self-attention and cross-attention. - Support arbitrary seqlens (not just multiples of 128), for both forward and backward. - Support all head dimensions up to 128 (not just 16, 32, 64, 128), for both forward and backward. - Support attention bias. - Speed up the forward pass a bit, and only store the LSE instead of m and l. - Make the backward for d=128 much faster by reducing register spilling. - Optionally parallelize the backward pass across seqlen_k, to deal with the case of small batch size * nheads.
Caution: - This is an experimental implementation. The forward pass should be quite robust but I'm not 100% sure that the backward pass doesn't have race conditions (due to the Triton compiler). - This implementation has only been tested on A100. - If you plan to use headdim other than 64 and 128, you should test for race conditions (due to the Triton compiler), as done in tests/test_flash_attn.py "test_flash_attn_triton_race_condition". I've tested and fixed many race conditions for different head dimensions (40, 48, 64, 128, 80, 88, 96), but I'm still not 100% confident that there are none left for other head dimensions.
Differences between this Triton version and the CUDA version: - Triton version doesn't support dropout. - Triton forward is generally faster than CUDA forward, while Triton backward is generally slower than CUDA backward. Overall Triton forward + backward is slightly slower than CUDA forward + backward. - Triton version doesn't support different sequence lengths in a batch (i.e., RaggedTensor/NestedTensor). - Triton version supports attention bias, while CUDA version doesn't.
FlashAttnFunc
Bases: Function
forward
staticmethod
Perform the forward pass of FlashAttention.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/flash_attn/flashattention.py
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 |
|
FlashAttnKVPackedFunc
Bases: Function
forward
staticmethod
Perform the forward pass of FlashAttention with packed key and value tensors.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/flash_attn/flashattention.py
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 |
|
FlashAttnQKVPackedFunc
Bases: Function
forward
staticmethod
Forward pass for FlashAttention.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/flash_attn/flashattention.py
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 |
|
scprint.model.flash_attn.activations
bias_gelu_back
Assume that y has shape (B, D) and bias has shape (D)
Source code in scprint/model/flash_attn/activations.py
24 25 26 27 28 29 30 31 32 33 34 |
|
scprint.model.flash_attn.layer_norm
layer_norm_ref
Reference implementation of Layer Normalization with optional dropout and residual connections.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/flash_attn/layer_norm.py
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 |
|
rms_norm_ref
Reference implementation of RMS Normalization with optional dropout and residual connections.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/flash_attn/layer_norm.py
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 |
|