-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtrain_student.py
More file actions
332 lines (282 loc) · 11.3 KB
/
train_student.py
File metadata and controls
332 lines (282 loc) · 11.3 KB
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
from dit import DiT
from splitmeanflow import SplitMeanFlowDiT, SplitMeanFlow, create_student_from_teacher
from model import RectifiedFlow
import torch
import torchvision
from torchvision import transforms as T
from torchvision.utils import make_grid, save_image
from tqdm import tqdm
from copy import deepcopy
from collections import OrderedDict
import torch.optim as optim
import os
import moviepy.editor as mpy
from comet_ml import Experiment
# Clean EMA functions (same as teacher)
@torch.no_grad()
def update_ema(ema_model, model, decay=0.9999):
"""Step the EMA model towards the current model."""
ema_params = OrderedDict(ema_model.named_parameters())
model_params = OrderedDict(model.named_parameters())
for name, param in model_params.items():
ema_params[name].mul_(decay).add_(param.data, alpha=1 - decay)
def requires_grad(model, flag=True):
"""Set requires_grad flag for all parameters in a model."""
for p in model.parameters():
p.requires_grad = flag
def main():
# Training configuration
n_steps = 100000
device = "cuda" if torch.cuda.is_available() else "cpu"
batch_size = 128
ema_decay = 0.9999
# Image settings (matching teacher)
image_size = 16
image_channels = 3
# SplitMeanFlow specific configs
flow_ratio = 0.7 # Higher for stability as paper suggests
teacher_cfg_scale = 3.0 # Fixed CFG scale for teacher guidance
time_sampling = "cosine" # Match teacher
sigma_min = 1e-06 # Match teacher
# Paths
teacher_checkpoint_path = "/mnt/nvme/checkpoint/dit_direct/model_direct_final.pth"
checkpoint_root_path = '/mnt/nvme/checkpoint/splitmeanflow/'
os.makedirs(checkpoint_root_path, exist_ok=True)
os.makedirs('/mnt/nvme/images_student', exist_ok=True)
# Initialize Comet ML experiment
experiment = Experiment(
project_name="splitmeanflow-student",
)
# Log hyperparameters
experiment.log_parameters({
"n_steps": n_steps,
"batch_size": batch_size,
"learning_rate": 1e-4,
"model": "SplitMeanFlow-DiT",
"teacher_checkpoint": teacher_checkpoint_path,
"flow_ratio": flow_ratio,
"teacher_cfg_scale": teacher_cfg_scale,
"time_sampling": time_sampling,
"sigma_min": sigma_min,
"ema_decay": ema_decay,
"image_size": image_size,
"optimizer": "Adam",
"mixed_precision": "bfloat16",
})
# Dataset (same as teacher)
dataset = torchvision.datasets.CIFAR10(
root="/mnt/nvme/",
train=True,
download=True,
transform=T.Compose([
T.Resize((image_size, image_size)),
T.RandomHorizontalFlip(),
T.ToTensor(), # [0, 1]
]),
)
def cycle(iterable):
while True:
for i in iterable:
yield i
train_dataloader = torch.utils.data.DataLoader(
dataset, batch_size=batch_size, shuffle=True, drop_last=True, num_workers=8
)
train_dataloader = cycle(train_dataloader)
# Load teacher model
print("Loading teacher model...")
teacher_dit = DiT(
input_size=image_size,
patch_size=2,
in_channels=image_channels,
dim=384,
depth=12,
num_heads=6,
num_classes=10,
learn_sigma=False,
class_dropout_prob=0.1,
).to(device)
# Load teacher checkpoint
checkpoint = torch.load(teacher_checkpoint_path, map_location=device)
if 'ema' in checkpoint:
teacher_dit.load_state_dict(checkpoint['ema'])
print("Loaded teacher EMA weights")
else:
teacher_dit.load_state_dict(checkpoint['model'])
print("Loaded teacher model weights")
# Create teacher RectifiedFlow wrapper
teacher_model = RectifiedFlow(
net=teacher_dit,
device=device,
channels=image_channels,
image_size=image_size,
num_classes=10,
)
teacher_model.eval()
# Create student model
print("Creating student model from teacher...")
student_net = create_student_from_teacher(teacher_dit).to(device)
# Create student EMA model (same as teacher)
print("Creating student EMA model...")
student_ema = deepcopy(student_net).to(device)
requires_grad(student_ema, False)
student_ema.eval()
update_ema(student_ema, student_net, decay=0) # Full copy
# Create SplitMeanFlow trainer
splitmeanflow = SplitMeanFlow(
student_net=student_net,
teacher_model=teacher_model,
device=device,
channels=image_channels,
image_size=image_size,
num_classes=10,
flow_ratio=flow_ratio,
cfg_scale=teacher_cfg_scale,
time_sampling=time_sampling,
sigma_min=sigma_min,
)
# Create SplitMeanFlow wrapper for EMA model (for sampling)
splitmeanflow_ema = SplitMeanFlow(
student_net=student_ema,
teacher_model=teacher_model,
device=device,
channels=image_channels,
image_size=image_size,
num_classes=10,
flow_ratio=flow_ratio,
cfg_scale=teacher_cfg_scale,
time_sampling=time_sampling,
sigma_min=sigma_min,
)
# Optimizer (same as teacher)
optimizer = optim.Adam(student_net.parameters(), lr=1e-4, weight_decay=0.01)
scaler = torch.cuda.amp.GradScaler()
def sample_and_log_images():
"""Sample images from both regular and EMA models"""
for num_steps in [1, 2]:
print(f"Sampling {num_steps}-step images at step {step}...")
# Sample from regular model
student_net.eval()
with torch.no_grad():
samples = splitmeanflow.sample_each_class(
n_per_class=10,
num_steps=num_steps,
cfg_scale=None # No CFG at inference
)
log_img = make_grid(samples, nrow=10)
img_save_path = f"/mnt/nvme/images_student/step{step}_{num_steps}step.png"
save_image(log_img, img_save_path)
experiment.log_image(
img_save_path,
name=f"{num_steps}step_generation",
step=step
)
student_net.train()
# Sample from EMA model
print(f"Sampling {num_steps}-step images from EMA model...")
with torch.no_grad():
samples_ema = splitmeanflow_ema.sample_each_class(
n_per_class=10,
num_steps=num_steps,
cfg_scale=None
)
log_img_ema = make_grid(samples_ema, nrow=10)
img_save_path_ema = f"/mnt/nvme/images_student/step{step}_{num_steps}step_ema.png"
save_image(log_img_ema, img_save_path_ema)
experiment.log_image(
img_save_path_ema,
name=f"{num_steps}step_generation_ema",
step=step
)
# Training loop
losses = []
boundary_losses = []
consistency_losses = []
with tqdm(range(n_steps), dynamic_ncols=True) as pbar:
pbar.set_description("Training SplitMeanFlow")
for step in pbar:
data = next(train_dataloader)
optimizer.zero_grad()
# Get images and labels
x1 = data[0].to(device) # Already in [0, 1]
y = data[1].to(device)
# Forward pass with mixed precision
with torch.cuda.amp.autocast(dtype=torch.bfloat16):
# Modified to return loss type
loss, loss_type = splitmeanflow(x1, y)
scaler.scale(loss).backward()
scaler.unscale_(optimizer)
torch.nn.utils.clip_grad_norm_(student_net.parameters(), max_norm=1.0)
scaler.step(optimizer)
scaler.update()
# Update EMA
update_ema(student_ema, student_net, decay=ema_decay)
# Track losses
losses.append(loss.item())
if loss_type == "boundary":
boundary_losses.append(loss.item())
else:
consistency_losses.append(loss.item())
pbar.set_postfix({"loss": loss.item(), "type": loss_type})
experiment.log_metric("loss", loss.item(), step=step)
experiment.log_metric(f"{loss_type}_loss", loss.item(), step=step)
if step % 100 == 0:
avg_loss = sum(losses[-100:]) / min(100, len(losses))
experiment.log_metric("avg_loss_100", avg_loss, step=step)
if step % 2000 == 0 or step == n_steps - 1:
avg_loss = sum(losses) / len(losses) if losses else 0
avg_boundary = sum(boundary_losses) / len(boundary_losses) if boundary_losses else 0
avg_consistency = sum(consistency_losses) / len(consistency_losses) if consistency_losses else 0
print(f"\nStep: {step+1}/{n_steps}")
print(f"Average loss: {avg_loss:.4f}")
print(f"Boundary loss: {avg_boundary:.4f} (n={len(boundary_losses)})")
print(f"Consistency loss: {avg_consistency:.4f} (n={len(consistency_losses)})")
losses.clear()
boundary_losses.clear()
consistency_losses.clear()
sample_and_log_images()
if step % 5000 == 0 or step == n_steps - 1:
# Save checkpoint
checkpoint_path = os.path.join(checkpoint_root_path, f"step_{step}.pth")
state_dict = {
"model": student_net.state_dict(),
"ema": student_ema.state_dict(),
"optimizer": optimizer.state_dict(),
"step": step,
"config": {
"image_size": image_size,
"image_channels": image_channels,
"ema_decay": ema_decay,
"sigma_min": sigma_min,
"flow_ratio": flow_ratio,
"teacher_cfg_scale": teacher_cfg_scale,
"time_sampling": time_sampling,
}
}
torch.save(state_dict, checkpoint_path)
experiment.log_model(
name=f"checkpoint_step_{step}",
file_or_folder=checkpoint_path
)
# Final save
checkpoint_path = os.path.join(checkpoint_root_path, "student_final.pth")
state_dict = {
"model": student_net.state_dict(),
"ema": student_ema.state_dict(),
"config": {
"image_size": image_size,
"image_channels": image_channels,
"ema_decay": ema_decay,
"sigma_min": sigma_min,
"flow_ratio": flow_ratio,
"teacher_cfg_scale": teacher_cfg_scale,
"time_sampling": time_sampling,
}
}
torch.save(state_dict, checkpoint_path)
experiment.log_model(
name="final_model",
file_or_folder=checkpoint_path
)
experiment.end()
if __name__ == "__main__":
main()