forked from NVlabs/FastGen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsft.py
More file actions
247 lines (206 loc) · 9.09 KB
/
Copy pathsft.py
File metadata and controls
247 lines (206 loc) · 9.09 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
# 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
from fastgen.methods import FastGenModel
from fastgen.methods.common_loss import denoising_score_matching_loss
from fastgen.utils import expand_like
from fastgen.utils import basic_utils
if TYPE_CHECKING:
from fastgen.configs.methods.config_sft import ModelConfig
from fastgen.networks.network import FastGenNetwork
class SFTModel(FastGenModel):
def __init__(self, config: ModelConfig):
"""
Args:
config (ModelConfig): The configuration for the DMD model
"""
super().__init__(config)
self.config = config
def build_model(self):
super().build_model()
self.load_student_weights_and_ema()
def _mix_condition(self, condition, neg_condition):
"""
Mix condition with neg_condition based on cond_dropout_prob.
Works for torch.Tensor or dict[str, torch.Tensor].
Args:
condition: torch.Tensor or dict of tensors
neg_condition: same type/structure as condition
"""
if self.config.cond_dropout_prob is None:
return condition
if isinstance(condition, torch.Tensor):
batch_size = condition.shape[0]
sample_mask = (
torch.rand(batch_size, device=condition.device, dtype=condition.dtype) >= self.config.cond_dropout_prob
)
mask = expand_like(sample_mask, condition)
return torch.where(mask, condition, neg_condition)
elif isinstance(condition, dict):
# Pick one tensor to generate the mask
keys_no_drop = self.config.cond_keys_no_dropout
assert set(keys_no_drop).issubset(
condition.keys()
), f"keys_no_drop: {keys_no_drop} not in {condition.keys()}"
# Generate per-sample mask once, reuse for all keys
first_key = next(iter(condition.keys() - keys_no_drop), None)
if first_key is not None:
tensor = condition[first_key]
batch_size = tensor.shape[0]
sample_mask = (
torch.rand(batch_size, device=tensor.device, dtype=tensor.dtype) >= self.config.cond_dropout_prob
)
condition = condition.copy()
for k in condition.keys() - keys_no_drop:
mask = expand_like(sample_mask, condition[k])
condition[k] = torch.where(mask, condition[k], neg_condition[k])
return condition
else:
raise TypeError(f"Unsupported type: {type(condition)}")
def _get_outputs(
self,
gen_data: torch.Tensor,
input_student: torch.Tensor = None,
condition: Any = None,
neg_condition: Any = None,
) -> Dict[str, torch.Tensor | Callable]:
noise = torch.randn_like(gen_data, dtype=self.precision)
gen_rand_func = partial(
self.generator_fn,
net=self.net_inference,
noise=noise,
condition=condition,
neg_condition=neg_condition,
precision_amp=self.precision_amp_infer,
guidance_scale=self.config.guidance_scale,
num_steps=self.config.student_sample_steps,
)
return {"gen_rand": gen_rand_func, "input_rand": noise}
@classmethod
def generator_fn(
cls,
net: FastGenNetwork,
noise: torch.Tensor,
precision_amp: Optional[torch.dtype] = None,
**kwargs,
):
assert hasattr(net, "sample"), "net must have the sample() method"
with basic_utils.inference_mode(net, precision_amp=precision_amp, device_type=noise.device.type):
x = net.sample(noise, **kwargs)
return x.to(dtype=noise.dtype)
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 supervised finetuning (sft)
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
"""
real_data, condition, neg_condition = self._prepare_training_data(data)
batch_size = real_data.shape[0]
# Perturb the data according to the underlying time sampling type
t = self.net.noise_scheduler.sample_t(
batch_size,
**basic_utils.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)
# replace condition with neg_condition with a probability for training
# but keep original condition for _get_outputs (sampling uses original condition)
condition_train = self._mix_condition(condition, neg_condition)
noisy_real_data = self.net.noise_scheduler.forward_process(real_data, eps, t)
net_pred = self.net(noisy_real_data, t, condition=condition_train)
loss = denoising_score_matching_loss(
self.net.net_pred_type,
net_pred=net_pred,
noise_scheduler=self.net.noise_scheduler,
x0=real_data,
eps=eps,
t=t,
)
# Build output dictionaries
loss_map = {
"total_loss": loss,
"dsm_loss": loss,
}
outputs = self._get_outputs(net_pred, condition=condition, neg_condition=neg_condition)
return loss_map, outputs
class CausalSFTModel(SFTModel):
def __init__(self, config: ModelConfig):
super().__init__(config)
def _get_outputs(
self,
gen_data: torch.Tensor,
input_student: torch.Tensor = None,
condition: Any = None,
neg_condition: Any = None,
) -> Dict[str, torch.Tensor | Callable]:
noise = torch.randn_like(gen_data, dtype=self.precision)
context_noise = getattr(self.config, "context_noise", 0)
gen_rand_func = partial(
self.generator_fn,
net=self.net_inference,
noise=noise,
condition=condition,
neg_condition=neg_condition,
precision_amp=self.precision_amp_infer,
guidance_scale=self.config.guidance_scale,
sample_steps=self.config.student_sample_steps,
context_noise=context_noise,
)
return {"gen_rand": gen_rand_func, "input_rand": noise}
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 supervised finetuning (sft)
Args:
data (Dict[str, Any]): Data dict for the current iteration.
iteration (int): Current training iteration
Returns:
loss (torch.Tensor): total loss
loss_map (dict[str, torch.Tensor]): Dictionary containing the loss values
outputs (dict[str, torch.Tensor]): Dictionary containing the network output
"""
real_data, condition, neg_condition = self._prepare_training_data(data)
batch_size = real_data.shape[0]
# Add noise to real image data (for multistep generation)
eps_inhom = torch.randn(batch_size, *self.input_shape, device=self.device, dtype=real_data.dtype)
assert hasattr(
self.net.noise_scheduler, "sample_t_inhom_sft"
), "net.noise_scheduler does not have the sample_t_inhom_sft() method"
t_inhom = self.net.noise_scheduler.sample_t_inhom_sft(
batch_size,
self.input_shape[1],
self.net.chunk_size,
**basic_utils.convert_cfg_to_dict(self.config.sample_t_cfg),
device=self.device,
)
t_inhom_expanded = t_inhom[:, None, :, None, None] # shape [B, 1, T, 1, 1]
noisy_real_data = self.net.noise_scheduler.forward_process(real_data, eps_inhom, t_inhom_expanded)
# replace condition with neg_condition with a probability for training
# but keep original condition for _get_outputs (sampling uses original condition)
condition_train = self._mix_condition(condition, neg_condition)
net_pred = self.net(noisy_real_data, t_inhom, condition=condition_train)
loss = denoising_score_matching_loss(
self.net.net_pred_type,
net_pred=net_pred,
noise_scheduler=self.net.noise_scheduler,
x0=real_data,
eps=eps_inhom,
t=t_inhom,
)
# Build output dictionaries
loss_map = {
"total_loss": loss,
"dsm_loss": loss,
}
outputs = self._get_outputs(net_pred, condition=condition, neg_condition=neg_condition)
return loss_map, outputs