-
Notifications
You must be signed in to change notification settings - Fork 5
/
trainer.py
259 lines (212 loc) · 12.2 KB
/
trainer.py
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
import torch
from torch import nn
from torch.optim import Adam
from torch.optim.lr_scheduler import LambdaLR
import wandb
import pytorch_lightning as pl
import numpy as np
from jiwer import wer
import torchmetrics
import random
import re
import json
from model.encoder import get_audio_encoder, TransformerAudioEnoder
from model.connector import get_connector, LinearConnector, LinearPoolConnector, CNNConnector
from model.llm import get_llm
class SpeechLLMLightning(pl.LightningModule):
def __init__(self,
audio_enc_dim=512,
llm_dim=2048,
audio_encoder_name="speech-tokenizer",
connector_name='linear-pool',
llm_name="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
finetune_encoder=False,
connector_k=5,
use_lora=True,
lora_r=32,
lora_alpha=2,
max_lr=3e-4,
total_training_step=500000,
warmup_steps=1000,
**kwargs
):
super().__init__()
self.save_hyperparameters()
self.audio_enc_dim = audio_enc_dim
self.llm_dim = llm_dim
self.llm_name = llm_name
self.finetune_encoder = finetune_encoder
self.use_lora = use_lora
self.audio_encoder = get_audio_encoder(audio_encoder_name, finetune_encoder)
self.connector = get_connector(connector_name, audio_enc_dim, llm_dim, connector_k)
self.llm_tokenizer, self.llm_model = get_llm(llm_name, use_lora, lora_r, lora_alpha)
self.max_lr = max_lr
self.total_training_step = total_training_step
self.warmup_steps = warmup_steps
self.use_embedding_loss = False
self.num_validation_samples = 5000
def configure_optimizers(self):
opt = [
{"params": self.audio_encoder.parameters(), "lr": 1e-5},
{"params": self.connector.parameters(), "lr": self.max_lr},
{"params": self.llm_model.parameters(), "lr": self.max_lr},
]
optimizer = Adam(opt, lr=self.max_lr)
return optimizer
def encode(self, mel, pre_tokenized_ids, post_tokenized_ids, output_tokenized_ids, return_embedding_loss=False):
batch_size = mel.shape[0]
speech_embeds = self.audio_encoder(mel)
speech_embeds = self.connector(speech_embeds)
embedder = self.llm_model.model.model.embed_tokens
pre_prompt_embeds = embedder(pre_tokenized_ids)
post_prompt_embeds = embedder(post_tokenized_ids)
output_prompt_embeds = embedder(output_tokenized_ids)
combined_embeds = torch.cat([pre_prompt_embeds, speech_embeds, post_prompt_embeds, output_prompt_embeds], dim=1)
atts = torch.ones(combined_embeds.size()[:-1], dtype=torch.long).to(combined_embeds.device)
input_token_length = pre_tokenized_ids.shape[1] + speech_embeds.shape[1] + post_tokenized_ids.shape[1]
label_ids = torch.cat([
torch.ones([batch_size, input_token_length], device=combined_embeds.device)*-100,
output_tokenized_ids
], 1).to(combined_embeds.device).to(torch.int64)
return combined_embeds, atts, label_ids
def forward(self, embeds, atts, label_ids):
out = self.llm_model(
inputs_embeds=embeds,
attention_mask=atts,
labels=label_ids,
)
return out
def training_step(self, batch, batch_idx):
mel, pre_tokenized_ids, post_tokenized_ids, output_tokenized_ids = batch
embeds, atts, label_ids = self.encode(mel, pre_tokenized_ids, post_tokenized_ids, output_tokenized_ids)
outputs = self.forward(embeds, atts, label_ids)
loss = outputs["loss"]
self.log("train/loss", loss, on_epoch=False)
return loss
def validation_step(self, batch, batch_idx):
mel, pre_tokenized_ids, post_tokenized_ids, output_tokenized_ids = batch
embeds, atts, label_ids = self.encode(mel, pre_tokenized_ids, post_tokenized_ids, output_tokenized_ids)
outputs = self.forward(embeds, atts, label_ids)
loss = outputs["loss"]
self.log("val/loss", loss, on_step=False, on_epoch=True, prog_bar=True, logger=True)
logits = outputs.logits
predicted_ids = torch.argmax(logits, dim=-1).cpu()
generated_output_text = self.llm_tokenizer.decode(predicted_ids[0], skip_special_tokens=False)
target_text = self.llm_tokenizer.decode(output_tokenized_ids[0], skip_special_tokens=False)
extracted_pred = self.extract_prediction_values(generated_output_text)
extracted_target = self.extract_prediction_values(target_text)
keys = extracted_target.keys()
pred_keys = extracted_pred.keys()
for key in keys:
if key not in pred_keys:
extracted_pred[key] = "NA"
if 'Transcript' in keys:
target_transcript = extracted_target['Transcript']
predicted_transcript = extracted_pred['Transcript']
wer_metric = wer(target_transcript.lower(), predicted_transcript.lower())
self.log("val/wer", wer_metric, on_step=False, on_epoch=True, prog_bar=True, logger=True)
if 'Response' in keys:
target_transcript = extracted_target['Response']
predicted_transcript = extracted_pred['Response']
wer_metric = wer(target_transcript.lower(), predicted_transcript.lower())
self.log("val/response_wer", wer_metric, on_step=False, on_epoch=True, prog_bar=True, logger=True)
if 'SpeechActivity' in keys:
target_isspeech = extracted_target['SpeechActivity']
predicted_isspeech = extracted_pred['SpeechActivity']
self.log("val/speech_activity", float(target_isspeech.lower()==predicted_isspeech.lower()), on_step=False, on_epoch=True, prog_bar=True, logger=True)
if 'Gender' in keys:
target_gender = extracted_target['Gender']
predicted_gender = extracted_pred['Gender']
self.log("val/gender", float(target_gender.lower()==predicted_gender.lower()), on_step=False, on_epoch=True, prog_bar=True, logger=True)
if 'Emotion' in keys:
target_emotion = extracted_target['Emotion']
predicted_emotion = extracted_pred['Emotion']
self.log("val/emotion", float(target_emotion.lower()==predicted_emotion.lower()), on_step=False, on_epoch=True, prog_bar=True, logger=True)
if 'Age' in keys:
target_age = extracted_target['Age']
predicted_age = extracted_pred['Age']
self.log("val/age", float(target_age.lower()==predicted_age.lower()), on_step=False, on_epoch=True, prog_bar=True, logger=True)
if 'Accent' in keys:
target_accent = extracted_target['Accent']
predicted_accent = extracted_pred['Accent']
self.log("val/accent", float(target_accent.lower()==predicted_accent.lower()), on_step=False, on_epoch=True, prog_bar=True, logger=True)
if batch_idx in self.selected_samples_for_logging:
sample_idx = self.selected_samples_for_logging.index(batch_idx)
# Use wandb.log to log prediction and truth texts
wandb.log({
f"val_sample_{sample_idx}_pred": wandb.Html(f"<pre>{str(extracted_pred)}</pre>"),
f"val_sample_{sample_idx}_target": wandb.Html(f"<pre>{str(target_text).replace('<s>', '').replace('</s>', '')}</pre>"),
f"val_sample_{sample_idx}_gen": wandb.Html(f"<pre>{generated_output_text.replace('<s>', '').replace('</s>', '')}</pre>"),
}, commit=False)
return {"val_loss": loss}
def test_step(self, batch, batch_idx):
mel, pre_tokenized_ids, post_tokenized_ids, output_tokenized_ids = batch
embeds, atts, label_ids = self.encode(mel, pre_tokenized_ids, post_tokenized_ids, output_tokenized_ids)
outputs = self.forward(embeds, atts, label_ids)
loss = outputs["loss"]
self.log("val/loss", loss, on_step=False, on_epoch=True, prog_bar=True, logger=True)
logits = outputs.logits
predicted_ids = torch.argmax(logits, dim=-1)
input_token_length = output_tokenized_ids.shape[1]
generated_output_text = self.llm_tokenizer.decode(predicted_ids[0], skip_special_tokens=False)
target_text = self.llm_tokenizer.decode(output_tokenized_ids[0], skip_special_tokens=False)
extracted_pred = self.extract_prediction_values(generated_output_text)
extracted_target = self.extract_prediction_values(target_text)
keys = extracted_target.keys()
pred_keys = extracted_pred.keys()
for key in keys:
if key not in pred_keys:
extracted_pred[key] = "NA"
if 'Transcript' in keys:
target_transcript = extracted_target['Transcript']
predicted_transcript = extracted_pred['Transcript']
wer_metric = wer(target_transcript.lower(), predicted_transcript.lower())
self.log("val/wer", wer_metric, on_step=False, on_epoch=True, prog_bar=True, logger=True)
if 'Response' in keys:
target_transcript = extracted_target['Response']
predicted_transcript = extracted_pred['Response']
wer_metric = wer(target_transcript.lower(), predicted_transcript.lower())
self.log("val/response_wer", wer_metric, on_step=False, on_epoch=True, prog_bar=True, logger=True)
if 'SpeechActivity' in keys:
target_isspeech = extracted_target['SpeechActivity']
predicted_isspeech = extracted_pred['SpeechActivity']
self.log("val/speech_activity", float(target_isspeech.lower()==predicted_isspeech.lower()), on_step=False, on_epoch=True, prog_bar=True, logger=True)
if 'Gender' in keys:
target_gender = extracted_target['Gender']
predicted_gender = extracted_pred['Gender']
self.log("val/gender", float(target_gender.lower()==predicted_gender.lower()), on_step=False, on_epoch=True, prog_bar=True, logger=True)
if 'Emotion' in keys:
target_emotion = extracted_target['Emotion']
predicted_emotion = extracted_pred['Emotion']
self.log("val/emotion", float(target_emotion.lower()==predicted_emotion.lower()), on_step=False, on_epoch=True, prog_bar=True, logger=True)
if 'Age' in keys:
target_age = extracted_target['Age']
predicted_age = extracted_pred['Age']
self.log("val/age", float(target_age.lower()==predicted_age.lower()), on_step=False, on_epoch=True, prog_bar=True, logger=True)
if 'Accent' in keys:
target_accent = extracted_target['Accent']
predicted_accent = extracted_pred['Accent']
self.log("val/accent", float(target_accent.lower()==predicted_accent.lower()), on_step=False, on_epoch=True, prog_bar=True, logger=True)
return {"val_loss": loss}
def on_validation_epoch_start(self):
"""Select two random validation samples to log for each epoch."""
self.selected_samples_for_logging = random.sample(range(self.num_validation_samples), 2)
def extract_dictionary(self, input_string):
pattern = r'<s>\s*(\{.*?\})\s*</s>'
match = re.search(pattern, input_string, re.DOTALL)
if match:
dict_string = match.group(1)
dict_string = re.sub(r',\s*}', '}', dict_string)
try:
return json.loads(dict_string)
except json.JSONDecodeError as e:
return {}
else:
return {}
def extract_prediction_values(self, input_string):
json_str_match = re.search(r'<s>\s*\{.*?\}\s*</s>', input_string)
try:
json_str = json_str_match.group(0)
except:
json_str = '{}'
return self.extract_dictionary(json_str)