(self, query_layer, key_layer, value_layer, attention_mask)
| 477 | self.attention_dropout = torch.nn.Dropout(config.attention_dropout) |
| 478 | |
| 479 | def forward(self, query_layer, key_layer, value_layer, attention_mask): |
| 480 | pytorch_major_version = int(torch.__version__.split('.')[0]) |
| 481 | if pytorch_major_version >= 2: |
| 482 | query_layer, key_layer, value_layer = [k.permute(1, 2, 0, 3) for k in [query_layer, key_layer, value_layer]] |
| 483 | if attention_mask is None and query_layer.shape[2] == key_layer.shape[2]: |
| 484 | context_layer = torch.nn.functional.scaled_dot_product_attention(query_layer, key_layer, value_layer, |
| 485 | is_causal=True) |
| 486 | else: |
| 487 | if attention_mask is not None: |
| 488 | attention_mask = ~attention_mask |
| 489 | context_layer = torch.nn.functional.scaled_dot_product_attention(query_layer, key_layer, value_layer, |
| 490 | attention_mask) |
| 491 | context_layer = context_layer.permute(2, 0, 1, 3) |
| 492 | new_context_layer_shape = context_layer.size()[:-2] + (self.hidden_size_per_partition,) |
| 493 | context_layer = context_layer.reshape(*new_context_layer_shape) |
| 494 | else: |
| 495 | # Raw attention scores |
| 496 | |
| 497 | # [b, np, sq, sk] |
| 498 | output_size = (query_layer.size(1), query_layer.size(2), query_layer.size(0), key_layer.size(0)) |
| 499 | |
| 500 | # [sq, b, np, hn] -> [sq, b * np, hn] |
| 501 | query_layer = query_layer.view(output_size[2], output_size[0] * output_size[1], -1) |
| 502 | # [sk, b, np, hn] -> [sk, b * np, hn] |
| 503 | key_layer = key_layer.view(output_size[3], output_size[0] * output_size[1], -1) |
| 504 | |
| 505 | # preallocting input tensor: [b * np, sq, sk] |
| 506 | matmul_input_buffer = torch.empty( |
| 507 | output_size[0] * output_size[1], output_size[2], output_size[3], dtype=query_layer.dtype, |
| 508 | device=query_layer.device |
| 509 | ) |
| 510 | |
| 511 | # Raw attention scores. [b * np, sq, sk] |
| 512 | matmul_result = torch.baddbmm( |
| 513 | matmul_input_buffer, |
| 514 | query_layer.transpose(0, 1), # [b * np, sq, hn] |
| 515 | key_layer.transpose(0, 1).transpose(1, 2), # [b * np, hn, sk] |
| 516 | beta=0.0, |
| 517 | alpha=(1.0 / self.norm_factor), |
| 518 | ) |
| 519 | |
| 520 | # change view to [b, np, sq, sk] |
| 521 | attention_scores = matmul_result.view(*output_size) |
| 522 | |
| 523 | # =========================== |
| 524 | # Attention probs and dropout |
| 525 | # =========================== |
| 526 | |
| 527 | # attention scores and attention mask [b, np, sq, sk] |
| 528 | if self.attention_softmax_in_fp32: |
| 529 | attention_scores = attention_scores.float() |
| 530 | if self.coeff is not None: |
| 531 | attention_scores = attention_scores * self.coeff |
| 532 | if attention_mask is None and attention_scores.shape[2] == attention_scores.shape[3]: |
| 533 | attention_mask = torch.ones(output_size[0], 1, output_size[2], output_size[3], |
| 534 | device=attention_scores.device, dtype=torch.bool) |
| 535 | attention_mask.tril_() |
| 536 | attention_mask = ~attention_mask |
nothing calls this directly
no outgoing calls
no test coverage detected