MCPcopy Create free account
hub / github.com/AIS-SNU/Smart-Infinity / BertModel

Class BertModel

DeepSpeedExample/megatron/model/bert_model.py:104–200  ·  view source on GitHub ↗

Bert Language model.

Source from the content-addressed store, hash-verified

102
103
104class BertModel(MegatronModule):
105 """Bert Language model."""
106
107 def __init__(self, num_tokentypes=2, add_binary_head=True,
108 parallel_output=True):
109 super(BertModel, self).__init__()
110 args = get_args()
111
112 self.fp16_lm_cross_entropy = args.fp16_lm_cross_entropy
113 self.add_binary_head = add_binary_head
114 self.parallel_output = parallel_output
115 init_method = init_method_normal(args.init_method_std)
116 scaled_init_method = scaled_init_method_normal(args.init_method_std,
117 args.num_layers)
118
119 self.language_model, self._language_model_key = get_language_model(
120 attention_mask_func=bert_attention_mask_func,
121 num_tokentypes=num_tokentypes,
122 add_pooler=self.add_binary_head,
123 init_method=init_method,
124 scaled_init_method=scaled_init_method)
125
126 self.lm_head = BertLMHead(
127 self.language_model.embedding.word_embeddings.weight.size(0),
128 args.hidden_size, init_method, args.layernorm_epsilon, parallel_output)
129 self._lm_head_key = 'lm_head'
130 if self.add_binary_head:
131 self.binary_head = get_linear_layer(args.hidden_size, 2,
132 init_method)
133 self._binary_head_key = 'binary_head'
134
135 def forward(self, input_ids, attention_mask,
136 tokentype_ids=None, lm_labels=None):
137
138 extended_attention_mask = bert_extended_attention_mask(attention_mask)
139 position_ids = bert_position_ids(input_ids)
140
141 if self.add_binary_head:
142 lm_output, pooled_output = self.language_model(
143 input_ids,
144 position_ids,
145 extended_attention_mask,
146 tokentype_ids=tokentype_ids)
147 else:
148 lm_output = self.language_model(
149 input_ids,
150 position_ids,
151 extended_attention_mask,
152 tokentype_ids=tokentype_ids)
153
154 # Output.
155 lm_logits = self.lm_head(
156 lm_output, self.language_model.embedding.word_embeddings.weight)
157
158 binary_logits = None
159 if self.add_binary_head:
160 binary_logits = self.binary_head(pooled_output)
161

Callers 1

model_providerFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected