File size: 78,143 Bytes
0a2e9f2 | 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 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 | '''
calculate diffusion loss according to different diffusion formula
'''
import torch
from diffusers import DDPMScheduler, PNDMScheduler, DPMSolverMultistepScheduler, FlowMatchEulerDiscreteScheduler
from opensora.utils.parallel_states import initialize_sequence_parallel_state, \
destroy_sequence_parallel_group, get_sequence_parallel_state, set_sequence_parallel_state
from opensora.utils.communications import prepare_parallel_data, broadcast
from diffusers.training_utils import compute_snr
from diffusers.utils.torch_utils import randn_tensor
from torch.nn import functional as F
import numpy as np
from opensora.train.edm import EDMPrecond, EDMLoss
from opensora.sample.pyramid_flow_matching_scheduler import AutoRegressivePyramidFlowMatchEulerDiscreteScheduler
from functools import partial
from copy import deepcopy
from einops import rearrange
import random
def compute_loss_weighting_for_sd3(weighting_scheme: str, sigmas=None):
"""Computes loss weighting scheme for SD3 training.
Courtesy: This was contributed by Rafie Walker in https://github.com/huggingface/diffusers/pull/8528.
SD3 paper reference: https://arxiv.org/abs/2403.03206v1.
"""
if weighting_scheme == "sigma_sqrt":
weighting = (sigmas**-2.0).float()
elif weighting_scheme == "cosmap":
bot = 1 - 2 * sigmas + 2 * sigmas**2
weighting = 2 / (math.pi * bot)
else:
weighting = torch.ones_like(sigmas)
return weighting
def compute_density_for_timestep_sampling(
weighting_scheme: str, batch_size: int, logit_mean: float = None, logit_std: float = None, mode_scale: float = None
):
"""Compute the density for sampling the timesteps when doing SD3 training.
Courtesy: This was contributed by Rafie Walker in https://github.com/huggingface/diffusers/pull/8528.
SD3 paper reference: https://arxiv.org/abs/2403.03206v1.
"""
if weighting_scheme == "logit_normal":
# See 3.1 in the SD3 paper ($rf/lognorm(0.00,1.00)$).
u = torch.normal(mean=logit_mean, std=logit_std, size=(batch_size,), device="cpu")
u = torch.nn.functional.sigmoid(u)
elif weighting_scheme == "mode":
u = torch.rand(size=(batch_size,), device="cpu")
u = 1 - u - mode_scale * (torch.cos(math.pi * u / 2) ** 2 - 1 + u)
else:
u = torch.rand(size=(batch_size,), device="cpu")
return u
try:
from diffusers.pipelines.cogvideo.pipeline_cogvideox import get_resize_crop_region_for_grid
from diffusers.models.embeddings import get_3d_rotary_pos_embed
except:
pass
def prepare_rotary_positional_embeddings(
height: int,
width: int,
num_frames: int,
vae_scale_factor_spatial: int = 8,
patch_size: int = 2,
attention_head_dim: int = 64,
device = None,
dtype = None,
base_height: int = 480,
base_width: int = 720,
):
grid_height = height // (vae_scale_factor_spatial * patch_size)
grid_width = width // (vae_scale_factor_spatial * patch_size)
base_size_width = base_width // (vae_scale_factor_spatial * patch_size)
base_size_height = base_height // (vae_scale_factor_spatial * patch_size)
grid_crops_coords = get_resize_crop_region_for_grid((grid_height, grid_width), base_size_width, base_size_height)
freqs_cos, freqs_sin = get_3d_rotary_pos_embed(
embed_dim=attention_head_dim,
crops_coords=grid_crops_coords,
grid_size=(grid_height, grid_width),
temporal_size=num_frames,
)
freqs_cos = freqs_cos.to(device=device, dtype=dtype)
freqs_sin = freqs_sin.to(device=device, dtype=dtype)
return freqs_cos, freqs_sin
class CustomDiffusion():
def __init__(self):
from diffusers.schedulers import CogVideoXDDIMScheduler, CogVideoXDPMScheduler
#TODO:优化了
self.noise_scheduler = CogVideoXDPMScheduler.from_config("/CogVideoX-5b-I2V/scheduler/scheduler_config.json")
def get_loss(self, model, model_input, model_kwargs, args, accelerator, **kwargs):
model.train()
if args.task == 'i2v':
model_input, image_latents = model_input
image_latents = image_latents.permute(0, 2, 1, 3, 4)
model_input = model_input.permute(0, 2, 1, 3, 4)
dtype = model_input.dtype
bsz = model_input.shape[0]
current_step_frame = model_input.shape[1] #注意是B T C H W
height, width = model_input.shape[3:]
model_config = model.module.config if hasattr(model, 'module') else model.config
patch_size_t = model_config.patch_size_t
additional_frames = 0
if patch_size_t is not None: #TODO: not sure,对齐训练测试
additional_frames = patch_size_t - current_step_frame % patch_size_t
pading_frames = model_input[:, :additional_frames]
model_input = torch.cat([pading_frames, model_input], dim=1)
# image_latents = torch.cat([image_latents[:, :additional_frames], image_latents], dim=1)
padding_shape = (model_input.shape[0], model_input.shape[1]-1, *model_input.shape[2:])
latent_padding = image_latents.new_zeros(padding_shape)
image_latents = torch.cat([image_latents, latent_padding], dim=1)
noise = torch.randn_like(model_input)
timesteps = torch.randint(0, self.noise_scheduler.config.num_train_timesteps, (bsz,), device=model_input.device)
noisy_video_latents = self.noise_scheduler.add_noise(model_input, noise, timesteps)
model_config = model.module.config if hasattr(model, 'module') else model.config
ofs_emb = None if model_config.ofs_embed_dim is None else noisy_video_latents.new_full((1,), fill_value=2.0)
if args.task == 't2v':
noisy_model_input = noisy_video_latents
elif args.task == 'i2v':
noisy_model_input = torch.cat([noisy_video_latents, image_latents], dim=2)
model_output = model(
hidden_states = noisy_model_input,
encoder_hidden_states = model_kwargs['encoder_hidden_states'],
encoder_attention_mask = model_kwargs['encoder_attention_mask'],
timestep = timesteps,
return_dict=False,
ofs = ofs_emb,
)[0]
model_pred = self.noise_scheduler.get_velocity(model_output, noisy_video_latents, timesteps) #[:, additional_frames:]
alphas_cumprod = self.noise_scheduler.alphas_cumprod[timesteps]
weights = 1 / (1 - alphas_cumprod)
while len(weights.shape) < len(model_pred.shape):
weights = weights.unsqueeze(-1)
target = model_input #[:, additional_frames:]
loss = torch.mean((weights * (model_pred - target) ** 2).reshape(bsz, -1), dim=1)
loss = loss.mean()
return loss
def get_loss_controlnet(self, condition, controlnet, model, model_input, model_kwargs, args, accelerator, **kwargs):
controlnet.train()
if args.task == 'i2v':
model_input, image_latents = model_input
image_latents = image_latents.permute(0, 2, 1, 3, 4)
model_input = model_input.permute(0, 2, 1, 3, 4)
dtype = model_input.dtype
bsz = model_input.shape[0]
current_step_frame = model_input.shape[1] #注意是B T C H W
height, width = model_input.shape[3:]
model_config = model.module.config if hasattr(model, 'module') else model.config
patch_size_t = model_config.patch_size_t
additional_frames = 0
if patch_size_t is not None: #TODO: not sure,对齐训练测试
additional_frames = patch_size_t - current_step_frame % patch_size_t
pading_frames = model_input[:, :additional_frames]
model_input = torch.cat([pading_frames, model_input], dim=1)
# image_latents = torch.cat([image_latents[:, :additional_frames], image_latents], dim=1)
padding_shape = (model_input.shape[0], model_input.shape[1]-1, *model_input.shape[2:])
latent_padding = image_latents.new_zeros(padding_shape)
image_latents = torch.cat([image_latents, latent_padding], dim=1)
noise = torch.randn_like(model_input)
timesteps = torch.randint(0, self.noise_scheduler.config.num_train_timesteps, (bsz,), device=model_input.device)
noisy_video_latents = self.noise_scheduler.add_noise(model_input, noise, timesteps)
model_config = model.module.config if hasattr(model, 'module') else model.config
ofs_emb = None if model_config.ofs_embed_dim is None else noisy_video_latents.new_full((1,), fill_value=2.0)
if args.task == 't2v':
noisy_model_input = noisy_video_latents
elif args.task == 'i2v':
noisy_model_input = torch.cat([noisy_video_latents, image_latents], dim=2)
controlnet_states = controlnet(
hidden_states=noisy_model_input,
encoder_hidden_states=model_kwargs['encoder_hidden_states'],
controlnet_cond=condition,
timestep=timesteps,
return_dict=False,
)[0]
if isinstance(controlnet_states, (tuple, list)):
controlnet_states = [x.to(dtype=noisy_video_latents.dtype) for x in controlnet_states]
else:
controlnet_states = controlnet_states.to(dtype=noisy_video_latents.dtype)
model_output = model(
hidden_states = noisy_model_input,
encoder_hidden_states = model_kwargs['encoder_hidden_states'],
encoder_attention_mask = model_kwargs['encoder_attention_mask'],
timestep = timesteps,
return_dict=False,
ofs = ofs_emb,
controlnet_states = controlnet_states,
controlnet_weights = 1.0
)[0]
model_pred = self.noise_scheduler.get_velocity(model_output, noisy_video_latents, timesteps) #[:, additional_frames:]
alphas_cumprod = self.noise_scheduler.alphas_cumprod[timesteps]
weights = 1 / (1 - alphas_cumprod)
while len(weights.shape) < len(model_pred.shape):
weights = weights.unsqueeze(-1)
target = model_input #[:, additional_frames:]
loss = torch.mean((weights * (model_pred - target) ** 2).reshape(bsz, -1), dim=1)
loss = loss.mean()
return loss
@torch.no_grad()
def get_valid_loss_controlnet(self, condition, controlnet, model, model_input, model_kwargs, args, **kwargs):
if args.task == 'i2v':
model_input, image_latents = model_input
image_latents = image_latents.permute(0, 2, 1, 3, 4)
model_input = model_input.permute(0, 2, 1, 3, 4)
dtype = model_input.dtype
bsz = model_input.shape[0]
current_step_frame = model_input.shape[1] #注意是B T C H W
height, width = model_input.shape[3:]
model_config = model.module.config if hasattr(model, 'module') else model.config
patch_size_t = model_config.patch_size_t
additional_frames = 0
if patch_size_t is not None: #TODO: not sure
additional_frames = patch_size_t - current_step_frame % patch_size_t
pading_frames = model_input[:, :additional_frames]
model_input = torch.cat([pading_frames, model_input], dim=1)
# image_latents = torch.cat([image_latents[:, :additional_frames], image_latents], dim=1)
padding_shape = (model_input.shape[0], model_input.shape[1]-1, *model_input.shape[2:])
latent_padding = image_latents.new_zeros(padding_shape)
image_latents = torch.cat([image_latents, latent_padding], dim=1)
evaluate_steps = kwargs['evaluate_steps'] # valid多少个timesteps的结果
generator = torch.torch.Generator(device = model_input.device)
generator.manual_seed(42)
loss_list = []
for step_i in range(evaluate_steps):
generator = torch.torch.Generator(device = model_input.device)
generator.manual_seed(42+step_i)
timesteps = torch.ones((bsz,), dtype=int) * (step_i * self.noise_scheduler.config.num_train_timesteps // evaluate_steps)
timesteps = timesteps.to(model_input.device)
noise = torch.randn_like(model_input)
noisy_video_latents = self.noise_scheduler.add_noise(model_input, noise, timesteps)
ofs_emb = None if model_config.ofs_embed_dim is None else noisy_video_latents.new_full((1,), fill_value=2.0)
if args.task == 't2v':
noisy_model_input = noisy_video_latents
elif args.task == 'i2v':
noisy_model_input = torch.cat([noisy_video_latents, image_latents], dim=2)
controlnet_states = controlnet(
hidden_states=noisy_model_input,
encoder_hidden_states=model_kwargs['encoder_hidden_states'],
controlnet_cond=condition,
timestep=timesteps,
return_dict=False,
)[0]
if isinstance(controlnet_states, (tuple, list)):
controlnet_states = [x.to(dtype=model_kwargs["weight_dtype"]) for x in controlnet_states]
else:
controlnet_states = controlnet_states.to(dtype=model_kwargs["weight_dtype"])
model_output = model(
hidden_states = noisy_model_input,
encoder_hidden_states = model_kwargs['encoder_hidden_states'],
encoder_attention_mask = model_kwargs['encoder_attention_mask'],
timestep = timesteps,
return_dict=False,
ofs = ofs_emb,
controlnet_states = controlnet_states,
controlnet_weights = args.controlnet_weights
)[0]
model_pred = self.noise_scheduler.get_velocity(model_output, noisy_video_latents, timesteps) #[:, additional_frames:]
alphas_cumprod = self.noise_scheduler.alphas_cumprod[timesteps]
weights = 1 / (1 - alphas_cumprod)
while len(weights.shape) < len(model_pred.shape):
weights = weights.unsqueeze(-1)
target = model_input #[:, additional_frames:]
loss = torch.mean((weights * (model_pred - target) ** 2).reshape(bsz, -1), dim=1)
loss = loss.mean()
loss_list.append(loss)
loss_list = torch.stack(loss_list)
return loss_list
@torch.no_grad()
def get_valid_loss(self, model, model_input, model_kwargs, args, **kwargs):
if args.task == 'i2v':
model_input, image_latents = model_input
image_latents = image_latents.permute(0, 2, 1, 3, 4)
model_input = model_input.permute(0, 2, 1, 3, 4)
dtype = model_input.dtype
bsz = model_input.shape[0]
current_step_frame = model_input.shape[1] #注意是B T C H W
height, width = model_input.shape[3:]
model_config = model.module.config if hasattr(model, 'module') else model.config
patch_size_t = model_config.patch_size_t
additional_frames = 0
if patch_size_t is not None: #TODO: not sure
additional_frames = patch_size_t - current_step_frame % patch_size_t
pading_frames = model_input[:, :additional_frames]
model_input = torch.cat([pading_frames, model_input], dim=1)
# image_latents = torch.cat([image_latents[:, :additional_frames], image_latents], dim=1)
padding_shape = (model_input.shape[0], model_input.shape[1]-1, *model_input.shape[2:])
latent_padding = image_latents.new_zeros(padding_shape)
image_latents = torch.cat([image_latents, latent_padding], dim=1)
evaluate_steps = kwargs['evaluate_steps'] # valid多少个timesteps的结果
generator = torch.torch.Generator(device = model_input.device)
generator.manual_seed(42)
loss_list = []
for step_i in range(evaluate_steps):
generator = torch.torch.Generator(device = model_input.device)
generator.manual_seed(42+step_i)
timesteps = torch.ones((bsz,), dtype=int) * (step_i * self.noise_scheduler.config.num_train_timesteps // evaluate_steps)
timesteps = timesteps.to(model_input.device)
noise = torch.randn_like(model_input)
noisy_video_latents = self.noise_scheduler.add_noise(model_input, noise, timesteps)
ofs_emb = None if model_config.ofs_embed_dim is None else noisy_video_latents.new_full((1,), fill_value=2.0)
if args.task == 't2v':
noisy_model_input = noisy_video_latents
elif args.task == 'i2v':
noisy_model_input = torch.cat([noisy_video_latents, image_latents], dim=2)
model_output = model(
hidden_states = noisy_model_input,
encoder_hidden_states = model_kwargs['encoder_hidden_states'],
encoder_attention_mask = model_kwargs['encoder_attention_mask'],
timestep = timesteps,
return_dict=False,
ofs = ofs_emb,
)[0]
model_pred = self.noise_scheduler.get_velocity(model_output, noisy_video_latents, timesteps) #[:, additional_frames:]
alphas_cumprod = self.noise_scheduler.alphas_cumprod[timesteps]
weights = 1 / (1 - alphas_cumprod)
while len(weights.shape) < len(model_pred.shape):
weights = weights.unsqueeze(-1)
target = model_input #[:, additional_frames:]
loss = torch.mean((weights * (model_pred - target) ** 2).reshape(bsz, -1), dim=1)
loss = loss.mean()
loss_list.append(loss)
loss_list = torch.stack(loss_list)
return loss_list
class CogVideoXDDPM():
def __init__(self, model_path, ori_height, ori_width, vae_scale_factor_spatial):
from diffusers import CogVideoXDPMScheduler
#TODO: create schedulers
self.noise_scheduler = CogVideoXDPMScheduler.from_pretrained(model_path, subfolder = 'scheduler')
self.ori_height = ori_height
self.ori_width = ori_width
self.vae_scale_factor_spatial = vae_scale_factor_spatial
def get_loss(self, model, model_input, model_kwargs, args, accelerator, **kwargs):
model.train()
if args.task == 'i2v':
model_input, image_latents = model_input
dtype = model_input.dtype
noise = torch.randn_like(model_input)
bsz = model_input.shape[0]
current_step_frame = model_input.shape[1]
height, width = model_input.shape[3:]
timesteps = torch.randint(0, self.noise_scheduler.config.num_train_timesteps, (bsz,), device=model_input.device)
noisy_video_latents = self.noise_scheduler.add_noise(model_input, noise, timesteps)
model_config = model.module.config if hasattr(model, 'module') else model.config
image_rotary_emb = (
prepare_rotary_positional_embeddings(
height=self.ori_height, #cogvideox固定分辨率
width=self.ori_width,
num_frames=current_step_frame,
vae_scale_factor_spatial=self.vae_scale_factor_spatial,
patch_size=model_config.patch_size,
attention_head_dim=model_config.attention_head_dim,
device=accelerator.device,
dtype=dtype
)
if model_config.use_rotary_positional_embeddings
else None
)
if args.task == 't2v':
noisy_model_input = noisy_video_latents
elif args.task == 'i2v':
noisy_model_input = torch.cat([noisy_video_latents, image_latents], dim=2)
model_output = model(
hidden_states = noisy_model_input,
encoder_hidden_states = model_kwargs['encoder_hidden_states'],
timestep = timesteps,
image_rotary_emb = image_rotary_emb,
return_dict=False,
)[0]
model_pred = self.noise_scheduler.get_velocity(model_output, noisy_video_latents, timesteps)
alphas_cumprod = self.noise_scheduler.alphas_cumprod[timesteps]
weights = 1 / (1 - alphas_cumprod)
while len(weights.shape) < len(model_pred.shape):
weights = weights.unsqueeze(-1)
target = model_input
loss = torch.mean((weights * (model_pred - target) ** 2).reshape(bsz, -1), dim=1)
loss = loss.mean()
return loss
class DDPM():
def __init__(self, use_flow=False):
if not use_flow:
self.noise_scheduler = DDPMScheduler()
else:
self.noise_scheduler = FlowMatchEulerDiscreteScheduler()
self.noise_scheduler_copy = deepcopy(self.noise_scheduler)
print(f"====> use rectified flow !!!")
self.use_flow = use_flow
# 前向运算计算loss
def get_loss(self, model, model_input, model_kwargs, args, **kwargs):
dtype = model_input.dtype
noise = torch.randn_like(model_input)
# if args.noise_offset:
# # https://www.crosslabs.org//blog/diffusion-with-offset-noise
# noise += args.noise_offset * torch.randn((model_input.shape[0], model_input.shape[1], 1, 1, 1),
# device=model_input.device)
bsz = model_input.shape[0]
current_step_frame = model_input.shape[2]
if args.reflow:
# Sample a random timestep for each image
# for weighting schemes where we sample timesteps non-uniformly
u = compute_density_for_timestep_sampling(
weighting_scheme=args.weighting_scheme,
batch_size=bsz,
logit_mean=args.logit_mean,
logit_std=args.logit_std,
mode_scale=args.mode_scale,
)
indices = (u * self.noise_scheduler_copy.config.num_train_timesteps).long()
timesteps = self.noise_scheduler_copy.timesteps[indices].to(device=model_input.device)
else:
# Sample a random timestep for each image without bias.
timesteps = torch.randint(0, self.noise_scheduler.config.num_train_timesteps, (bsz,), device=model_input.device)
if current_step_frame != 1 and get_sequence_parallel_state(): # image do not need sp
broadcast(timesteps)
if self.use_flow:
sigmas = self.noise_scheduler_copy.sigmas.to(device=model_input.device, dtype=dtype)
schedule_timesteps = self.noise_scheduler_copy.timesteps.to(model_input.device)
timesteps = timesteps.to(model_input.device)
step_indices = [(schedule_timesteps == t).nonzero().item() for t in timesteps]
sigma = sigmas[step_indices].flatten()
while len(sigma.shape) < model_input.ndim:
sigma = sigma.unsqueeze(-1)
sigmas = sigma
noisy_model_input = (1.0 - sigmas) * model_input + sigmas * noise
else:
noisy_model_input = self.noise_scheduler.add_noise(model_input, noise, timesteps)
model_pred = model(
noisy_model_input,
timesteps,
**model_kwargs
)[0]
if args.reflow:
# model_pred = model_pred * (-sigmas) + noisy_model_input
# these weighting schemes use a uniform timestep sampling
# and instead post-weight the loss
weighting = compute_loss_weighting_for_sd3(weighting_scheme=args.weighting_scheme, sigmas=sigmas)
if args.prediction_type is not None:
# set prediction_type of scheduler if defined
self.noise_scheduler.register_to_config(prediction_type=args.prediction_type)
if hasattr(self.noise_scheduler.config, 'prediction_type'):
if self.noise_scheduler.config.prediction_type == "epsilon":
target = noise
elif self.noise_scheduler.config.prediction_type == "v_prediction":
target = self.noise_scheduler.get_velocity(model_input, noise, timesteps)
elif self.noise_scheduler.config.prediction_type == "sample":
# We set the target to latents here, but the model_pred will return the noise sample prediction.
target = model_input
# We will have to subtract the noise residual from the prediction to get the target sample.
model_pred = model_pred - noise
else:
raise ValueError(f"Unknown prediction type {self.noise_scheduler.config.prediction_type}")
else: # reflow
# target = model_input
target = noise - model_input
mask = model_kwargs.get('attention_mask', None)
if torch.all(mask.bool()):
mask = None
assert mask is None
b, c, t_shard, _, _ = model_pred.shape
if mask is not None:
# 实际上不知道这里是哪里,因此需要所有帧的attention mask是一样的,确保在时序上不会补帧即可
mask = mask.unsqueeze(1).repeat(1, c, 1, 1, 1).float()[:, :, -t_shard:, :, :] # b t h w -> b c t h w
mask = mask.reshape(b, -1)
if args.reflow:
# Compute regular loss.
loss = (weighting.float() * (model_pred.float() - target.float()) ** 2).reshape(target.shape[0], -1)
if mask is not None:
loss = (loss * mask).sum() / mask.sum() # mean loss on unpad patches
else:
loss = loss.mean()
else:
if args.snr_gamma is None:
# model_pred: b c t h w, attention_mask: b t h w
loss = F.mse_loss(model_pred.float(), target.float(), reduction="none")
loss = loss.reshape(b, -1)
if mask is not None:
loss = (loss * mask).sum() / mask.sum() # mean loss on unpad patches
else:
loss = loss.mean()
else:
# Compute loss-weights as per Section 3.4 of https://arxiv.org/abs/2303.09556.
# Since we predict the noise instead of x_0, the original formulation is slightly changed.
# This is discussed in Section 4.2 of the same paper.
snr = compute_snr(self.noise_scheduler, timesteps)
mse_loss_weights = torch.stack([snr, args.snr_gamma * torch.ones_like(timesteps)], dim=1).min(
dim=1
)[0]
if self.noise_scheduler.config.prediction_type == "epsilon":
mse_loss_weights = mse_loss_weights / snr
elif self.noise_scheduler.config.prediction_type == "v_prediction":
mse_loss_weights = mse_loss_weights / (snr + 1)
loss = F.mse_loss(model_pred.float(), target.float(), reduction="none")
loss = loss.reshape(b, -1)
mse_loss_weights = mse_loss_weights.reshape(b, 1)
if mask is not None:
loss = (loss * mask * mse_loss_weights).sum() / mask.sum() # mean loss on unpad patches
else:
loss = (loss * mse_loss_weights).mean()
return loss
class FlowMatching():
def __init__(self):
self.noise_scheduler = FlowMatchEulerDiscreteScheduler(
shift=1.0,
num_train_timesteps=1000,
use_dynamic_shifting = False
)
# hunyuan
def get_loss(self, model, model_input, model_kwargs, args, **kwargs):
model.train()
if args.task == 'i2v':
model_input, image_latents = model_input
padding_shape = (model_input.shape[0], model_input.shape[1], model_input.shape[2]-1, *model_input.shape[3:])
latent_padding = image_latents.new_zeros(padding_shape)
image_latents = torch.cat([image_latents, latent_padding], dim=2)
dtype = model_input.dtype
noise = torch.randn_like(model_input)
bsz = model_input.shape[0]
current_step_frame = model_input.shape[2]
u = compute_density_for_timestep_sampling(
weighting_scheme=args.weighting_scheme,
batch_size=bsz,
logit_mean=args.logit_mean,
logit_std=args.logit_std,
mode_scale=args.mode_scale,
)
indices = (u * self.noise_scheduler.config.num_train_timesteps).long()
timesteps = self.noise_scheduler.timesteps[indices].to(device=model_input.device)
sigmas = self.noise_scheduler.sigmas.to(device=model_input.device, dtype=dtype)
schedule_timesteps = self.noise_scheduler.timesteps.to(model_input.device)
timesteps = timesteps.to(model_input.device)
step_indices = [(schedule_timesteps == t).nonzero().item() for t in timesteps]
sigma = sigmas[step_indices].flatten()
while len(sigma.shape) < model_input.ndim:
sigma = sigma.unsqueeze(-1)
sigmas = sigma
if not self.noise_scheduler.config.invert_sigmas:
noisy_video_latents = (1.0 - sigmas) * model_input + sigmas * noise
else:
noisy_video_latents = sigmas * model_input + (1 - sigmas) * noise
if args.task == 't2v':
noisy_model_input = noisy_video_latents
elif args.task == 'i2v':
noisy_model_input = torch.cat([noisy_video_latents, image_latents], dim=1)
# no image_emb (pay attention)
model_pred = model(
hidden_states = noisy_model_input,
encoder_hidden_states = model_kwargs['encoder_hidden_states'],
encoder_attention_mask = model_kwargs['encoder_attention_mask'],
embed_cfg_scale = model_kwargs['batch_embed_scale'],
timestep = timesteps,
return_dict=False,
)[0]
weighting = compute_loss_weighting_for_sd3(weighting_scheme=args.weighting_scheme, sigmas=sigmas)
if not self.noise_scheduler.config.invert_sigmas:
target = noise - model_input
else:
target = model_input - noise
loss = (weighting.float() * (model_pred.float() - target.float()) ** 2).reshape(target.shape[0], -1)
loss = loss.mean()
return loss
# hunyuan
def get_loss_controlnet(self, condition, model, model_input, model_kwargs, args, **kwargs):
model.train()
if args.task == 'i2v':
model_input, image_latents = model_input
if args.repeat:
image_latents = image_latents.repeat(1, 1, model_input.shape[2], 1, 1)
else:
padding_shape = (model_input.shape[0], model_input.shape[1], model_input.shape[2]-1, *model_input.shape[3:])
latent_padding = image_latents.new_zeros(padding_shape)
image_latents = torch.cat([image_latents, latent_padding], dim=2)
dtype = model_input.dtype
noise = torch.randn_like(model_input)
bsz = model_input.shape[0]
current_step_frame = model_input.shape[2]
u = compute_density_for_timestep_sampling(
weighting_scheme=args.weighting_scheme,
batch_size=bsz,
logit_mean=args.logit_mean,
logit_std=args.logit_std,
mode_scale=args.mode_scale,
)
indices = (u * self.noise_scheduler.config.num_train_timesteps).long()
timesteps = self.noise_scheduler.timesteps[indices].to(device=model_input.device)
sigmas = self.noise_scheduler.sigmas.to(device=model_input.device, dtype=dtype)
schedule_timesteps = self.noise_scheduler.timesteps.to(model_input.device)
timesteps = timesteps.to(model_input.device)
step_indices = [(schedule_timesteps == t).nonzero().item() for t in timesteps]
sigma = sigmas[step_indices].flatten()
while len(sigma.shape) < model_input.ndim:
sigma = sigma.unsqueeze(-1)
sigmas = sigma
if not self.noise_scheduler.config.invert_sigmas:
noisy_video_latents = (1.0 - sigmas) * model_input + sigmas * noise
else:
noisy_video_latents = sigmas * model_input + (1 - sigmas) * noise
if args.task == 't2v':
noisy_model_input = noisy_video_latents
elif args.task == 'i2v':
noisy_model_input = torch.cat([noisy_video_latents, image_latents], dim=1)
model_pred = model(
hidden_states = noisy_model_input,
encoder_hidden_states = model_kwargs['encoder_hidden_states'],
encoder_attention_mask = model_kwargs['encoder_attention_mask'],
embed_cfg_scale = model_kwargs['batch_embed_scale'],
timestep = timesteps,
controlnet_cond = condition,
conditioning_scale = model_kwargs.get('conditioning_scale',1),
controlnet_blocks_repeat = False, #NOTE:pay attention
return_dict=False,
image_emb = model_kwargs['image_emb'],
image_latents = model_kwargs['image_latents']
)[0]
weighting = compute_loss_weighting_for_sd3(weighting_scheme=args.weighting_scheme, sigmas=sigmas)
if not self.noise_scheduler.config.invert_sigmas:
target = noise - model_input
else:
target = model_input - noise
# add_ocr mask here
b, c, t, h, w = model_input.shape
# print(b, c, t, h, w)
ocr_mask = model_kwargs["ocr_mask"]
ocr_mask = rearrange(ocr_mask, "b c t H W -> (b t) c H W")
# 确保是0,1不会出现其他的值, 插值到latent shape
ocr_mask = F.interpolate(ocr_mask, size=(h, w), mode='nearest')
ocr_mask = rearrange(ocr_mask, "(b t) c h w -> b c t h w", t=t) # c = 1
ocr_mask = ocr_mask.expand(-1, c, -1, -1, -1)
# auto broadcast
loss = (weighting.float() * (model_pred.float() - target.float()) ** 2 * ocr_mask.float())
if args.face_mask:
face_mask = model_kwargs["face_mask"]
face_mask = rearrange(face_mask, "b c t H W -> (b t) c H W")
face_mask = F.interpolate(face_mask, size=(h, w), mode='nearest')
face_mask = rearrange(face_mask, "(b t) c h w -> b c t h w", t=t)
face_mask = face_mask.expand(-1, c, -1, -1, -1)
loss = loss * ((args.face_weight - 1) * face_mask + 1).float()
loss = loss.reshape(target.shape[0], -1).mean()
return loss
# 4.14待完善
def get_loss_wanx(self, condition, model, model_input, model_kwargs, args, **kwargs):
model.train()
if args.task == 'i2v':
model_input, image_latents = model_input
if args.repeat:
image_latents = image_latents.repeat(1, 1, model_input.shape[2], 1, 1)
dtype = model_input.dtype
noise = torch.randn_like(model_input)
bsz = model_input.shape[0]
current_step_frame = model_input.shape[2]
u = compute_density_for_timestep_sampling(
weighting_scheme=args.weighting_scheme,
batch_size=bsz,
logit_mean=args.logit_mean,
logit_std=args.logit_std,
mode_scale=args.mode_scale,
)
indices = (u * self.noise_scheduler.config.num_train_timesteps).long()
timesteps = self.noise_scheduler.timesteps[indices].to(device=model_input.device)
sigmas = self.noise_scheduler.sigmas.to(device=model_input.device, dtype=dtype)
schedule_timesteps = self.noise_scheduler.timesteps.to(model_input.device)
timesteps = timesteps.to(model_input.device)
step_indices = [(schedule_timesteps == t).nonzero().item() for t in timesteps]
sigma = sigmas[step_indices].flatten()
while len(sigma.shape) < model_input.ndim:
sigma = sigma.unsqueeze(-1)
sigmas = sigma
if not self.noise_scheduler.config.invert_sigmas:
noisy_video_latents = (1.0 - sigmas) * model_input + sigmas * noise
else:
noisy_video_latents = sigmas * model_input + (1 - sigmas) * noise
if args.task == 't2v':
noisy_model_input = noisy_video_latents
# add i2v
elif args.task == 'i2v':
noisy_model_input = torch.cat([noisy_video_latents, image_latents], dim=1)
# add token replace
do_token_replace = False
loss_mask = torch.ones_like(model_input)
token_replace_prob = model_kwargs.get('token_replace_prob', 0.0)
num_frames = model_input.shape[2]
num_replace_frames = 2
if random.random() < token_replace_prob and num_frames > num_replace_frames:
do_token_replace = True
loss_mask[:, :, :num_replace_frames, :, :] = 0
noisy_model_input[:, :, :num_replace_frames, :, :] = model_input[:, :, :num_replace_frames, :, :]
model_pred = model(
hidden_states = noisy_model_input,
encoder_hidden_states = model_kwargs['encoder_hidden_states'],
timestep = timesteps,
controlnet_cond = condition,
conditioning_scale = model_kwargs['conditioning_scale'],
controlnet_blocks_repeat = False, #NOTE:pay attention
image_pose = model_kwargs.get('image_pose',None),
return_dict=False,
image_latents = model_kwargs['image_latents'],
token_replace = do_token_replace
)[0]
weighting = compute_loss_weighting_for_sd3(weighting_scheme=args.weighting_scheme, sigmas=sigmas)
if not self.noise_scheduler.config.invert_sigmas:
target = noise - model_input
else:
target = model_input - noise
# add_ocr mask here
b, c, t, h, w = model_input.shape
ocr_mask = model_kwargs["ocr_mask"]
ocr_mask = rearrange(ocr_mask, "b c t H W -> (b t) c H W")
# 确保是0,1不会出现其他的值, 插值到latent shape
ocr_mask = F.interpolate(ocr_mask, size=(h, w), mode='nearest')
ocr_mask = rearrange(ocr_mask, "(b t) c h w -> b c t h w", t=t) # c = 1
ocr_mask = ocr_mask.expand(-1, c, -1, -1, -1)
final_mask = loss_mask.float() * ocr_mask.float()
# auto broadcast
loss = (weighting.float() * (model_pred.float() - target.float()) ** 2 * final_mask)
if args.face_mask:
face_mask = model_kwargs["face_mask"]
face_mask = rearrange(face_mask, "b c t H W -> (b t) c H W")
face_mask = F.interpolate(face_mask, size=(h, w), mode='nearest')
face_mask = rearrange(face_mask, "(b t) c h w -> b c t h w", t=t)
face_mask = face_mask.expand(-1, c, -1, -1, -1)
loss = loss * ((args.face_weight - 1) * face_mask + 1).float()
loss = loss.reshape(target.shape[0], -1).mean()
return loss
class PyramidFlowMatching():
#TODO: 相对SD3中的flow matching,缺少了两个部分,分别是timesteps的非均匀采样,以及loss的weighting机制
def __init__(self):
# self.stages = stages
# self.scheduler = AutoRegressivePyramidFlowMatchEulerDiscreteScheduler(stages = stages, stage_range = stage_range)
from opensora.sample.flow_matching_scheduler import PyramidFlowMatchEulerDiscreteScheduler
self.scheduler = PyramidFlowMatchEulerDiscreteScheduler(stages=3, stage_range=[0, 1/3, 2/3, 1])
self.stages = 3
# 前向运算计算loss
# 暂时的训练逻辑:防止batch_size过大,先每个step计算一个stage,然后累积3步
def get_loss(self, model, model_input, model_kwargs, args, **kwargs):
dtype = model_input.dtype
assert 'stage_index' in kwargs, "passing stage index for pyramid training"
stage_index = kwargs['stage_index'] # need
# 这里对当前latent下采样到当前的stage进行计算,由于patch_size是2,所以下采样之后的长宽需要能够整除2
bsz = model_input.shape[0]
current_step_frame = model_input.shape[2]
height, width = model_input.shape[3:]
resize_scale = 2**(self.stages - stage_index - 1)
resized_height, resized_width = int(height / resize_scale), int(width / resize_scale)
resized_height += resized_height % 2
resized_width += resized_width % 2
model_input = rearrange(model_input, "b c t h w -> (b t) c h w")
model_input = F.interpolate(model_input, size = (resized_height, resized_width), mode = 'nearest')
model_input = rearrange(model_input, "(b t) c h w -> b c t h w", t = current_step_frame)
height, width = model_input.shape[3:]
noise = torch.randn_like(model_input)
import ipdb
ipdb.set_trace()
#TODO: 目前是随机采样
timesteps_indices = torch.randint(0, len(self.scheduler.timesteps_per_stage[stage_index]), (bsz,))
timesteps = self.scheduler.timesteps_per_stage[stage_index][timesteps_indices].to(model_input.device)
sigmas = self.scheduler.sigmas_per_stage[stage_index][timesteps_indices].to(model_input.device)
start_sigma = self.scheduler.start_sigmas[stage_index]
end_sigma = self.scheduler.end_sigmas[stage_index]
stage_start_point = rearrange(model_input, "b c t h w -> (b t) c h w")
stage_start_point = F.interpolate(stage_start_point, size = (int(height//2), int(width//2)), mode = 'bilinear')
stage_start_point = F.interpolate(stage_start_point, size = (height, width), mode = 'nearest')
stage_start_point = rearrange(stage_start_point, "(b t) c h w -> b c t h w", t = current_step_frame)
if self.scheduler.invert_sigmas:
stage_start_point = start_sigma * stage_start_point + (1 - start_sigma) * noise
stage_end_point = end_sigma * model_input + (1 - end_sigma) * noise
else:
stage_start_point = (1 - start_sigma) * stage_start_point + start_sigma * noise
stage_end_point = (1 - end_sigma) * model_input + end_sigma * noise
while len(sigmas.shape) < model_input.ndim:
sigmas = sigmas.unsqueeze(-1)
if self.scheduler.invert_sigmas:
noisy_model_input = (1.0 - sigmas) * stage_start_point + sigmas * stage_end_point
else:
noisy_model_input = sigmas * stage_start_point + (1 - sigmas) * stage_end_point
model_pred = model(
noisy_model_input,
timesteps,
**model_kwargs
)[0]
if args.prediction_type is not None:
# set prediction_type of scheduler if defined
self.noise_scheduler.register_to_config(prediction_type=args.prediction_type)
if self.scheduler.invert_sigmas:
target = stage_end_point - stage_start_point
else:
target = stage_start_point - stage_end_point
b, c, t_shard, _, _ = model_pred.shape
# Compute regular loss.
loss = ((model_pred.float() - target.float()) ** 2).reshape(target.shape[0], -1)
loss = loss.mean()
return loss
class AutoRegressivePyramidFlowMatching():
#TODO: 相对SD3中的flow matching,缺少了两个部分,分别是timesteps的非均匀采样,以及loss的weighting机制
def __init__(self, stages, stage_range, process_index, model_name='arpf', max_unit_num=8, frame_per_unit=1, max_history_len=4, extra_sample_steps=1):
self.stages = stages
self.scheduler = AutoRegressivePyramidFlowMatchEulerDiscreteScheduler(stages = stages, stage_range = stage_range)
self.frame_per_unit = frame_per_unit # 每次训练多少个latent
if model_name == 'arpf':
self.vae_shift_factor = 0.1490
self.vae_scale_factor = 1 / 1.8415
elif model_name == 'flux_arpf':
self.vae_shift_factor = -0.04
self.vae_scale_factor = 1 / 1.8726
# For the video latent
self.vae_video_shift_factor = -0.2343
self.vae_video_scale_factor = 1 / 3.0986
self.max_history_len = max_history_len
assert self.max_history_len > self.stages - 1
self.extra_sample_steps = extra_sample_steps
self.iters = 0
self.process_index = process_index
self.max_unit_num = max_unit_num
self.sample_ratios = [1,2,1]
@torch.no_grad()
def get_pyramid_latent(self, x, stage_num, generator=None):
# TODO: 增加noise
vae_latent_list = []
vae_latent_list.append(x) #当前stage
temp, height, width = x.shape[-3:]
for _ in range(stage_num):
height //= 2
width //= 2
height += height % 2
width += width % 2
x = rearrange(x, 'b c t h w -> (b t) c h w')
x = F.interpolate(x, size = (height, width), mode='bilinear')
x = rearrange(x, '(b t) c h w -> b c t h w', t=temp)
vae_latent_list.append(x)
vae_latent_list = list(reversed(vae_latent_list)) # idx和stage_index相对应
return vae_latent_list
def sample_stage_length(self, num_stages, max_units = None):
self.video_sync_group = self.max_unit_num
max_units_in_training = 1 + ((self.max_unit_num - 1) // self.frame_per_unit)
cur_rank = self.process_index
self.iters = self.iters + 1
total_turns = max_units_in_training // self.video_sync_group #NOTE: video_sync_group
update_turn = self.iters % total_turns
# # uniformly sampling each position
cur_highres_unit = max(int((cur_rank % self.video_sync_group + 1) + update_turn * self.video_sync_group), 1)
cur_mid_res_unit = max(1 + max_units_in_training - cur_highres_unit, 1)
cur_low_res_unit = cur_mid_res_unit #平衡下速度的考量
if max_units is not None:
cur_highres_unit = min(cur_highres_unit, max_units)
cur_mid_res_unit = min(cur_mid_res_unit, max_units)
cur_low_res_unit = min(cur_low_res_unit, max_units)
length_list = [cur_low_res_unit, cur_mid_res_unit, cur_highres_unit]
assert len(length_list) == num_stages
return length_list
@torch.no_grad()
def add_pyramid_noise(
self,
latents_list,
sample_ratios=[1, 1, 1],
):
"""
add the noise for each pyramidal stage
noting that, this method is a general strategy for pyramid-flow, it
can be used for both image and video training.
You can also use this method to train pyramid-flow with full-sequence
diffusion in video generation (without using temporal pyramid and autoregressive modeling)
Params:
latent_list: [low_res, mid_res, high_res] The vae latents of all stages
sample_ratios: The proportion of each stage in the training batch
"""
noise = torch.randn_like(latents_list[-1])
device = noise.device
dtype = latents_list[-1].dtype
t = noise.shape[2]
stages = self.stages
tot_samples = noise.shape[0]
assert tot_samples % (int(sum(sample_ratios))) == 0
assert stages == len(sample_ratios)
height, width = noise.shape[-2], noise.shape[-1]
noise_list = [noise]
cur_noise = noise
for i_s in range(stages-1):
height //= 2;width //= 2
height += height % 2
width += width % 2
cur_noise = rearrange(cur_noise, 'b c t h w -> (b t) c h w')
cur_noise = F.interpolate(cur_noise, size=(height, width), mode='bilinear') * 2
cur_noise = rearrange(cur_noise, '(b t) c h w -> b c t h w', t=t)
noise_list.append(cur_noise)
noise_list = list(reversed(noise_list)) # make sure from low res to high res
# To calculate the padding batchsize and column size
batch_size = tot_samples // int(sum(sample_ratios))
column_size = int(sum(sample_ratios))
column_to_stage = {} #每个数据对应哪个stage
i_sum = 0
for i_s, column_num in enumerate(sample_ratios):
for index in range(i_sum, i_sum + column_num):
column_to_stage[index] = i_s
i_sum += column_num
noisy_latents_list = []
ratios_list = []
targets_list = []
timesteps_list = []
training_steps = self.scheduler.config.num_train_timesteps
# from low resolution to high resolution
for index in range(column_size):
i_s = column_to_stage[index]
clean_latent = latents_list[i_s][index::column_size] # 哪些样本在这个column # [bs, c, t, h, w]
last_clean_latent = None if i_s == 0 else latents_list[i_s-1][index::column_size] # 否则的话,就是更早一级的latent
start_sigma = self.scheduler.start_sigmas[i_s]
end_sigma = self.scheduler.end_sigmas[i_s]
if i_s == 0:
start_point = noise_list[i_s][index::column_size] #stage 0的,初始的latent就是noise
else:
# Get the upsampled latent
last_clean_latent = rearrange(last_clean_latent, 'b c t h w -> (b t) c h w')
last_clean_latent = F.interpolate(last_clean_latent, size=(clean_latent.shape[-2], clean_latent.shape[-1]), mode='nearest')
last_clean_latent = rearrange(last_clean_latent, '(b t) c h w -> b c t h w', t=t)
start_point = start_sigma * noise_list[i_s][index::column_size] + (1 - start_sigma) * last_clean_latent
if i_s == stages - 1:
end_point = clean_latent
else:
end_point = end_sigma * noise_list[i_s][index::column_size] + (1 - end_sigma) * clean_latent
# To sample a timestep
u = compute_density_for_timestep_sampling(
weighting_scheme='random',
batch_size=batch_size,
logit_mean=0.0,
logit_std=1.0,
mode_scale=1.29,
)
indices = (u * training_steps).long() # Totally 1000 training steps per stage
indices = indices.clamp(0, training_steps-1)
timesteps = self.scheduler.timesteps_per_stage[i_s][indices].to(device=device)
ratios = self.scheduler.sigmas_per_stage[i_s][indices].to(device=device)
while len(ratios.shape) < start_point.ndim:
ratios = ratios.unsqueeze(-1)
# interpolate the latent
noisy_latents = ratios * start_point + (1 - ratios) * end_point
# last_cond_noisy_sigma = torch.rand(size=(batch_size,), device=device) * self.corrupt_ratio
# [stage1_latent, stage2_latent, ..., stagen_latent], which will be concat after patching
noisy_latents_list.append([noisy_latents.to(dtype)])
ratios_list.append(ratios.to(dtype))
timesteps_list.append(timesteps.to(dtype))
targets_list.append(start_point - end_point) # The standard rectified flow matching objective
return noisy_latents_list, ratios_list, timesteps_list, targets_list
@torch.no_grad()
def add_pyramid_noise_with_temporal_pyramid(
self,
latents_list,
sample_ratios=[1, 1, 1],
):
stages = self.stages
tot_samples = latents_list[0].shape[0]
device = latents_list[0].device
dtype = latents_list[0].dtype
assert tot_samples % (int(sum(sample_ratios))) == 0
assert stages == len(sample_ratios)
noise = torch.randn_like(latents_list[-1])
t = noise.shape[2]
max_units = 1 + (t - 1) // self.frame_per_unit
num_units_per_stage = self.sample_stage_length(stages, max_units=max_units) # [The unit number of each stage]
height, width = noise.shape[-2], noise.shape[-1]
noise_list = [noise]
cur_noise = noise
for i_s in range(stages-1):
height //= 2;width //= 2
height += height % 2
width += width % 2
cur_noise = rearrange(cur_noise, 'b c t h w -> (b t) c h w')
cur_noise = F.interpolate(cur_noise, size=(height, width), mode='bilinear') * 2
cur_noise = rearrange(cur_noise, '(b t) c h w -> b c t h w', t=t)
noise_list.append(cur_noise)
noise_list = list(reversed(noise_list))
batch_size = tot_samples // int(sum(sample_ratios))
column_size = int(sum(sample_ratios))
column_to_stage = {}
i_sum = 0
for i_s, column_num in enumerate(sample_ratios):
for index in range(i_sum, i_sum + column_num):
column_to_stage[index] = i_s
i_sum += column_num
noisy_latents_list = []
ratios_list = []
targets_list = []
timesteps_list = []
training_steps = self.scheduler.config.num_train_timesteps
for index in range(column_size):
# First prepare the trainable latent construction
i_s = column_to_stage[index]
clean_latent = latents_list[i_s][index::column_size] # [bs, c, t, h, w]
last_clean_latent = None if i_s == 0 else latents_list[i_s-1][index::column_size]
start_sigma = self.scheduler.start_sigmas[i_s]
end_sigma = self.scheduler.end_sigmas[i_s]
if i_s == 0:
start_point = noise_list[i_s][index::column_size]
else:
# Get the upsampled latent
last_clean_latent = rearrange(last_clean_latent, 'b c t h w -> (b t) c h w')
last_clean_latent = F.interpolate(last_clean_latent, size=(clean_latent.shape[-2], clean_latent.shape[-1]), mode='nearest')
last_clean_latent = rearrange(last_clean_latent, '(b t) c h w -> b c t h w', t=t)
start_point = start_sigma * noise_list[i_s][index::column_size] + (1 - start_sigma) * last_clean_latent
if i_s == stages - 1:
end_point = clean_latent
else:
end_point = end_sigma * noise_list[i_s][index::column_size] + (1 - end_sigma) * clean_latent
u = compute_density_for_timestep_sampling(
weighting_scheme='random',
batch_size=batch_size,
logit_mean=0.0,
logit_std=1.0,
mode_scale=1.29,
)
indices = (u * training_steps).long() # Totally 1000 training steps per stage
indices = indices.clamp(0, training_steps-1)
timesteps = self.scheduler.timesteps_per_stage[i_s][indices].to(device=device)
ratios = self.scheduler.sigmas_per_stage[i_s][indices].to(device=device)
noise_ratios = ratios * start_sigma + (1 - ratios) * end_sigma
while len(ratios.shape) < start_point.ndim:
ratios = ratios.unsqueeze(-1)
noisy_latents = ratios * start_point + (1 - ratios) * end_point
# The flow matching object
target_latents = start_point - end_point
num_units = num_units_per_stage[i_s]
num_units = min(num_units, 1 + (t - 1) // self.frame_per_unit)
actual_frames = 1 + (num_units - 1) * self.frame_per_unit
noisy_latents = noisy_latents[:, :, :actual_frames]
target_latents = target_latents[:, :, :actual_frames]
clean_latent = clean_latent[:, :, :actual_frames]
stage_noise = noise_list[i_s][index::column_size][:, :, :actual_frames]
# only the last latent takes part in training
noisy_latents = noisy_latents[:, :, -self.frame_per_unit:]
target_latents = target_latents[:, :, -self.frame_per_unit:]
last_cond_noisy_sigma = torch.rand(size=(batch_size,), device=device) * 1/3
if num_units == 1:
stage_input = [noisy_latents.to(dtype)]
else:
last_cond_latent = clean_latent[:, :, -(2*self.frame_per_unit):-self.frame_per_unit]
while len(last_cond_noisy_sigma.shape) < last_cond_latent.ndim:
last_cond_noisy_sigma = last_cond_noisy_sigma.unsqueeze(-1)
last_cond_latent = last_cond_noisy_sigma * torch.randn_like(last_cond_latent) + (1 - last_cond_noisy_sigma) * last_cond_latent
stage_input = [noisy_latents.to(dtype), last_cond_latent.to(dtype)]
cur_unit_num = 2
cur_stage = i_s
while cur_unit_num < num_units:
cur_stage = max(cur_stage - 1, 0)
if cur_stage == 0:
break
cur_unit_num += 1
cond_latents = latents_list[cur_stage][index::column_size][:, :, :actual_frames]
cond_latents = cond_latents[:, :, -(cur_unit_num * self.frame_per_unit) : -((cur_unit_num - 1) * self.frame_per_unit)]
cond_latents = last_cond_noisy_sigma * torch.randn_like(cond_latents) + (1 - last_cond_noisy_sigma) * cond_latents
stage_input.append(cond_latents.to(dtype))
if cur_stage == 0 and cur_unit_num < num_units:
cond_latents = latents_list[0][index::column_size][:, :, :actual_frames]
cond_latents = cond_latents[:, :, :-(cur_unit_num * self.frame_per_unit)]
cond_latents = last_cond_noisy_sigma * torch.randn_like(cond_latents) + (1 - last_cond_noisy_sigma) * cond_latents
stage_input.append(cond_latents.to(dtype))
stage_input = list(reversed(stage_input))
noisy_latents_list.append(stage_input)
ratios_list.append(ratios.to(dtype))
timesteps_list.append(timesteps.to(dtype))
targets_list.append(target_latents)
return noisy_latents_list, ratios_list, timesteps_list, targets_list
@torch.no_grad()
def get_vae_latent(self, video, use_temporal_pyramid=True):
# if video.shape[2] == 1:
# # is image
# video = (video - self.vae_shift_factor) * self.vae_scale_factor
# else:
# # is video
# video[:, :, :1] = (video[:, :, :1] - self.vae_shift_factor) * self.vae_scale_factor
# video[:, :, 1:] = (video[:, :, 1:] - self.vae_video_shift_factor) * self.vae_video_scale_factor
vae_latent_list = self.get_pyramid_latent(video, self.stages - 1)
if use_temporal_pyramid:
noisy_latents_list, ratios_list, timesteps_list, targets_list = self.add_pyramid_noise_with_temporal_pyramid(vae_latent_list, self.sample_ratios)
else:
noisy_latents_list, ratios_list, timesteps_list, targets_list = self.add_pyramid_noise(vae_latent_list, self.sample_ratios)
return noisy_latents_list, ratios_list, timesteps_list, targets_list
def calculate_loss(self, model_preds_list, targets_list):
loss_list = []
for model_pred, target in zip(model_preds_list, targets_list):
loss_weight = torch.ones_like(target)
loss = torch.mean(
(loss_weight.float() * (model_pred.float() - target.float()) ** 2).reshape(target.shape[0], -1),
1,
)
loss_list.append(loss)
diffusion_loss = torch.cat(loss_list, dim=0).mean()
return diffusion_loss
def get_loss(self, model, model_input, model_kwargs, args, accelerator, **kwargs): #代码部分仍需要注意,因为这里会根据rank来进行划分
video = model_input
xdim = video.ndim
device = video.device
with torch.no_grad(), accelerator.autocast():
batch_size = len(video)
prompt_embeds, prompt_attention_mask, pooled_prompt_embeds = \
model_kwargs['encoder_hidden_states'], model_kwargs['encoder_attention_mask'], model_kwargs['encoder_pooled_projections']
noisy_latents_list, ratios_list, timesteps_list, targets_list = self.get_vae_latent(video, use_temporal_pyramid=True)
timesteps = torch.cat([timestep.unsqueeze(-1) for timestep in timesteps_list], dim=-1)
timesteps = timesteps.reshape(-1)
assert timesteps.shape[0] == prompt_embeds.shape[0]
model_preds_list = model(
sample=noisy_latents_list,
timestep_ratio=timesteps,
encoder_hidden_states=prompt_embeds,
encoder_attention_mask=prompt_attention_mask,
pooled_projections=pooled_prompt_embeds,
)
return self.calculate_loss(model_preds_list, targets_list)
# 前向运算计算loss
def get_loss_2(self, model, model_input, model_kwargs, args, **kwargs): #实际会更慢
# TODO: 优化自回归训练策略
# 如果使用不同的自回归长度,无疑会让sample的数量增长很多
# 修改成不同stages
dtype = model_input.dtype
assert 'stage_index' in kwargs, "passing stage index for pyramid training"
stage_index = kwargs['stage_index'] # need
bsz = model_input.shape[0]
temp = model_input.shape[2]
frame_per_unit = self.frame_per_unit
model_input[:, :, 0:1] = (model_input[:, :, 0:1] - self.vae_shift_factor) * self.vae_scale_factor
if model_input.shape[2] > 1:
model_input[:, :, 1:] = (model_input[:, :, 1:] - self.vae_video_shift_factor) * self.vae_video_scale_factor
clean_latents_list = self.get_pyramid_latent(model_input, self.stages-1)
#TODO: 目前是随机采样 timesteps用相同的
#NOTE: 开头和结尾都能够取到
#TODO: 暂时只能支持frame_per_unit=1的情况
sample_list = []
target_list = []
timesteps_list = []
# stage_list = np.arange(self.stages) #同时包含所有stages
self.iters += 1
assert self.stages == 3
stage_list = [1] #进行某个特定stage的优化
if (self.iters + self.process_index) % 2 == 0:
stage_list.append(0)
elif (self.iters + self.process_index) % 2 == 1:
stage_list.append(2)
else:
raise ValueError
# stage_list = [0,1,1,2]
# stage_2_length = (self.iters + self.process_index) % (self.max_unit_num-1) + 1
# stage_1_length = self.max_unit_num - 1 - (self.iters + self.process_index) % (self.max_unit_num-1)
# stage_0_length = stage_2_length #按照这个逻辑的话,stage2的耗时最长,因此最好让stage0和stage1用相同的步数
# 1到6
#如果每个stage包含多个自回归长度的话,不如从这里入手,更加均匀 应该是0到6
frame_idx_list = [(self.process_index) % (self.max_unit_num), self.max_unit_num - 1 - (self.process_index) % (self.max_unit_num)]
print(self.process_index, stage_list, frame_idx_list)
# frame_idx_list = [stage_0_length, stage_1_length, stage_1_length, stage_2_length]
for idx, stage_index in enumerate(stage_list):
#TODO: 按照rank进行分配
# u = compute_density_for_timestep_sampling(
# weighting_scheme='random',
# batch_size=bsz,
# logit_mean=0.0,
# logit_std=1.0,
# mode_scale=1.29,
# )
# indices = (u * len(self.scheduler.timesteps_per_stage[stage_index])).long()
# timesteps_indices = indices.clamp(0, len(self.scheduler.timesteps_per_stage[stage_index])-1)
timesteps_indices = torch.randint(0, len(self.scheduler.timesteps_per_stage[stage_index]), (bsz,))
timesteps = self.scheduler.timesteps_per_stage[stage_index][timesteps_indices].to(model_input.device, dtype=dtype)
sigmas = self.scheduler.sigmas_per_stage[stage_index][timesteps_indices].to(model_input.device, dtype=dtype)
start_sigma = self.scheduler.start_sigmas[stage_index]
end_sigma = self.scheduler.end_sigmas[stage_index]
while len(sigmas.shape) < model_input.ndim:
sigmas = sigmas.unsqueeze(-1)
# timesteps_list.append(timesteps.unsqueeze(1))
#可以最多选择两种长度?
#TODO: 可以修改成根据rank来在不同的steps中进行均衡的策略
frame_idx_list_stage = [frame_idx_list[idx]]
# if temp > 2:
# frame_idx_list = list(np.random.permutation(np.arange(1, temp)))[:self.extra_sample_steps]
timesteps_list.append(timesteps.unsqueeze(1).repeat(1, len(frame_idx_list_stage)))
for frame_idx in frame_idx_list_stage:
frame_idx = min(model_input.shape[2]-1, frame_idx)
cur_latent = model_input[:, :, frame_idx:frame_idx+1]
if frame_idx == 0:
stage_input = []
else:
last_cond_latent = clean_latents_list[stage_index][:, :, frame_idx-frame_per_unit:frame_idx]
last_cond_noisy_sigma = torch.rand(size=(bsz,), device=model_input.device, dtype=cur_latent.dtype) * 1 / 3
while len(last_cond_noisy_sigma.shape) < last_cond_latent.ndim:
last_cond_noisy_sigma = last_cond_noisy_sigma.unsqueeze(-1)
last_cond_latent = last_cond_noisy_sigma * torch.randn_like(last_cond_latent, device=model_input.device, dtype=cur_latent.dtype) + (1 - last_cond_noisy_sigma) * last_cond_latent
stage_input = [last_cond_latent]
cur_unit_num = frame_idx
cur_stage = stage_index
cur_unit_ptx = 1 #这里是diff
while cur_unit_ptx < cur_unit_num:
cur_stage = max(cur_stage-1, 0)
if cur_stage == 0:
break
cur_unit_ptx += 1
cond_latents = clean_latents_list[cur_stage][:, :, frame_idx-cur_unit_ptx * frame_per_unit:frame_idx - (cur_unit_ptx-1)*frame_per_unit]
cond_latents = last_cond_noisy_sigma * torch.randn_like(cond_latents, device=model_input.device, dtype=cur_latent.dtype) + (1 - last_cond_noisy_sigma) * cond_latents
stage_input.append(cond_latents)
if cur_stage == 0 and cur_unit_ptx < cur_unit_num:
cond_latents = clean_latents_list[cur_stage][:, :, :frame_idx - cur_unit_ptx * frame_per_unit]
if cond_latents.shape[2] > self.max_history_len-len(stage_input):
cond_latents = cond_latents[:, :, -(self.max_history_len-len(stage_input)):]
cond_latents = last_cond_noisy_sigma * torch.randn_like(cond_latents, device=model_input.device, dtype=cur_latent.dtype) + (1 - last_cond_noisy_sigma) * cond_latents
stage_input.append(cond_latents)
stage_input = list(reversed(stage_input))
height, width = cur_latent.shape[3:]
cur_latent = rearrange(cur_latent, "b c t h w -> (b t) c h w")
noise = torch.randn_like(cur_latent, device = cur_latent.device, dtype=cur_latent.dtype) #noise考虑用不同的
for _ in range(self.stages - stage_index - 1):
height //= 2
width //= 2
height += height % 2
width += width % 2
cur_latent = F.interpolate(cur_latent, size = (height, width), mode = 'bilinear')
noise = F.interpolate(noise, size = (height, width), mode = 'bilinear') * 2
cur_latent = rearrange(cur_latent, "(b t) c h w -> b c t h w", t = frame_per_unit)
noise = rearrange(noise, "(b t) c h w -> b c t h w", t = frame_per_unit)
height, width = cur_latent.shape[3:]
stage_start_point = rearrange(cur_latent, "b c t h w -> (b t) c h w")
stage_start_point = F.interpolate(stage_start_point, size = (int(height//2), int(width//2)), mode = 'bilinear')
stage_start_point = F.interpolate(stage_start_point, size = (height, width), mode = 'nearest') #TODO: check这里是否修改
stage_start_point = rearrange(stage_start_point, "(b t) c h w -> b c t h w", t = frame_per_unit)
stage_start_point = (1 - start_sigma) * stage_start_point + start_sigma * noise
stage_end_point = (1 - end_sigma) * cur_latent + end_sigma * noise
noisy_model_input = (1 - sigmas) * stage_end_point + sigmas * stage_start_point
noisy_model_input = stage_input + [noisy_model_input]
target = stage_start_point - stage_end_point
sample_list.append(noisy_model_input)
target_list.append(target)
timesteps = torch.cat(timesteps_list, dim=1).reshape(len(sample_list)*bsz)
model_pred = model(
sample = sample_list, #实际上可以同一个样本构建多个自回归的序列。这样就不会出现问题了
timestep_ratio = timesteps, # torch.repeat_interleave(timesteps, repeats = len(sample_list), dim=0),
encoder_hidden_states = torch.repeat_interleave(model_kwargs['encoder_hidden_states'], repeats = len(sample_list), dim=0),
encoder_attention_mask = torch.repeat_interleave(model_kwargs['encoder_attention_mask'], repeats = len(sample_list), dim=0),
pooled_projections = torch.repeat_interleave(model_kwargs['encoder_pooled_projections'], repeats = len(sample_list), dim=0)
)
mask = model_kwargs.get('attention_mask', None)
if torch.all(mask.bool()):
mask = None
assert mask is None #否则的话,就需要对mask也进行下采样了
# model_pred = torch.stack(model_pred)
# target = torch.stack(target_list)
model_pred_ = torch.cat([pred.reshape(bsz, -1) for pred in model_pred], dim=1)
target_ = torch.cat([gt.reshape(bsz, -1) for gt in target_list], dim=1)
loss = ((model_pred_.float() - target_.float()) ** 2).reshape(bsz, -1)
loss = loss.mean()
return loss
@torch.no_grad()
def get_valid_loss(self, model, model_input, model_kwargs, args, **kwargs):
#验证的逻辑: 3个stage,所有可能的自回归长度
dtype = model_input.dtype
assert 'stage_index' in kwargs, "passing stage index for pyramid training"
stage_index = kwargs['stage_index'] # need
assert 'evaluate_steps' in kwargs, "passing how many steps to evaluate"
evaluate_steps = kwargs['evaluate_steps']
bsz = model_input.shape[0]
temp = model_input.shape[2]
frame_per_unit = self.frame_per_unit
# model_input[:, :, 0:1] = (model_input[:, :, 0:1] - self.vae_shift_factor) * self.vae_scale_factor
# if model_input.shape[2] > 1:
# model_input[:, :, 1:] = (model_input[:, :, 1:] - self.vae_video_shift_factor) * self.vae_video_scale_factor
generator = torch.torch.Generator(device = model_input.device)
generator.manual_seed(42)
clean_latents_list = self.get_pyramid_latent(model_input, self.stages-1, generator=generator)
past_condition_list = []
cur_latent_list = []
for frame_idx in range(temp):
cur_latent = model_input[:, :, frame_idx:frame_idx+1]
if frame_idx == 0:
stage_input = []
else:
last_cond_latent = clean_latents_list[stage_index][:, :, frame_idx-frame_per_unit:frame_idx]
stage_input = [last_cond_latent]
cur_unit_num = frame_idx
cur_stage = stage_index
cur_unit_ptx = 1
while cur_unit_ptx < cur_unit_num:
cur_stage = max(cur_stage-1, 0)
if cur_stage == 0:
break
cur_unit_ptx += 1
cond_latents = clean_latents_list[cur_stage][:, :, frame_idx-cur_unit_ptx * frame_per_unit:frame_idx - (cur_unit_ptx-1)*frame_per_unit]
stage_input.append(cond_latents)
if cur_stage == 0 and cur_unit_ptx < cur_unit_num:
cond_latents = clean_latents_list[cur_stage][:, :, :frame_idx - cur_unit_ptx * frame_per_unit]
if cond_latents.shape[2] > self.max_history_len-len(stage_input):
cond_latents = cond_latents[:, :, -(self.max_history_len-len(stage_input)):]
stage_input.append(cond_latents)
stage_input = list(reversed(stage_input))
past_condition_list.append(stage_input)
height, width = cur_latent.shape[3:]
cur_latent = rearrange(cur_latent, "b c t h w -> (b t) c h w")
for _ in range(self.stages - stage_index - 1):
height //= 2
width //= 2
height += height % 2
width += width % 2
cur_latent = F.interpolate(cur_latent, size = (height, width), mode = 'bilinear')
cur_latent = rearrange(cur_latent, "(b t) c h w -> b c t h w", t = frame_per_unit)
cur_latent_list.append(cur_latent)
loss_list = []
for step_i in range(evaluate_steps):
generator = torch.torch.Generator(device = model_input.device)
generator.manual_seed(42+step_i)
timesteps_indices = torch.ones((bsz,), dtype=int) * (step_i * len(self.scheduler.timesteps_per_stage[stage_index]) // evaluate_steps)
timesteps = self.scheduler.timesteps_per_stage[stage_index][timesteps_indices].to(model_input.device, dtype=dtype)
sigmas = self.scheduler.sigmas_per_stage[stage_index][timesteps_indices].to(model_input.device, dtype=dtype)
start_sigma = self.scheduler.start_sigmas[stage_index]
end_sigma = self.scheduler.end_sigmas[stage_index]
while len(sigmas.shape) < model_input.ndim:
sigmas = sigmas.unsqueeze(-1)
#TODO: 暂时只能支持frame_per_unit=1的情况
sample_list = []
target_list = []
for frame_idx in range(temp):
stage_input = past_condition_list[frame_idx]
cur_latent = cur_latent_list[frame_idx]
height, width = cur_latent.shape[3:]
# noise = torch.randn_like(cur_latent, generator=generator) #noise考虑用不同的
noise = randn_tensor(cur_latent.shape, generator=generator, device=cur_latent.device, dtype=cur_latent.dtype)
stage_start_point = rearrange(cur_latent, "b c t h w -> (b t) c h w")
stage_start_point = F.interpolate(stage_start_point, size = (int(height//2), int(width//2)), mode = 'bilinear')
stage_start_point = F.interpolate(stage_start_point, size = (height, width), mode = 'nearest') #TODO: check这里是否修改
stage_start_point = rearrange(stage_start_point, "(b t) c h w -> b c t h w", t = frame_per_unit)
stage_start_point = (1 - start_sigma) * stage_start_point + start_sigma * noise
stage_end_point = (1 - end_sigma) * cur_latent + end_sigma * noise
noisy_model_input = (1 - sigmas) * stage_end_point + sigmas * stage_start_point
noisy_model_input = stage_input + [noisy_model_input]
target = stage_start_point - stage_end_point
sample_list.append(noisy_model_input)
target_list.append(target)
model_pred = model(
sample = sample_list, #实际上可以同一个样本构建多个自回归的序列。这样就不会出现问题了
timestep_ratio = torch.repeat_interleave(timesteps, repeats = len(sample_list), dim=0),
encoder_hidden_states = torch.repeat_interleave(model_kwargs['encoder_hidden_states'], repeats = len(sample_list), dim=0),
encoder_attention_mask = torch.repeat_interleave(model_kwargs['encoder_attention_mask'], repeats = len(sample_list), dim=0),
pooled_projections = torch.repeat_interleave(model_kwargs['encoder_pooled_projections'], repeats = len(sample_list), dim=0)
)
# mask = model_kwargs.get('attention_mask', None)
# if torch.all(mask.bool()):
# mask = None
# assert mask is None #否则的话,就需要对mask也进行下采样了
model_pred = torch.stack(model_pred)
target = torch.stack(target_list)
loss = ((model_pred.float() - target.float()) ** 2).reshape(target.shape[0], -1)
# loss = loss.mean()
loss_list.append(loss)
loss_list = torch.stack(loss_list)
return loss_list
class EDM():
def __init__(self):
self.edm_loss = EDMLoss()
print('====> use EDM Precondition !!!')
def get_loss(self, model, model_input, model_kwargs, args, **kwargs):
model = partial(model, **model_kwargs)
loss = self.edm_loss(model, model_input)
mask = model_kwargs.get('attention_mask', None)
if torch.all(mask.bool()):
mask = None
b, c, t_shard, _, _ = loss.shape
if mask is not None:
# 实际上不知道这里是哪里,因此需要所有帧的attention mask是一样的,确保在时序上不会补帧即可
mask = mask.unsqueeze(1).repeat(1, c, 1, 1, 1).float()[:, :, -t_shard:, :, :] # b t h w -> b c t h w
mask = mask.reshape(b, -1)
loss = loss.reshape(b, -1)
if mask is not None:
loss = (loss * mask).sum() / mask.sum()
else:
loss = loss.mean()
return loss |