File size: 14,573 Bytes
52146a5 |
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 |
import copy
import json
import os
from typing import Optional, Union
import librosa
import numpy as np
import torch
import torch.nn.functional as F
from datasets import Audio
from safetensors.torch import load, load_model
from torch import nn
from .configuring_diva import DiVAConfig
from transformers import (
AutoProcessor,
AutoTokenizer,
AutoModelForCausalLM,
PreTrainedModel,
WhisperModel,
)
class WhisperConnector(nn.Module):
def __init__(
self,
):
super().__init__()
self.decoder = None
self.projection = nn.Linear(1280, 4096)
self.query_tokens = nn.Parameter(torch.randn(448, 1280))
def forward(self, x, output_device="cuda:1"):
bsz = x.shape[0]
query_tokens = self.query_tokens[None, :, :].expand(bsz, -1, -1)
virt_whisper_tokens = self.decoder(
inputs_embeds=query_tokens, encoder_hidden_states=x
)
if self.projection.weight.shape[-1] == 5120:
virtual_tokens = self.projection(virt_whisper_tokens[0].reshape(112, 5120))
else:
virtual_tokens = self.projection(virt_whisper_tokens[0])
return virtual_tokens.to(output_device)
class DiVAModel(PreTrainedModel):
config_class = DiVAConfig
def __init__(
self, via_path=None, config_dict={}, device_map=None, speech_encoder_device=None
):
super().__init__(DiVAConfig.from_dict(config_dict))
if speech_encoder_device is None:
speech_encoder_device = "cuda:0"
whisper = WhisperModel.from_pretrained(config_dict["reference_encoder"])
connector = WhisperConnector()
connector.decoder = copy.deepcopy(whisper.decoder)
if via_path is not None:
with open(via_path, "rb") as f:
sd = load(f.read())
with torch.no_grad():
connector.query_tokens = nn.Parameter(sd["query_tokens"])
connector.projection.weight = nn.Parameter(sd["projection.weight"].T)
connector.projection.bias = nn.Parameter(sd["projection.bias"])
wsd = {
key.replace("connector.", ""): sd[key]
for key in sd
if key.startswith("connector.")
}
connector.decoder.load_state_dict(wsd)
if device_map == None:
num_layers = 32
num_gpus = 2
device_map = dict(
**{"model.embed_tokens": 1, "model.norm": 1, "lm_head": 2},
**{
"model.layers." + str(i): 1 + (i // (num_layers // num_gpus))
for i in range(num_layers)
},
)
self.connector = connector.to(speech_encoder_device)
self.whisper_encoder = whisper.encoder.to(speech_encoder_device)
self.llm_decoder = AutoModelForCausalLM.from_pretrained(
config_dict["reference_decoder"],
device_map=device_map,
torch_dtype=torch.float16,
)
self.processor = AutoProcessor.from_pretrained(config_dict["reference_encoder"])
self.tokenizer = AutoTokenizer.from_pretrained(config_dict["reference_decoder"])
if self.tokenizer.pad_token_id == None:
override_token = list(self.tokenizer.added_tokens_decoder.items())[-1]
self.tokenizer.pad_token_id = override_token[0]
self.tokenizer.pad_tokn = str(override_token[1])
prefix, suffix = self.tokenizer.apply_chat_template(
[{"role": "user", "content": "PLACEHOLDER"}],
tokenize=False,
add_generation_prompt=True,
).split("PLACEHOLDER")
non_null = [line for line in prefix.split("\n") if line.strip()]
prefix_tok = self.tokenizer.encode(prefix, add_special_tokens=False)
suffix_tok = self.tokenizer.encode(suffix, add_special_tokens=False)
self.prefix = torch.tensor(prefix_tok).to(
self.llm_decoder.model.embed_tokens.weight.device
)
self.pre_system = torch.tensor(
self.tokenizer.encode(non_null[0] + "\n", add_special_tokens=False)
).to(self.llm_decoder.model.embed_tokens.weight.device)
self.post_system = torch.tensor(
self.tokenizer.encode("\n" + non_null[-1] + "\n", add_special_tokens=False)
).to(self.llm_decoder.model.embed_tokens.weight.device)
self.final_header = torch.tensor(suffix_tok).to(
self.llm_decoder.model.embed_tokens.weight.device
)
self.speech_encoder_device = speech_encoder_device
def can_generate(cls):
return False
@classmethod
def from_pretrained(
cls,
pretrained_model_name_or_path: Optional[Union[str, os.PathLike]],
*model_args,
config=None,
cache_dir=None,
**kwargs,
):
if os.path.isdir(pretrained_model_name_or_path):
via_path = pretrained_model_name_or_path + "/model.safetensors"
config_path = pretrained_model_name_or_path + "/config.json"
else:
# Loading from huggingface repo
from huggingface_hub import hf_hub_download
hf_hub_download(
repo_id=pretrained_model_name_or_path,
filename="model.safetensors",
token=kwargs.get("token", None),
local_dir=os.path.dirname(__file__),
)
hf_hub_download(
repo_id=pretrained_model_name_or_path,
filename="config.json",
token=kwargs.get("token", None),
local_dir=os.path.dirname(__file__),
)
via_path = os.path.dirname(__file__) + "/model.safetensors"
config_path = os.path.dirname(__file__) + "/config.json"
with open(config_path, "r") as f:
config_dict = json.loads(f.read())
return cls(
via_path,
config_dict,
kwargs["device_map"] if "device_map" in kwargs else "auto",
(
kwargs["speech_encoder_device"]
if "speech_encoder_device" in kwargs
else None
),
)
def forward(self, audio, prefix_text_tokens, suffix_text_tokens):
inputs = self.processor(audio, return_tensors="pt", sampling_rate=16_000)
input_features = inputs.input_features.to(self.speech_encoder_device)
hidden_states = self.whisper_encoder(input_features=input_features)[
"last_hidden_state"
]
virt_tokens = self.connector(
hidden_states,
output_device=self.llm_decoder.model.embed_tokens.weight.device,
).squeeze()
prefix_embed = self.llm_decoder.model.embed_tokens(prefix_text_tokens)
suffix_embed = self.llm_decoder.model.embed_tokens(suffix_text_tokens)
inputs_embeds = torch.cat(
[prefix_embed, virt_tokens, suffix_embed], axis=0
).unsqueeze(0)
outputs = self.llm_decoder(
inputs_embeds=inputs_embeds.to(
self.llm_decoder.model.embed_tokens.weight.device
).half(),
return_dict=True,
output_hidden_states=True,
past_key_values=past_key_values,
)
return outputs
@torch.no_grad()
def generate(
self,
audio,
text_prompt=None,
do_sample=False,
logits_processor=None,
max_new_tokens=128,
):
inputs = self.processor(audio, return_tensors="pt", sampling_rate=16_000)
input_features = inputs.input_features.to(self.speech_encoder_device)
hidden_states = self.whisper_encoder(input_features=input_features)[
"last_hidden_state"
]
virt_tokens = self.connector(
hidden_states,
output_device=self.llm_decoder.model.embed_tokens.weight.device,
)
bsz = virt_tokens.shape[0]
if text_prompt != None and text_prompt != "":
user_prompt_text = torch.tensor(
self.tokenizer(
text_prompt,
add_special_tokens=False,
padding=True,
padding_side="right",
)["input_ids"],
device=self.pre_system.device,
)
prefix = torch.cat(
[
self.pre_system.expand(
bsz,
-1,
),
user_prompt_text,
self.post_system.expand(
bsz,
-1,
),
],
axis=1,
)
else:
prefix = self.prefix
prefix_embed = self.llm_decoder.model.embed_tokens(prefix).expand(bsz, -1, -1)
suffix = self.final_header
suffix_embed = self.llm_decoder.model.embed_tokens(suffix).expand(bsz, -1, -1)
inputs_embeds = torch.cat([prefix_embed, virt_tokens, suffix_embed], axis=1)
outs = [[] for i in range(bsz)]
complete = [False] * bsz
outputs = None
greedy = 1
i = 0
while not all(complete) and len(outs[0]) < max_new_tokens:
past_key_values = outputs.past_key_values if outputs else None
outputs = self.llm_decoder(
inputs_embeds=inputs_embeds.to(
self.llm_decoder.model.embed_tokens.weight.device
).half(),
return_dict=True,
output_hidden_states=True,
past_key_values=past_key_values,
)
next_token_logits = outputs.logits[:, -1, :]
if logits_processor:
local_outs = torch.tensor(outs) if outs != [] else suffix
local_outs = local_outs.reshape(1, -1)
next_token_logits = logits_processor(
local_outs,
next_token_logits.reshape(1, -1),
)
next_token_logits = next_token_logits.flatten()
if do_sample:
logits = next_token_logits / temperature
probs = F.softmax(logits, dim=-1)
greedy = torch.multinomial(probs, num_samples=1)[0]
else:
greedy = next_token_logits.argmax(dim=-1)
for token_index, out in enumerate(greedy.flatten().tolist()):
if not complete[token_index]:
outs[token_index].append(out)
if out == 128009:
complete[token_index] = True
next_embed = self.llm_decoder.model.embed_tokens(greedy.reshape(-1, 1))
inputs_embeds = next_embed
return self.tokenizer.batch_decode(outs, skip_special_tokens=True)
def generate_stream(
self,
audio,
text_prompt,
do_sample=False,
logits_processor=None,
max_new_tokens=128,
return_outputs=False,
init_outputs=None,
):
inputs = self.processor(audio, return_tensors="pt", sampling_rate=16_000)
input_features = inputs.input_features.to(self.whisper_encoder.device)
hidden_states = self.whisper_encoder(input_features=input_features)[
"last_hidden_state"
]
virt_tokens = self.connector(
hidden_states,
output_device=self.llm_decoder.model.embed_tokens.weight.device,
).squeeze()
if text_prompt != None and text_prompt != "":
user_prompt_text = torch.tensor(
self.tokenizer(
text_prompt,
add_special_tokens=False,
padding=True,
padding_side="right",
)["input_ids"],
device=self.pre_system.device,
)
prefix = torch.cat(
[self.pre_system, user_prompt_text, self.post_system],
axis=0,
)
else:
prefix = self.prefix
prefix_embed = self.llm_decoder.model.embed_tokens(prefix)
suffix = self.final_header
suffix_embed = self.llm_decoder.model.embed_tokens(suffix)
inputs_embeds = torch.cat(
[prefix_embed, virt_tokens, suffix_embed], axis=0
).unsqueeze(0)
outs = []
outputs = init_outputs
greedy = 1
i = 0
while greedy != 128009 and len(outs) < max_new_tokens:
past_key_values = outputs.past_key_values if outputs else None
outputs = self.llm_decoder(
inputs_embeds=inputs_embeds.to(
self.llm_decoder.model.embed_tokens.weight.device
).half(),
return_dict=True,
output_hidden_states=True,
past_key_values=past_key_values,
)
next_token_logits = outputs.logits[-1, -1, :]
if logits_processor:
local_outs = torch.tensor(outs) if outs != [] else suffix
local_outs = local_outs.reshape(1, -1)
next_token_logits = logits_processor(
local_outs,
next_token_logits.reshape(1, -1),
)
next_token_logits = next_token_logits.flatten()
if do_sample:
logits = next_token_logits / temperature
probs = F.softmax(logits, dim=-1)
greedy = torch.multinomial(probs, num_samples=1)[0]
else:
greedy = next_token_logits.argmax()
outs.append(greedy)
next_embed = self.llm_decoder.model.embed_tokens(greedy.reshape(1, 1))
inputs_embeds = next_embed
if not return_outputs:
yield self.tokenizer.decode(outs, skip_special_tokens=True).replace(
"<|eot_id|>", ""
)
else:
yield (
self.tokenizer.decode(outs, skip_special_tokens=True).replace(
"<|eot_id|>", ""
),
outputs,
)
if not return_outputs:
return self.tokenizer.decode(outs, skip_special_tokens=True).replace(
"<|eot_id|>", ""
)
else:
return (
self.tokenizer.decode(outs, skip_special_tokens=True).replace(
"<|eot_id|>", ""
),
outputs,
)
|