Spaces:
Running
on
Zero
Running
on
Zero
ahmed-masry
commited on
Commit
•
1312eac
1
Parent(s):
8ee10ab
Update modeling_colflor.py
Browse files- modeling_colflor.py +0 -46
modeling_colflor.py
CHANGED
@@ -6,52 +6,6 @@ from modeling_florence2 import Florence2ForConditionalGeneration, Florence2Visio
|
|
6 |
from configuration_florence2 import Florence2Config
|
7 |
|
8 |
|
9 |
-
class ColFlor2Old(Florence2ForConditionalGeneration):
|
10 |
-
"""
|
11 |
-
ColFlor2 model implementation from the "ColPali: Efficient Document Retrieval with Vision Language Models" paper.
|
12 |
-
"""
|
13 |
-
|
14 |
-
main_input_name: ClassVar[str] = "doc_input_ids" # transformers-related
|
15 |
-
|
16 |
-
def __init__(self, config: Florence2Config, use_cache=False):
|
17 |
-
super().__init__(config=config)
|
18 |
-
|
19 |
-
self.dim = 128
|
20 |
-
self.custom_text_proj = nn.Linear(self.config.text_config.d_model, self.dim)
|
21 |
-
# Now initialize weights properly
|
22 |
-
self.custom_text_proj.weight.data.normal_(mean=0.0, std=0.02)
|
23 |
-
self.custom_text_proj.bias.data.zero_()
|
24 |
-
|
25 |
-
self.padding_side = "right"
|
26 |
-
self.post_init()
|
27 |
-
|
28 |
-
def forward(self, *args, **kwargs) -> torch.Tensor:
|
29 |
-
# Delete output_hidden_states from kwargs
|
30 |
-
kwargs.pop("output_hidden_states", None)
|
31 |
-
|
32 |
-
# TO BE DELETED
|
33 |
-
kwargs['decoder_input_ids'] = kwargs['input_ids']
|
34 |
-
|
35 |
-
# Create Full Attention Mask that includes the image
|
36 |
-
if 'full_attention_mask' in kwargs:
|
37 |
-
full_attention_mask = kwargs['full_attention_mask']
|
38 |
-
del kwargs['full_attention_mask']
|
39 |
-
else:
|
40 |
-
full_attention_mask = kwargs['attention_mask']
|
41 |
-
|
42 |
-
outputs = super().forward(*args,
|
43 |
-
**kwargs) # (batch_size, sequence_length, hidden_size)
|
44 |
-
|
45 |
-
last_hidden_states = outputs['encoder_last_hidden_state'] # (batch_size, sequence_length, hidden_size)
|
46 |
-
|
47 |
-
proj = self.custom_text_proj(last_hidden_states) # (batch_size, sequence_length, dim)
|
48 |
-
# L2 normalization
|
49 |
-
proj = proj / proj.norm(dim=-1, keepdim=True) # (batch_size, sequence_length, dim)
|
50 |
-
proj = proj * full_attention_mask.unsqueeze(-1) # (batch_size, sequence_length, dim)
|
51 |
-
|
52 |
-
return proj
|
53 |
-
|
54 |
-
|
55 |
class ColFlor(Florence2VisionLanguageModel):
|
56 |
"""
|
57 |
ColFlor model implementation from the "ColPali: Efficient Document Retrieval with Vision Language Models" paper.
|
|
|
6 |
from configuration_florence2 import Florence2Config
|
7 |
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
class ColFlor(Florence2VisionLanguageModel):
|
10 |
"""
|
11 |
ColFlor model implementation from the "ColPali: Efficient Document Retrieval with Vision Language Models" paper.
|