scheduling_unipc_multistep ¶
Classes¶
fastvideo.models.schedulers.scheduling_unipc_multistep.UniPCMultistepScheduler ¶
UniPCMultistepScheduler(num_train_timesteps: int = 1000, beta_start: float = 0.0001, beta_end: float = 0.02, beta_schedule: str = 'linear', trained_betas: Optional[Union[ndarray, List[float]]] = None, solver_order: int = 2, prediction_type: str = 'epsilon', thresholding: bool = False, dynamic_thresholding_ratio: float = 0.995, sample_max_value: float = 1.0, predict_x0: bool = True, solver_type: str = 'bh2', lower_order_final: bool = True, disable_corrector: List[int] = [], solver_p: SchedulerMixin = None, use_karras_sigmas: Optional[bool] = False, use_exponential_sigmas: Optional[bool] = False, use_beta_sigmas: Optional[bool] = False, use_flow_sigmas: Optional[bool] = False, flow_shift: Optional[float] = 1.0, timestep_spacing: str = 'linspace', steps_offset: int = 0, final_sigmas_type: Optional[str] = 'zero', rescale_betas_zero_snr: bool = False, use_dynamic_shifting: bool = False, time_shift_type: str = 'exponential')
Bases: SchedulerMixin, ConfigMixin, BaseScheduler
UniPCMultistepScheduler is a training-free framework designed for the fast sampling of diffusion models.
This model inherits from [SchedulerMixin] and [ConfigMixin]. Check the superclass documentation for the generic methods the library implements for all schedulers such as loading and saving.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_train_timesteps | `int`, defaults to 1000 | The number of diffusion steps to train the model. | 1000 |
beta_start | `float`, defaults to 0.0001 | The starting | 0.0001 |
beta_end | `float`, defaults to 0.02 | The final | 0.02 |
beta_schedule | `str`, defaults to `"linear"` | The beta schedule, a mapping from a beta range to a sequence of betas for stepping the model. Choose from | 'linear' |
trained_betas | `np.ndarray`, *optional* | Pass an array of betas directly to the constructor to bypass | None |
solver_order | `int`, default `2` | The UniPC order which can be any positive integer. The effective order of accuracy is | 2 |
prediction_type | `str`, defaults to `epsilon`, *optional* | Prediction type of the scheduler function; can be | 'epsilon' |
thresholding | `bool`, defaults to `False` | Whether to use the "dynamic thresholding" method. This is unsuitable for latent-space diffusion models such as Stable Diffusion. | False |
dynamic_thresholding_ratio | `float`, defaults to 0.995 | The ratio for the dynamic thresholding method. Valid only when | 0.995 |
sample_max_value | `float`, defaults to 1.0 | The threshold value for dynamic thresholding. Valid only when | 1.0 |
predict_x0 | `bool`, defaults to `True` | Whether to use the updating algorithm on the predicted x0. | True |
solver_type | `str`, default `bh2` | Solver type for UniPC. It is recommended to use | 'bh2' |
lower_order_final | `bool`, default `True` | Whether to use lower-order solvers in the final steps. Only valid for < 15 inference steps. This can stabilize the sampling of DPMSolver for steps < 15, especially for steps <= 10. | True |
disable_corrector | `list`, default `[]` | Decides which step to disable the corrector to mitigate the misalignment between | [] |
solver_p | `SchedulerMixin`, default `None` | Any other scheduler that if specified, the algorithm becomes | None |
use_karras_sigmas | `bool`, *optional*, defaults to `False` | Whether to use Karras sigmas for step sizes in the noise schedule during the sampling process. If | False |
use_exponential_sigmas | `bool`, *optional*, defaults to `False` | Whether to use exponential sigmas for step sizes in the noise schedule during the sampling process. | False |
use_beta_sigmas | `bool`, *optional*, defaults to `False` | Whether to use beta sigmas for step sizes in the noise schedule during the sampling process. Refer to Beta Sampling is All You Need for more information. | False |
use_flow_sigmas | `bool`, *optional*, defaults to `False` | Whether to use flow sigmas for step sizes in the noise schedule during the sampling process. | False |
timestep_spacing | `str`, defaults to `"linspace"` | The way the timesteps should be scaled. Refer to Table 2 of the Common Diffusion Noise Schedules and Sample Steps are Flawed for more information. | 'linspace' |
steps_offset | `int`, defaults to 0 | An offset added to the inference steps, as required by some model families. | 0 |
final_sigmas_type | `str`, defaults to `"zero"` | The final | 'zero' |
rescale_betas_zero_snr | `bool`, defaults to `False` | Whether to rescale the betas to have zero terminal SNR. This enables the model to generate very bright and dark samples instead of limiting it to samples with medium brightness. Loosely related to | False |
Source code in fastvideo/models/schedulers/scheduling_unipc_multistep.py
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 | |
Attributes¶
fastvideo.models.schedulers.scheduling_unipc_multistep.UniPCMultistepScheduler.begin_index property ¶
The index for the first timestep. It should be set from pipeline with set_begin_index method.
fastvideo.models.schedulers.scheduling_unipc_multistep.UniPCMultistepScheduler.step_index property ¶
The index counter for current timestep. It will increase 1 after each scheduler step.
Methods:¶
fastvideo.models.schedulers.scheduling_unipc_multistep.UniPCMultistepScheduler.convert_model_output ¶
Convert the model output to the corresponding type the UniPC algorithm needs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_output | `torch.Tensor` | The direct output from the learned diffusion model. | required |
timestep | `int` | The current discrete timestep in the diffusion chain. | required |
sample | `torch.Tensor` | A current instance of a sample created by the diffusion process. | None |
Returns:
| Type | Description |
|---|---|
Tensor |
|
Source code in fastvideo/models/schedulers/scheduling_unipc_multistep.py
fastvideo.models.schedulers.scheduling_unipc_multistep.UniPCMultistepScheduler.multistep_uni_c_bh_update ¶
multistep_uni_c_bh_update(this_model_output: Tensor, *args, last_sample: Tensor = None, this_sample: Tensor = None, order: int = None, **kwargs) -> Tensor
One step for the UniC (B(h) version).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
this_model_output | `torch.Tensor` | The model outputs at | required |
this_timestep | `int` | The current timestep | required |
last_sample | `torch.Tensor` | The generated sample before the last predictor | None |
this_sample | `torch.Tensor` | The generated sample after the last predictor | None |
order | `int` | The | None |
Returns:
| Type | Description |
|---|---|
Tensor |
|
Source code in fastvideo/models/schedulers/scheduling_unipc_multistep.py
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 | |
fastvideo.models.schedulers.scheduling_unipc_multistep.UniPCMultistepScheduler.multistep_uni_p_bh_update ¶
multistep_uni_p_bh_update(model_output: Tensor, *args, sample: Tensor = None, order: int = None, **kwargs) -> Tensor
One step for the UniP (B(h) version). Alternatively, self.solver_p is used if is specified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_output | `torch.Tensor` | The direct output from the learned diffusion model at the current timestep. | required |
prev_timestep | `int` | The previous discrete timestep in the diffusion chain. | required |
sample | `torch.Tensor` | A current instance of a sample created by the diffusion process. | None |
order | `int` | The order of UniP at this timestep (corresponds to the p in UniPC-p). | None |
Returns:
| Type | Description |
|---|---|
Tensor |
|
Source code in fastvideo/models/schedulers/scheduling_unipc_multistep.py
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 | |
fastvideo.models.schedulers.scheduling_unipc_multistep.UniPCMultistepScheduler.scale_model_input ¶
Ensures interchangeability with schedulers that need to scale the denoising model input depending on the current timestep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample | `torch.Tensor` | The input sample. | required |
Returns:
| Type | Description |
|---|---|
Tensor |
|
Source code in fastvideo/models/schedulers/scheduling_unipc_multistep.py
fastvideo.models.schedulers.scheduling_unipc_multistep.UniPCMultistepScheduler.set_begin_index ¶
set_begin_index(begin_index: int = 0)
Sets the begin index for the scheduler. This function should be run from pipeline before the inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
begin_index | `int` | The begin index for the scheduler. | 0 |
Source code in fastvideo/models/schedulers/scheduling_unipc_multistep.py
fastvideo.models.schedulers.scheduling_unipc_multistep.UniPCMultistepScheduler.set_timesteps ¶
set_timesteps(num_inference_steps: int, device: Union[str, device] = None, mu: Optional[float] = None)
Sets the discrete timesteps used for the diffusion chain (to be run before inference).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_inference_steps | `int` | The number of diffusion steps used when generating samples with a pre-trained model. | required |
device | `str` or `torch.device`, *optional* | The device to which the timesteps should be moved to. If | None |
Source code in fastvideo/models/schedulers/scheduling_unipc_multistep.py
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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
fastvideo.models.schedulers.scheduling_unipc_multistep.UniPCMultistepScheduler.step ¶
step(model_output: Tensor, timestep: Union[int, Tensor], sample: Tensor, return_dict: bool = True) -> Union[SchedulerOutput, Tuple]
Predict the sample from the previous timestep by reversing the SDE. This function propagates the sample with the multistep UniPC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_output | `torch.Tensor` | The direct output from learned diffusion model. | required |
timestep | `int` | The current discrete timestep in the diffusion chain. | required |
sample | `torch.Tensor` | A current instance of a sample created by the diffusion process. | required |
return_dict | `bool` | Whether or not to return a [ | True |
Returns:
| Type | Description |
|---|---|
Union[SchedulerOutput, Tuple] | [ |
Source code in fastvideo/models/schedulers/scheduling_unipc_multistep.py
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 | |
Functions:¶
fastvideo.models.schedulers.scheduling_unipc_multistep.betas_for_alpha_bar ¶
Create a beta schedule that discretizes the given alpha_t_bar function, which defines the cumulative product of (1-beta) over time from t = [0,1].
Contains a function alpha_bar that takes an argument t and transforms it to the cumulative product of (1-beta) up to that part of the diffusion process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_diffusion_timesteps | `int` | the number of betas to produce. | required |
max_beta | `float` | the maximum beta to use; use values lower than 1 to prevent singularities. | 0.999 |
alpha_transform_type | `str`, *optional*, default to `cosine` | the type of noise schedule for alpha_bar. Choose from | 'cosine' |
Returns:
| Name | Type | Description |
|---|---|---|
betas | `np.ndarray` | the betas used by the scheduler to step the model outputs |
Source code in fastvideo/models/schedulers/scheduling_unipc_multistep.py
fastvideo.models.schedulers.scheduling_unipc_multistep.rescale_zero_terminal_snr ¶
Rescales betas to have zero terminal SNR Based on https://huggingface.co/papers/2305.08891 (Algorithm 1)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
betas | `torch.Tensor` | the betas that the scheduler is being initialized with. | required |
Returns:
| Type | Description |
|---|---|
|
|