MCPcopy Create free account
hub / github.com/SooLab/CGFormer / get_head_mask

Method get_head_mask

bert/modeling_utils.py:232–251  ·  view source on GitHub ↗

# Prepare head mask if needed # 1.0 in head_mask indicate we keep the head attention_probs has shape bsz x n_heads x N x N Arguments: head_mask: torch.Tensor or None: has shape [num_heads] or [num_hidden_layers x num_heads] num_hidden_layers:

(self, head_mask: Tensor, num_hidden_layers: int, is_attention_chunked: bool = False)

Source from the content-addressed store, hash-verified

230 return extended_attention_mask
231
232 def get_head_mask(self, head_mask: Tensor, num_hidden_layers: int, is_attention_chunked: bool = False) -> Tensor:
233 """
234 # Prepare head mask if needed
235 # 1.0 in head_mask indicate we keep the head
236 attention_probs has shape bsz x n_heads x N x N
237 Arguments:
238 head_mask: torch.Tensor or None: has shape [num_heads] or [num_hidden_layers x num_heads]
239 num_hidden_layers: int
240 Returns:
241 Tensor of shape shape [num_hidden_layers x batch x num_heads x seq_length x seq_length]
242 or list with [None] for each layer
243 """
244 if head_mask is not None:
245 head_mask = self._convert_head_mask_to_5d(head_mask, num_hidden_layers)
246 if is_attention_chunked is True:
247 head_mask = head_mask.unsqueeze(-1)
248 else:
249 head_mask = [None] * num_hidden_layers
250
251 return head_mask
252
253 def _convert_head_mask_to_5d(self, head_mask, num_hidden_layers):
254 """-> [num_hidden_layers x batch x num_heads x seq_length x seq_length]"""

Callers 1

forwardMethod · 0.80

Calls 1

Tested by

no test coverage detected