Overall BERT model. Args: config: a BertConfig class instance with the configuration to build a new model Inputs: `input_ids`: a torch.LongTensor of shape [batch_size, sequence_length] with the word token indices in the vocabulary(see the tokens preprocessing lo
| 128 | |
| 129 | |
| 130 | class BertModel(BertPreTrainedModel): |
| 131 | """Overall BERT model. |
| 132 | |
| 133 | Args: |
| 134 | config: a BertConfig class instance with the configuration to build a new model |
| 135 | |
| 136 | Inputs: |
| 137 | `input_ids`: a torch.LongTensor of shape [batch_size, sequence_length] |
| 138 | with the word token indices in the vocabulary(see the tokens preprocessing logic in the scripts |
| 139 | `extract_features.py`, `run_classifier.py` and `run_squad.py`) |
| 140 | `token_type_ids`: an optional torch.LongTensor of shape [batch_size, sequence_length] with the token |
| 141 | types indices selected in [0, 1]. Type 0 corresponds to a `sentence A` and type 1 corresponds to |
| 142 | a `sentence B` token (see BERT paper for more details). |
| 143 | `attention_mask`: an optional torch.LongTensor of shape [batch_size, sequence_length] with indices |
| 144 | selected in [0, 1]. It's a mask to be used if the input sequence length is smaller than the max |
| 145 | input sequence length in the current batch. It's the mask that we typically use for attention when |
| 146 | a batch has varying length sentences. |
| 147 | `output_all_encoded_layers`: boolean which controls the content of the `encoded_layers` output as described below. Default: `True`. |
| 148 | |
| 149 | Outputs: Tuple of (encoded_layers, pooled_output) |
| 150 | `encoded_layers`: controlled by `output_all_encoded_layers` argument: |
| 151 | - `output_all_encoded_layers=True`: outputs a list of the full sequences of encoded-hidden-states at the end |
| 152 | of each attention block (i.e. 12 full sequences for BERT-base, 24 for BERT-large), each |
| 153 | encoded-hidden-state is a torch.FloatTensor of size [batch_size, sequence_length, hidden_size], |
| 154 | - `output_all_encoded_layers=False`: outputs only the full sequence of hidden-states corresponding |
| 155 | to the last attention block of shape [batch_size, sequence_length, hidden_size], |
| 156 | `pooled_output`: a torch.FloatTensor of size [batch_size, hidden_size] which is the output of a |
| 157 | classifier pretrained on top of the hidden state associated to the first character of the |
| 158 | input (`CLS`) to train on the Next-Sentence task (see BERT's paper). |
| 159 | |
| 160 | Example usage: |
| 161 | ```python |
| 162 | # Already been converted into WordPiece token ids |
| 163 | input_ids = torch.LongTensor([[31, 51, 99], [15, 5, 0]]) |
| 164 | input_mask = torch.LongTensor([[1, 1, 1], [1, 1, 0]]) |
| 165 | token_type_ids = torch.LongTensor([[0, 0, 1], [0, 1, 0]]) |
| 166 | config = modeling.BertConfig(vocab_size_or_config_json_file=32000, hidden_size=768, |
| 167 | num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072) |
| 168 | model = BertModel(config=config) |
| 169 | all_encoder_layers, pooled_output = model(input_ids, token_type_ids, input_mask) |
| 170 | ``` |
| 171 | """ |
| 172 | |
| 173 | def __init__( |
| 174 | self, |
| 175 | config, |
| 176 | add_pooling_layer: bool = True, |
| 177 | ): |
| 178 | super(BertModel, self).__init__(config) |
| 179 | self.embeddings = BertAlibiEmbeddings(config) |
| 180 | self.encoder = BertAlibiEncoder(config) |
| 181 | self.pooler = BertPooler(config) if add_pooling_layer else None |
| 182 | self.post_init() |
| 183 | |
| 184 | def get_input_embeddings(self): |
| 185 | return self.embeddings.word_embeddings |
| 186 | |
| 187 | def set_input_embeddings(self, value): |