MCPcopy Create free account
hub / github.com/THUDM/GLM / forward

Method forward

model/modeling_bert.py:904–941  ·  view source on GitHub ↗
(self, input_ids, token_type_ids=None, attention_mask=None, output_all_encoded_layers=True,
                checkpoint_activations=False)

Source from the content-addressed store, hash-verified

902 self.apply(self.init_bert_weights)
903
904 def forward(self, input_ids, token_type_ids=None, attention_mask=None, output_all_encoded_layers=True,
905 checkpoint_activations=False):
906 if attention_mask is None:
907 attention_mask = torch.ones_like(input_ids)
908 if token_type_ids is None:
909 token_type_ids = torch.zeros_like(input_ids)
910
911 # We create a 3D attention mask from a 2D tensor mask.
912 # Sizes are [batch_size, 1, 1, to_seq_length]
913 # So we can broadcast to [batch_size, num_heads, from_seq_length, to_seq_length]
914 # this attention mask is more simple than the triangular masking of causal attention
915 # used in OpenAI GPT, we just need to prepare the broadcast dimension here.
916 extended_attention_mask = attention_mask.unsqueeze(1).unsqueeze(2)
917
918 # Since attention_mask is 1.0 for positions we want to attend and 0.0 for
919 # masked positions, this operation will create a tensor which is 0.0 for
920 # positions we want to attend and -10000.0 for masked positions.
921 # Since we are adding it to the raw scores before the softmax, this is
922 # effectively the same as removing these entirely.
923 extended_attention_mask = extended_attention_mask.to(
924 dtype=next(self.encoder.parameters()).dtype) # fp16 compatibility
925 extended_attention_mask = (1.0 - extended_attention_mask) * -10000.0
926
927 embedding_output = self.embeddings(input_ids, token_type_ids)
928 encoded_layers = self.encoder(embedding_output,
929 extended_attention_mask,
930 output_all_encoded_layers=output_all_encoded_layers,
931 checkpoint_activations=checkpoint_activations)
932 sequence_output = encoded_layers[-1]
933 for p in self.pooler.parameters():
934 if p is None:
935 continue
936 sequence_output = sequence_output.type_as(p)
937 break
938 pooled_output = self.pooler(sequence_output)
939 if not output_all_encoded_layers or checkpoint_activations:
940 encoded_layers = encoded_layers[-1]
941 return encoded_layers, pooled_output
942
943
944class BertForPreTraining(PreTrainedBertModel):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected