forked from acids-ircam/RAVE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_rave_accel.py
executable file
·163 lines (133 loc) · 5.73 KB
/
train_rave_accel.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
#! /usr/bin/env python3
import accelerate
import torch
from torch.utils.data import DataLoader, random_split
from tqdm import tqdm
from rave.model_accel import RAVE, Profiler
from rave.core import random_phase_mangle, EMAModelCheckPoint
from rave.core import search_for_run
#from udls import SimpleDataset, simple_audio_preprocess
#from effortless_config import Config
#from rave.audiodata import AudioDataset
from aeiou.datasets import AudioDataset
from prefigure.prefigure import get_all_args, push_wandb_config
from os import environ, path
import numpy as np
from torch import multiprocessing as mp
import GPUtil as gpu
from udls.transforms import Compose, RandomApply, Dequantize, RandomCrop
import wandb
if __name__ == "__main__":
args = get_all_args()
torch.manual_seed(args.seed)
try:
mp.set_start_method(args.start_method)
except RuntimeError:
pass
accelerator = accelerate.Accelerator()
device = accelerator.device
print('Using device:', device, flush=True)
# special parsing for arg lists (TODO: could add this functionality to prefigure later):
args.ratios = eval(''.join(args.ratios))
args.transforms = eval(args.transforms)
print("args = ",args)
assert args.name is not None
model = RAVE(data_size=args.data_size,
capacity=args.capacity,
latent_size=args.latent_size,
ratios=args.ratios,
bias=args.bias,
loud_stride=args.loud_stride,
use_noise=args.use_noise,
noise_ratios=args.noise_ratios,
noise_bands=args.noise_bands,
d_capacity=args.d_capacity,
d_multiplier=args.d_multiplier,
d_n_layers=args.d_n_layers,
warmup=args.warmup,
mode=args.mode,
no_latency=args.no_latency,
sr=args.sr,
min_kl=args.min_kl,
max_kl=args.max_kl,
cropped_latent_size=args.cropped_latent_size,
feature_match=args.feature_match,
device=accelerator.device)
gen_opt, dis_opt = model.configure_optimizers()
if True: # new aeiou dataset class
dataset = AudioDataset(args.wav, sample_size=args.n_signal, sample_rate=args.sr, augs=args.augs, load_frac=args.load_frac)
else: # antoine's old class that called preprocessing for you.
dataset = SimpleDataset(
args.preprocessed,
args.wav,
extension="*.wav,*.aif,*.flac",
preprocess_function=simple_audio_preprocess(args.sr, 2 * args.n_signal),
split_set="full",
transforms=Compose( args.transforms ),
)
train = DataLoader(dataset, args.batch, True, drop_last=True, num_workers=8)
model, gen_opt, dis_opt, train = accelerator.prepare(model, gen_opt, dis_opt, train)
if accelerator.is_main_process:
x = torch.zeros(args.batch, 2**14).to(device)
accelerator.unwrap_model(model).validation_step(x)
use_wandb = accelerator.is_main_process and args.name
if use_wandb:
import wandb
config = vars(args) # dict(args)
#config['params'] = utils.n_params(model)
wandb.init(project=args.name, config=config, save_code=True)
if use_wandb:
wandb.watch(model)
step = 0
epoch = 0
try:
while step < args.max_steps:
for batch in tqdm(train, disable=not accelerator.is_main_process):
#print(f"\nbatch.shape = {batch.shape}",flush=True)
#print(f"batch = {batch}",flush=True)
p = Profiler()
model_unwrap = accelerator.unwrap_model(model)
p.tick("unwrap model")
loss_gen, loss_dis, log_dict = model_unwrap.training_step(batch, step)
p.tick("training step")
#sched.step()
# OPTIMIZATION
if step % 2 and model_unwrap.warmed_up:
dis_opt.zero_grad()
loss_dis.backward()
dis_opt.step()
p.tick("dis_opt step")
else:
gen_opt.zero_grad()
loss_gen.backward()
gen_opt.step()
p.tick("gen_opt step")
if accelerator.is_main_process:
if step % 500 == 0:
tqdm.write(f'Epoch: {epoch}, step: {step}, loss: {loss_gen.item():g}')
if use_wandb and step % 50 == 0:
log_dict['epoch'] = epoch
# log_dict['loss'] = loss.item(),
# log_dict['lr'] = sched.get_last_lr()[0]
wandb.log(log_dict, step=step)
p.tick("logging")
output = model_unwrap.validation_step(batch.detach())
p.tick("Validation")
if step % args.val_every == 0:
model_unwrap.validation_epoch_end([output])
p.tick("Demo")
# if step > 0 and step % args.checkpoint_every == 0:
# save()
#print(p)
step += 1
epoch += 1
except RuntimeError as err:
# Error reporting / detect faulty GPUs on AWS cluster
import requests
import datetime
ts = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
resp = requests.get('http://169.254.169.254/latest/meta-data/instance-id')
print(f'ERROR at {ts} on {resp.text} {device}: {type(err).__name__}: {err}', flush=True)
raise err
except KeyboardInterrupt:
pass