Nekochu commited on
Commit
a18ef2d
·
1 Parent(s): 9a7501f

vendor get_head_mask, removed from transformers 5

Browse files
Files changed (1) hide show
  1. minigpt4/models/Qformer.py +15 -1
minigpt4/models/Qformer.py CHANGED
@@ -56,6 +56,20 @@ from transformers.models.bert.configuration_bert import BertConfig
56
  logger = logging.get_logger(__name__)
57
 
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  class BertEmbeddings(nn.Module):
60
  """Construct the embeddings from word and position embeddings."""
61
 
@@ -940,7 +954,7 @@ class BertModel(BertPreTrainedModel):
940
  # attention_probs has shape bsz x n_heads x N x N
941
  # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads]
942
  # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length]
943
- head_mask = self.get_head_mask(head_mask, self.config.num_hidden_layers)
944
 
945
  encoder_outputs = self.encoder(
946
  embedding_output,
 
56
  logger = logging.get_logger(__name__)
57
 
58
 
59
+ def _get_head_mask(head_mask, num_hidden_layers):
60
+ """ModuleUtilsMixin.get_head_mask was removed in transformers 5.x. Head masking
61
+ is a training/analysis feature; at inference head_mask is always None, so the
62
+ contract is simply one None per layer."""
63
+ if head_mask is None:
64
+ return [None] * num_hidden_layers
65
+ if head_mask.dim() == 1:
66
+ head_mask = head_mask.unsqueeze(0).unsqueeze(0).unsqueeze(-1).unsqueeze(-1)
67
+ head_mask = head_mask.expand(num_hidden_layers, -1, -1, -1, -1)
68
+ elif head_mask.dim() == 2:
69
+ head_mask = head_mask.unsqueeze(1).unsqueeze(-1).unsqueeze(-1)
70
+ return head_mask
71
+
72
+
73
  class BertEmbeddings(nn.Module):
74
  """Construct the embeddings from word and position embeddings."""
75
 
 
954
  # attention_probs has shape bsz x n_heads x N x N
955
  # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads]
956
  # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length]
957
+ head_mask = _get_head_mask(head_mask, self.config.num_hidden_layers)
958
 
959
  encoder_outputs = self.encoder(
960
  embedding_output,