Update modeling_norbert.py
Browse files- modeling_norbert.py +12 -40
modeling_norbert.py
CHANGED
@@ -57,14 +57,6 @@ class MaskClassifier(nn.Module):
|
|
57 |
nn.Dropout(config.hidden_dropout_prob),
|
58 |
nn.Linear(subword_embedding.size(1), subword_embedding.size(0))
|
59 |
)
|
60 |
-
self.initialize(config.hidden_size, subword_embedding)
|
61 |
-
|
62 |
-
def initialize(self, hidden_size, embedding):
|
63 |
-
std = math.sqrt(2.0 / (5.0 * hidden_size))
|
64 |
-
nn.init.trunc_normal_(self.nonlinearity[1].weight, mean=0.0, std=std, a=-2*std, b=2*std)
|
65 |
-
self.nonlinearity[-1].weight = embedding
|
66 |
-
self.nonlinearity[1].bias.data.zero_()
|
67 |
-
self.nonlinearity[-1].bias.data.zero_()
|
68 |
|
69 |
def forward(self, x, masked_lm_labels=None):
|
70 |
if masked_lm_labels is not None:
|
@@ -104,12 +96,6 @@ class FeedForward(nn.Module):
|
|
104 |
nn.Linear(config.intermediate_size, config.hidden_size, bias=False),
|
105 |
nn.Dropout(config.hidden_dropout_prob)
|
106 |
)
|
107 |
-
self.initialize(config.hidden_size)
|
108 |
-
|
109 |
-
def initialize(self, hidden_size):
|
110 |
-
std = math.sqrt(2.0 / (5.0 * hidden_size))
|
111 |
-
nn.init.trunc_normal_(self.mlp[1].weight, mean=0.0, std=std, a=-2*std, b=2*std)
|
112 |
-
nn.init.trunc_normal_(self.mlp[-2].weight, mean=0.0, std=std, a=-2*std, b=2*std)
|
113 |
|
114 |
def forward(self, x):
|
115 |
return self.mlp(x)
|
@@ -160,7 +146,6 @@ class Attention(nn.Module):
|
|
160 |
|
161 |
self.dropout = nn.Dropout(config.attention_probs_dropout_prob)
|
162 |
self.scale = 1.0 / math.sqrt(3 * self.head_size)
|
163 |
-
self.initialize()
|
164 |
|
165 |
def make_log_bucket_position(self, relative_pos, bucket_size, max_position):
|
166 |
sign = torch.sign(relative_pos)
|
@@ -170,15 +155,6 @@ class Attention(nn.Module):
|
|
170 |
bucket_pos = torch.where(abs_pos <= mid, relative_pos, log_pos * sign).long()
|
171 |
return bucket_pos
|
172 |
|
173 |
-
def initialize(self):
|
174 |
-
std = math.sqrt(2.0 / (5.0 * self.hidden_size))
|
175 |
-
nn.init.trunc_normal_(self.in_proj_qk.weight, mean=0.0, std=std, a=-2*std, b=2*std)
|
176 |
-
nn.init.trunc_normal_(self.in_proj_v.weight, mean=0.0, std=std, a=-2*std, b=2*std)
|
177 |
-
nn.init.trunc_normal_(self.out_proj.weight, mean=0.0, std=std, a=-2*std, b=2*std)
|
178 |
-
self.in_proj_qk.bias.data.zero_()
|
179 |
-
self.in_proj_v.bias.data.zero_()
|
180 |
-
self.out_proj.bias.data.zero_()
|
181 |
-
|
182 |
def compute_attention_scores(self, hidden_states, relative_embedding):
|
183 |
key_len, batch_size, _ = hidden_states.size()
|
184 |
query_len = key_len
|
@@ -246,13 +222,6 @@ class Embedding(nn.Module):
|
|
246 |
self.relative_embedding = nn.Parameter(torch.empty(2 * config.position_bucket_size - 1, config.hidden_size))
|
247 |
self.relative_layer_norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
248 |
|
249 |
-
self.initialize()
|
250 |
-
|
251 |
-
def initialize(self):
|
252 |
-
std = math.sqrt(2.0 / (5.0 * self.hidden_size))
|
253 |
-
nn.init.trunc_normal_(self.relative_embedding, mean=0.0, std=std, a=-2*std, b=2*std)
|
254 |
-
nn.init.trunc_normal_(self.word_embedding.weight, mean=0.0, std=std, a=-2*std, b=2*std)
|
255 |
-
|
256 |
def forward(self, input_ids):
|
257 |
word_embedding = self.dropout(self.word_layer_norm(self.word_embedding(input_ids)))
|
258 |
relative_embeddings = self.relative_layer_norm(self.relative_embedding)
|
@@ -273,13 +242,24 @@ class NorbertPreTrainedModel(PreTrainedModel):
|
|
273 |
module.activation_checkpointing = value
|
274 |
|
275 |
def _init_weights(self, module):
|
276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
|
278 |
|
279 |
class NorbertModel(NorbertPreTrainedModel):
|
280 |
def __init__(self, config, add_mlm_layer=False, gradient_checkpointing=False, **kwargs):
|
281 |
super().__init__(config, **kwargs)
|
282 |
self.config = config
|
|
|
283 |
|
284 |
self.embedding = Embedding(config)
|
285 |
self.transformer = Encoder(config, activation_checkpointing=gradient_checkpointing)
|
@@ -414,14 +394,6 @@ class Classifier(nn.Module):
|
|
414 |
nn.Dropout(drop_out),
|
415 |
nn.Linear(config.hidden_size, num_labels)
|
416 |
)
|
417 |
-
self.initialize(config.hidden_size)
|
418 |
-
|
419 |
-
def initialize(self, hidden_size):
|
420 |
-
std = math.sqrt(2.0 / (5.0 * hidden_size))
|
421 |
-
nn.init.trunc_normal_(self.nonlinearity[1].weight, mean=0.0, std=std, a=-2*std, b=2*std)
|
422 |
-
nn.init.trunc_normal_(self.nonlinearity[-1].weight, mean=0.0, std=std, a=-2*std, b=2*std)
|
423 |
-
self.nonlinearity[1].bias.data.zero_()
|
424 |
-
self.nonlinearity[-1].bias.data.zero_()
|
425 |
|
426 |
def forward(self, x):
|
427 |
x = self.nonlinearity(x)
|
|
|
57 |
nn.Dropout(config.hidden_dropout_prob),
|
58 |
nn.Linear(subword_embedding.size(1), subword_embedding.size(0))
|
59 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
def forward(self, x, masked_lm_labels=None):
|
62 |
if masked_lm_labels is not None:
|
|
|
96 |
nn.Linear(config.intermediate_size, config.hidden_size, bias=False),
|
97 |
nn.Dropout(config.hidden_dropout_prob)
|
98 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
def forward(self, x):
|
101 |
return self.mlp(x)
|
|
|
146 |
|
147 |
self.dropout = nn.Dropout(config.attention_probs_dropout_prob)
|
148 |
self.scale = 1.0 / math.sqrt(3 * self.head_size)
|
|
|
149 |
|
150 |
def make_log_bucket_position(self, relative_pos, bucket_size, max_position):
|
151 |
sign = torch.sign(relative_pos)
|
|
|
155 |
bucket_pos = torch.where(abs_pos <= mid, relative_pos, log_pos * sign).long()
|
156 |
return bucket_pos
|
157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
def compute_attention_scores(self, hidden_states, relative_embedding):
|
159 |
key_len, batch_size, _ = hidden_states.size()
|
160 |
query_len = key_len
|
|
|
222 |
self.relative_embedding = nn.Parameter(torch.empty(2 * config.position_bucket_size - 1, config.hidden_size))
|
223 |
self.relative_layer_norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
def forward(self, input_ids):
|
226 |
word_embedding = self.dropout(self.word_layer_norm(self.word_embedding(input_ids)))
|
227 |
relative_embeddings = self.relative_layer_norm(self.relative_embedding)
|
|
|
242 |
module.activation_checkpointing = value
|
243 |
|
244 |
def _init_weights(self, module):
|
245 |
+
std = math.sqrt(2.0 / (5.0 * self.hidden_size))
|
246 |
+
|
247 |
+
if isinstance(module, nn.Linear):
|
248 |
+
nn.init.trunc_normal_(module.weight.data, mean=0.0, std=std, a=-2*std, b=2*std)
|
249 |
+
if module.bias is not None:
|
250 |
+
module.bias.data.zero_()
|
251 |
+
elif isinstance(module, nn.Embedding):
|
252 |
+
nn.init.trunc_normal_(module.weight.data, mean=0.0, std=std, a=-2*std, b=2*std)
|
253 |
+
elif isinstance(module, nn.LayerNorm):
|
254 |
+
module.bias.data.zero_()
|
255 |
+
module.weight.data.fill_(1.0)
|
256 |
|
257 |
|
258 |
class NorbertModel(NorbertPreTrainedModel):
|
259 |
def __init__(self, config, add_mlm_layer=False, gradient_checkpointing=False, **kwargs):
|
260 |
super().__init__(config, **kwargs)
|
261 |
self.config = config
|
262 |
+
self.hidden_size = config.hidden_size
|
263 |
|
264 |
self.embedding = Embedding(config)
|
265 |
self.transformer = Encoder(config, activation_checkpointing=gradient_checkpointing)
|
|
|
394 |
nn.Dropout(drop_out),
|
395 |
nn.Linear(config.hidden_size, num_labels)
|
396 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
397 |
|
398 |
def forward(self, x):
|
399 |
x = self.nonlinearity(x)
|