| 343 | |
| 344 | |
| 345 | class DPM_Solver: |
| 346 | def __init__( |
| 347 | self, |
| 348 | model_fn, |
| 349 | noise_schedule, |
| 350 | algorithm_type="dpmsolver++", |
| 351 | correcting_x0_fn=None, |
| 352 | correcting_xt_fn=None, |
| 353 | thresholding_max_val=1., |
| 354 | dynamic_thresholding_ratio=0.995, |
| 355 | ): |
| 356 | """Construct a DPM-Solver. |
| 357 | |
| 358 | We support both DPM-Solver (`algorithm_type="dpmsolver"`) and DPM-Solver++ (`algorithm_type="dpmsolver++"`). |
| 359 | |
| 360 | We also support the "dynamic thresholding" method in Imagen[1]. For pixel-space diffusion models, you |
| 361 | can set both `algorithm_type="dpmsolver++"` and `correcting_x0_fn="dynamic_thresholding"` to use the |
| 362 | dynamic thresholding. The "dynamic thresholding" can greatly improve the sample quality for pixel-space |
| 363 | DPMs with large guidance scales. Note that the thresholding method is **unsuitable** for latent-space |
| 364 | DPMs (such as stable-diffusion). |
| 365 | |
| 366 | To support advanced algorithms in image-to-image applications, we also support corrector functions for |
| 367 | both x0 and xt. |
| 368 | |
| 369 | Args: |
| 370 | model_fn: A noise prediction model function which accepts the continuous-time input (t in [epsilon, T]): |
| 371 | `` |
| 372 | def model_fn(x, t_continuous): |
| 373 | return noise |
| 374 | `` |
| 375 | The shape of `x` is `(batch_size, **shape)`, and the shape of `t_continuous` is `(batch_size,)`. |
| 376 | noise_schedule: A noise schedule object, such as NoiseScheduleVP. |
| 377 | algorithm_type: A `str`. Either "dpmsolver" or "dpmsolver++". |
| 378 | correcting_x0_fn: A `str` or a function with the following format: |
| 379 | ``` |
| 380 | def correcting_x0_fn(x0, t): |
| 381 | x0_new = ... |
| 382 | return x0_new |
| 383 | ``` |
| 384 | This function is to correct the outputs of the data prediction model at each sampling step. e.g., |
| 385 | ``` |
| 386 | x0_pred = data_pred_model(xt, t) |
| 387 | if correcting_x0_fn is not None: |
| 388 | x0_pred = correcting_x0_fn(x0_pred, t) |
| 389 | xt_1 = update(x0_pred, xt, t) |
| 390 | ``` |
| 391 | If `correcting_x0_fn="dynamic_thresholding"`, we use the dynamic thresholding proposed in Imagen[1]. |
| 392 | correcting_xt_fn: A function with the following format: |
| 393 | ``` |
| 394 | def correcting_xt_fn(xt, t, step): |
| 395 | x_new = ... |
| 396 | return x_new |
| 397 | ``` |
| 398 | This function is to correct the intermediate samples xt at each sampling step. e.g., |
| 399 | ``` |
| 400 | xt = ... |
| 401 | xt = correcting_xt_fn(xt, t, step) |
| 402 | ``` |
no outgoing calls
no test coverage detected