def _sample(zs, labels, sampling_kwargs, priors, sample_levels, hps): alignments = None for level in reversed(sample_levels): prior = priors[level] prior.cuda() empty_cache() # Set correct total_length, hop_length, labels and sampling_kwargs for level assert hps.sample_length % prior.raw_to_tokens == 0, f"Expected sample_length {hps.sample_length} to be multiple of {prior.raw_to_tokens}" total_length = hps.sample_length//prior.raw_to_tokens hop_length = int(hps.hop_fraction[level]*prior.n_ctx) zs = sample_level(zs, labels[level], sampling_kwargs[level], level, prior, total_length, hop_length, hps) prior.cpu() empty_cache() # Decode sample x = prior.decode(zs[level:], start_level=level, bs_chunks=zs[level].shape[0]) logdir = f"{hps.name}/level_{level}" if not os.path.exists(logdir): os.makedirs(logdir) t.save(dict(zs=zs, labels=labels, sampling_kwargs=sampling_kwargs, x=x), f"{logdir}/data.pth.tar") save_wav(logdir, x, hps.sr) if alignments is None and priors[-1] is not None and priors[-1].n_tokens > 0: alignments = get_alignment(x, zs, labels[-1], priors[-1], sampling_kwargs[-1]['fp16'], hps) save_html(logdir, x, zs, labels[-1], alignments, hps) return zs
def _sample(zs, labels, sampling_kwargs, priors, sample_levels, hps): alignments = None for level in reversed(sample_levels): prior = priors[level] prior.cuda() empty_cache() # Set correct total_length, hop_length, labels and sampling_kwargs for level assert hps.sample_length % prior.raw_to_tokens == 0, f"Expected sample_length {hps.sample_length} to be multiple of {prior.raw_to_tokens}" total_length = hps.sample_length // prior.raw_to_tokens hop_length = int(hps.hop_fraction[level] * prior.n_ctx) zs = sample_level(zs, labels[level], sampling_kwargs[level], level, prior, total_length, hop_length, hps) prior.cpu() empty_cache() # Decode sample x = prior.decode(zs[level:], start_level=level, bs_chunks=zs[level].shape[0]) if dist.get_world_size() > 1: logdir = f"{hps.name}_rank_{dist.get_rank()}/level_{level}" else: logdir = f"{hps.name}/level_{level}" if not os.path.exists(logdir): os.makedirs(logdir) t.save( dict(zs=zs, labels=labels, sampling_kwargs=sampling_kwargs, x=x), f"{logdir}/data.pth.tar") save_wav(logdir, x, hps.sr) #if alignments is None and priors[-1] is not None and priors[-1].n_tokens > 0 and not isinstance(priors[-1].labeller, EmptyLabeller): #alignments = get_alignment(x, zs, labels[-1], priors[-1], sampling_kwargs[-1]['fp16'], hps) lepath = hps.name if level == 2: for filex in glob(os.path.join(lepath + '/level_2', 'item_*.wav')): os.rename(filex, filex.replace('item_', lepath.split('/')[-1] + '-')) if level == 1: for filex in glob(os.path.join(lepath + '/level_1', 'item_*.wav')): os.rename( filex, filex.replace('item_', lepath.split('/')[-1] + '-L1-')) if level == 0: for filex in glob(os.path.join(lepath + '/level_0', 'item_*.wav')): os.rename( filex, filex.replace('item_', lepath.split('/')[-1] + '-L0-')) #save_html(logdir, x, zs, labels[-1], alignments, hps) return zs
def _sample(zs, labels_1, labels_2, sampling_kwargs, priors, sample_levels, hps): alignments = None for level in reversed(sample_levels): prior = priors[level] prior.cuda() empty_cache() # Set correct total_length, hop_length, labels and sampling_kwargs for level assert hps.sample_length % prior.raw_to_tokens == 0, f"Expected sample_length {hps.sample_length} to be multiple of {prior.raw_to_tokens}" total_length = hps.sample_length // prior.raw_to_tokens hop_length = int(hps.hop_fraction[level] * prior.n_ctx) zs = sample_level(zs, labels_1[level], labels_2[level], sampling_kwargs[level], level, prior, total_length, hop_length, hps) prior.cpu() empty_cache() # Decode sample x = prior.decode(zs[level:], start_level=level, bs_chunks=zs[level].shape[0]) if dist.get_world_size() > 1: logdir = f"{hps.name}_rank_{dist.get_rank()}/level_{level}" else: logdir = f"{hps.name}/level_{level}" if not os.path.exists(logdir): os.makedirs(logdir) t.save( dict(zs=zs, labels=labels_1, sampling_kwargs=sampling_kwargs, x=x), f"{logdir}/data.pth.tar") save_wav(logdir, x, hps.sr) if alignments is None and priors[ -1] is not None and priors[-1].n_tokens > 0 and not isinstance( priors[-1].labeller, EmptyLabeller): try: labels_1[-1], priors[-1], sampling_kwargs[-1]['fp16'] except: import ipdb ipdb.set_trace() alignments = get_alignment(x, zs, labels_1[-1], priors[-1], sampling_kwargs[-1]['fp16'], hps) # don't care # save_html(logdir, x, zs, labels_1[-1], alignments, hps) return zs
def sample_level(zs, labels, sampling_kwargs, level, prior, total_length, hop_length, hps): print_once(f"Sampling level {level}") if total_length >= prior.n_ctx: starts = get_starts(total_length, prior.n_ctx, hop_length) counterr = 0 x = None for start in starts: counterr += 1 datea = datetime.now() zs = sample_single_window(zs, labels, sampling_kwargs, level, prior, start, hps) if newtosample and counterr < len(starts): del x x = None prior.cpu() empty_cache() x = prior.decode(zs[level:], start_level=level, bs_chunks=zs[level].shape[0]) logdir = f"{hps.name}/level_{level}" if not os.path.exists(logdir): os.makedirs(logdir) t.save( dict(zs=zs, labels=labels, sampling_kwargs=sampling_kwargs, x=x), f"{logdir}/data.pth.tar") save_wav(logdir, x, hps.sr) del x prior.cuda() empty_cache() x = None dateb = datetime.now() timex = ((dateb - datea).total_seconds() / 60.0) * (len(starts) - counterr) print(f"Step " + colored(counterr, 'blue') + "/" + colored(len(starts), 'red') + " ~ New to Sample: " + str(newtosample) + " ~ estimated remaining minutes: " + (colored('???', 'yellow'), colored(timex, 'magenta'))[counterr > 1 and newtosample]) else: zs = sample_partial_window(zs, labels, sampling_kwargs, level, prior, total_length, hps) return zs