Spaces:
Running
on
Zero
Running
on
Zero
File size: 7,967 Bytes
7c34c28 |
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 |
from typing import List, Dict
from abc import ABC, abstractmethod
from torch.nn.functional import conv1d
import torch
import logging
from multi_token.modalities.base_modality import Modality
from multi_token.model_utils import MultiTaskType
from torchviz import make_dot
class LMMMetaModel:
def __init__(self, config):
super(LMMMetaModel, self).__init__(config)
def _load_projector_weights(self, weights: Dict):
weights = {
(k[23:] if k.startswith("base_model.model.model.") else k): v
for k, v in weights.items()
}
logging.info(f"Loading pretrained weights: {list(weights.keys())}")
load_result = self.load_state_dict(weights, strict=False)
assert (
len(load_result.unexpected_keys) == 0
), "Unexpected weights, is this the right model?"
def initialize_pretrained_modules(self, modalities: List[Modality], weights: Dict):
for m in modalities:
# projector = m.build_projector(self.config.hidden_size)
# setattr(self, m.name + "_lmm_projector", projector)
projector = m.build_projector(self.config.hidden_size)
if m.use_multi_task == MultiTaskType.SIMPLE_MULTI_TASK:
for task_name in m.tasks["task_heads"].keys():
task_model = projector[task_name]
setattr(self, m.name + "_" + task_name, task_model)
else:
setattr(self, m.name + "_lmm_projector", projector)
self._load_projector_weights(weights)
def initialize_modules(self, modalities: List[Modality], weights: Dict):
names = [m.name for m in modalities]
self.config.modalities = names
for m in modalities:
# projector = m.build_projector(self.config.hidden_size)
# setattr(self, m.name + "_lmm_projector", projector)
projector = m.build_projector(self.config.hidden_size)
if m.use_multi_task == MultiTaskType.SIMPLE_MULTI_TASK:
for task_name in m.tasks["task_heads"].keys():
task_model = projector[task_name]
setattr(self, m.name + "_" + task_name, task_model)
else:
setattr(self, m.name + "_lmm_projector", projector)
self._load_projector_weights(weights)
class LMMMetaForCausalLM(ABC):
@abstractmethod
def get_model(self) -> "LMMMetaForCausalLM":
pass
def prepare_inputs_labels_for_multimodal(
self, input_ids, attention_mask, past_key_values, labels, **kwargs
):
model = self.get_model()
batch_size, seq_len = input_ids.shape
# batch_size x seq_len x embedding_hidden_size
inputs_embeds = torch.zeros(
(batch_size, seq_len, self.config.hidden_size),
dtype=self.dtype,
device=self.device,
)
# modality x batch_size x instance_idx x modality_token_width x embedding_hidden_size
projected_tensors = []
# assuming that if caching is enabled, we'll never have past_key_values AND need to encode the instruction modality values
task_vals = {}
#print("here past_key_values", past_key_values)
#past_key_values == None
if past_key_values is None:
for m in self.modalities:
m_vals = m.forward(kwargs.get(m.name))
mp_vals = []
if m.use_multi_task == MultiTaskType.SIMPLE_MULTI_TASK:
proj = {}
for task_name in m.tasks["task_heads"].keys():
proj[task_name] = getattr(model, m.name + "_" + task_name)
else:
proj = getattr(model, m.name + "_lmm_projector")
# project each batch into language model token space
for m_val in m_vals:
if m.use_multi_task == MultiTaskType.SIMPLE_MULTI_TASK:
for task_name in m.tasks["task_heads"].keys():
if task_name == "lmm_projector":
mp_vals.append(proj[task_name](m_val))
# make_dot(mp_vals[-1], params=dict(list(model.named_parameters()))).render(task_name, format="png")
else:
if task_name not in task_vals:
task_vals[task_name] = [proj[task_name](m_val)]
else:
task_vals[task_name].append(proj[task_name](m_val))
# make_dot(task_vals[task_name], params=dict(list(model.named_parameters()))).render(task_name, format="png")
elif m.use_multi_task == MultiTaskType.PROJECTED_MULTI_TASK:
task_outputs = proj(m_val)
mp_vals.append(task_outputs.pop("projectors"))
for task_name in task_outputs.keys():
if not task_name in task_vals:
task_vals[task_name] = [task_outputs[task_name]]
else:
task_vals[task_name].append(task_outputs[task_name])
else:
mp_vals.append(proj(m_val))
assert all(
mp_val.shape[1:] == (m.token_width, self.config.hidden_size)
for mp_val in mp_vals
), (
"Modality tensors have incorrect shape, check your projector implementation "
+ str([mp_val.shape[1:] for mp_val in mp_vals])
+ " vs expected "
+ str((m.token_width, self.config.hidden_size))
)
projected_tensors.append(mp_vals)
indices = None
for i, input_ids_sample in enumerate(input_ids):
is_text_mask = input_ids_sample >= 0
# fill in all the LLM-based text embeddings
inputs_embeds[i, is_text_mask] = model.embed_tokens(
input_ids_sample[is_text_mask]
)
# skip if all tokens are text tokens
if is_text_mask.sum() == seq_len:
continue
assert (
past_key_values is None
), "We shouldn't have cached keys if this is the first instruction pass"
#past_key_values = None
for mi, m in enumerate(self.modalities):
# locate the group of tokens for this modality
m_mask = (input_ids_sample == m.token_idx).float()
m_kernel = torch.tensor(
[-1] * m.token_width, dtype=m_mask.dtype, device=m_mask.device
)
m_conv = conv1d(
m_mask.unsqueeze(0).unsqueeze(0),
m_kernel.unsqueeze(0).unsqueeze(0),
)
# where do we see `token_width`-tokens in a row?
indices = (m_conv[0, 0] == -m.token_width).nonzero(as_tuple=True)[0]
# fill these embeddings with the projected modality tensor
last_covered_idx = -1
k = 0
for possible_token_idx in indices:
if possible_token_idx <= last_covered_idx:
# make sure we don't overwrite an instance we've already covered
# handles bug caused by back-to-back tokens
continue
batch_modality_tensor = projected_tensors[mi][i][k]
inputs_embeds[
i, possible_token_idx : possible_token_idx + m.token_width
] = batch_modality_tensor
last_covered_idx = possible_token_idx + m.token_width - 1
k += 1
return None, attention_mask, past_key_values, inputs_embeds, labels, task_vals
|