|
| 1 | +# Inspired from https://github.com/w86763777/pytorch-ddpm/tree/master. |
| 2 | + |
| 3 | +# Authors: Kilian Fatras |
| 4 | +# Alexander Tong |
| 5 | +# Imahn Shekhzadeh |
| 6 | + |
| 7 | +import copy |
| 8 | +import math |
| 9 | +import os |
| 10 | + |
| 11 | +import torch |
| 12 | +from absl import app, flags |
| 13 | +from torch.nn.parallel import DistributedDataParallel |
| 14 | +from torch.utils.data import DistributedSampler |
| 15 | +from torchdyn.core import NeuralODE |
| 16 | +from torchvision import datasets, transforms |
| 17 | +from tqdm import trange |
| 18 | +from utils_cifar import ema, generate_samples, infiniteloop, setup |
| 19 | + |
| 20 | +from torchcfm.conditional_flow_matching import ( |
| 21 | + ConditionalFlowMatcher, |
| 22 | + ExactOptimalTransportConditionalFlowMatcher, |
| 23 | + TargetConditionalFlowMatcher, |
| 24 | + VariancePreservingConditionalFlowMatcher, |
| 25 | +) |
| 26 | +from torchcfm.models.unet.unet import UNetModelWrapper |
| 27 | + |
| 28 | +FLAGS = flags.FLAGS |
| 29 | + |
| 30 | +flags.DEFINE_string("model", "otcfm", help="flow matching model type") |
| 31 | +flags.DEFINE_string("output_dir", "./results/", help="output_directory") |
| 32 | +# UNet |
| 33 | +flags.DEFINE_integer("num_channel", 128, help="base channel of UNet") |
| 34 | + |
| 35 | +# Training |
| 36 | +flags.DEFINE_float("lr", 2e-4, help="target learning rate") # TRY 2e-4 |
| 37 | +flags.DEFINE_float("grad_clip", 1.0, help="gradient norm clipping") |
| 38 | +flags.DEFINE_integer( |
| 39 | + "total_steps", 400001, help="total training steps" |
| 40 | +) # Lipman et al uses 400k but double batch size |
| 41 | +flags.DEFINE_integer("warmup", 5000, help="learning rate warmup") |
| 42 | +flags.DEFINE_integer("batch_size", 128, help="batch size") # Lipman et al uses 128 |
| 43 | +flags.DEFINE_integer("num_workers", 4, help="workers of Dataloader") |
| 44 | +flags.DEFINE_float("ema_decay", 0.9999, help="ema decay rate") |
| 45 | +flags.DEFINE_bool("parallel", False, help="multi gpu training") |
| 46 | +flags.DEFINE_string( |
| 47 | + "master_addr", "localhost", help="master address for Distributed Data Parallel" |
| 48 | +) |
| 49 | +flags.DEFINE_string("master_port", "12355", help="master port for Distributed Data Parallel") |
| 50 | + |
| 51 | +# Evaluation |
| 52 | +flags.DEFINE_integer( |
| 53 | + "save_step", |
| 54 | + 20000, |
| 55 | + help="frequency of saving checkpoints, 0 to disable during training", |
| 56 | +) |
| 57 | + |
| 58 | + |
| 59 | +def warmup_lr(step): |
| 60 | + return min(step, FLAGS.warmup) / FLAGS.warmup |
| 61 | + |
| 62 | + |
| 63 | +def train(rank, total_num_gpus, argv): |
| 64 | + print( |
| 65 | + "lr, total_steps, ema decay, save_step:", |
| 66 | + FLAGS.lr, |
| 67 | + FLAGS.total_steps, |
| 68 | + FLAGS.ema_decay, |
| 69 | + FLAGS.save_step, |
| 70 | + ) |
| 71 | + |
| 72 | + if FLAGS.parallel and total_num_gpus > 1: |
| 73 | + # When using `DistributedDataParallel`, we need to divide the batch |
| 74 | + # size ourselves based on the total number of GPUs of the current node. |
| 75 | + batch_size_per_gpu = FLAGS.batch_size // total_num_gpus |
| 76 | + setup(rank, total_num_gpus, FLAGS.master_addr, FLAGS.master_port) |
| 77 | + else: |
| 78 | + batch_size_per_gpu = FLAGS.batch_size |
| 79 | + |
| 80 | + # DATASETS/DATALOADER |
| 81 | + dataset = datasets.CIFAR10( |
| 82 | + root="./data", |
| 83 | + train=True, |
| 84 | + download=True, |
| 85 | + transform=transforms.Compose( |
| 86 | + [ |
| 87 | + transforms.RandomHorizontalFlip(), |
| 88 | + transforms.ToTensor(), |
| 89 | + transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), |
| 90 | + ] |
| 91 | + ), |
| 92 | + ) |
| 93 | + sampler = DistributedSampler(dataset) if FLAGS.parallel else None |
| 94 | + dataloader = torch.utils.data.DataLoader( |
| 95 | + dataset, |
| 96 | + batch_size=batch_size_per_gpu, |
| 97 | + sampler=sampler, |
| 98 | + shuffle=False if FLAGS.parallel else True, |
| 99 | + num_workers=FLAGS.num_workers, |
| 100 | + drop_last=True, |
| 101 | + ) |
| 102 | + |
| 103 | + datalooper = infiniteloop(dataloader) |
| 104 | + |
| 105 | + # Calculate number of epochs |
| 106 | + steps_per_epoch = math.ceil(len(dataset) / FLAGS.batch_size) |
| 107 | + num_epochs = math.ceil(FLAGS.total_steps / steps_per_epoch) |
| 108 | + |
| 109 | + # MODELS |
| 110 | + net_model = UNetModelWrapper( |
| 111 | + dim=(3, 32, 32), |
| 112 | + num_res_blocks=2, |
| 113 | + num_channels=FLAGS.num_channel, |
| 114 | + channel_mult=[1, 2, 2, 2], |
| 115 | + num_heads=4, |
| 116 | + num_head_channels=64, |
| 117 | + attention_resolutions="16", |
| 118 | + dropout=0.1, |
| 119 | + ).to( |
| 120 | + rank |
| 121 | + ) # new dropout + bs of 128 |
| 122 | + |
| 123 | + ema_model = copy.deepcopy(net_model) |
| 124 | + optim = torch.optim.Adam(net_model.parameters(), lr=FLAGS.lr) |
| 125 | + sched = torch.optim.lr_scheduler.LambdaLR(optim, lr_lambda=warmup_lr) |
| 126 | + if FLAGS.parallel: |
| 127 | + net_model = DistributedDataParallel(net_model, device_ids=[rank]) |
| 128 | + ema_model = DistributedDataParallel(ema_model, device_ids=[rank]) |
| 129 | + |
| 130 | + # show model size |
| 131 | + model_size = 0 |
| 132 | + for param in net_model.parameters(): |
| 133 | + model_size += param.data.nelement() |
| 134 | + print("Model params: %.2f M" % (model_size / 1024 / 1024)) |
| 135 | + |
| 136 | + ################################# |
| 137 | + # OT-CFM |
| 138 | + ################################# |
| 139 | + |
| 140 | + sigma = 0.0 |
| 141 | + if FLAGS.model == "otcfm": |
| 142 | + FM = ExactOptimalTransportConditionalFlowMatcher(sigma=sigma) |
| 143 | + elif FLAGS.model == "icfm": |
| 144 | + FM = ConditionalFlowMatcher(sigma=sigma) |
| 145 | + elif FLAGS.model == "fm": |
| 146 | + FM = TargetConditionalFlowMatcher(sigma=sigma) |
| 147 | + elif FLAGS.model == "si": |
| 148 | + FM = VariancePreservingConditionalFlowMatcher(sigma=sigma) |
| 149 | + else: |
| 150 | + raise NotImplementedError( |
| 151 | + f"Unknown model {FLAGS.model}, must be one of ['otcfm', 'icfm', 'fm', 'si']" |
| 152 | + ) |
| 153 | + |
| 154 | + savedir = FLAGS.output_dir + FLAGS.model + "/" |
| 155 | + os.makedirs(savedir, exist_ok=True) |
| 156 | + |
| 157 | + global_step = 0 # to keep track of the global step in training loop |
| 158 | + |
| 159 | + with trange(num_epochs, dynamic_ncols=True) as epoch_pbar: |
| 160 | + for epoch in epoch_pbar: |
| 161 | + epoch_pbar.set_description(f"Epoch {epoch + 1}/{num_epochs}") |
| 162 | + if sampler is not None: |
| 163 | + sampler.set_epoch(epoch) |
| 164 | + |
| 165 | + with trange(steps_per_epoch, dynamic_ncols=True) as step_pbar: |
| 166 | + for step in step_pbar: |
| 167 | + global_step += step |
| 168 | + |
| 169 | + optim.zero_grad() |
| 170 | + x1 = next(datalooper).to(rank) |
| 171 | + x0 = torch.randn_like(x1) |
| 172 | + t, xt, ut = FM.sample_location_and_conditional_flow(x0, x1) |
| 173 | + vt = net_model(t, xt) |
| 174 | + loss = torch.mean((vt - ut) ** 2) |
| 175 | + loss.backward() |
| 176 | + torch.nn.utils.clip_grad_norm_(net_model.parameters(), FLAGS.grad_clip) # new |
| 177 | + optim.step() |
| 178 | + sched.step() |
| 179 | + ema(net_model, ema_model, FLAGS.ema_decay) # new |
| 180 | + |
| 181 | + # sample and Saving the weights |
| 182 | + if FLAGS.save_step > 0 and global_step % FLAGS.save_step == 0: |
| 183 | + generate_samples( |
| 184 | + net_model, FLAGS.parallel, savedir, global_step, net_="normal" |
| 185 | + ) |
| 186 | + generate_samples( |
| 187 | + ema_model, FLAGS.parallel, savedir, global_step, net_="ema" |
| 188 | + ) |
| 189 | + torch.save( |
| 190 | + { |
| 191 | + "net_model": net_model.state_dict(), |
| 192 | + "ema_model": ema_model.state_dict(), |
| 193 | + "sched": sched.state_dict(), |
| 194 | + "optim": optim.state_dict(), |
| 195 | + "step": global_step, |
| 196 | + }, |
| 197 | + savedir + f"{FLAGS.model}_cifar10_weights_step_{global_step}.pt", |
| 198 | + ) |
| 199 | + |
| 200 | + |
| 201 | +def main(argv): |
| 202 | + # get world size (number of GPUs) |
| 203 | + total_num_gpus = int(os.getenv("WORLD_SIZE", 1)) |
| 204 | + |
| 205 | + if FLAGS.parallel and total_num_gpus > 1: |
| 206 | + train(rank=int(os.getenv("RANK", 0)), total_num_gpus=total_num_gpus, argv=argv) |
| 207 | + else: |
| 208 | + use_cuda = torch.cuda.is_available() |
| 209 | + device = torch.device("cuda" if use_cuda else "cpu") |
| 210 | + train(rank=device, total_num_gpus=total_num_gpus, argv=argv) |
| 211 | + |
| 212 | + |
| 213 | +if __name__ == "__main__": |
| 214 | + app.run(main) |
0 commit comments