-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclip_encoder.py
434 lines (335 loc) · 18.3 KB
/
clip_encoder.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel
from .configuration_evaclip import EvaCLIPVisionConfig
from .modeling_evaclip import EvaCLIPVisionModel
# test hired or prumerge or prumerge_plus
hired = True
prumerge = False
prumerge_plus = False
budget = 0.4
# adding prumerge code below
# credit: https://github.com/42Shawn/LLaVA-PruMerge
def complement_idx(idx, dim):
a = torch.arange(dim, device=idx.device)
ndim = idx.ndim
dims = idx.shape
n_idx = dims[-1]
dims = dims[:-1] + (-1, )
for i in range(1, ndim):
a = a.unsqueeze(0)
a = a.expand(*dims)
masked = torch.scatter(a, -1, idx, 0)
compl, _ = torch.sort(masked, dim=-1, descending=False)
compl = compl.permute(-1, *tuple(range(ndim - 1)))
compl = compl[n_idx:].permute(*(tuple(range(1, ndim)) + (0,)))
return compl
outputs = {}
def hook_k(module, input, output):
outputs['desired_k'] = output
def hook_q(module, input, output):
outputs['desired_q'] = output
def outlier_dectection(attn):
attn_np = attn.to(dtype=torch.float32).cpu().numpy().flatten()
Q1 = np.percentile(attn_np, 25)
Q3 = np.percentile(attn_np, 75)
IQR = Q3 - Q1
# lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
outlier_indices = np.where((attn_np > upper_bound))[0]
ratio = len(outlier_indices) / len(attn_np)
return ratio
# add hired code below
class ViT_Attn_Hook:
def __init__(self, model, layer_id, num_heads):
self.model = model
self.layer_id = layer_id
self.num_heads = num_heads
self.outputs = {}
self.key_hook_handle = None
self.query_hook_handle = None
# register hooks
self._register_hooks()
def _hook_key(self, module, input, output):
self.outputs['key_output'] = output
def _hook_query(self, module, input, output):
self.outputs['query_output'] = output
def _register_hooks(self):
self.key_hook_handle = self.model.vision_tower.vision_model.encoder.layers[self.layer_id].self_attn.k_proj.register_forward_hook(self._hook_key)
self.query_hook_handle = self.model.vision_tower.vision_model.encoder.layers[self.layer_id].self_attn.q_proj.register_forward_hook(self._hook_query)
def _remove_hooks(self):
if self.key_hook_handle:
self.key_hook_handle.remove()
if self.query_hook_handle:
self.query_hook_handle.remove()
def get_attn(self):
key = self.outputs.get('key_output')
query = self.outputs.get('query_output')
if key is None or query is None:
raise ValueError("Key or query output is not available. Ensure hooks are registered and the model has been run.")
batch_size, num_tokens, embedding_dim = query.shape
head_dim = embedding_dim // self.num_heads
query = query.view(batch_size, num_tokens, self.num_heads, head_dim).transpose(1, 2)
key = key.view(batch_size, num_tokens, self.num_heads, head_dim).transpose(1, 2)
attn = (query @ key.transpose(-2, -1)) * head_dim ** -0.5
attn = F.softmax(attn, dim=-1)
# remove hooks
self._remove_hooks()
return attn
def HiRED_token_selection(bottom_layer_attn, num_vision_tokens, token_budget_rate):
# bottom_layer_attn: torch.tensor of shape: [1, num_heads, num_context, num_context]
# clip_attn_layer: list of int
# num_vision_tokens: int, usually 576
# alpha_vision_token_budget: float
# rank tokens
attn = bottom_layer_attn[0, :, 0, -num_vision_tokens:] # [num_heads, num_vision_tokens] # class attn
attn = attn.sum(dim=0) # aggregate over heads [num_vision_tokens]
mask = torch.zeros_like(attn, dtype=torch.bool) # [num_vision_tokens]
# select top-k tokens
num_topk = min(num_vision_tokens, int(num_vision_tokens * token_budget_rate))
mask[attn.topk(num_topk, largest=True).indices] = True
return mask
class CLIPVisionTower(nn.Module):
def __init__(self, vision_tower, args, delay_load=False):
super().__init__()
self.is_loaded = False
self.vision_tower_name = vision_tower
self.select_layer = args.mm_vision_select_layer
self.select_feature = getattr(
args, 'mm_vision_select_feature', 'patch')
if not delay_load:
self.load_model()
else:
self.cfg_only = CLIPVisionConfig.from_pretrained(
self.vision_tower_name)
def load_model(self):
print(f'Load vision tower from {self.vision_tower_name}')
self.image_processor = CLIPImageProcessor.from_pretrained(
self.vision_tower_name)
if 'eva' in self.vision_tower_name.lower():
vision_cfg = EvaCLIPVisionConfig.from_pretrained(
self.vision_tower_name)
self.vision_tower = EvaCLIPVisionModel.from_pretrained(
self.vision_tower_name, config=vision_cfg)
else:
self.vision_tower = CLIPVisionModel.from_pretrained(
self.vision_tower_name)
self.vision_tower.requires_grad_(False)
self.is_loaded = True
def feature_select(self, image_forward_outs):
image_features = image_forward_outs.hidden_states[self.select_layer]
if self.select_feature == 'patch':
image_features = image_features[:, 1:]
elif self.select_feature == 'cls_patch':
image_features = image_features
else:
raise ValueError(
f'Unexpected select feature: {self.select_feature}')
return image_features
def token_prune_merge_advanced(self, images, if_adaptive=True, reduction_ratio = 1/8):
'''
version 10/03/2024 using the key*key matrix to calculate the cosine similarity
'''
# token_indix_list = []
# token_indix_dict = {}
#set hooks for extracting desired layer's k and q
hook_handle_k = self.vision_tower.vision_model.encoder.layers[23].self_attn.k_proj.register_forward_hook(hook_k)
hook_handle_q = self.vision_tower.vision_model.encoder.layers[23].self_attn.q_proj.register_forward_hook(hook_q)
#forward pass
image_forward_outs = self.vision_tower(images.to(device=self.device, dtype=self.dtype), output_hidden_states=True)
cls_token_last_layer =image_forward_outs.hidden_states[self.select_layer][:, 0:1]
image_features = self.feature_select(image_forward_outs).to(images.dtype)
B, N, C = image_features.shape
#extract desired layer's k and q and remove hooks; calculate attention
desired_layer_k = outputs["desired_k"]
desired_layer_q = outputs["desired_q"]
hook_handle_k.remove()
hook_handle_q.remove()
attn = (desired_layer_q @ desired_layer_k.transpose(-2, -1)) * C ** -0.5
attn = F.softmax(attn, dim=-1)
cls_attn = attn[:, 0, 1:]
if if_adaptive:
reduction_ratio = outlier_dectection(cls_attn)#*3.5
_, idx = torch.topk(cls_attn, int(N*reduction_ratio), dim=1, largest=True) # [B, left_tokens] , sorted=True
index = idx.unsqueeze(-1).expand(-1, -1, C) # [B, left_tokens, C]
Key_wo_cls = desired_layer_k[:, 1:] # [B, N-1, C]
x_others = torch.gather(image_features, dim=1, index=index) # [B, left_tokens, C]
x_others_attn = torch.gather(cls_attn, dim=1, index=idx)
Key_others = torch.gather(Key_wo_cls, dim=1, index=index) # [B, left_tokens, C]
compl = complement_idx(idx, N) # [B, N-1-left_tokens]
non_topk = torch.gather(image_features, dim=1, index=compl.unsqueeze(-1).expand(-1, -1, C)) # [B, N-1-left_tokens, C]
non_topk_Key = torch.gather(Key_wo_cls, dim=1, index=compl.unsqueeze(-1).expand(-1, -1, C))
non_topk_attn = torch.gather(cls_attn, dim=1, index=compl) # [B, N-1-left_tokens]
Key_others_norm = F.normalize(Key_others, p=2, dim=-1)
non_topk_Key_norm = F.normalize(non_topk_Key, p=2, dim=-1)
# cos_sim = torch.bmm(Key_others_norm, non_topk_Key_norm.transpose(1, 2)) # [B, left_tokens, N-1-left_tokens]
# _, cluster_indices = torch.topk(cos_sim, k=4, dim=2, largest=True)
B, left_tokens, C = x_others.size()
updated_x_others = torch.zeros_like(x_others)
for b in range(B):
for i in range(left_tokens):
key_others_norm = Key_others_norm[b,i,:].unsqueeze(0).unsqueeze(0)
before_i_Key = Key_others_norm[b, :i, :].unsqueeze(0)
after_i_Key = Key_others_norm[b, i+1:, :].unsqueeze(0)
before_i_x_others = x_others[b, :i, :].unsqueeze(0)
after_i_x_others = x_others[b, i+1:, :].unsqueeze(0)
rest_x_others = torch.cat([before_i_x_others, after_i_x_others, non_topk[b,:,:].unsqueeze(0)], dim=1)
before_i_x_others_attn = x_others_attn[b, :i].unsqueeze(0)
after_i_x_others_attn = x_others_attn[b, i+1:].unsqueeze(0)
rest_x_others_attn = torch.cat([before_i_x_others_attn, after_i_x_others_attn, non_topk_attn[b,:].unsqueeze(0)], dim=1)
rest_Keys = torch.cat([before_i_Key, after_i_Key, non_topk_Key_norm[b,:,:].unsqueeze(0)], dim=1)
cos_sim_matrix = torch.bmm(key_others_norm, rest_Keys.transpose(1, 2))
_, cluster_indices = torch.topk(cos_sim_matrix, k=int(32), dim=2, largest=True)
cluster_tokens = rest_x_others[:,cluster_indices.squeeze(),:]
weights = rest_x_others_attn[:,cluster_indices.squeeze()].unsqueeze(-1)
# update cluster centers
weighted_avg = torch.sum(cluster_tokens * weights, dim=1) #/ torch.sum(weights)
updated_center = weighted_avg + x_others[b, i, :]
updated_x_others[b, i, :] = updated_center
extra_one_token = torch.sum(non_topk * non_topk_attn.unsqueeze(-1), dim=1, keepdim=True) # [B, 1, C]
updated_x_others = torch.cat([updated_x_others, extra_one_token],dim=1)
image_features = updated_x_others
return image_features
def token_prune_merge_advanced_plus(self, images, if_adaptive=True, reduction_ratio = 1/8):
'''
version 24/03/2024 using the spacially smapled tokens to supplement the pruned tokens
'''
# token_indix_list = []
# token_indix_dict = {}
#set hooks for extracting desired layer's k and q
hook_handle_k = self.vision_tower.vision_model.encoder.layers[23].self_attn.k_proj.register_forward_hook(hook_k)
hook_handle_q = self.vision_tower.vision_model.encoder.layers[23].self_attn.q_proj.register_forward_hook(hook_q)
#forward pass
image_forward_outs = self.vision_tower(images.to(device=self.device, dtype=self.dtype), output_hidden_states=True)
cls_token_last_layer =image_forward_outs.hidden_states[self.select_layer][:, 0:1]
image_features = self.feature_select(image_forward_outs).to(images.dtype)
B, N, C = image_features.shape
#extract desired layer's k and q and remove hooks; calculate attention
desired_layer_k = outputs["desired_k"]
desired_layer_q = outputs["desired_q"]
hook_handle_k.remove()
hook_handle_q.remove()
attn = (desired_layer_q @ desired_layer_k.transpose(-2, -1)) * C ** -0.5
attn = F.softmax(attn, dim=-1)
cls_attn = attn[:, 0, 1:]
if if_adaptive:
reduction_ratio = outlier_dectection(cls_attn)#*3.5
_, idx = torch.topk(cls_attn, int(N*reduction_ratio), dim=1, largest=True) # [B, left_tokens] , sorted=True
# # # print("idx: ", idx)
if if_adaptive:
step_length = int(1/reduction_ratio)
arithmetic_sequence = torch.arange(0, 575, int(step_length/3)).to(device=self.device)
original_tensor_1d = idx.flatten().to(device=self.device)
filtered_sequence = torch.tensor([x for x in arithmetic_sequence if x not in original_tensor_1d]).to(device=self.device)
concatenated_tensor = torch.cat((idx, filtered_sequence.unsqueeze(0)), dim=1)
idx = concatenated_tensor
# # print("idx_new: ", idx)
else:
# # this is for training
step_length = int(1/reduction_ratio)
new_idx = torch.zeros((idx.size(0), idx.size(1)*2), dtype=torch.long).to(device=self.device)
for i in range(idx.size(0)):
arithmetic_sequence = torch.arange(int(step_length/2), 575, int(step_length)).to(device=self.device)
original_tensor_1d = idx[i].flatten().to(device=self.device)
filtered_sequence = arithmetic_sequence
# filtered_sequence = torch.tensor([x for x in arithmetic_sequence if x not in original_tensor_1d]).to(device=self.device)
concatenated_tensor = torch.cat((original_tensor_1d, filtered_sequence), dim=0)
new_idx[i] = concatenated_tensor
idx = new_idx
index = idx.unsqueeze(-1).expand(-1, -1, C) # [B, left_tokens, C]
Key_wo_cls = desired_layer_k[:, 1:] # [B, N-1, C]
x_others = torch.gather(image_features, dim=1, index=index) # [B, left_tokens, C]
x_others_attn = torch.gather(cls_attn, dim=1, index=idx)
Key_others = torch.gather(Key_wo_cls, dim=1, index=index) # [B, left_tokens, C]
compl = complement_idx(idx, N) # [B, N-1-left_tokens]
non_topk = torch.gather(image_features, dim=1, index=compl.unsqueeze(-1).expand(-1, -1, C)) # [B, N-1-left_tokens, C]
non_topk_Key = torch.gather(Key_wo_cls, dim=1, index=compl.unsqueeze(-1).expand(-1, -1, C))
non_topk_attn = torch.gather(cls_attn, dim=1, index=compl) # [B, N-1-left_tokens]
Key_others_norm = F.normalize(Key_others, p=2, dim=-1)
non_topk_Key_norm = F.normalize(non_topk_Key, p=2, dim=-1)
# cos_sim = torch.bmm(Key_others_norm, non_topk_Key_norm.transpose(1, 2)) # [B, left_tokens, N-1-left_tokens]
# _, cluster_indices = torch.topk(cos_sim, k=4, dim=2, largest=True)
B, left_tokens, C = x_others.size()
updated_x_others = torch.zeros_like(x_others)
for b in range(B):
for i in range(left_tokens):
key_others_norm = Key_others_norm[b,i,:].unsqueeze(0).unsqueeze(0)
before_i_Key = Key_others_norm[b, :i, :].unsqueeze(0)
after_i_Key = Key_others_norm[b, i+1:, :].unsqueeze(0)
before_i_x_others = x_others[b, :i, :].unsqueeze(0)
after_i_x_others = x_others[b, i+1:, :].unsqueeze(0)
rest_x_others = torch.cat([before_i_x_others, after_i_x_others, non_topk[b,:,:].unsqueeze(0)], dim=1)
before_i_x_others_attn = x_others_attn[b, :i].unsqueeze(0)
after_i_x_others_attn = x_others_attn[b, i+1:].unsqueeze(0)
rest_x_others_attn = torch.cat([before_i_x_others_attn, after_i_x_others_attn, non_topk_attn[b,:].unsqueeze(0)], dim=1)
rest_Keys = torch.cat([before_i_Key, after_i_Key, non_topk_Key_norm[b,:,:].unsqueeze(0)], dim=1)
cos_sim_matrix = torch.bmm(key_others_norm, rest_Keys.transpose(1, 2))
_, cluster_indices = torch.topk(cos_sim_matrix, k=int(32), dim=2, largest=True)
cluster_tokens = rest_x_others[:,cluster_indices.squeeze(),:]
weights = rest_x_others_attn[:,cluster_indices.squeeze()].unsqueeze(-1)
# update cluster centers
weighted_avg = torch.sum(cluster_tokens * weights, dim=1) #/ torch.sum(weights)
updated_center = x_others[b, i, :] + weighted_avg
updated_x_others[b, i, :] = updated_center
extra_one_token = torch.sum(non_topk * non_topk_attn.unsqueeze(-1), dim=1, keepdim=True) # [B, 1, C]
updated_x_others = torch.cat([updated_x_others, extra_one_token],dim=1)
image_features = updated_x_others
return image_features
# @torch.no_grad() comment to enable fine-tune vit
def forward(self, images):
if prumerge:
image_features = self.token_prune_merge_advanced(images, if_adaptive=True, reduction_ratio=1/8)
# print("selected image features: ",image_features.shape)
return image_features
if prumerge_plus:
image_features = self.token_prune_merge_advanced_plus(images, if_adaptive=True)
print("selected image features: ",image_features.shape)
return image_features
# HiRED
bottom_attn_hook = ViT_Attn_Hook(self,layer_id=22, num_heads=16)
if type(images) is list:
image_features = []
for image in images:
image_forward_out = self.vision_tower(image.to(
device=self.device, dtype=self.dtype).unsqueeze(0), output_hidden_states=True)
image_feature = self.feature_select(
image_forward_out).to(image.dtype)
image_features.append(image_feature)
else:
image_forward_outs = self.vision_tower(
images.to(device=self.device, dtype=self.dtype), output_hidden_states=True)
image_features = self.feature_select(
image_forward_outs).to(images.dtype)
if hired:
selected_tokens_mask = HiRED_token_selection(
bottom_layer_attn=bottom_attn_hook.get_attn(),
num_vision_tokens=576,
token_budget_rate=budget
)
image_features = image_features[:, selected_tokens_mask.bool(), :]
# print("selected image features: ",image_features.shape)
return image_features
return image_features
@property
def dummy_feature(self):
return torch.zeros(1, self.hidden_size, device=self.device, dtype=self.dtype)
@property
def dtype(self):
return self.vision_tower.dtype
@property
def device(self):
return self.vision_tower.device
@property
def config(self):
if self.is_loaded:
return self.vision_tower.config
else:
return self.cfg_only
@property
def hidden_size(self):
return self.config.hidden_size
@property
def num_patches(self):
return (self.config.image_size // self.config.patch_size) ** 2