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AndCrossAttentions
5
+ from transformers.modeling_utils import PreTrainedModel
6
+ from transformers.pytorch_utils import find_pruneable_heads_and_indices, prune_linear_layer
7
+
8
+ import numpy as np
9
+ import torch
10
+ import torch.nn as nn
11
+ import torch.utils.checkpoint
12
+ from icecream import ic
13
+
14
+ def get_abs_pos(abs_pos, tgt_size):
15
+ # abs_pos: L, C
16
+ # tgt_size: M
17
+ # return: M, C
18
+ src_size = int(math.sqrt(abs_pos.size(0)))
19
+ tgt_size = int(math.sqrt(tgt_size))
20
+ dtype = abs_pos.dtype
21
+
22
+ if src_size != tgt_size:
23
+ return F.interpolate(
24
+ abs_pos.float().reshape(1, src_size, src_size, -1).permute(0, 3, 1, 2),
25
+ size=(tgt_size, tgt_size),
26
+ mode="bicubic",
27
+ align_corners=False,
28
+ ).permute(0, 2, 3, 1).flatten(0, 2).to(dtype=dtype)
29
+ else:
30
+ return abs_pos
31
+
32
+ # https://github.com/facebookresearch/mae/blob/efb2a8062c206524e35e47d04501ed4f544c0ae8/util/pos_embed.py#L20
33
+ def get_2d_sincos_pos_embed(embed_dim, grid_size, cls_token=False):
34
+ """
35
+ grid_size: int of the grid height and width
36
+ return:
37
+ pos_embed: [grid_size*grid_size, embed_dim] or [1+grid_size*grid_size, embed_dim] (w/ or w/o cls_token)
38
+ """
39
+ grid_h = np.arange(grid_size, dtype=np.float32)
40
+ grid_w = np.arange(grid_size, dtype=np.float32)
41
+ grid = np.meshgrid(grid_w, grid_h) # here w goes first
42
+ grid = np.stack(grid, axis=0)
43
+
44
+ grid = grid.reshape([2, 1, grid_size, grid_size])
45
+ pos_embed = get_2d_sincos_pos_embed_from_grid(embed_dim, grid)
46
+ if cls_token:
47
+ pos_embed = np.concatenate([np.zeros([1, embed_dim]), pos_embed], axis=0)
48
+ return pos_embed
49
+
50
+
51
+ def get_2d_sincos_pos_embed_from_grid(embed_dim, grid):
52
+ assert embed_dim % 2 == 0
53
+
54
+ # use half of dimensions to encode grid_h
55
+ emb_h = get_1d_sincos_pos_embed_from_grid(embed_dim // 2, grid[0]) # (H*W, D/2)
56
+ emb_w = get_1d_sincos_pos_embed_from_grid(embed_dim // 2, grid[1]) # (H*W, D/2)
57
+
58
+ emb = np.concatenate([emb_h, emb_w], axis=1) # (H*W, D)
59
+ return emb
60
+
61
+
62
+ def get_1d_sincos_pos_embed_from_grid(embed_dim, pos):
63
+ """
64
+ embed_dim: output dimension for each position
65
+ pos: a list of positions to be encoded: size (M,)
66
+ out: (M, D)
67
+ """
68
+ assert embed_dim % 2 == 0
69
+ omega = np.arange(embed_dim // 2, dtype=np.float32)
70
+ omega /= embed_dim / 2.
71
+ omega = 1. / 10000**omega # (D/2,)
72
+
73
+ pos = pos.reshape(-1) # (M,)
74
+ out = np.einsum('m,d->md', pos, omega) # (M, D/2), outer product
75
+
76
+ emb_sin = np.sin(out) # (M, D/2)
77
+ emb_cos = np.cos(out) # (M, D/2)
78
+
79
+ emb = np.concatenate([emb_sin, emb_cos], axis=1) # (M, D)
80
+ return emb
81
+
82
+
83
+
84
+ class MplugOwlVisionEmbeddings(nn.Module):
85
+ def __init__(self, config):
86
+ super().__init__()
87
+ self.config = config
88
+ self.hidden_size = config.hidden_size
89
+ self.image_size = config.image_size
90
+ self.patch_size = config.patch_size
91
+
92
+ self.cls_token = nn.Parameter(torch.randn(1, 1, self.hidden_size))
93
+
94
+ self.patch_embed = nn.Conv2d(
95
+ in_channels=3,
96
+ out_channels=self.hidden_size,
97
+ kernel_size=self.patch_size,
98
+ stride=self.patch_size,
99
+ bias=False,
100
+ )
101
+
102
+ self.num_patches = (self.image_size // self.patch_size) ** 2
103
+
104
+ self.position_embedding = nn.Parameter(torch.randn(1, self.num_patches + 1, self.hidden_size))
105
+
106
+ self.pre_layernorm = nn.LayerNorm(self.hidden_size, eps=config.layer_norm_eps)
107
+
108
+ def forward(self, pixel_values: torch.FloatTensor) -> torch.Tensor:
109
+ batch_size = pixel_values.size(0)
110
+ image_embeds = self.patch_embed(pixel_values)
111
+ image_embeds = image_embeds.flatten(2).transpose(1, 2)
112
+
113
+ class_embeds = self.cls_token.expand(batch_size, 1, -1).to(image_embeds.dtype)
114
+ embeddings = torch.cat([class_embeds, image_embeds], dim=1)
115
+ embeddings = embeddings + self.position_embedding[:, : embeddings.size(1)].to(image_embeds.dtype)
116
+ embeddings = self.pre_layernorm(embeddings)
117
+ return embeddings
118
+
119
+
120
+
121
+ class MplugOwlVisionAttention(nn.Module):
122
+ """Multi-headed attention from 'Attention Is All You Need' paper"""
123
+
124
+ def __init__(self, config):
125
+ super().__init__()
126
+ self.config = config
127
+ self.hidden_size = config.hidden_size
128
+ self.num_heads = config.num_attention_heads
129
+ self.head_dim = self.hidden_size // self.num_heads
130
+ if self.head_dim * self.num_heads != self.hidden_size:
131
+ raise ValueError(
132
+ f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size} and `num_heads`:"
133
+ f" {self.num_heads})."
134
+ )
135
+ self.scale = self.head_dim**-0.5
136
+ self.dropout = nn.Dropout(config.attention_dropout)
137
+
138
+ self.query_key_value = nn.Linear(self.hidden_size, 3 * self.hidden_size)
139
+ self.dense = nn.Linear(self.hidden_size, self.hidden_size)
140
+
141
+ def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
142
+ return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous()
143
+
144
+ def forward(
145
+ self,
146
+ hidden_states: torch.Tensor,
147
+ head_mask: Optional[torch.Tensor] = None,
148
+ output_attentions: Optional[bool] = False,
149
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
150
+ """Input shape: Batch x Time x Channel"""
151
+
152
+ bsz, seq_len, embed_dim = hidden_states.size()
153
+
154
+ mixed_qkv = self.query_key_value(hidden_states)
155
+
156
+ mixed_qkv = mixed_qkv.reshape(bsz, seq_len, self.num_heads, 3, embed_dim // self.num_heads).permute(
157
+ 3, 0, 2, 1, 4
158
+ ) # [3, b, np, sq, hn]
159
+ query_states, key_states, value_states = (
160
+ mixed_qkv[0],
161
+ mixed_qkv[1],
162
+ mixed_qkv[2],
163
+ )
164
+ # if self.config.use_flash_attn and flash_attn_func is not None:
165
+ if False:
166
+ # [b*sq, np, hn]
167
+ query_states = query_states.permute(0, 2, 1, 3).contiguous()
168
+ query_states = query_states.view(query_states.size(0) * query_states.size(1), query_states.size(2), -1)
169
+
170
+ key_states = key_states.permute(0, 2, 1, 3).contiguous()
171
+ key_states = key_states.view(key_states.size(0) * key_states.size(1), key_states.size(2), -1)
172
+
173
+ value_states = value_states.permute(0, 2, 1, 3).contiguous()
174
+ value_states = value_states.view(value_states.size(0) * value_states.size(1), value_states.size(2), -1)
175
+
176
+ cu_seqlens = torch.arange(
177
+ 0, (bsz + 1) * seq_len, step=seq_len, dtype=torch.int32, device=query_states.device
178
+ )
179
+
180
+ context_layer = flash_attn_func(
181
+ query_states,
182
+ key_states,
183
+ value_states,
184
+ cu_seqlens,
185
+ cu_seqlens,
186
+ seq_len,
187
+ seq_len,
188
+ self.dropout if self.training else 0.0,
189
+ softmax_scale=self.scale,
190
+ causal=False,
191
+ return_attn_probs=False,
192
+ )
193
+ # [b*sq, np, hn] => [b, sq, np, hn]
194
+ context_layer = context_layer.view(bsz, seq_len, context_layer.size(1), context_layer.size(2))
195
+ else:
196
+ # Take the dot product between "query" and "key" to get the raw attention scores.
197
+ attention_scores = torch.matmul(query_states, key_states.transpose(-1, -2))
198
+
199
+ attention_scores = attention_scores * self.scale
200
+
201
+ # Normalize the attention scores to probabilities.
202
+ attention_probs = torch.softmax(attention_scores, dim=-1)
203
+
204
+ # This is actually dropping out entire tokens to attend to, which might
205
+ # seem a bit unusual, but is taken from the original Transformer paper.
206
+ attention_probs = self.dropout(attention_probs)
207
+
208
+ # Mask heads if we want to
209
+ if head_mask is not None:
210
+ attention_probs = attention_probs * head_mask
211
+
212
+ context_layer = torch.matmul(attention_probs, value_states).permute(0, 2, 1, 3)
213
+
214
+ new_context_layer_shape = context_layer.size()[:-2] + (self.hidden_size,)
215
+ context_layer = context_layer.reshape(new_context_layer_shape)
216
+
217
+ output = self.dense(context_layer)
218
+
219
+ outputs = (output, attention_probs) if output_attentions else (output, None)
220
+
221
+ return outputs
222
+
223
+
224
+ class QuickGELU(nn.Module):
225
+ def forward(self, x: torch.Tensor):
226
+ return x * torch.sigmoid(1.702 * x)
227
+
228
+
229
+ class MplugOwlMLP(nn.Module):
230
+ def __init__(self, config):
231
+ super().__init__()
232
+ self.config = config
233
+ self.activation_fn = QuickGELU()
234
+ self.fc1 = nn.Linear(config.hidden_size, config.intermediate_size)
235
+ self.fc2 = nn.Linear(config.intermediate_size, config.hidden_size)
236
+
237
+ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
238
+ hidden_states = self.fc1(hidden_states)
239
+ hidden_states = self.activation_fn(hidden_states)
240
+ hidden_states = self.fc2(hidden_states)
241
+ return hidden_states
242
+
243
+
244
+ class MplugOwlVisionEncoderLayer(nn.Module):
245
+ def __init__(self, config):
246
+ super().__init__()
247
+ self.hidden_size = config.hidden_size
248
+ self.self_attn = MplugOwlVisionAttention(config)
249
+ self.input_layernorm = nn.LayerNorm(self.hidden_size, eps=config.layer_norm_eps)
250
+ self.mlp = MplugOwlMLP(config)
251
+ self.post_attention_layernorm = nn.LayerNorm(self.hidden_size, eps=config.layer_norm_eps)
252
+
253
+ def forward(
254
+ self,
255
+ hidden_states: torch.Tensor,
256
+ attention_mask: torch.Tensor,
257
+ output_attentions: Optional[bool] = False,
258
+ ) -> Tuple[torch.FloatTensor]:
259
+ """
260
+ Args:
261
+ hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
262
+ attention_mask (`torch.FloatTensor`): attention mask of size
263
+ `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
264
+ `(config.encoder_attention_heads,)`.
265
+ output_attentions (`bool`, *optional*):
266
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under
267
+ returned tensors for more detail.
268
+ """
269
+ residual = hidden_states
270
+
271
+ hidden_states = self.input_layernorm(hidden_states)
272
+ hidden_states, attn_weights = self.self_attn(
273
+ hidden_states=hidden_states,
274
+ head_mask=attention_mask,
275
+ output_attentions=output_attentions,
276
+ )
277
+ hidden_states = hidden_states + residual
278
+ residual = hidden_states
279
+ hidden_states = self.post_attention_layernorm(hidden_states)
280
+ hidden_states = self.mlp(hidden_states)
281
+
282
+ hidden_states = hidden_states + residual
283
+
284
+ outputs = (hidden_states,)
285
+
286
+ if output_attentions:
287
+ outputs += (attn_weights,)
288
+
289
+ return outputs
290
+
291
+
292
+ class MplugOwlVisionEncoder(nn.Module):
293
+ """
294
+ Transformer encoder consisting of `config.num_hidden_layers` self attention layers. Each layer is a
295
+ [`MplugOwlVisionEncoderLayer`].
296
+
297
+ Args:
298
+ config (`MplugOwlVisionConfig`):
299
+ The corresponding vision configuration for the `MplugOwlEncoder`.
300
+ """
301
+
302
+ def __init__(self, config):
303
+ super().__init__()
304
+ self.config = config
305
+ self.layers = nn.ModuleList([MplugOwlVisionEncoderLayer(config) for _ in range(config.num_hidden_layers)])
306
+ self.gradient_checkpointing = True
307
+
308
+ def forward(
309
+ self,
310
+ inputs_embeds,
311
+ attention_mask: Optional[torch.Tensor] = None,
312
+ output_attentions: Optional[bool] = None,
313
+ output_hidden_states: Optional[bool] = None,
314
+ return_dict: Optional[bool] = None,
315
+ ) -> Union[Tuple, BaseModelOutput]:
316
+ r"""
317
+ Args:
318
+ inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
319
+ Embedded representation of the inputs. Should be float, not int tokens.
320
+ attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
321
+ Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
322
+
323
+ - 1 for tokens that are **not masked**,
324
+ - 0 for tokens that are **masked**.
325
+
326
+ [What are attention masks?](../glossary#attention-mask)
327
+ output_attentions (`bool`, *optional*):
328
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under
329
+ returned tensors for more detail.
330
+ output_hidden_states (`bool`, *optional*):
331
+ Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors
332
+ for more detail.
333
+ return_dict (`bool`, *optional*):
334
+ Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
335
+ """
336
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
337
+ output_hidden_states = (
338
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
339
+ )
340
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
341
+
342
+ encoder_states = () if output_hidden_states else None
343
+ all_attentions = () if output_attentions else None
344
+
345
+ hidden_states = inputs_embeds
346
+ for idx, encoder_layer in enumerate(self.layers):
347
+ if output_hidden_states:
348
+ encoder_states = encoder_states + (hidden_states,)
349
+ if self.gradient_checkpointing and self.training:
350
+
351
+ def create_custom_forward(module):
352
+ def custom_forward(*inputs):
353
+ return module(*inputs, output_attentions)
354
+
355
+ return custom_forward
356
+
357
+ layer_outputs = torch.utils.checkpoint.checkpoint(
358
+ create_custom_forward(encoder_layer),
359
+ hidden_states,
360
+ attention_mask,
361
+ )
362
+ else:
363
+ layer_outputs = encoder_layer(
364
+ hidden_states,
365
+ attention_mask,
366
+ output_attentions=output_attentions,
367
+ )
368
+
369
+ hidden_states = layer_outputs[0]
370
+
371
+ if output_attentions:
372
+ all_attentions = all_attentions + (layer_outputs[1],)
373
+
374
+ if output_hidden_states:
375
+ encoder_states = encoder_states + (hidden_states,)
376
+
377
+ if not return_dict:
378
+ return tuple(v for v in [hidden_states, encoder_states, all_attentions] if v is not None)
379
+ return BaseModelOutput(
380
+ last_hidden_state=hidden_states, hidden_states=encoder_states, attentions=all_attentions
381
+ )
382
+
383
+
384
+ class MplugOwlVisionModel(PreTrainedModel):
385
+ main_input_name = "pixel_values"
386
+ _no_split_modules = ["MplugOwlVisionEncoderLayer"]
387
+
388
+ def __init__(self, config):
389
+ super().__init__(config)
390
+ self.config = config
391
+ self.hidden_size = config.hidden_size
392
+
393
+ self.embeddings = MplugOwlVisionEmbeddings(config)
394
+ self.encoder = MplugOwlVisionEncoder(config)
395
+ self.post_layernorm = nn.LayerNorm(self.hidden_size, eps=config.layer_norm_eps)
396
+
397
+ self.post_init()
398
+
399
+
400
+ def forward(
401
+ self,
402
+ pixel_values: Optional[torch.FloatTensor] = None,
403
+ output_attentions: Optional[bool] = None,
404
+ output_hidden_states: Optional[bool] = None,
405
+ return_dict: Optional[bool] = None,
406
+ ) -> Union[Tuple, BaseModelOutputWithPooling]:
407
+ r"""
408
+ Returns:
409
+
410
+ """
411
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
412
+ output_hidden_states = (
413
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
414
+ )
415
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
416
+
417
+ if pixel_values is None:
418
+ raise ValueError("You have to specify pixel_values")
419
+
420
+ hidden_states = self.embeddings(pixel_values)
421
+
422
+ encoder_outputs = self.encoder(
423
+ inputs_embeds=hidden_states,
424
+ output_attentions=output_attentions,
425
+ output_hidden_states=output_hidden_states,
426
+ return_dict=return_dict,
427
+ )
428
+
429
+ last_hidden_state = encoder_outputs[0]
430
+ last_hidden_state = self.post_layernorm(last_hidden_state)
431
+
432
+ pooled_output = last_hidden_state[:, 0, :]
433
+ pooled_output = self.post_layernorm(pooled_output)
434
+
435
+ if not return_dict:
436
+ return (last_hidden_state, pooled_output) + encoder_outputs[1:]
437
+
438
+ return BaseModelOutputWithPooling(
439
+ last_hidden_state=last_hidden_state,
440
+ pooler_output=pooled_output,
441
+ hidden_states=encoder_outputs.hidden_states,
442
+ attentions=encoder_outputs.attentions,
443
+ )
444
+
445
+ def get_input_embeddings(self):
446
+ return self.embeddings
447
+
448
+
449
+ class MplugOwlVisualAbstractorMLP(nn.Module):
450
+ def __init__(self, config):
451
+ super().__init__()
452
+ self.config = config
453
+ in_features = config.hidden_size
454
+ self.act = nn.SiLU()
455
+
456
+ self.w1 = nn.Linear(in_features, config.intermediate_size)
457
+ self.w2 = nn.Linear(config.intermediate_size, in_features)
458
+ self.w3 = nn.Linear(in_features, config.intermediate_size)
459
+ self.ffn_ln = nn.LayerNorm(config.intermediate_size, eps=config.layer_norm_eps)
460
+
461
+ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
462
+ hidden_states = self.act(self.w1(hidden_states)) * self.w3(hidden_states)
463
+ hidden_states = self.ffn_ln(hidden_states)
464
+ hidden_states = self.w2(hidden_states)
465
+ return hidden_states
466
+
467
+
468
+ class MplugOwlVisualAbstractorMultiHeadAttention(nn.Module):
469
+ def __init__(self, config):
470
+ super().__init__()
471
+ self.config = config
472
+ if config.hidden_size % config.num_attention_heads != 0:
473
+ raise ValueError(
474
+ "The hidden size (%d) is not a multiple of the number of attention heads (%d)"
475
+ % (config.hidden_size, config.num_attention_heads)
476
+ )
477
+
478
+ self.num_attention_heads = config.num_attention_heads
479
+ self.attention_head_size = int(config.hidden_size / config.num_attention_heads)
480
+ self.all_head_size = self.num_attention_heads * self.attention_head_size
481
+
482
+ self.query = nn.Linear(config.hidden_size, self.all_head_size)
483
+ self.key = nn.Linear(config.encoder_hidden_size, self.all_head_size)
484
+ self.value = nn.Linear(config.encoder_hidden_size, self.all_head_size)
485
+
486
+ self.dropout = nn.Dropout(config.attention_probs_dropout_prob)
487
+ self.save_attention = False
488
+
489
+ # self.q_pos_embed = nn.Parameter(
490
+ # torch.from_numpy(get_1d_sincos_pos_embed_from_grid(config.hidden_size, np.arange(config.num_learnable_queries, dtype=np.float32))).float()
491
+ # ).requires_grad_(False)
492
+ # grids = config.grid_size
493
+ # self.k_pos_embed = nn.Parameter(
494
+ # torch.from_numpy(get_2d_sincos_pos_embed(config.hidden_size, grids, cls_token=True)).float()
495
+ # ).requires_grad_(False)
496
+ grids = config.grid_size
497
+ self.register_buffer(
498
+ 'q_pos_embed',
499
+ torch.from_numpy(get_1d_sincos_pos_embed_from_grid(config.hidden_size, np.arange(config.num_learnable_queries, dtype=np.float32))).float()
500
+ )
501
+ self.register_buffer(
502
+ 'k_pos_embed',
503
+ torch.from_numpy(get_2d_sincos_pos_embed(config.hidden_size, grids, cls_token=True)).float()
504
+ )
505
+
506
+
507
+ def save_attn_gradients(self, attn_gradients):
508
+ self.attn_gradients = attn_gradients
509
+
510
+ def get_attn_gradients(self):
511
+ return self.attn_gradients
512
+
513
+ def save_attention_map(self, attention_map):
514
+ self.attention_map = attention_map
515
+
516
+ def get_attention_map(self):
517
+ return self.attention_map
518
+
519
+ def transpose_for_scores(self, x):
520
+ new_x_shape = x.size()[:-1] + (self.num_attention_heads, self.attention_head_size)
521
+ x = x.view(*new_x_shape)
522
+ return x.permute(0, 2, 1, 3)
523
+
524
+ def forward(
525
+ self,
526
+ hidden_states,
527
+ attention_mask=None,
528
+ head_mask=None,
529
+ encoder_hidden_states=None,
530
+ encoder_attention_mask=None,
531
+ past_key_value=None,
532
+ output_attentions=False,
533
+ ):
534
+ # If this is instantiated as a cross-attention module, the keys
535
+ # and values come from an encoder; the attention mask needs to be
536
+ # such that the encoder's padding tokens are not attended to.
537
+
538
+ qk_pos_embed = torch.cat([self.q_pos_embed, self.k_pos_embed], dim = 0).unsqueeze(0).to(dtype=hidden_states.dtype)
539
+
540
+ key_layer = self.transpose_for_scores(self.key(encoder_hidden_states + qk_pos_embed))
541
+ value_layer = self.transpose_for_scores(self.value(encoder_hidden_states))
542
+ attention_mask = encoder_attention_mask
543
+
544
+ mixed_query_layer = self.query(hidden_states + self.q_pos_embed.unsqueeze(0).to(dtype=hidden_states.dtype))
545
+
546
+ query_layer = self.transpose_for_scores(mixed_query_layer)
547
+
548
+ past_key_value = (key_layer, value_layer)
549
+
550
+ # Take the dot product between "query" and "key" to get the raw attention scores.
551
+ attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2))
552
+
553
+ attention_scores = attention_scores / math.sqrt(self.attention_head_size)
554
+
555
+ if attention_mask is not None:
556
+ # Apply the attention mask is (precomputed for all layers in BertModel forward() function)
557
+ attention_scores = attention_scores + attention_mask
558
+
559
+ # Normalize the attention scores to probabilities.
560
+ attention_probs = nn.Softmax(dim=-1)(attention_scores)
561
+
562
+ if self.save_attention:
563
+ self.save_attention_map(attention_probs)
564
+ attention_probs.register_hook(self.save_attn_gradients)
565
+
566
+ # This is actually dropping out entire tokens to attend to, which might
567
+ # seem a bit unusual, but is taken from the original Transformer paper.
568
+ attention_probs_dropped = self.dropout(attention_probs)
569
+
570
+ # Mask heads if we want to
571
+ if head_mask is not None:
572
+ attention_probs_dropped = attention_probs_dropped * head_mask
573
+
574
+ context_layer = torch.matmul(attention_probs_dropped, value_layer)
575
+
576
+ context_layer = context_layer.permute(0, 2, 1, 3).contiguous()
577
+ new_context_layer_shape = context_layer.size()[:-2] + (self.all_head_size,)
578
+ context_layer = context_layer.view(*new_context_layer_shape)
579
+
580
+ outputs = (context_layer, attention_probs) if output_attentions else (context_layer,)
581
+
582
+ outputs = outputs + (past_key_value,)
583
+ return outputs
584
+
585
+
586
+ class MplugOwlVisualAbstractorCrossOutput(nn.Module):
587
+ def __init__(self, config):
588
+ super().__init__()
589
+ dim = config.hidden_size
590
+ self.out_proj = nn.Linear(dim, dim, bias=True)
591
+ self.norm2 = nn.LayerNorm(dim)
592
+ self.mlp = MplugOwlVisualAbstractorMLP(config)
593
+
594
+ def forward(self, hidden_states: torch.Tensor, input_tensor: torch.Tensor) -> torch.Tensor:
595
+ input_tensor = input_tensor + self.out_proj(hidden_states)
596
+ input_tensor = input_tensor + self.mlp(self.norm2(input_tensor))
597
+ return input_tensor
598
+
599
+
600
+ class MplugOwlVisualAbstractorAttention(nn.Module):
601
+ def __init__(self, config):
602
+ super().__init__()
603
+ self.attention = MplugOwlVisualAbstractorMultiHeadAttention(config)
604
+ self.output = MplugOwlVisualAbstractorCrossOutput(config)
605
+ self.pruned_heads = set()
606
+ self.norm1 = nn.LayerNorm(config.hidden_size)
607
+ self.normk = nn.LayerNorm(config.hidden_size)
608
+
609
+ def prune_heads(self, heads):
610
+ if len(heads) == 0:
611
+ return
612
+ heads, index = find_pruneable_heads_and_indices(
613
+ heads, self.attention.num_attention_heads, self.attention.attention_head_size, self.pruned_heads
614
+ )
615
+
616
+ # Prune linear layers
617
+ self.attention.query = prune_linear_layer(self.attention.query, index)
618
+ self.attention.key = prune_linear_layer(self.attention.key, index)
619
+ self.attention.value = prune_linear_layer(self.attention.value, index)
620
+ self.output.dense = prune_linear_layer(self.output.out_proj, index, dim=1)
621
+
622
+ # Update hyper params and store pruned heads
623
+ self.attention.num_attention_heads = self.attention.num_attention_heads - len(heads)
624
+ self.attention.all_head_size = self.attention.attention_head_size * self.attention.num_attention_heads
625
+ self.pruned_heads = self.pruned_heads.union(heads)
626
+
627
+ def forward(
628
+ self,
629
+ hidden_states: torch.Tensor,
630
+ attention_mask: Optional[torch.FloatTensor] = None,
631
+ head_mask: Optional[torch.FloatTensor] = None,
632
+ encoder_hidden_states: Optional[torch.FloatTensor] = None,
633
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
634
+ past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
635
+ output_attentions: Optional[bool] = False,
636
+ ) -> Tuple[torch.Tensor]:
637
+ # HACK we apply norm on q and k
638
+ hidden_states = self.norm1(hidden_states)
639
+ encoder_hidden_states = self.normk(encoder_hidden_states)
640
+ encoder_hidden_states = torch.cat([hidden_states, encoder_hidden_states], dim=1)
641
+ encoder_attention_mask = torch.cat([attention_mask, encoder_attention_mask], dim=-1)
642
+ self_outputs = self.attention(
643
+ hidden_states,
644
+ attention_mask,
645
+ head_mask,
646
+ encoder_hidden_states,
647
+ encoder_attention_mask,
648
+ past_key_value,
649
+ output_attentions,
650
+ )
651
+ attention_output = self.output(self_outputs[0], hidden_states)
652
+ # add attentions if we output them
653
+ outputs = (attention_output,) + self_outputs[1:]
654
+ return outputs
655
+
656
+
657
+ class MplugOwlVisualAbstractorLayer(nn.Module):
658
+ def __init__(self, config, layer_idx):
659
+ super().__init__()
660
+ self.chunk_size_feed_forward = config.chunk_size_feed_forward
661
+ self.seq_len_dim = 1
662
+
663
+ self.layer_idx = layer_idx
664
+
665
+ self.crossattention = MplugOwlVisualAbstractorAttention(config)
666
+ self.has_cross_attention = True
667
+
668
+ def forward(
669
+ self,
670
+ hidden_states,
671
+ attention_mask=None,
672
+ head_mask=None,
673
+ encoder_hidden_states=None,
674
+ encoder_attention_mask=None,
675
+ output_attentions=False,
676
+ ):
677
+ if encoder_hidden_states is None:
678
+ raise ValueError("encoder_hidden_states must be given for cross-attention layers")
679
+ cross_attention_outputs = self.crossattention(
680
+ hidden_states,
681
+ attention_mask,
682
+ head_mask,
683
+ encoder_hidden_states,
684
+ encoder_attention_mask,
685
+ output_attentions=output_attentions,
686
+ )
687
+ query_attention_output = cross_attention_outputs[0]
688
+
689
+ outputs = (query_attention_output,)
690
+ return outputs
691
+
692
+
693
+ class MplugOwlVisualAbstractorEncoder(nn.Module):
694
+ def __init__(self, config):
695
+ super().__init__()
696
+ self.config = config
697
+ self.layers = nn.ModuleList(
698
+ [MplugOwlVisualAbstractorLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
699
+ )
700
+ self.gradient_checkpointing = True
701
+
702
+ def forward(
703
+ self,
704
+ hidden_states,
705
+ attention_mask=None,
706
+ head_mask=None,
707
+ encoder_hidden_states=None,
708
+ encoder_attention_mask=None,
709
+ past_key_values=None,
710
+ output_attentions=False,
711
+ output_hidden_states=False,
712
+ return_dict=True,
713
+ ):
714
+ all_hidden_states = () if output_hidden_states else None
715
+
716
+ for i in range(self.config.num_hidden_layers):
717
+ layer_module = self.layers[i]
718
+ if output_hidden_states:
719
+ all_hidden_states = all_hidden_states + (hidden_states,)
720
+
721
+ layer_head_mask = head_mask[i] if head_mask is not None else None
722
+ past_key_value = past_key_values[i] if past_key_values is not None else None
723
+
724
+ if getattr(self.config, "gradient_checkpointing", False) and self.training:
725
+
726
+ def create_custom_forward(module):
727
+ def custom_forward(*inputs):
728
+ return module(*inputs, past_key_value, output_attentions)
729
+
730
+ return custom_forward
731
+
732
+ layer_outputs = torch.utils.checkpoint.checkpoint(
733
+ create_custom_forward(layer_module),
734
+ hidden_states,
735
+ attention_mask,
736
+ layer_head_mask,
737
+ encoder_hidden_states,
738
+ encoder_attention_mask,
739
+ )
740
+ else:
741
+ layer_outputs = layer_module(
742
+ hidden_states,
743
+ attention_mask,
744
+ layer_head_mask,
745
+ encoder_hidden_states,
746
+ encoder_attention_mask,
747
+ output_attentions,
748
+ )
749
+
750
+ hidden_states = layer_outputs[0]
751
+
752
+ return BaseModelOutput(
753
+ last_hidden_state=hidden_states,
754
+ )
755
+
756
+
757
+ class MplugOwlVisualAbstractorModel(PreTrainedModel):
758
+ _no_split_modules = ["MplugOwlVisualAbstractorLayer"]
759
+ def __init__(self, config, language_hidden_size):
760
+ super().__init__(config)
761
+ self.config = config
762
+
763
+ self.encoder = MplugOwlVisualAbstractorEncoder(config)
764
+ self.visual_fc = torch.nn.Linear(config.hidden_size, language_hidden_size)
765
+ self.query_embeds = torch.nn.Parameter(torch.randn(1, config.num_learnable_queries, config.hidden_size))
766
+ self.vit_eos = torch.nn.Parameter(torch.randn(1, 1, language_hidden_size))
767
+
768
+ self.post_init()
769
+
770
+ def _prune_heads(self, heads_to_prune):
771
+ """
772
+ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
773
+ class PreTrainedModel
774
+ """
775
+ for layer, heads in heads_to_prune.items():
776
+ self.encoder.layer[layer].attention.prune_heads(heads)
777
+
778
+ def get_extended_attention_mask(
779
+ self,
780
+ attention_mask: torch.Tensor,
781
+ input_shape: Tuple[int],
782
+ device: torch.device,
783
+ ) -> torch.Tensor:
784
+ """
785
+ Makes broadcastable attention and causal masks so that future and masked tokens are ignored.
786
+
787
+ Arguments:
788
+ attention_mask (`torch.Tensor`):
789
+ Mask with ones indicating tokens to attend to, zeros for tokens to ignore.
790
+ input_shape (`Tuple[int]`):
791
+ The shape of the input to the model.
792
+ device: (`torch.device`):
793
+ The device of the input to the model.
794
+
795
+ Returns:
796
+ `torch.Tensor` The extended attention mask, with a the same dtype as `attention_mask.dtype`.
797
+ """
798
+ # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length]
799
+ # ourselves in which case we just need to make it broadcastable to all heads.
800
+ if attention_mask.dim() == 3:
801
+ extended_attention_mask = attention_mask[:, None, :, :]
802
+ elif attention_mask.dim() == 2:
803
+ # Provided a padding mask of dimensions [batch_size, seq_length]
804
+ # - the model is an encoder, so make the mask broadcastable to [batch_size, num_heads, seq_length, seq_length]
805
+ extended_attention_mask = attention_mask[:, None, None, :]
806
+ else:
807
+ raise ValueError(
808
+ "Wrong shape for input_ids (shape {}) or attention_mask (shape {})".format(
809
+ input_shape, attention_mask.shape
810
+ )
811
+ )
812
+
813
+ # Since attention_mask is 1.0 for positions we want to attend and 0.0 for
814
+ # masked positions, this operation will create a tensor which is 0.0 for
815
+ # positions we want to attend and -10000.0 for masked positions.
816
+ # Since we are adding it to the raw scores before the softmax, this is
817
+ # effectively the same as removing these entirely.
818
+ extended_attention_mask = extended_attention_mask.to(dtype=self.dtype) # fp16 compatibility
819
+ extended_attention_mask = (1.0 - extended_attention_mask) * -10000.0
820
+ return extended_attention_mask
821
+
822
+ def forward(
823
+ self,
824
+ attention_mask=None,
825
+ head_mask=None,
826
+ encoder_hidden_states=None,
827
+ encoder_attention_mask=None,
828
+ past_key_values=None,
829
+ output_attentions=None,
830
+ output_hidden_states=None,
831
+ return_dict=None,
832
+ ):
833
+ r"""
834
+ encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, `optional`):
835
+ Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if
836
+ the model is configured as a decoder.
837
+ encoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, `optional`):
838
+ Mask to avoid performing attention on the padding token indices of the encoder input. This mask is used in
839
+ the cross-attention if the model is configured as a decoder. Mask values selected in `[0, 1]`:
840
+ - 1 for tokens that are **not masked**,
841
+ - 0 for tokens that are **masked**.
842
+ past_key_values (`tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of:
843
+ shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): Contains precomputed key and
844
+ value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are
845
+ used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key
846
+ value states given to this model) of shape `(batch_size, 1)` instead of all `decoder_input_ids` of shape
847
+ `(batch_size, sequence_length)`.
848
+ """
849
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
850
+ output_hidden_states = (
851
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
852
+ )
853
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
854
+
855
+ query_embeds = self.query_embeds.repeat(encoder_hidden_states.shape[0], 1, 1)
856
+ embedding_output = query_embeds
857
+ input_shape = embedding_output.size()[:-1]
858
+ batch_size, seq_length = input_shape
859
+ device = embedding_output.device
860
+
861
+ # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length]
862
+ # ourselves in which case we just need to make it broadcastable to all heads.
863
+ if attention_mask is None:
864
+ attention_mask = torch.ones(
865
+ (query_embeds.shape[0], query_embeds.shape[1]), dtype=torch.long, device=query_embeds.device
866
+ )
867
+ extended_attention_mask = self.get_extended_attention_mask(attention_mask, input_shape, device)
868
+
869
+ # If a 2D or 3D attention mask is provided for the cross-attention
870
+ # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length]
871
+ if encoder_hidden_states is not None:
872
+ if type(encoder_hidden_states) == list:
873
+ encoder_batch_size, encoder_sequence_length, _ = encoder_hidden_states[0].size()
874
+ else:
875
+ (
876
+ encoder_batch_size,
877
+ encoder_sequence_length,
878
+ _,
879
+ ) = encoder_hidden_states.size()
880
+ encoder_hidden_shape = (encoder_batch_size, encoder_sequence_length)
881
+
882
+ if type(encoder_attention_mask) == list:
883
+ encoder_extended_attention_mask = [self.invert_attention_mask(mask) for mask in encoder_attention_mask]
884
+ elif encoder_attention_mask is None:
885
+ encoder_attention_mask = torch.ones(encoder_hidden_shape, device=device)
886
+ encoder_extended_attention_mask = self.invert_attention_mask(encoder_attention_mask)
887
+ else:
888
+ encoder_extended_attention_mask = self.invert_attention_mask(encoder_attention_mask)
889
+ else:
890
+ encoder_extended_attention_mask = None
891
+
892
+ # Prepare head mask if needed
893
+ # 1.0 in head_mask indicate we keep the head
894
+ # attention_probs has shape bsz x n_heads x N x N
895
+ # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads]
896
+ # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length]
897
+ head_mask = self.get_head_mask(head_mask, self.config.num_hidden_layers)
898
+
899
+ encoder_outputs = self.encoder(
900
+ embedding_output,
901
+ attention_mask=extended_attention_mask,
902
+ head_mask=head_mask,
903
+ encoder_hidden_states=encoder_hidden_states,
904
+ encoder_attention_mask=encoder_extended_attention_mask,
905
+ past_key_values=past_key_values,
906
+ output_attentions=output_attentions,
907
+ output_hidden_states=output_hidden_states,
908
+ return_dict=return_dict,
909
+ )
910
+ sequence_output = encoder_outputs[0]
911
+ pooled_output = sequence_output[:, 0, :]
912
+
913
+ sequence_output = self.visual_fc(sequence_output)
914
+ sequence_output = torch.cat([sequence_output, self.vit_eos.repeat(sequence_output.shape[0], 1, 1)], dim=1)
915
+
916
+ return BaseModelOutputWithPooling(
917
+ last_hidden_state=sequence_output,
918
+ pooler_output=pooled_output,
919
+ hidden_states=encoder_outputs.hidden_states,
920
+ )
921
+
922
+