forked from Cerebras/modelzoo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodeling_llava.py
More file actions
209 lines (187 loc) · 7.49 KB
/
modeling_llava.py
File metadata and controls
209 lines (187 loc) · 7.49 KB
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
# Copyright 2022 Cerebras Systems.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch
from torch import nn
from cerebras.modelzoo.models.multimodal.multimodal_base_model import (
ModalityType,
)
from cerebras.modelzoo.models.multimodal.multimodal_utils import freeze_modules
class Llava(nn.Module):
def __init__(
self,
image_model,
text_model,
image_start_idx,
projector_image_model=None,
projector_text_model=None,
freeze=None, # list of regex strings
image_feature_select_layer_idx=None,
image_feature_select_mode="patch",
):
super(Llava, self).__init__()
self._modalities = [ModalityType.IMAGE, ModalityType.TEXT]
self.image_model = image_model
self.text_model = text_model
self.projector_image_model = projector_image_model
self.projector_text_model = projector_text_model
self.image_start_idx = image_start_idx
self.image_feature_select_mode = image_feature_select_mode
self.tie_weights()
# Freeze specified parameters
freeze_modules(self, freeze)
self.image_feature_select_layer_idx = image_feature_select_layer_idx
@property
def modalities(self):
return self._modalities
def tie_weights(self):
self.image_model.tie_weights()
self.text_model.tie_weights()
if self.projector_image_model and hasattr(
self.projector_image_model, "tie_weights"
):
self.projector_image_model.tie_weights()
if self.projector_text_model and hasattr(
self.projector_text_model, "tie_weights"
):
self.projector_text_model.tie_weights()
def reset_parameters(self):
self.image_model.reset_parameters()
self.text_model.reset_parameters()
if self.projector_image_model and hasattr(
self.projector_image_model, "reset_parameters"
):
self.projector_image_model.reset_parameters()
if self.projector_text_model and hasattr(
self.projector_text_model, "reset_parameters"
):
self.projector_text_model.reset_parameters()
def forward(
self,
image_data=None,
text_input_ids=None,
image_embeddings=None,
text_embeddings=None,
attention_mask=None,
tgt_key_padding_mask=None, # 1 where pad and not attend
attention_span=None,
position_ids=None,
img_start_idx=None,
):
input_embeddings = self.compute_input_embeddings(
image_data,
image_embeddings,
text_input_ids,
text_embeddings,
position_ids,
img_start_idx,
)
logits = self.text_model(
input_ids=None,
attention_mask=attention_mask, # Does nothing in decoder models fwd pass
tgt_key_padding_mask=tgt_key_padding_mask,
attention_span=attention_span,
position_ids=position_ids,
input_embeddings=input_embeddings,
)
return logits
def compute_input_embeddings(
self,
image_data=None,
image_embeddings=None,
text_input_ids=None,
text_embeddings=None,
position_ids=None,
img_start_idx=None,
):
if image_data is not None and image_embeddings is not None:
raise ValueError(
f"Only one of `image_data` or `image_embeddings` should be passed to model.forward"
)
elif image_data is None and image_embeddings is None:
raise ValueError(
f"Both `image_data` or `image_embeddings` are None, "
f"either one of the them should be passed to model.forward"
)
if text_input_ids is not None and text_embeddings is not None:
raise ValueError(
f"Only one of `text_input_ids` or `text_embeddings` should be passed to model.forward"
)
elif text_input_ids is None and text_embeddings is None:
raise ValueError(
f"Both `text_input_ids` or `text_embeddings` are None, "
f"either one of the them should be passed to model.forward"
)
# Compute image_features by passing through embedding layer of image_model
if image_data is not None:
image_embeddings = self.image_model.compute_input_embeddings(
image_data
)
image_features_w_cls = self.image_model.extract_features(
image_embeddings, self.image_feature_select_layer_idx
)
if self.image_feature_select_mode == "patch":
image_features = image_features_w_cls[
:, 1:, :
] # Remove CLS features
else:
image_features = image_features_w_cls
num_patches = image_features.shape[1]
# Pass image_embeddings through projector
if self.projector_image_model is not None:
image_features = self.projector_image_model(image_features)
# Compute text embeddings
if text_input_ids is not None:
# If position_embeddings_type is `learned` or `fixed`, we'd like to
# use them when computing embeddings for text_tokens
# since the forward pass, when supplied with `input_embeddings`
# does not call EmbeddingLayer fwd pass again.
text_embeddings = self.text_model.compute_input_embeddings(
text_input_ids, position_ids
)
if self.projector_text_model is not None:
text_embeddings = self.projector_text_model(text_embeddings)
# Replace patch positions with image_features
# position_ids and key_padding_mask will ensure
# appropriate positions to attend and
# positional encoding
if img_start_idx is None:
# The `img_start_idx` is not provided, the <image> is at the beginning of the sentence.
image_text_embeddings = torch.cat(
(
text_embeddings[:, 0 : self.image_start_idx, :],
image_features,
text_embeddings[:, self.image_start_idx + num_patches :, :],
),
dim=1,
)
return image_text_embeddings
else:
# The `img_start_idx` is not None, the location of <image> is arbitrary.
index = torch.arange(
0,
num_patches,
device=image_features.device,
dtype=torch.float32,
)
index = index[None, :].broadcast_to(
text_embeddings.shape[0], num_patches
)
index = (index + img_start_idx).to(torch.int64)
index = index[:, :, None].broadcast_to(
text_embeddings.shape[0], num_patches, text_embeddings.shape[-1]
)
text_embeddings.scatter_(
1, index, image_features.to(text_embeddings.dtype)
)
return text_embeddings