forked from NVlabs/FastGen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathladd.py
More file actions
386 lines (326 loc) · 14.2 KB
/
Copy pathladd.py
File metadata and controls
386 lines (326 loc) · 14.2 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
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
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
from functools import partial
from typing import Dict, Any, TYPE_CHECKING, Callable, Optional
import torch
import torch.nn.functional as F
from fastgen.configs.opt import get_scheduler
from fastgen.methods import FastGenModel
from fastgen.methods.common_loss import (
gan_loss_generator,
gan_loss_discriminator,
)
from fastgen.utils import instantiate
import fastgen.utils.logging_utils as logger
from fastgen.utils.basic_utils import convert_cfg_to_dict
if TYPE_CHECKING:
from fastgen.configs.methods.config_ladd import ModelConfig
class LADDModel(FastGenModel):
def __init__(self, config: ModelConfig):
"""
Args:
config (ModelConfig): The configuration for the LADD model
"""
super().__init__(config)
self.config = config
def build_model(self):
super().build_model()
# instantiate the teacher and load student weights
self.build_teacher()
self.load_student_weights_and_ema()
# instantiate the discriminator
logger.info("Instantiating the discriminator")
# TODO: Discriminators do not yet support meta initialization
self.discriminator = instantiate(self.config.discriminator)
def _setup_grad_requirements(self, iteration: int) -> None:
if iteration % self.config.student_update_freq == 0:
# update the student
self.net.train().requires_grad_(True)
self.discriminator.eval().requires_grad_(False)
else:
# update the discriminator
self.net.eval().requires_grad_(False)
self.discriminator.train().requires_grad_(True)
def _generate_noise_and_time(
self, real_data: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
"""Generate random noises and time step
Args:
batch_size: Batch size
real_data: Real data tensor for dtype/device reference
Returns:
rand_z_max: Random noise used by the student
t_max: Time step used by the student
t: Time step
eps: Random noise used by a forward process
"""
batch_size = real_data.shape[0]
eps_student = torch.randn(batch_size, *self.input_shape, device=self.device, dtype=real_data.dtype)
if self.config.student_sample_steps == 1:
# perform single-step distillation
# input noise to student (sigma * eps)
t_student = torch.full(
(batch_size,),
self.net.noise_scheduler.max_t,
device=self.device,
dtype=self.net.noise_scheduler.t_precision,
)
input_student = self.net.noise_scheduler.latents(noise=eps_student)
else:
# perform multiple-step distillation
# Add noise to real image data (for multistep generation)
t_student = self.net.noise_scheduler.sample_from_t_list(
batch_size,
sample_steps=self.config.student_sample_steps,
t_list=self.config.sample_t_cfg.t_list,
device=self.device,
)
input_student = self.net.noise_scheduler.forward_process(real_data, eps_student, t_student)
t = self.net.noise_scheduler.sample_t(
batch_size, **convert_cfg_to_dict(self.config.sample_t_cfg), device=self.device
)
eps = torch.randn_like(real_data, device=self.device, dtype=real_data.dtype)
return input_student, t_student, t, eps
def _student_update_step(
self,
input_student: torch.Tensor,
t_student: torch.Tensor,
t: torch.Tensor,
eps: torch.Tensor,
data: Dict[str, Any],
condition: Optional[Any] = None,
) -> tuple[dict[str, torch.Tensor], dict[str, torch.Tensor | Callable]]:
"""Perform student model update step.
Args:
input_student: Input tensor to student network
t_student: Input time to student network
t: Time step
eps: Noise tensor
data: Original data batch
condition: Conditioning information
Returns:
tuple of (loss_map, outputs)
"""
# Generate data from student
gen_data = self.gen_data_from_net(input_student, t_student, condition=condition)
perturbed_data = self.net.noise_scheduler.forward_process(gen_data, eps, t)
# Compute the discriminator score
fake_feat = self.teacher(
perturbed_data,
t,
condition=condition,
return_features_early=True,
feature_indices=self.discriminator.feature_indices,
)
# Compute the GAN loss for the generator
gan_loss_gen = gan_loss_generator(self.discriminator(fake_feat))
# Build output dictionaries
loss_map = {
"total_loss": gan_loss_gen,
"gan_loss_gen": gan_loss_gen,
}
outputs = self._get_outputs(gen_data, input_student, condition=condition)
return loss_map, outputs
def _compute_real_feat(
self, real_data: torch.Tensor, t: torch.Tensor, eps: torch.Tensor, condition: Optional[Any] = None
) -> tuple[torch.Tensor, torch.Tensor]:
"""Compute discriminator features for both real and fake data.
Args:
real_data: Real data tensor
condition: Conditioning information
t: Time step
eps: Noise tensor
Returns:
tuple of (real_feat, t_real)
"""
# decide whether to use the same t and noise for real and fake data
if self.config.gan_use_same_t_noise:
t_real = t
eps_real = eps
else:
t_real = self.net.noise_scheduler.sample_t(
real_data.shape[0],
**convert_cfg_to_dict(self.config.sample_t_cfg),
device=self.device,
)
eps_real = torch.randn_like(real_data)
# Perturb the real data according to the given forward process
perturbed_real = self.net.noise_scheduler.forward_process(real_data, eps_real, t_real)
real_feat = self.teacher(
perturbed_real,
t_real,
condition=condition,
return_features_early=True,
feature_indices=self.discriminator.feature_indices,
)
return real_feat, t_real
def _compute_r1_regularization(
self,
real_feat_logit: torch.Tensor,
real_data: torch.Tensor,
t_real: torch.Tensor,
condition: Optional[Any] = None,
) -> torch.Tensor:
"""Compute R1 regularization loss for discriminator.
Args:
real_feat_logit: Real feature logits
real_data: Real data tensor
t_real: Time step for real data
condition: Conditioning information
Returns:
R1 regularization loss
"""
perturbed_real_alpha = real_data.add(self.config.gan_r1_reg_alpha * torch.randn_like(real_data))
with torch.no_grad():
real_feat_alpha = self.teacher(
perturbed_real_alpha,
t_real,
condition=condition,
return_features_early=True,
feature_indices=self.discriminator.feature_indices,
)
real_feat_alpha_logit = self.discriminator(real_feat_alpha)
gan_loss_ar1 = F.mse_loss(real_feat_logit, real_feat_alpha_logit, reduction="mean")
return gan_loss_ar1
def _discriminator_update_step(
self,
input_student: torch.Tensor,
t_student: torch.Tensor,
t: torch.Tensor,
eps: torch.Tensor,
real_data: torch.Tensor,
condition: Optional[Any] = None,
) -> tuple[dict[str, torch.Tensor], dict[str, torch.Tensor]]:
"""Perform fake score and discriminator update step.
Args:
input_student: Input tensor to student network
t_student: Input time to student network
t: Time steps
eps: Noise tensor
real_data: Real data tensor
condition: Conditioning information
Returns:
tuple of (loss_map, outputs)
"""
with torch.no_grad():
# generate data and compute fake score loss
gen_data = self.gen_data_from_net(input_student, t_student, condition=condition)
x_t_sg = self.net.noise_scheduler.forward_process(gen_data, eps, t)
# extract real and fake features from teacher
fake_feat = self.teacher(
x_t_sg,
t,
condition=condition,
return_features_early=True,
feature_indices=self.discriminator.feature_indices,
)
real_feat, t_real = self._compute_real_feat(real_data=real_data, t=t, eps=eps, condition=condition)
real_feat_logit = self.discriminator(real_feat)
gan_loss_disc = gan_loss_discriminator(real_feat_logit, self.discriminator(fake_feat))
# Use approximate R1 regularization in the APT paper to regularize the discriminator head
gan_loss_ar1 = torch.zeros_like(gan_loss_disc)
if self.config.gan_r1_reg_weight > 0:
gan_loss_ar1 = self._compute_r1_regularization(real_feat_logit, real_data, t_real, condition=condition)
total_loss = gan_loss_disc + self.config.gan_r1_reg_weight * gan_loss_ar1
loss_map = {
"gan_loss_disc": gan_loss_disc,
"total_loss": total_loss,
}
if self.config.gan_r1_reg_weight > 0:
loss_map.update({"gan_loss_ar1": gan_loss_ar1})
outputs = self._get_outputs(gen_data, input_student, condition=condition)
return loss_map, outputs
def _get_outputs(
self,
gen_data: torch.Tensor,
input_student: torch.Tensor = None,
condition: Any = None,
) -> Dict[str, torch.Tensor | Callable]:
if self.config.student_sample_steps == 1:
assert input_student is not None, "input_student must not be None"
noise = input_student / self.net.noise_scheduler.max_sigma
return {"gen_rand": gen_data, "input_rand": noise}
else:
noise = torch.randn_like(gen_data, dtype=self.precision)
gen_rand_func = partial(
self.generator_fn,
net=self.net_inference,
noise=noise,
condition=condition,
student_sample_steps=self.config.student_sample_steps,
student_sample_type=self.config.student_sample_type,
t_list=self.config.sample_t_cfg.t_list,
precision_amp=self.precision_amp_infer,
)
return {"gen_rand": gen_rand_func, "input_rand": noise, "gen_rand_train": gen_data}
def single_train_step(
self, data: Dict[str, Any], iteration: int
) -> tuple[dict[str, torch.Tensor], dict[str, torch.Tensor | Callable]]:
"""
Single training step for Latent Adversarial Distillation (LADD)
Args:
data (Dict[str, Any]): Data dict for the current iteration.
iteration (int): Current training iteration
Returns:
loss_map (dict[str, torch.Tensor]): Dictionary containing the loss values
outputs (dict[str, torch.Tensor]): Dictionary containing the network output
"""
# Prepare training data and conditions
real_data, condition, _ = self._prepare_training_data(data)
# Set up gradient requirements based on training phase
self._setup_grad_requirements(iteration)
# Generate noise and time steps
input_student, t_student, t, eps = self._generate_noise_and_time(real_data)
# Choose between student update or fake_score/discriminator update
if iteration % self.config.student_update_freq == 0:
return self._student_update_step(input_student, t_student, t, eps, data, condition=condition)
else:
return self._discriminator_update_step(input_student, t_student, t, eps, real_data, condition=condition)
def init_optimizers(self):
"""Initialize optimizers, lr_schedulers and grad_scalers"""
super().init_optimizers()
# instantiate the optimizer for discriminator
self.discriminator_optimizer = instantiate(self.config.discriminator_optimizer, model=self.discriminator)
# instantiate the lr scheduler for discriminator
self.discriminator_lr_scheduler = get_scheduler(
self.discriminator_optimizer, self.config.discriminator_scheduler
)
def get_optimizers(self, iteration: int) -> list[torch.optim.Optimizer]:
"""
Get the optimizers for the current iteration
Args:
iteration (int): The current training iteration
"""
if iteration % self.config.student_update_freq == 0:
return [self.net_optimizer]
else:
return [self.discriminator_optimizer]
def get_lr_schedulers(self, iteration: int) -> list[torch.optim.lr_scheduler]:
"""
Get the lr schedulers for the current iteration
Args:
iteration (int): The current training iteration
"""
if iteration % self.config.student_update_freq == 0:
return [self.net_lr_scheduler]
else:
return [self.discriminator_lr_scheduler]
@property
def model_dict(self):
"""Return the model dict containing the student and discriminator models"""
_model_dict = super().model_dict
_model_dict["discriminator"] = self.discriminator
return _model_dict
@property
def optimizer_dict(self):
"""Return a dict containing all the optimizers"""
_optimizer_dict = super().optimizer_dict
_optimizer_dict["discriminator"] = self.discriminator_optimizer
return _optimizer_dict
@property
def scheduler_dict(self):
"""Return a dict containing all the lr schedulers"""
_scheduler_dict = super().scheduler_dict
_scheduler_dict["discriminator"] = self.discriminator_lr_scheduler
return _scheduler_dict