-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler_diffusers.py
223 lines (174 loc) · 9.26 KB
/
scheduler_diffusers.py
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
from dataclasses import dataclass
from typing import Tuple, Any, Optional, Union
import torch
from diffusers.configuration_utils import ConfigMixin, register_to_config
from diffusers.utils import BaseOutput
from diffusers.schedulers.scheduling_utils import SchedulerMixin
@dataclass
class FlowMatchingEulerSchedulerOutput(BaseOutput):
"""
Output class for the scheduler's `step` function output.
Args:
prev_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images):
Computed sample `(x_{t-1})` of previous timestep (which in flow-matching notation should be noted as
`(x_{t+h})`). `prev_sample` should be used as next model input in the denoising loop.
pred_original_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images):
The predicted denoised sample `(x_{0})` (which in flow-matching notation should be noted as
`(x_{1})`) based on the model output from the current timestep.
`pred_original_sample` can be used to preview progress or for guidance.
"""
prev_sample: torch.Tensor
pred_original_sample: Optional[torch.Tensor] = None
@dataclass
class InverseFlowMatchingEulerSchedulerOutput(BaseOutput):
"""
Output class for the scheduler's `step` function output.
Args:
prev_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images):
Computed sample `(x_{t-1})` of previous timestep (which in flow-matching notation should be noted as
`(x_{t+h})`). `prev_sample` should be used as next model input in the denoising loop.
pred_original_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images):
The predicted denoised sample `(x_{0})` (which in flow-matching notation should be noted as
`(x_{1})`) based on the model output from the current timestep.
`pred_original_sample` can be used to preview progress or for guidance.
"""
prev_sample: torch.Tensor
pred_original_sample: Optional[torch.Tensor] = None
def get_time_coefficients(timestep: torch.Tensor, ndim: int) -> torch.Tensor:
return timestep.reshape((timestep.shape[0], *([1] * (ndim - 1))))
class FlowMatchingEulerScheduler(SchedulerMixin, ConfigMixin):
"""
`FlowMatchingEulerScheduler` is a scheduler for training and inferencing Conditional Flow Matching models (CFMs).
Flow Matching (FM) is a novel, simulation-free methodology for training Continuous Normalizing Flows (CNFs) by
regressing vector fields of predetermined conditional probability paths, facilitating scalable training and
efficient sample generation through the utilization of various probability paths, including Gaussian and
Optimal Transport (OT) paths, thereby enhancing model performance and generalization capabilities
Args:
num_inference_steps (`int`, defaults to 100):
The number of steps on inference.
"""
@register_to_config
def __init__(self, num_inference_steps: int = 100):
self.timesteps = None
self.num_inference_steps = None
self.h = None
if num_inference_steps is not None:
self.set_timesteps(num_inference_steps)
@staticmethod
def add_noise(original_samples: torch.Tensor, noise: torch.Tensor, timestep: torch.Tensor) -> torch.Tensor:
"""
Add noise to the given sample
Args:
original_samples (`torch.Tensor`):
The original sample that is to be noised
noise (`torch.Tensor`):
The noise that is used to noise the image
timestep (`torch.Tensor`):
Timestep used to create linear interpolation `x_t = t * x_1 + (1 - t) * x_0`.
Where x_1 is a target distribution, x_0 is a source distribution and t (timestep) ∈ [0, 1]
"""
t = get_time_coefficients(timestep, original_samples.ndim)
noised_sample = t * original_samples + (1 - t) * noise
return noised_sample
def set_timesteps(self, num_inference_steps: int = 100) -> None:
"""
Set number of inference steps (Euler integration steps)
Args:
num_inference_steps (`int`, defaults to 100):
The number of steps on inference.
"""
self.num_inference_steps = num_inference_steps
self.h = 1 / num_inference_steps
self.timesteps = torch.arange(0, 1, self.h)
def set_num_inference_steps(self, num_inference_steps: int = 100) -> None:
"""
Set number of inference steps (Euler intagration steps)
Args:
num_inference_steps (`int`, defaults to 100):
The number of steps on inference.
"""
self.set_timesteps(num_inference_steps)
def step(self, model_output: torch.Tensor, timestep: torch.Tensor, sample: torch.Tensor,
return_dict: bool = True) -> Union[FlowMatchingEulerSchedulerOutput, Tuple]:
"""
Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
process from the learned model outputs (most often the predicted noise).
Args:
model_output (`torch.Tensor`):
The direct output from learned diffusion model.
timestep (`float`):
Timestep used to perform Euler Method `x_t = h * f(x_t, t) + x_{t-1}`.
Where x_1 is a target distribution, x_0 is a source distribution and t (timestep) ∈ [0, 1]
sample (`torch.Tensor`):
A current instance of a sample created by the diffusion process.
return_dict (`bool`, *optional*, defaults to `True`):
Whether or not to return a [`~schedulers.scheduling_ddpm.DDPMSchedulerOutput`] or `tuple`.
Returns:
[`~schedulers.scheduling_ddpm.DDPMSchedulerOutput`] or `tuple`:
If return_dict is `True`, [`~schedulers.scheduling_ddpm.DDPMSchedulerOutput`] is returned, otherwise a
tuple is returned where the first element is the sample tensor.
"""
step = FlowMatchingEulerSchedulerOutput(
prev_sample=sample + self.h * model_output,
pred_original_sample=sample + (1 - get_time_coefficients(timestep, model_output.ndim)) * model_output
)
if return_dict:
return step
return step.prev_sample,
@staticmethod
def get_velocity(original_samples: torch.Tensor, noise: torch.Tensor) -> torch.Tensor:
"""
Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
process from the learned model outputs (most often the predicted noise).
Args:
original_samples (`torch.Tensor`):
The original sample that is to be noised
noise (`torch.Tensor`):
The noise that is used to noise the image
Returns:
`torch.Tensor`
"""
return original_samples - noise
@staticmethod
def scale_model_input(sample: torch.Tensor, timestep: Optional[int] = None) -> torch.Tensor:
"""
Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
current timestep.
Args:
sample (`torch.Tensor`):
The input sample.
timestep (`float`, *optional*):
The current timestep in the diffusion chain.
Returns:
`torch.Tensor`:
A scaled input sample.
"""
return sample
class InverseFlowMatchingEulerScheduler(FlowMatchingEulerScheduler):
def step(self, model_output: torch.Tensor, timestep: torch.Tensor, sample: torch.Tensor,
return_dict: bool = True) -> Union[InverseFlowMatchingEulerSchedulerOutput, Tuple]:
"""
Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
process from the learned model outputs (most often the predicted noise).
Args:
model_output (`torch.Tensor`):
The direct output from learned diffusion model.
timestep (`float`):
Timestep used to perform Euler Method `x_t = h * f(x_t, t) + x_{t-1}`.
Where x_1 is a target distribution, x_0 is a source distribution and t (timestep) ∈ [0, 1]
sample (`torch.Tensor`):
A current instance of a sample created by the diffusion process.
return_dict (`bool`, *optional*, defaults to `True`):
Whether or not to return a [`~schedulers.scheduling_ddpm.DDPMSchedulerOutput`] or `tuple`.
Returns:
[`~schedulers.scheduling_ddpm.DDPMSchedulerOutput`] or `tuple`:
If return_dict is `True`, [`~schedulers.scheduling_ddpm.DDPMSchedulerOutput`] is returned, otherwise a
tuple is returned where the first element is the sample tensor.
"""
step = InverseFlowMatchingEulerSchedulerOutput(
prev_sample=sample - self.h * model_output,
pred_original_sample=sample - (1 - get_time_coefficients(timestep, model_output.ndim)) * model_output
)
if return_dict:
return step
return step.prev_sample,