-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
389 lines (321 loc) · 13.4 KB
/
train.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
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
382
383
384
385
386
387
388
389
#!/usr/bin/env python3
"""Recipe for pretraining BestRQ (TODO CITATION).
See config file for model definition.
See the readme of the recipe for advice on the pretraining that may appear
a bit challenging depending on your available resources.
To run this recipe call python train_best_rq.py best_rq.yaml --find_unused_parameters --max_grad_norm 0.0
Authors
* Ryan Whetten 2023
"""
import os
import logging
import sys
import time
from functools import partial
import speechbrain as sb
import torch
import torch.nn.functional as F
from torch.nn.parallel import DistributedDataParallel
from hyperpyyaml import load_hyperpyyaml
from speechbrain import Stage
from speechbrain.utils.distributed import run_on_main
from speechbrain.dataio.dataloader import SaveableDataLoader
from speechbrain.dataio.sampler import DynamicBatchSampler
from mask import brq_mask_collate_fn
import wandb
logger = logging.getLogger(__name__)
class BestRQBrain(sb.core.Brain):
def compute_forward(self, batch, stage):
"""Computes forward pass through BestRQ model and returns encoded and
target embeddings as well as other metrics of interest.
"""
# get batch and mask
wavs, wav_lens, mask = batch
wavs, wav_lens, mask = (
wavs.to(self.device),
wav_lens.to(self.device),
mask.to(self.device),
)
############### START ##############
### get fbanks and normalize
feats = self.hparams.compute_features(wavs)
current_epoch = self.hparams.epoch_counter.current
feats = self.modules.normalize(feats, wav_lens, epoch=current_epoch)
B, T, C = feats.shape
divis_by = self.hparams.pad_to_divisible_by
#### pad features
current_dim_size = T
dim_to_pad = 1 # Pad along the second dimension (i.e. time)
# Calculate the amount of padding needed to make the tensor divisible by 4
current_dim_size = feats.shape[dim_to_pad]
padding_needed = (4 - (current_dim_size % 4)) % 4 # Ensure positive padding
# Define the padding
padding = [0, 0, 0, 0, 0, 0] # Initialize padding for all dimensions
padding[dim_to_pad * 2] = padding_needed # Set padding for the chosen dimension
# add in padding to features and mask
feats = torch.nn.functional.pad(feats, padding)
# print('features w/padding: ', feats.shape)
# get targets from quantizer
targets = self.modules.Quantizer(feats.view(B, feats.shape[1]//divis_by, -1))
# print('targets: ', targets.shape)
# generate random noise
noise = torch.normal(
mean=self.hparams.noise_mean,
std=self.hparams.noise_std,
size=(B, mask.shape[0], C),
device=self.device
)
# replace with random noise
feats[:,mask,:] = noise
#### convolutions
src = self.modules.CNN(feats)
# print('after cnn: ', src.shape)
##### transformer
enc_out = self.modules.wrapper(src, wav_lens) # only use encoder
# print('enc out: ', enc_out.shape)
##### linear
logits = self.modules.linear(enc_out)
# print('linear layer out: ', logits.shape)
mask_idx = mask[::divis_by] // divis_by
logits[:,mask_idx,:]
targets[:,mask_idx].shape
if not torch.isfinite(logits).all():
print('feats: ', torch.isfinite(feats).all())
print('src: ', torch.isfinite(src).all())
print('enc: ', torch.isfinite(enc_out).all())
print('logits: ', torch.isfinite(logits).all())
print('targets: ', torch.isfinite(targets).all())
# print('scaler: ', self.scaler.state_dict())
##### get masked region
logits = logits[:,mask_idx,:]
targets = targets[:,mask_idx]
B, T, C = logits.shape
return logits.view(B * T, C), targets.view(B*T)
def compute_objectives(self, predictions, batch, stage):
pred, targets = predictions
if stage != sb.Stage.TRAIN and sb.utils.distributed.if_main_process():
predicted_classes = torch.argmax(pred, dim=-1)
correct_predictions = (predicted_classes == targets)
accuracy = correct_predictions.sum().item() / len(correct_predictions)
self.acc_metric.append(accuracy)
loss = F.cross_entropy(pred, targets)
if self.step % 100 == 0 and stage == sb.Stage.TRAIN:
param_norms =[
param.detach().flatten()
for param in self.modules.parameters()
if param is not None
]
param_norms = torch.cat(param_norms).norm()
grads = [
param.grad.detach().flatten()
for param in self.modules.parameters()
if param.grad is not None
]
cb_usage = targets.unique().shape[0] / self.hparams.cb_vocab
norm = torch.cat(grads).norm()
if sb.utils.distributed.if_main_process():
wandb.log({
'loss': loss,
'param_norm':param_norms,
'grad_norm':norm,
'cb_usage':cb_usage,
**self.scaler.state_dict()
})
return loss
def on_fit_batch_end(self, batch, outputs, loss, should_step):
""" Called after fit_batch(), updates learning rate and does per-step logging. """
if should_step:
self.hparams.noam_annealing(self.optimizer)
# Perform step-wise logging
if (
hasattr(self.hparams, "log_interval")
and self.optimizer_step % self.hparams.log_interval == 0
):
# Create a dictionary and fill it with everything we
# want to log such as contrastive loss, diversity loss,
# learning rate etc.
log_dct = {}
# log_dct = {
# k: (v.item() if isinstance(v, torch.Tensor) else v)
# for k, v in objectives.items()
# }
current_lr = self.optimizer.param_groups[0]["lr"]
log_dct["steps"] = self.optimizer_step
log_dct["lr"] = current_lr
log_dct["avg_loss"] = self.avg_train_loss
if hasattr(self, "time_last_log"):
run_time_since_last_log = time.time() - self.time_last_log
log_dct["run_time"] = run_time_since_last_log
self.time_last_log = time.time()
if sb.utils.distributed.if_main_process():
self.hparams.train_steps_logger.log_stats(stats_meta=log_dct,)
# def evaluate_batch(self, batch, stage):
# """ Returns accuracy on contrastive objective. """
# out = self.compute_forward(batch, stage=stage)
# objectives = self.compute_objectives(out, batch, stage=stage)
# return objectives["backprop_loss"].detach().cpu()
def on_stage_start(self, stage, epoch):
"""Gets called at the beginning of each epoch"""
if stage != sb.Stage.TRAIN:
self.acc_metric = []
def on_stage_end(self, stage, stage_loss, epoch=None):
stage_stats = {"loss": stage_loss}
if stage == sb.Stage.TRAIN:
self.train_stats = stage_stats
if stage == sb.Stage.VALID:
if self.acc_metric:
# print('acc_metric', self.acc_metric)
stage_stats["accuracy"] = sum(self.acc_metric) / len(
self.acc_metric
)
self.hparams.train_stage_logger.log_stats(
stats_meta={
"epoch": epoch,
"steps": self.optimizer_step,
"lr": self.optimizer.param_groups[0]["lr"],
"VRAM": torch.cuda.max_memory_allocated(),
},
train_stats=self.train_stats,
valid_stats=stage_stats,
)
self.checkpointer.save_and_keep_only(
end_of_epoch=True,
num_to_keep=5,
meta={"valid_loss": stage_loss},
)
def dataio_prepare(hparams):
data_folder = hparams["data_folder"]
train_data = sb.dataio.dataset.DynamicItemDataset.from_csv(
csv_path=hparams["train_csv"], replacements={"data_root": data_folder},
)
# We remove longer and shorter files from the train.
train_data = train_data.filtered_sorted(
sort_key="duration",
key_max_value={"duration": hparams["avoid_if_longer_than"]},
key_min_value={"duration": hparams["avoid_if_shorter_than"]},
)
valid_data = sb.dataio.dataset.DynamicItemDataset.from_csv(
csv_path=hparams["valid_csv"], replacements={"data_root": data_folder},
)
datasets = [train_data, valid_data]
def get_output_lengths(input_lengths):
""" Function to get the output length of the feature extractor this is
necessery to compute the masks of BestRQ.
"""
sr = hparams["sample_rate"]
hop_length = hparams["hop_length"]
return (input_lengths // (sr*hop_length / 1000) + 1).to(torch.long)
@sb.utils.data_pipeline.takes("wav")
@sb.utils.data_pipeline.provides("sig")
def audio_pipeline(wav):
sig = sb.dataio.dataio.read_audio(wav)
return sig
sb.dataio.dataset.add_dynamic_item(datasets, audio_pipeline)
sb.dataio.dataset.set_output_keys(datasets, ["id", "sig"])
# We create the DynamicBatch Sampler
train_sampler = DynamicBatchSampler(
train_data,
hparams["seconds_per_batch"],
num_buckets=hparams["train_num_buckets"],
length_func=lambda x: x["duration"],
batch_ordering="random",
shuffle=True,
)
# We define the custom collation function that is necessary for best-rq to
# generate masks.
brq_mask_collate_fn_partial = partial(
brq_mask_collate_fn,
get_out_len_fn=get_output_lengths,
mask_prob=hparams["mask_prob"],
mask_length=hparams["mask_length"],
n_mels=hparams["n_mels"],
)
train_loader_kwargs = {
"batch_sampler": train_sampler,
"collate_fn": brq_mask_collate_fn_partial,
"num_workers": hparams["train_dataloader_options"]["num_workers"],
"pin_memory": True,
}
valid_loader = SaveableDataLoader(
valid_data,
collate_fn=brq_mask_collate_fn_partial,
num_workers=hparams["test_dataloader_options"]["num_workers"],
batch_size=hparams["test_dataloader_options"]["batch_size"],
pin_memory=True,
)
if 'wandb_offline' in hparams and hparams['wandb_offline']:
os.environ["WANDB_MODE"] = "offline"
wandb.init(
# Set the project where this run will be logged
project=hparams["project_name"],
# We pass a run name (otherwise it’ll be randomly assigned, like sunshine-lollypop-10)
# name=f"exp_0",
name=hparams["experiment_name"],
# Track hyperparameters and run metadata
config={
"learning_rate": hparams["lr"],
"architecture": hparams["architecture"],
"dataset": "Libri",
"number_of_epochs": hparams["number_of_epochs"],
"precision": hparams["precision"],
"max_grad_norm": hparams["max_grad_norm"],
"grad_accumulation_factor": hparams["grad_accumulation_factor"],
"seconds_per_batch": hparams["seconds_per_batch"], # Fits in a 32GB GPUs (V100)
"train_num_buckets": hparams["train_num_buckets"],
"d_model": hparams["d_model"],
"d_ffn": hparams["d_ffn"],
"nhead": hparams["nhead"],
"num_encoder_layers": hparams["num_encoder_layers"],
"seed": hparams["seed"],
"cb_vocab": hparams["cb_vocab"],
"p_input": hparams["p_input"],
"cb_dim": hparams["cb_dim"],
"mask_prob": hparams["mask_prob"],
})
return train_data, valid_loader, train_loader_kwargs
def main():
logger.setLevel(logging.INFO)
hparams_file, run_opts, overrides = sb.parse_arguments(sys.argv[1:])
sb.utils.distributed.ddp_init_group(run_opts)
with open(hparams_file) as fin:
hparams = load_hyperpyyaml(fin, overrides)
hparams.update(run_opts)
sb.create_experiment_directory(
experiment_directory=hparams["output_folder"],
hyperparams_to_save=hparams_file,
overrides=overrides,
)
from librispeech_prepare import prepare_librispeech
run_on_main(
prepare_librispeech,
kwargs={
"data_folder": hparams["data_folder"],
"tr_splits": hparams["train_splits"],
"dev_splits": hparams["dev_splits"],
"te_splits": hparams["test_splits"],
"save_folder": hparams["output_folder"],
"merge_lst": hparams["train_splits"],
"merge_name": "train.csv",
"skip_prep": hparams["skip_prep"],
},
)
# Part that matters starts here.
train_dataset, valid_loader, train_loader_kwargs = dataio_prepare(hparams)
brain = BestRQBrain(
modules=hparams["modules"],
opt_class=hparams["optimizer"],
hparams=hparams,
run_opts=run_opts,
checkpointer=hparams["checkpointer"],
)
# with torch.autograd.detect_anomaly():
brain.fit(
brain.hparams.epoch_counter,
train_dataset,
valid_loader,
train_loader_kwargs=train_loader_kwargs,
progressbar=True,
)
wandb.finish()
if __name__ == "__main__":
main()