@@ -118,6 +118,7 @@ def forward(
118
118
return embeddings
119
119
120
120
121
+ # Copied from transformers.modeling_bert.BertSelfAttention with Bert->LayoutLM
121
122
class LayoutLMSelfAttention (nn .Module ):
122
123
def __init__ (self , config ):
123
124
super ().__init__ ()
@@ -172,6 +173,7 @@ def forward(
172
173
attention_scores = torch .matmul (query_layer , key_layer .transpose (- 1 , - 2 ))
173
174
attention_scores = attention_scores / math .sqrt (self .attention_head_size )
174
175
if attention_mask is not None :
176
+ # Apply the attention mask is (precomputed for all layers in LayoutLMModel forward() function)
175
177
attention_scores = attention_scores + attention_mask
176
178
177
179
# Normalize the attention scores to probabilities.
@@ -195,6 +197,7 @@ def forward(
195
197
return outputs
196
198
197
199
200
+ # Copied from transformers.modeling_bert.BertSelfOutput with Bert->LayoutLM
198
201
class LayoutLMSelfOutput (nn .Module ):
199
202
def __init__ (self , config ):
200
203
super ().__init__ ()
@@ -209,6 +212,7 @@ def forward(self, hidden_states, input_tensor):
209
212
return hidden_states
210
213
211
214
215
+ # Copied from transformers.modeling_bert.BertAttention with Bert->LayoutLM
212
216
class LayoutLMAttention (nn .Module ):
213
217
def __init__ (self , config ):
214
218
super ().__init__ ()
@@ -256,6 +260,7 @@ def forward(
256
260
return outputs
257
261
258
262
263
+ # Copied from transformers.modeling_bert.BertIntermediate
259
264
class LayoutLMIntermediate (nn .Module ):
260
265
def __init__ (self , config ):
261
266
super ().__init__ ()
@@ -271,6 +276,7 @@ def forward(self, hidden_states):
271
276
return hidden_states
272
277
273
278
279
+ # Copied from transformers.modeling_bert.BertOutput with Bert->LayoutLM
274
280
class LayoutLMOutput (nn .Module ):
275
281
def __init__ (self , config ):
276
282
super ().__init__ ()
@@ -285,6 +291,7 @@ def forward(self, hidden_states, input_tensor):
285
291
return hidden_states
286
292
287
293
294
+ # Copied from transformers.modeling_bert.BertLayer with Bert->LayoutLM
288
295
class LayoutLMLayer (nn .Module ):
289
296
def __init__ (self , config ):
290
297
super ().__init__ ()
@@ -344,6 +351,7 @@ def feed_forward_chunk(self, attention_output):
344
351
return layer_output
345
352
346
353
354
+ # Copied from transformers.modeling_bert.BertEncoder with Bert->LayoutLM
347
355
class LayoutLMEncoder (nn .Module ):
348
356
def __init__ (self , config ):
349
357
super ().__init__ ()
@@ -408,6 +416,7 @@ def custom_forward(*inputs):
408
416
)
409
417
410
418
419
+ # Copied from transformers.modeling_bert.BertPooler
411
420
class LayoutLMPooler (nn .Module ):
412
421
def __init__ (self , config ):
413
422
super ().__init__ ()
@@ -423,6 +432,7 @@ def forward(self, hidden_states):
423
432
return pooled_output
424
433
425
434
435
+ # Copied from transformers.modeling_bert.BertPredictionHeadTransform with Bert->LayoutLM
426
436
class LayoutLMPredictionHeadTransform (nn .Module ):
427
437
def __init__ (self , config ):
428
438
super ().__init__ ()
@@ -440,6 +450,7 @@ def forward(self, hidden_states):
440
450
return hidden_states
441
451
442
452
453
+ # Copied from transformers.modeling_bert.BertLMPredictionHead with Bert->LayoutLM
443
454
class LayoutLMLMPredictionHead (nn .Module ):
444
455
def __init__ (self , config ):
445
456
super ().__init__ ()
@@ -460,6 +471,7 @@ def forward(self, hidden_states):
460
471
return hidden_states
461
472
462
473
474
+ # Copied from transformers.modeling_bert.BertOnlyMLMHead with Bert->LayoutLM
463
475
class LayoutLMOnlyMLMHead (nn .Module ):
464
476
def __init__ (self , config ):
465
477
super ().__init__ ()
@@ -470,28 +482,6 @@ def forward(self, sequence_output):
470
482
return prediction_scores
471
483
472
484
473
- class LayoutLMOnlyNSPHead (nn .Module ):
474
- def __init__ (self , config ):
475
- super ().__init__ ()
476
- self .seq_relationship = nn .Linear (config .hidden_size , 2 )
477
-
478
- def forward (self , pooled_output ):
479
- seq_relationship_score = self .seq_relationship (pooled_output )
480
- return seq_relationship_score
481
-
482
-
483
- class LayoutLMPreTrainingHeads (nn .Module ):
484
- def __init__ (self , config ):
485
- super ().__init__ ()
486
- self .predictions = LayoutLMLMPredictionHead (config )
487
- self .seq_relationship = nn .Linear (config .hidden_size , 2 )
488
-
489
- def forward (self , sequence_output , pooled_output ):
490
- prediction_scores = self .predictions (sequence_output )
491
- seq_relationship_score = self .seq_relationship (pooled_output )
492
- return prediction_scores , seq_relationship_score
493
-
494
-
495
485
class LayoutLMPreTrainedModel (PreTrainedModel ):
496
486
"""An abstract class to handle weights initialization and
497
487
a simple interface for downloading and loading pretrained models.
0 commit comments