File size: 8,732 Bytes
4a9ad28 |
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 |
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.utils as vutils
from datasets import load_dataset
from torch.utils.data import DataLoader, TensorDataset
from schedulefree import AdamWScheduleFree
from torch.utils.tensorboard import SummaryWriter
from safetensors.torch import save_file, load_file
import os, time
from models import AsymmetricResidualUDiT
from torch.cuda.amp import autocast
def preload_dataset(image_size=256, device="cuda"):
"""Preload and cache the entire dataset in GPU memory"""
print("Loading and preprocessing dataset...")
#dataset = load_dataset("jiovine/pixel-art-nouns-2k", split="train")
dataset = load_dataset("reach-vb/pokemon-blip-captions", split="train")
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Resize((image_size, image_size), antialias=True),
transforms.Lambda(lambda x: (x * 2) - 1) # Scale to [-1, 1]
])
all_images = []
for example in dataset:
img_tensor = transform(example['image'])
all_images.append(img_tensor)
# Stack entire dataset onto gpu
images_tensor = torch.stack(all_images).to(device)
print(f"Dataset loaded: {images_tensor.shape} ({images_tensor.element_size() * images_tensor.nelement() / 1024/1024:.2f} MB)")
return TensorDataset(images_tensor)
def count_parameters(model):
total_params = sum(p.numel() for p in model.parameters())
print(f'Total parameters: {total_params:,} ({total_params/1e6:.2f}M)')
def save_checkpoint(model, optimizer, filename="checkpoint.safetensors"):
model_state = model.state_dict()
save_file(model_state, filename)
def load_checkpoint(model, optimizer, filename="checkpoint.safetensors"):
model_state = load_file(filename)
model.load_state_dict(model_state)
# https://arxiv.org/abs/2210.02747
class OptimalTransportLinearFlowGenerator():
def __init__(self, sigma_min=0.001):
self.sigma_min = sigma_min
def loss(self, model, x1, device):
batch_size = x1.shape[0]
# Sample t uniform in [0,1]
t = torch.rand(batch_size, 1, 1, 1, device=device)
# Sample noise
x0 = torch.randn_like(x1)
x1 = x1
# Compute OT path interpolation (equation 22)
sigma_t = 1 - (1 - self.sigma_min) * t
mu_t = t * x1
x_t = sigma_t * x0 + mu_t
# Compute target (equation 23)
target = x1 - (1 - self.sigma_min) * x0
v_t = model(x_t, t)
loss = F.mse_loss(v_t, target)
return loss
def write_logs(writer, model, loss, batch_idx, epoch, epoch_time, batch_size, lr, log_gradients=True):
"""
TensorBoard logging
Args:
writer: torch.utils.tensorboard.SummaryWriter instance
model: torch.nn.Module - the model being trained
loss: float or torch.Tensor - the loss value to log
batch_idx: int - current batch index
epoch: int - current epoch
epoch_time: float - time taken for epoch
batch_size: int - current batch size
lr: float - current learning rate
samples: Optional[torch.Tensor] - generated samples to log (only passed every 50 epochs)
log_gradients: bool - whether to log gradient norms
"""
total_steps = epoch * batch_idx
writer.add_scalar('Loss/batch', loss, total_steps)
writer.add_scalar('Time/epoch', epoch_time, epoch)
writer.add_scalar('Training/batch_size', batch_size, epoch)
writer.add_scalar('Training/learning_rate', lr, epoch)
if log_gradients:
total_norm = 0.0
for p in model.parameters():
if p.grad is not None:
param_norm = p.grad.detach().data.norm(2)
total_norm += param_norm.item() ** 2
total_norm = total_norm ** 0.5
writer.add_scalar('Gradients/total_norm', total_norm, total_steps)
def train_udit_flow(num_epochs=5000, initial_batch_sizes=[8, 16, 32, 64, 128], epoch_batch_drop_at=40, device="cuda", dtype=torch.float32):
dataset = preload_dataset(device=device)
temp_loader = DataLoader(dataset, batch_size=initial_batch_sizes[0], shuffle=True)
first_batch = next(iter(temp_loader))
image_shape = first_batch[0].shape[1:]
writer = SummaryWriter('logs/current_run')
model = AsymmetricResidualUDiT(
in_channels=3,
base_channels=128,
num_levels=3,
patch_size=4,
encoder_blocks=3,
decoder_blocks=7,
encoder_transformer_thresh=2,
decoder_transformer_thresh=4,
mid_blocks=8
).to(device).to(dtype)
model.train()
count_parameters(model)
optimizer = AdamWScheduleFree(
model.parameters(),
lr=1e-4,
warmup_steps=100
)
optimizer.train()
current_batch_sizes = initial_batch_sizes.copy()
next_drop_epoch = epoch_batch_drop_at
interval_multiplier = 2
torch.set_float32_matmul_precision('high')
model = torch.compile(
model,
backend='inductor',
mode='max-autotune',
fullgraph=True,
)
flow_transport = OptimalTransportLinearFlowGenerator(sigma_min=0.001)
for epoch in range(num_epochs):
epoch_start_time = time.time()
total_loss = 0
# Batch size decay logic
# Geomtric growth, every X*N+(X-1*N+...) use the number batch size in the list.
if epoch > 0 and epoch == next_drop_epoch and len(current_batch_sizes) > 1:
current_batch_sizes.pop()
next_interval = epoch_batch_drop_at * interval_multiplier
next_drop_epoch += next_interval
interval_multiplier += 1
print(f"\nEpoch {epoch}: Reducing batch size to {current_batch_sizes[-1]}")
print(f"Next drop will occur at epoch {next_drop_epoch} (interval: {next_interval})")
current_batch_size = current_batch_sizes[-1]
dataloader = DataLoader(dataset, batch_size=current_batch_size, shuffle=True)
curr_lr = optimizer.param_groups[0]['lr']
with torch.amp.autocast('cuda', dtype=dtype):
for batch_idx, batch in enumerate(dataloader):
x1 = batch[0]
batch_size = x1.shape[0]
loss = flow_transport.loss(model, x1, device)
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.item()
avg_loss = total_loss / len(dataloader)
epoch_time = time.time() - epoch_start_time
print(f"Epoch {epoch}, Took: {epoch_time:.2f}s, Batch Size: {current_batch_size}, "
f"Average Loss: {avg_loss:.4f}, Learning Rate: {curr_lr:.6f}")
write_logs(writer, model, avg_loss, batch_idx, epoch, epoch_time, current_batch_size, curr_lr)
if (epoch + 1) % 50 == 0:
with torch.amp.autocast('cuda', dtype=dtype):
sampling_start_time = time.time()
samples = sample(model, device=device, dtype=dtype)
os.makedirs("samples", exist_ok=True)
vutils.save_image(samples, f"samples/epoch_{epoch}.png", nrow=4, padding=2)
sample_time = time.time() - sampling_start_time
print(f"Sampling took: {sample_time:.2f}s")
if (epoch + 1) % 200 == 0:
save_checkpoint(model, optimizer, f"step_{epoch}.safetensors")
return model
def sample(model, n_samples=16, n_steps=50, image_size=256, device="cuda", sigma_min=0.001, dtype=torch.float32):
with torch.amp.autocast('cuda', dtype=dtype):
x = torch.randn(n_samples, 3, image_size, image_size, device=device)
ts = torch.linspace(0, 1, n_steps, device=device)
dt = 1/n_steps
# Forward Euler Integration step 0..1
with torch.no_grad():
for i in range(len(ts)):
t = ts[i]
t_input = t.repeat(n_samples, 1, 1, 1)
v_t = model(x, t_input)
x = x + v_t * dt
return x.float()
if __name__ == "__main__":
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")
model = train_udit_flow(
device=device,
initial_batch_sizes=[8, 16],
epoch_batch_drop_at=600,
dtype=torch.float32
)
print("Training complete! Samples saved in 'samples' directory") |