Attempt to fix head index
This commit is contained in:
@@ -233,12 +233,14 @@ class Attention(nn.Module):
|
|||||||
self.c_proj = Conv1D(n_state, nx)
|
self.c_proj = Conv1D(n_state, nx)
|
||||||
self.attn_dropout = nn.Dropout(config.attn_pdrop)
|
self.attn_dropout = nn.Dropout(config.attn_pdrop)
|
||||||
self.resid_dropout = nn.Dropout(config.resid_pdrop)
|
self.resid_dropout = nn.Dropout(config.resid_pdrop)
|
||||||
|
self.pruned_heads = []
|
||||||
|
|
||||||
def prune_heads(self, heads):
|
def prune_heads(self, heads):
|
||||||
if len(heads) == 0:
|
if len(heads) == 0:
|
||||||
return
|
return
|
||||||
mask = torch.ones(self.n_head, self.split_size // self.n_head)
|
mask = torch.ones(self.n_head, self.split_size // self.n_head)
|
||||||
for head in heads:
|
for head in heads:
|
||||||
|
head -= len(list(filter(lambda h: h < head, self.pruned_heads)))
|
||||||
mask[head] = 0
|
mask[head] = 0
|
||||||
mask = mask.view(-1).contiguous().eq(1)
|
mask = mask.view(-1).contiguous().eq(1)
|
||||||
index = torch.arange(len(mask))[mask].long()
|
index = torch.arange(len(mask))[mask].long()
|
||||||
@@ -249,6 +251,7 @@ class Attention(nn.Module):
|
|||||||
# Update hyper params
|
# Update hyper params
|
||||||
self.split_size = (self.split_size // self.n_head) * (self.n_head - len(heads))
|
self.split_size = (self.split_size // self.n_head) * (self.n_head - len(heads))
|
||||||
self.n_head = self.n_head - len(heads)
|
self.n_head = self.n_head - len(heads)
|
||||||
|
self.pruned_heads.extend(heads)
|
||||||
|
|
||||||
def _attn(self, q, k, v, head_mask=None):
|
def _attn(self, q, k, v, head_mask=None):
|
||||||
w = torch.matmul(q, k)
|
w = torch.matmul(q, k)
|
||||||
|
|||||||
Reference in New Issue
Block a user