MCPcopy Create free account
hub / github.com/294coder/Dif-PAN / model_wrapper

Function model_wrapper

solver/dpm_solver.py:178–342  ·  view source on GitHub ↗

Create a wrapper function for the noise prediction model. DPM-Solver needs to solve the continuous-time diffusion ODEs. For DPMs trained on discrete-time labels, we need to firstly wrap the model function to a noise prediction model that accepts the continuous time as the input. We sup

(
    model,
    noise_schedule,
    model_type="noise",
    model_kwargs={},
    guidance_type="uncond",
    condition=None,
    unconditional_condition=None,
    guidance_scale=1.,
    classifier_fn=None,
    classifier_kwargs={},
)

Source from the content-addressed store, hash-verified

176
177
178def model_wrapper(
179 model,
180 noise_schedule,
181 model_type="noise",
182 model_kwargs={},
183 guidance_type="uncond",
184 condition=None,
185 unconditional_condition=None,
186 guidance_scale=1.,
187 classifier_fn=None,
188 classifier_kwargs={},
189):
190 """Create a wrapper function for the noise prediction model.
191
192 DPM-Solver needs to solve the continuous-time diffusion ODEs. For DPMs trained on discrete-time labels, we need to
193 firstly wrap the model function to a noise prediction model that accepts the continuous time as the input.
194
195 We support four types of the diffusion model by setting `model_type`:
196
197 1. "noise": noise prediction model. (Trained by predicting noise).
198
199 2. "x_start": data prediction model. (Trained by predicting the data x_0 at time 0).
200
201 3. "v": velocity prediction model. (Trained by predicting the velocity).
202 The "v" prediction is derivation detailed in Appendix D of [1], and is used in Imagen-Video [2].
203
204 [1] Salimans, Tim, and Jonathan Ho. "Progressive distillation for fast sampling of diffusion models."
205 arXiv preprint arXiv:2202.00512 (2022).
206 [2] Ho, Jonathan, et al. "Imagen Video: High Definition Video Generation with Diffusion Models."
207 arXiv preprint arXiv:2210.02303 (2022).
208
209 4. "score": marginal score function. (Trained by denoising score matching).
210 Note that the score function and the noise prediction model follows a simple relationship:
211 ```
212 noise(x_t, t) = -sigma_t * score(x_t, t)
213 ```
214
215 We support three types of guided sampling by DPMs by setting `guidance_type`:
216 1. "uncond": unconditional sampling by DPMs.
217 The input `model` has the following format:
218 ``
219 model(x, t_input, **model_kwargs) -> noise | x_start | v | score
220 ``
221
222 2. "classifier": classifier guidance sampling [3] by DPMs and another classifier.
223 The input `model` has the following format:
224 ``
225 model(x, t_input, **model_kwargs) -> noise | x_start | v | score
226 ``
227
228 The input `classifier_fn` has the following format:
229 ``
230 classifier_fn(x, t_input, cond, **classifier_kwargs) -> logits(x, t_input, cond)
231 ``
232
233 [3] P. Dhariwal and A. Q. Nichol, "Diffusion models beat GANs on image synthesis,"
234 in Advances in Neural Information Processing Systems, vol. 34, 2021, pp. 8780-8794.
235

Callers 2

forwardMethod · 0.90
dpm_solver.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected