forked from ulzee/dkbehrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattention.py
215 lines (188 loc) · 9.47 KB
/
attention.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union
import math
import torch
import torch.nn as nn
import torch.utils.checkpoint
from packaging import version
from torch import nn
from transformers.utils import get_torch_version
from transformers.pytorch_utils import find_pruneable_heads_and_indices, prune_linear_layer
from transformers.models.bert.modeling_bert import BertSelfAttention, BertSelfOutput, BERT_SELF_ATTENTION_CLASSES
class BertAttention(nn.Module):
def __init__(self, config, position_embedding_type=None):
super().__init__()
self.self = BERT_SELF_ATTENTION_CLASSES[config._attn_implementation](
config, position_embedding_type=position_embedding_type
)
self.output = BertSelfOutput(config)
self.pruned_heads = set()
def prune_heads(self, heads):
if len(heads) == 0:
return
heads, index = find_pruneable_heads_and_indices(
heads, self.self.num_attention_heads, self.self.attention_head_size, self.pruned_heads
)
# Prune linear layers
self.self.query = prune_linear_layer(self.self.query, index)
self.self.key = prune_linear_layer(self.self.key, index)
self.self.value = prune_linear_layer(self.self.value, index)
self.output.dense = prune_linear_layer(self.output.dense, index, dim=1)
# Update hyper params and store pruned heads
self.self.num_attention_heads = self.self.num_attention_heads - len(heads)
self.self.all_head_size = self.self.attention_head_size * self.self.num_attention_heads
self.pruned_heads = self.pruned_heads.union(heads)
def forward(
self,
hidden_states: torch.Tensor,
attention_mask: Optional[torch.FloatTensor] = None,
head_mask: Optional[torch.FloatTensor] = None,
encoder_hidden_states: Optional[torch.FloatTensor] = None,
encoder_attention_mask: Optional[torch.FloatTensor] = None,
past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
output_attentions: Optional[bool] = False,
) -> Tuple[torch.Tensor]:
self_outputs = self.self(
hidden_states,
attention_mask,
head_mask,
encoder_hidden_states,
encoder_attention_mask,
past_key_value,
output_attentions,
)
attention_output = self.output(self_outputs[0], hidden_states)
outputs = (attention_output,) + self_outputs[1:] # add attentions if we output them
return outputs
def weighted_scaled_dot_product_attention(
query, key, value, weighting_matrix, attn_mask=None, dropout_p=0.0, is_causal=False, scale=None
) -> torch.Tensor:
L, S = query.size(-2), key.size(-2)
scale_factor = 1 / math.sqrt(query.size(-1)) if scale is None else scale
attn_bias = torch.zeros(L, S, dtype=query.dtype).to(query.device)
if is_causal:
assert attn_mask is None
temp_mask = torch.ones(L, S, dtype=torch.bool).tril(diagonal=0)
attn_bias.masked_fill_(temp_mask.logical_not(), float("-inf"))
attn_bias.to(query.dtype)
if attn_mask is not None:
if attn_mask.dtype == torch.bool:
attn_bias.masked_fill_(attn_mask.logical_not(), float("-inf"))
else:
# attn_bias += attn_mask # NOTE: fixed below for broadcasting, maybe a version issue?
attn_bias = attn_bias.unsqueeze(0).unsqueeze(0).to(query.device) + attn_mask
attn_weight = query @ key.transpose(-2, -1) * scale_factor
attn_weight += attn_bias
# NOTE: NEW
attn_weight += weighting_matrix.unsqueeze(1)
attn_weight = torch.softmax(attn_weight, dim=-1)
attn_weight = torch.dropout(attn_weight, dropout_p, train=True)
return attn_weight @ value
class WeightedAttention(BertSelfAttention):
def __init__(self, config, embeddings, current_input, position_embedding_type='absolute', use_proj='linear'):
super().__init__(config, position_embedding_type=position_embedding_type)
self.dropout_prob = config.attention_probs_dropout_prob
self.require_contiguous_qkv = version.parse(get_torch_version()) < version.parse("2.2.0")
self.embeddings = embeddings
self.current_input = current_input
self.use_proj = use_proj
self.emb_mlp = nn.ModuleList()
self.emb_scaler = nn.ParameterList()
for embedding in self.embeddings:
nembs, edim = embedding.extra_embeddings.weight.shape
if use_proj == 'linear':
self.emb_mlp.append(nn.Linear(edim, edim))
elif use_proj == 'linear_scaler':
self.emb_mlp.append(nn.Linear(edim, 1024))
# nn.init.eye_(self.emb_mlp[-1].weight)
# nn.init.zeros_(self.emb_mlp[-1].bias)
self.emb_scaler.append(nn.Parameter(torch.ones(nembs), requires_grad=True))
elif use_proj == 'linear_scaler_v0':
self.emb_mlp.append(nn.Linear(edim, edim))
nn.init.eye_(self.emb_mlp[-1].weight)
nn.init.zeros_(self.emb_mlp[-1].bias)
self.emb_scaler.append(nn.Parameter(torch.ones(nembs), requires_grad=True))
else:
self.emb_mlp.append(nn.Identity())
def forward(
self,
hidden_states: torch.Tensor,
attention_mask: Optional[torch.Tensor] = None,
head_mask: Optional[torch.FloatTensor] = None,
encoder_hidden_states: Optional[torch.FloatTensor] = None,
encoder_attention_mask: Optional[torch.FloatTensor] = None,
past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
output_attentions: Optional[bool] = False,
) -> Tuple[torch.Tensor]:
if self.position_embedding_type != "absolute" or output_attentions or head_mask is not None:
return super().forward(
hidden_states,
attention_mask,
head_mask,
encoder_hidden_states,
encoder_attention_mask,
past_key_value,
output_attentions,
)
bsz, tgt_len, _ = hidden_states.size()
query_layer = self.transpose_for_scores(self.query(hidden_states))
is_cross_attention = encoder_hidden_states is not None
current_states = encoder_hidden_states if is_cross_attention else hidden_states
attention_mask = encoder_attention_mask if is_cross_attention else attention_mask
if is_cross_attention and past_key_value and past_key_value[0].shape[2] == current_states.shape[1]:
key_layer, value_layer = past_key_value
else:
key_layer = self.transpose_for_scores(self.key(current_states))
value_layer = self.transpose_for_scores(self.value(current_states))
if past_key_value is not None and not is_cross_attention:
key_layer = torch.cat([past_key_value[0], key_layer], dim=2)
value_layer = torch.cat([past_key_value[1], value_layer], dim=2)
if self.is_decoder:
past_key_value = (key_layer, value_layer)
if self.require_contiguous_qkv and query_layer.device.type == "cuda" and attention_mask is not None:
query_layer = query_layer.contiguous()
key_layer = key_layer.contiguous()
value_layer = value_layer.contiguous()
is_causal = (
True if self.is_decoder and not is_cross_attention and attention_mask is None and tgt_len > 1 else False
)
# NOTE: New
if self.use_proj == 'linear_scaler_v0':
emb_sim = 0
for embset_idx, embedding in enumerate(self.embeddings):
user_embs = embedding.extra_embeddings.to(
self.current_input.input_ids.device)
indexed_embs = user_embs(self.current_input.input_ids)
user_embs_mlp = self.emb_mlp[embset_idx](indexed_embs)
user_embs_mlp = self.emb_scaler[embset_idx][self.current_input.input_ids].unsqueeze(-1) * user_embs_mlp
emb_sim += torch.matmul(user_embs_mlp, user_embs_mlp.transpose(1, 2))
emb_sim /= len(self.embeddings)
else:
prior_embs = 0
for embset_idx, embedding in enumerate(self.embeddings):
user_embs = embedding.extra_embeddings.to(
self.current_input.input_ids.device)
indexed_embs = user_embs(self.current_input.input_ids)
if self.use_proj == 'linear':
user_embs_mlp = self.emb_mlp[embset_idx](indexed_embs)
elif self.use_proj == 'linear_scaler':
user_embs_mlp = self.emb_mlp[embset_idx](indexed_embs)
user_embs_mlp = self.emb_scaler[embset_idx][self.current_input.input_ids].unsqueeze(-1) * user_embs_mlp
prior_embs += user_embs_mlp
prior_embs /= len(self.embeddings)
emb_sim = torch.matmul(prior_embs, prior_embs.transpose(1, 2))
attn_output = weighted_scaled_dot_product_attention(
query_layer,
key_layer,
value_layer,
weighting_matrix=emb_sim,
attn_mask=attention_mask,
dropout_p=self.dropout_prob if self.training else 0.0,
is_causal=is_causal,
)
attn_output = attn_output.transpose(1, 2)
attn_output = attn_output.reshape(bsz, tgt_len, self.all_head_size)
outputs = (attn_output,)
if self.is_decoder:
outputs = outputs + (past_key_value,)
return outputs