A stack of BERT layers providing the backbone of FlexBERT. This module is modeled after the Hugging Face BERT's :class:`~transformers.model.bert.modeling_bert.BertAlibiEncoder`, but with substantial modifications to implement unpadding and ALiBi. Compared to the analogous Hugging Face
| 663 | |
| 664 | |
| 665 | class FlexBertPaddedEncoder(FlexBertEncoderBase): |
| 666 | """A stack of BERT layers providing the backbone of FlexBERT. |
| 667 | |
| 668 | This module is modeled after the Hugging Face BERT's :class:`~transformers.model.bert.modeling_bert.BertAlibiEncoder`, |
| 669 | but with substantial modifications to implement unpadding and ALiBi. |
| 670 | |
| 671 | Compared to the analogous Hugging Face BERT module, this module handles unpadding to reduce unnecessary computation |
| 672 | at padded tokens, and pre-computes attention biases to implement ALiBi. |
| 673 | """ |
| 674 | |
| 675 | def __init__(self, config: FlexBertConfig): |
| 676 | super().__init__() |
| 677 | self.layers = nn.ModuleList([get_bert_layer(config, layer_id=i) for i in range(config.num_hidden_layers)]) |
| 678 | self.num_attention_heads = config.num_attention_heads |
| 679 | |
| 680 | def forward(self, hidden_states: torch.Tensor, attention_mask: torch.Tensor, **kwargs) -> torch.Tensor: |
| 681 | for layer_module in self.layers: |
| 682 | hidden_states = layer_module(hidden_states, attn_mask=attention_mask) |
| 683 | |
| 684 | return hidden_states |
| 685 | |
| 686 | |
| 687 | ENC2CLS = { |
nothing calls this directly
no outgoing calls
no test coverage detected