| 589 | |
| 590 | |
| 591 | class DecSALayer(nn.Module): |
| 592 | def __init__(self, c, num_heads, dropout, attention_dropout=0.1, relu_dropout=0.1, kernel_size=9, act='gelu'): |
| 593 | super().__init__() |
| 594 | self.c = c |
| 595 | self.dropout = dropout |
| 596 | self.layer_norm1 = LayerNorm(c) |
| 597 | self.self_attn = MultiheadAttention( |
| 598 | c, num_heads, self_attention=True, dropout=attention_dropout, bias=False |
| 599 | ) |
| 600 | self.layer_norm2 = LayerNorm(c) |
| 601 | self.encoder_attn = MultiheadAttention( |
| 602 | c, num_heads, encoder_decoder_attention=True, dropout=attention_dropout, bias=False, |
| 603 | ) |
| 604 | self.layer_norm3 = LayerNorm(c) |
| 605 | self.ffn = TransformerFFNLayer( |
| 606 | c, 4 * c, padding='LEFT', kernel_size=kernel_size, dropout=relu_dropout, act=act) |
| 607 | |
| 608 | def forward( |
| 609 | self, |
| 610 | x, |
| 611 | encoder_out=None, |
| 612 | encoder_padding_mask=None, |
| 613 | incremental_state=None, |
| 614 | self_attn_mask=None, |
| 615 | self_attn_padding_mask=None, |
| 616 | attn_out=None, |
| 617 | reset_attn_weight=None, |
| 618 | **kwargs, |
| 619 | ): |
| 620 | layer_norm_training = kwargs.get('layer_norm_training', None) |
| 621 | if layer_norm_training is not None: |
| 622 | self.layer_norm1.training = layer_norm_training |
| 623 | self.layer_norm2.training = layer_norm_training |
| 624 | self.layer_norm3.training = layer_norm_training |
| 625 | residual = x |
| 626 | x = self.layer_norm1(x) |
| 627 | x, _ = self.self_attn( |
| 628 | query=x, |
| 629 | key=x, |
| 630 | value=x, |
| 631 | key_padding_mask=self_attn_padding_mask, |
| 632 | incremental_state=incremental_state, |
| 633 | attn_mask=self_attn_mask |
| 634 | ) |
| 635 | x = F.dropout(x, self.dropout, training=self.training) |
| 636 | x = residual + x |
| 637 | |
| 638 | residual = x |
| 639 | x = self.layer_norm2(x) |
| 640 | if encoder_out is not None: |
| 641 | x, attn = self.encoder_attn( |
| 642 | query=x, |
| 643 | key=encoder_out, |
| 644 | value=encoder_out, |
| 645 | key_padding_mask=encoder_padding_mask, |
| 646 | incremental_state=incremental_state, |
| 647 | static_kv=True, |
| 648 | enc_dec_attn_constraint_mask=None, #utils.get_incremental_state(self, incremental_state, 'enc_dec_attn_constraint_mask'), |
nothing calls this directly
no outgoing calls
no test coverage detected