Spaces:
Running
on
Zero
Running
on
Zero
File size: 42,823 Bytes
b5eac81 |
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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 |
# import pytorch_lightning as pl
import lightning as L
from lightning.pytorch.callbacks import Callback
import sys, gc
import random
import torch
import torchaudio
import typing as tp
import wandb
# from beartype.typing import Tuple
from aeiou.viz import pca_point_cloud, audio_spectrogram_image, tokens_spectrogram_image
import auraloss
from ema_pytorch import EMA
from einops import rearrange
from safetensors.torch import save_file
from torch import optim
from torch.nn import functional as F
from pytorch_lightning.utilities.rank_zero import rank_zero_only
from ..inference.sampling import get_alphas_sigmas, sample, sample_discrete_euler
from ..models.diffusion import DiffusionModelWrapper, ConditionedDiffusionModelWrapper
from ..models.autoencoders import DiffusionAutoencoder
from .autoencoders import create_loss_modules_from_bottleneck
from .losses import AuralossLoss, MSELoss, MultiLoss
from .utils import create_optimizer_from_config, create_scheduler_from_config, mask_from_frac_lengths, generate_mask, generate_channel_mask
import os
from pathlib import Path
from time import time
import numpy as np
class Profiler:
def __init__(self):
self.ticks = [[time(), None]]
def tick(self, msg):
self.ticks.append([time(), msg])
def __repr__(self):
rep = 80 * "=" + "\n"
for i in range(1, len(self.ticks)):
msg = self.ticks[i][1]
ellapsed = self.ticks[i][0] - self.ticks[i - 1][0]
rep += msg + f": {ellapsed*1000:.2f}ms\n"
rep += 80 * "=" + "\n\n\n"
return rep
class DiffusionUncondTrainingWrapper(L.LightningModule):
'''
Wrapper for training an unconditional audio diffusion model (like Dance Diffusion).
'''
def __init__(
self,
model: DiffusionModelWrapper,
lr: float = 1e-4,
pre_encoded: bool = False
):
super().__init__()
self.diffusion = model
self.diffusion_ema = EMA(
self.diffusion.model,
beta=0.9999,
power=3/4,
update_every=1,
update_after_step=1
)
self.lr = lr
self.rng = torch.quasirandom.SobolEngine(1, scramble=True)
loss_modules = [
MSELoss("v",
"targets",
weight=1.0,
name="mse_loss"
)
]
self.losses = MultiLoss(loss_modules)
self.pre_encoded = pre_encoded
def configure_optimizers(self):
return optim.Adam([*self.diffusion.parameters()], lr=self.lr)
def training_step(self, batch, batch_idx):
reals = batch[0]
if reals.ndim == 4 and reals.shape[0] == 1:
reals = reals[0]
diffusion_input = reals
loss_info = {}
if not self.pre_encoded:
loss_info["audio_reals"] = diffusion_input
if self.diffusion.pretransform is not None:
if not self.pre_encoded:
with torch.set_grad_enabled(self.diffusion.pretransform.enable_grad):
diffusion_input = self.diffusion.pretransform.encode(diffusion_input)
else:
# Apply scale to pre-encoded latents if needed, as the pretransform encode function will not be run
if hasattr(self.diffusion.pretransform, "scale") and self.diffusion.pretransform.scale != 1.0:
diffusion_input = diffusion_input / self.diffusion.pretransform.scale
loss_info["reals"] = diffusion_input
# Draw uniformly distributed continuous timesteps
t = self.rng.draw(reals.shape[0])[:, 0].to(self.device)
# Calculate the noise schedule parameters for those timesteps
alphas, sigmas = get_alphas_sigmas(t)
# Combine the ground truth data and the noise
alphas = alphas[:, None, None]
sigmas = sigmas[:, None, None]
noise = torch.randn_like(diffusion_input)
noised_inputs = diffusion_input * alphas + noise * sigmas
targets = noise * alphas - diffusion_input * sigmas
with torch.amp.autocast('cuda'):
v = self.diffusion(noised_inputs, t)
loss_info.update({
"v": v,
"targets": targets
})
loss, losses = self.losses(loss_info)
log_dict = {
'train/loss': loss.detach(),
'train/std_data': diffusion_input.std(),
}
for loss_name, loss_value in losses.items():
log_dict[f"train/{loss_name}"] = loss_value.detach()
self.log_dict(log_dict, prog_bar=True, on_step=True)
return loss
def on_before_zero_grad(self, *args, **kwargs):
self.diffusion_ema.update()
def export_model(self, path, use_safetensors=False):
self.diffusion.model = self.diffusion_ema.ema_model
if use_safetensors:
save_file(self.diffusion.state_dict(), path)
else:
torch.save({"state_dict": self.diffusion.state_dict()}, path)
class DiffusionUncondDemoCallback(Callback):
def __init__(self,
demo_every=2000,
num_demos=8,
demo_steps=250,
sample_rate=48000
):
super().__init__()
self.demo_every = demo_every
self.num_demos = num_demos
self.demo_steps = demo_steps
self.sample_rate = sample_rate
self.last_demo_step = -1
@rank_zero_only
@torch.no_grad()
def on_train_batch_end(self, trainer, module, outputs, batch, batch_idx):
if (trainer.global_step - 1) % self.demo_every != 0 or self.last_demo_step == trainer.global_step:
return
self.last_demo_step = trainer.global_step
demo_samples = module.diffusion.sample_size
if module.diffusion.pretransform is not None:
demo_samples = demo_samples // module.diffusion.pretransform.downsampling_ratio
noise = torch.randn([self.num_demos, module.diffusion.io_channels, demo_samples]).to(module.device)
try:
with torch.amp.autocast('cuda'):
fakes = sample(module.diffusion_ema, noise, self.demo_steps, 0)
if module.diffusion.pretransform is not None:
fakes = module.diffusion.pretransform.decode(fakes)
# Put the demos together
fakes = rearrange(fakes, 'b d n -> d (b n)')
log_dict = {}
filename = f'demo_{trainer.global_step:08}.wav'
fakes = fakes.to(torch.float32).div(torch.max(torch.abs(fakes))).mul(32767).to(torch.int16).cpu()
torchaudio.save(filename, fakes, self.sample_rate)
log_dict[f'demo'] = wandb.Audio(filename,
sample_rate=self.sample_rate,
caption=f'Reconstructed')
log_dict[f'demo_melspec_left'] = wandb.Image(audio_spectrogram_image(fakes))
trainer.logger.experiment.log(log_dict)
del fakes
except Exception as e:
print(f'{type(e).__name__}: {e}')
finally:
gc.collect()
torch.cuda.empty_cache()
class DiffusionInfillTrainingWrapper(L.LightningModule):
'''
Wrapper for training an unconditional audio diffusion model (like Dance Diffusion).
'''
def __init__(
self,
model: ConditionedDiffusionModelWrapper,
lr: float = 1e-4,
optimizer_configs: dict = None,
pre_encoded: bool = False,
frac_lengths_mask = (0.7, 1.),
min_span_len = 10,
timestep_sampler: tp.Literal["uniform", "logit_normal"] = "uniform",
diffusion_objective = 'rectified_flow',
ctx_drop: float = 0.1,
r_drop: float = 0.0,
):
super().__init__()
self.diffusion = model
self.diffusion_ema = EMA(
self.diffusion.model,
beta=0.9999,
power=3/4,
update_every=1,
update_after_step=1
)
if optimizer_configs is None:
optimizer_configs = {
"diffusion": {
"optimizer": {
"type": "Adam",
"config": {
"lr": lr
}
}
}
}
else:
if lr is not None:
print(f"WARNING: learning_rate and optimizer_configs both specified in config. Ignoring learning_rate and using optimizer_configs.")
self.optimizer_configs = optimizer_configs
self.rng = torch.quasirandom.SobolEngine(1, scramble=True)
self.frac_lengths_mask = frac_lengths_mask
self.min_span_len = min_span_len
self.timestep_sampler = timestep_sampler
self.ctx_drop = ctx_drop
self.r_drop = r_drop
self.diffusion_objective = diffusion_objective
print(f'Training in the {diffusion_objective} formulation')
loss_modules = [
MSELoss("v",
"targets",
weight=1.0,
name="mse_loss",
mask_key="mask"
)
]
self.losses = MultiLoss(loss_modules)
self.pre_encoded = pre_encoded
def configure_optimizers(self):
diffusion_opt_config = self.optimizer_configs['diffusion']
opt_diff = create_optimizer_from_config(diffusion_opt_config['optimizer'], self.diffusion.parameters())
if "scheduler" in diffusion_opt_config:
sched_diff = create_scheduler_from_config(diffusion_opt_config['scheduler'], opt_diff)
sched_diff_config = {
"scheduler": sched_diff,
"interval": "step"
}
return [opt_diff], [sched_diff_config]
return [opt_diff]
def training_step(self, batch, batch_idx):
reals, metadata = batch
if reals.ndim == 4 and reals.shape[0] == 1:
reals = reals[0]
# import ipdb
# ipdb.set_trace()
p_drop = torch.rand(1).item()
# r_drop = torch.rand(1).item()
# if p_drop >= self.ctx_drop and self.r_drop > 0.0 and r_drop < self.r_drop:
# generate_channel_mask(reals)
diffusion_input = reals
assert torch.all(torch.isfinite(diffusion_input)), "Non-finite values detected in diffusion_input"
p = Profiler()
loss_info = {}
if not self.pre_encoded:
loss_info["audio_reals"] = diffusion_input
p.tick("setup")
conditioning = {}
p.tick("conditioning")
if self.diffusion.pretransform is not None:
if not self.pre_encoded:
with torch.set_grad_enabled(self.diffusion.pretransform.enable_grad):
diffusion_input = self.diffusion.pretransform.encode(diffusion_input)
else:
# Apply scale to pre-encoded latents if needed, as the pretransform encode function will not be run
if hasattr(self.diffusion.pretransform, "scale") and self.diffusion.pretransform.scale != 1.0:
diffusion_input = diffusion_input / self.diffusion.pretransform.scale
loss_info["reals"] = diffusion_input
if self.timestep_sampler == "uniform":
# Draw uniformly distributed continuous timesteps
t = self.rng.draw(reals.shape[0])[:, 0].to(self.device)
elif self.timestep_sampler == "logit_normal":
t = torch.sigmoid(torch.randn(reals.shape[0], device=self.device))
# # Calculate the noise schedule parameters for those timesteps
# alphas, sigmas = get_alphas_sigmas(t)
# Calculate the noise schedule parameters for those timesteps
if self.diffusion_objective == "v":
alphas, sigmas = get_alphas_sigmas(t)
elif self.diffusion_objective == "rectified_flow":
alphas, sigmas = 1-t, t
# Combine the ground truth data and the noise
alphas = alphas[:, None, None]
sigmas = sigmas[:, None, None]
noise = torch.randn_like(diffusion_input)
noised_inputs = diffusion_input * alphas + noise * sigmas
# x_ctx = diffusion_input.detach().clone().transpose(1,2)
bsz, dim, seq_len = diffusion_input.shape
if p_drop < self.ctx_drop:
ctx_mask = torch.ones((bsz, seq_len), device = diffusion_input.device, dtype = torch.bool)
# elif self.r_drop > 0.0 and r_drop < self.r_drop:
# ctx_mask = torch.zeros((bsz, seq_len), device=diffusion_input.device, dtype=torch.bool)
else:
# 计算 frac_lengths 提前使用
frac_lengths = torch.zeros((bsz,), device=diffusion_input.device).uniform_(*self.frac_lengths_mask)
# if self.r_drop > 0.0 and r_drop < self.r_drop:
# import ipdb
# ipdb.set_trace()
# ctx_mask = torch.zeros((bsz, seq_len), device=diffusion_input.device, dtype=torch.bool)
# else:
ctx_mask = generate_mask(bsz, seq_len, frac_lengths, self.min_span_len)
if ctx_mask.dim() == 2:
ctx_mask = ctx_mask.unsqueeze(1)
masked_sequence = diffusion_input * ~ctx_mask
conditioning['x_ctx'] = [masked_sequence]
if self.diffusion_objective == "v":
targets = noise * alphas - diffusion_input * sigmas
elif self.diffusion_objective == "rectified_flow":
targets = noise - diffusion_input
with torch.amp.autocast('cuda'):
p.tick("amp")
v = self.diffusion(noised_inputs, t, cond=conditioning)
p.tick("diffusion")
loss_info.update({
"v": v,
"targets": targets,
"mask": ctx_mask.squeeze(-1)
})
# import ipdb
# ipdb.set_trace()
loss, losses = self.losses(loss_info)
log_dict = {
'train/loss': loss.detach(),
'train/std_data': diffusion_input.std(),
'train/lr': self.trainer.optimizers[0].param_groups[0]['lr']
}
for loss_name, loss_value in losses.items():
log_dict[f"train/{loss_name}"] = loss_value.detach()
self.log_dict(log_dict, prog_bar=True, on_step=True)
p.tick("log")
return loss
def on_before_zero_grad(self, *args, **kwargs):
self.diffusion_ema.update()
def export_model(self, path, use_safetensors=False):
self.diffusion.model = self.diffusion_ema.ema_model
if use_safetensors:
save_file(self.diffusion.state_dict(), path)
else:
torch.save({"state_dict": self.diffusion.state_dict()}, path)
class DiffusionInfillDemoCallback(Callback):
def __init__(self,
demo_dl,
demo_every=2000,
num_demos=8,
demo_steps=250,
sample_rate=48000
):
super().__init__()
self.demo_dl = iter(demo_dl)
self.demo_every = demo_every
self.num_demos = num_demos
self.demo_steps = demo_steps
self.sample_rate = sample_rate
self.last_demo_step = -1
@rank_zero_only
@torch.no_grad()
def on_train_batch_end(self, trainer, module, outputs, batch, batch_idx):
if (trainer.global_step - 1) % self.demo_every != 0 or self.last_demo_step == trainer.global_step:
return
self.last_demo_step = trainer.global_step
try:
demo_reals, _ = next(self.demo_dl)
# Remove extra dimension added by WebDataset
if demo_reals.ndim == 4 and demo_reals.shape[0] == 1:
demo_reals = demo_reals[0]
demo_reals = demo_reals.to(module.device)
reals = demo_reals
log_dict = {}
if not module.pre_encoded:
# Log the real audio
log_dict[f'demo_reals_melspec_left'] = wandb.Image(audio_spectrogram_image(rearrange(demo_reals, "b d n -> d (b n)").mul(32767).to(torch.int16).cpu()))
# log_dict[f'demo_reals'] = wandb.Audio(rearrange(demo_reals, "b d n -> d (b n)").mul(32767).to(torch.int16).cpu(), sample_rate=self.sample_rate, caption="demo reals")
if module.diffusion.pretransform is not None:
module.diffusion.pretransform.to(module.device)
with torch.amp.autocast('cuda'):
demo_reals = module.diffusion.pretransform.encode(demo_reals)
demo_samples = demo_reals.shape[2]
# Get conditioning
conditioning = {}
noise = torch.randn([demo_reals.shape[0], module.diffusion.io_channels, demo_samples]).to(module.device)
frac_lengths = torch.zeros((demo_reals.shape[0],), device = module.device).uniform_(*(0.3,0.5))
ctx_mask = generate_mask(demo_reals.shape[0],demo_reals.shape[2], frac_lengths, module.min_span_len)
# x_ctx = (demo_reals * ~ctx_mask.unsqueeze(1)).transpose(1,2)
x_ctx = demo_reals * ~ctx_mask.unsqueeze(1)
conditioning['x_ctx'] = [x_ctx]
# x_ctx_mask = x_ctx * ~ctx_mask.unsqueeze(-1)
if module.diffusion.pretransform is not None:
log_dict[f'demo_masked_input'] = wandb.Image(tokens_spectrogram_image(x_ctx.cpu()))
else:
log_dict[f'demo_masked_input'] = wandb.Image(audio_spectrogram_image(rearrange(x_ctx, "b c t -> c (b t)").mul(32767).to(torch.int16).cpu()))
cond_inputs = module.diffusion.get_conditioning_inputs(conditioning)
with torch.amp.autocast('cuda'):
if module.diffusion_objective == "v":
fakes = sample(module.diffusion_ema, noise, self.demo_steps, 0)
elif module.diffusion_objective == "rectified_flow":
fakes = sample_discrete_euler(module.diffusion_ema, noise, self.demo_steps, **cond_inputs)
# fakes = sample(module.diffusion_ema, noise, self.demo_steps, 0)
if module.diffusion.pretransform is not None:
fakes = module.diffusion.pretransform.decode(fakes)
# #Interleave reals and fakes
# reals_fakes = rearrange([reals, fakes], 'i b d n -> (b i) d n')
# Put the demos together
fakes = rearrange(fakes, 'b d n -> d (b n)')
filename = f'results/audio_ssl/demo_ssl_{trainer.global_step:08}.wav'
os.makedirs(Path(filename).parent,exist_ok=True)
fakes = fakes.to(torch.float32).div(torch.max(torch.abs(fakes))).mul(32767).to(torch.int16).cpu()
torchaudio.save(filename, fakes, self.sample_rate)
log_dict[f'demo'] = wandb.Audio(filename,
sample_rate=self.sample_rate,
caption=f'Reconstructed')
log_dict[f'demo_melspec_left'] = wandb.Image(audio_spectrogram_image(fakes))
trainer.logger.experiment.log(log_dict)
del fakes
except Exception as e:
print(f'{type(e).__name__}: {e}')
finally:
gc.collect()
torch.cuda.empty_cache()
class DiffusionCondTrainingWrapper(L.LightningModule):
'''
Wrapper for training a conditional audio diffusion model.
'''
def __init__(
self,
model: ConditionedDiffusionModelWrapper,
lr: float = None,
mask_padding: bool = False,
mask_padding_dropout: float = 0.0,
use_ema: bool = True,
log_loss_info: bool = False,
optimizer_configs: dict = None,
diffusion_objective: tp.Literal["rectified_flow", "v"] = "v",
pre_encoded: bool = False,
cfg_dropout_prob = 0.1,
timestep_sampler: tp.Literal["uniform", "logit_normal"] = "uniform",
max_mask_segments = 0,
):
super().__init__()
self.diffusion = model
if use_ema:
self.diffusion_ema = EMA(
self.diffusion.model,
beta=0.9999,
power=3/4,
update_every=1,
update_after_step=1,
include_online_model=False
)
else:
self.diffusion_ema = None
self.mask_padding = mask_padding
self.mask_padding_dropout = mask_padding_dropout
self.cfg_dropout_prob = cfg_dropout_prob
self.rng = torch.quasirandom.SobolEngine(1, scramble=True)
self.timestep_sampler = timestep_sampler
self.diffusion_objective = model.diffusion_objective
print(f'Training in the {self.diffusion_objective} formulation with timestep sampler: {timestep_sampler}')
self.max_mask_segments = max_mask_segments
self.loss_modules = [
MSELoss("output",
"targets",
weight=1.0,
mask_key="padding_mask" if self.mask_padding else None,
name="mse_loss"
)
]
self.losses = MultiLoss(self.loss_modules)
self.log_loss_info = log_loss_info
assert lr is not None or optimizer_configs is not None, "Must specify either lr or optimizer_configs in training config"
if optimizer_configs is None:
optimizer_configs = {
"diffusion": {
"optimizer": {
"type": "Adam",
"config": {
"lr": lr
}
}
}
}
else:
if lr is not None:
print(f"WARNING: learning_rate and optimizer_configs both specified in config. Ignoring learning_rate and using optimizer_configs.")
self.optimizer_configs = optimizer_configs
self.pre_encoded = pre_encoded
def configure_optimizers(self):
diffusion_opt_config = self.optimizer_configs['diffusion']
opt_diff = create_optimizer_from_config(diffusion_opt_config['optimizer'], self.diffusion.parameters())
if "scheduler" in diffusion_opt_config:
sched_diff = create_scheduler_from_config(diffusion_opt_config['scheduler'], opt_diff)
sched_diff_config = {
"scheduler": sched_diff,
"interval": "step"
}
return [opt_diff], [sched_diff_config]
return [opt_diff]
def training_step(self, batch, batch_idx):
reals, metadata = batch
# import ipdb
# ipdb.set_trace()
p = Profiler()
if reals.ndim == 4 and reals.shape[0] == 1:
reals = reals[0]
loss_info = {}
diffusion_input = reals
if not self.pre_encoded:
loss_info["audio_reals"] = diffusion_input
p.tick("setup")
with torch.amp.autocast('cuda'):
conditioning = self.diffusion.conditioner(metadata, self.device)
video_exist = torch.stack([item['video_exist'] for item in metadata],dim=0)
conditioning['metaclip_features'][~video_exist] = self.diffusion.model.model.empty_clip_feat
conditioning['sync_features'][~video_exist] = self.diffusion.model.model.empty_sync_feat
# If mask_padding is on, randomly drop the padding masks to allow for learning silence padding
use_padding_mask = self.mask_padding and random.random() > self.mask_padding_dropout
# Create batch tensor of attention masks from the "mask" field of the metadata array
if use_padding_mask:
padding_masks = torch.stack([md["padding_mask"][0] for md in metadata], dim=0).to(self.device) # Shape (batch_size, sequence_length)
p.tick("conditioning")
if self.diffusion.pretransform is not None:
self.diffusion.pretransform.to(self.device)
if not self.pre_encoded:
with torch.amp.autocast('cuda') and torch.set_grad_enabled(self.diffusion.pretransform.enable_grad):
self.diffusion.pretransform.train(self.diffusion.pretransform.enable_grad)
diffusion_input = self.diffusion.pretransform.encode(diffusion_input)
p.tick("pretransform")
# If mask_padding is on, interpolate the padding masks to the size of the pretransformed input
if use_padding_mask:
padding_masks = F.interpolate(padding_masks.unsqueeze(1).float(), size=diffusion_input.shape[2], mode="nearest").squeeze(1).bool()
else:
# Apply scale to pre-encoded latents if needed, as the pretransform encode function will not be run
if hasattr(self.diffusion.pretransform, "scale") and self.diffusion.pretransform.scale != 1.0:
diffusion_input = diffusion_input / self.diffusion.pretransform.scale
if self.max_mask_segments > 0:
# Max mask size is the full sequence length
max_mask_length = diffusion_input.shape[2]
# Create a mask of random length for a random slice of the input
masked_input, mask = self.random_mask(diffusion_input, max_mask_length)
conditioning['inpaint_mask'] = [mask]
conditioning['inpaint_masked_input'] = masked_input
if self.timestep_sampler == "uniform":
# Draw uniformly distributed continuous timesteps
t = self.rng.draw(reals.shape[0])[:, 0].to(self.device)
elif self.timestep_sampler == "logit_normal":
t = torch.sigmoid(torch.randn(reals.shape[0], device=self.device))
# import ipdb
# ipdb.set_trace()
# Calculate the noise schedule parameters for those timesteps
if self.diffusion_objective == "v":
alphas, sigmas = get_alphas_sigmas(t)
elif self.diffusion_objective == "rectified_flow":
alphas, sigmas = 1-t, t
# Combine the ground truth data and the noise
alphas = alphas[:, None, None]
sigmas = sigmas[:, None, None]
noise = torch.randn_like(diffusion_input)
noised_inputs = diffusion_input * alphas + noise * sigmas
if self.diffusion_objective == "v":
targets = noise * alphas - diffusion_input * sigmas
elif self.diffusion_objective == "rectified_flow":
targets = noise - diffusion_input
p.tick("noise")
extra_args = {}
if use_padding_mask:
extra_args["mask"] = padding_masks
with torch.amp.autocast('cuda'):
p.tick("amp")
output = self.diffusion(noised_inputs, t, cond=conditioning, cfg_dropout_prob = self.cfg_dropout_prob, **extra_args)
p.tick("diffusion")
loss_info.update({
"output": output,
"targets": targets,
"padding_mask": padding_masks if use_padding_mask else None,
})
loss, losses = self.losses(loss_info)
p.tick("loss")
if self.log_loss_info:
# Loss debugging logs
num_loss_buckets = 10
bucket_size = 1 / num_loss_buckets
loss_all = F.mse_loss(output, targets, reduction="none")
sigmas = rearrange(self.all_gather(sigmas), "w b c n -> (w b) c n").squeeze()
# gather loss_all across all GPUs
loss_all = rearrange(self.all_gather(loss_all), "w b c n -> (w b) c n")
# Bucket loss values based on corresponding sigma values, bucketing sigma values by bucket_size
loss_all = torch.stack([loss_all[(sigmas >= i) & (sigmas < i + bucket_size)].mean() for i in torch.arange(0, 1, bucket_size).to(self.device)])
# Log bucketed losses with corresponding sigma bucket values, if it's not NaN
debug_log_dict = {
f"model/loss_all_{i/num_loss_buckets:.1f}": loss_all[i].detach() for i in range(num_loss_buckets) if not torch.isnan(loss_all[i])
}
self.log_dict(debug_log_dict)
log_dict = {
'train/loss': loss.detach(),
'train/std_data': diffusion_input.std(),
'train/lr': self.trainer.optimizers[0].param_groups[0]['lr']
}
for loss_name, loss_value in losses.items():
log_dict[f"train/{loss_name}"] = loss_value.detach()
self.log_dict(log_dict, prog_bar=True, on_step=True)
p.tick("log")
#print(f"Profiler: {p}")
return loss
def validation_step(self, batch, batch_idx):
reals, metadata = batch
# breakpoint()
if reals.ndim == 4 and reals.shape[0] == 1:
reals = reals[0]
loss_info = {}
diffusion_input = reals
if not self.pre_encoded:
loss_info["audio_reals"] = diffusion_input
with torch.amp.autocast('cuda'):
conditioning = self.diffusion.conditioner(metadata, self.device)
video_exist = torch.stack([item['video_exist'] for item in metadata],dim=0)
conditioning['metaclip_features'][~video_exist] = self.diffusion.model.model.empty_clip_feat
conditioning['sync_features'][~video_exist] = self.diffusion.model.model.empty_sync_feat
if self.diffusion.pretransform is not None:
if not self.pre_encoded:
self.diffusion.pretransform.to(self.device)
with torch.amp.autocast('cuda') and torch.set_grad_enabled(self.diffusion.pretransform.enable_grad):
self.diffusion.pretransform.train(self.diffusion.pretransform.enable_grad)
diffusion_input = self.diffusion.pretransform.encode(diffusion_input)
else:
# Apply scale to pre-encoded latents if needed, as the pretransform encode function will not be run
if hasattr(self.diffusion.pretransform, "scale") and self.diffusion.pretransform.scale != 1.0:
diffusion_input = diffusion_input / self.diffusion.pretransform.scale
if self.max_mask_segments > 0:
# Max mask size is the full sequence length
max_mask_length = diffusion_input.shape[2]
# Create a mask of random length for a random slice of the input
masked_input, mask = self.random_mask(diffusion_input, max_mask_length)
conditioning['inpaint_mask'] = [mask]
conditioning['inpaint_masked_input'] = masked_input
if self.timestep_sampler == "uniform":
# Draw uniformly distributed continuous timesteps
t = self.rng.draw(reals.shape[0])[:, 0].to(self.device)
elif self.timestep_sampler == "logit_normal":
t = torch.sigmoid(torch.randn(reals.shape[0], device=self.device))
# Calculate the noise schedule parameters for those timesteps
if self.diffusion_objective == "v":
alphas, sigmas = get_alphas_sigmas(t)
elif self.diffusion_objective == "rectified_flow":
alphas, sigmas = 1-t, t
# Combine the ground truth data and the noise
alphas = alphas[:, None, None]
sigmas = sigmas[:, None, None]
noise = torch.randn_like(diffusion_input)
noised_inputs = diffusion_input * alphas + noise * sigmas
if self.diffusion_objective == "v":
targets = noise * alphas - diffusion_input * sigmas
elif self.diffusion_objective == "rectified_flow":
targets = noise - diffusion_input
with torch.amp.autocast('cuda'):
output = self.diffusion(noised_inputs, t, cond=conditioning, cfg_dropout_prob = 0.0)
loss_info.update({
"output": output,
"targets": targets,
})
loss, losses = self.losses(loss_info)
log_dict = {
'val_loss': loss.detach(),
}
self.log_dict(log_dict, prog_bar=True, batch_size=diffusion_input.size(0))
def predict_step(self, batch, batch_idx):
reals, metadata = batch
ids = [item['id'] for item in metadata]
batch_size, length = reals.shape[0], reals.shape[2]
print(f"Predicting {batch_size} samples with length {length} for ids: {ids}")
with torch.amp.autocast('cuda'):
conditioning = self.diffusion.conditioner(metadata, self.device)
video_exist = torch.stack([item['video_exist'] for item in metadata],dim=0)
conditioning['metaclip_features'][~video_exist] = self.diffusion.model.model.empty_clip_feat
conditioning['sync_features'][~video_exist] = self.diffusion.model.model.empty_sync_feat
cond_inputs = self.diffusion.get_conditioning_inputs(conditioning)
if batch_size > 1:
noise_list = []
for _ in range(batch_size):
noise_1 = torch.randn([1, self.diffusion.io_channels, length]).to(self.device) # 每次生成推进RNG状态
noise_list.append(noise_1)
noise = torch.cat(noise_list, dim=0)
else:
noise = torch.randn([batch_size, self.diffusion.io_channels, length]).to(self.device)
with torch.amp.autocast('cuda'):
model = self.diffusion.model
if self.diffusion_objective == "v":
fakes = sample(model, noise, 24, 0, **cond_inputs, cfg_scale=5, batch_cfg=True)
elif self.diffusion_objective == "rectified_flow":
import time
start_time = time.time()
fakes = sample_discrete_euler(model, noise, 24, **cond_inputs, cfg_scale=5, batch_cfg=True)
end_time = time.time()
execution_time = end_time - start_time
print(f"执行时间: {execution_time:.2f} 秒")
if self.diffusion.pretransform is not None:
fakes = self.diffusion.pretransform.decode(fakes)
audios = fakes.to(torch.float32).div(torch.max(torch.abs(fakes))).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
return audios
# # Put the demos together
# fakes = rearrange(fakes, 'b d n -> d (b n)')
def random_mask(self, sequence, max_mask_length):
b, _, sequence_length = sequence.size()
# Create a mask tensor for each batch element
masks = []
for i in range(b):
mask_type = random.randint(0, 2)
if mask_type == 0: # Random mask with multiple segments
num_segments = random.randint(1, self.max_mask_segments)
max_segment_length = max_mask_length // num_segments
segment_lengths = random.sample(range(1, max_segment_length + 1), num_segments)
mask = torch.ones((1, 1, sequence_length))
for length in segment_lengths:
mask_start = random.randint(0, sequence_length - length)
mask[:, :, mask_start:mask_start + length] = 0
elif mask_type == 1: # Full mask
mask = torch.zeros((1, 1, sequence_length))
elif mask_type == 2: # Causal mask
mask = torch.ones((1, 1, sequence_length))
mask_length = random.randint(1, max_mask_length)
mask[:, :, -mask_length:] = 0
mask = mask.to(sequence.device)
masks.append(mask)
# Concatenate the mask tensors into a single tensor
mask = torch.cat(masks, dim=0).to(sequence.device)
# Apply the mask to the sequence tensor for each batch element
masked_sequence = sequence * mask
return masked_sequence, mask
def on_before_zero_grad(self, *args, **kwargs):
if self.diffusion_ema is not None:
self.diffusion_ema.update()
def export_model(self, path, use_safetensors=False):
if self.diffusion_ema is not None:
self.diffusion.model = self.diffusion_ema.ema_model
if use_safetensors:
save_file(self.diffusion.state_dict(), path)
else:
torch.save({"state_dict": self.diffusion.state_dict()}, path)
class DiffusionCondDemoCallback(Callback):
def __init__(self,
demo_every=2000,
num_demos=8,
sample_size=65536,
demo_steps=250,
sample_rate=48000,
demo_conditioning: tp.Optional[tp.Dict[str, tp.Any]] = {},
demo_cfg_scales: tp.Optional[tp.List[int]] = [3, 5, 7],
demo_cond_from_batch: bool = False,
display_audio_cond: bool = False
):
super().__init__()
self.demo_every = demo_every
self.num_demos = num_demos
self.demo_samples = sample_size
self.demo_steps = demo_steps
self.sample_rate = sample_rate
self.last_demo_step = -1
self.demo_conditioning = demo_conditioning
self.demo_cfg_scales = demo_cfg_scales
# If true, the callback will use the metadata from the batch to generate the demo conditioning
self.demo_cond_from_batch = demo_cond_from_batch
# If true, the callback will display the audio conditioning
self.display_audio_cond = display_audio_cond
@rank_zero_only
@torch.no_grad()
def on_train_batch_end(self, trainer, module: DiffusionCondTrainingWrapper, outputs, batch, batch_idx):
if (trainer.global_step - 1) % self.demo_every != 0 or self.last_demo_step == trainer.global_step:
return
module.eval()
print(f"Generating demo")
self.last_demo_step = trainer.global_step
demo_samples = self.demo_samples
demo_cond = self.demo_conditioning
if self.demo_cond_from_batch:
# Get metadata from the batch
demo_cond = batch[1][:self.num_demos]
if '.pth' in demo_cond[0]:
demo_cond_data = []
for path in demo_cond:
# info = {}
data = torch.load(path, weights_only=True)
if 'caption_t5' not in data.keys():
data['caption_t5'] = data['caption']
data['seconds_start'] = 0
data['seconds_total'] = 10
demo_cond_data.append(data)
demo_cond = demo_cond_data
elif '.npz' in demo_cond[0]:
demo_cond_data = []
for path in demo_cond:
# info = {}
npz_data = np.load(path,allow_pickle=True)
data = {key: npz_data[key] for key in npz_data.files}
for key in data.keys():
# print(key)
if isinstance(data[key], np.ndarray) and np.issubdtype(data[key].dtype, np.number):
data[key] = torch.from_numpy(data[key])
demo_cond_data.append(data)
demo_cond = demo_cond_data
if module.diffusion.pretransform is not None:
demo_samples = demo_samples // module.diffusion.pretransform.downsampling_ratio
noise = torch.randn([self.num_demos, module.diffusion.io_channels, demo_samples]).to(module.device)
try:
print("Getting conditioning")
with torch.amp.autocast('cuda'):
conditioning = module.diffusion.conditioner(demo_cond, module.device)
cond_inputs = module.diffusion.get_conditioning_inputs(conditioning)
log_dict = {}
if self.display_audio_cond:
audio_inputs = torch.cat([cond["audio"] for cond in demo_cond], dim=0)
audio_inputs = rearrange(audio_inputs, 'b d n -> d (b n)')
filename = f'demo_audio_cond_{trainer.global_step:08}.wav'
audio_inputs = audio_inputs.to(torch.float32).mul(32767).to(torch.int16).cpu()
torchaudio.save(filename, audio_inputs, self.sample_rate)
log_dict[f'demo_audio_cond'] = wandb.Audio(filename, sample_rate=self.sample_rate, caption="Audio conditioning")
log_dict[f"demo_audio_cond_melspec_left"] = wandb.Image(audio_spectrogram_image(audio_inputs))
trainer.logger.experiment.log(log_dict)
for cfg_scale in self.demo_cfg_scales:
print(f"Generating demo for cfg scale {cfg_scale}")
with torch.amp.autocast('cuda'):
# model = module.diffusion_ema.model if module.diffusion_ema is not None else module.diffusion.model
model = module.diffusion.model
if module.diffusion_objective == "v":
fakes = sample(model, noise, self.demo_steps, 0, **cond_inputs, cfg_scale=cfg_scale, batch_cfg=True)
elif module.diffusion_objective == "rectified_flow":
fakes = sample_discrete_euler(model, noise, self.demo_steps, **cond_inputs, cfg_scale=cfg_scale, batch_cfg=True)
if module.diffusion.pretransform is not None:
fakes = module.diffusion.pretransform.decode(fakes)
# Put the demos together
fakes = rearrange(fakes, 'b d n -> d (b n)')
log_dict = {}
filename = f'demos/demo_cfg_{cfg_scale}_{trainer.global_step:08}.wav'
fakes = fakes.div(torch.max(torch.abs(fakes))).mul(32767).to(torch.int16).cpu()
torchaudio.save(filename, fakes, self.sample_rate)
log_dict[f'demo_cfg_{cfg_scale}'] = wandb.Audio(filename,
sample_rate=self.sample_rate,
caption=f'Reconstructed')
log_dict[f'demo_melspec_left_cfg_{cfg_scale}'] = wandb.Image(audio_spectrogram_image(fakes))
trainer.logger.experiment.log(log_dict)
del fakes
except Exception as e:
raise e
finally:
gc.collect()
torch.cuda.empty_cache()
module.train()
|