§
    ‘M§j¥‚  ã                   ó¤   — d dl Z d dlmZmZmZmZ d dlZd dlZd dl	m
Z
mZ d dlmZmZmZ d dlmZmZ  e¦   «         rd dlZ G d„ dee
¦  «        ZdS )é    N)ÚListÚOptionalÚTupleÚUnion)ÚConfigMixinÚregister_to_config)ÚKarrasDiffusionSchedulersÚSchedulerMixinÚSchedulerOutput)Ú	deprecateÚis_scipy_availablec            !       óŽ  — e Zd ZdZd„ eD ¦   «         ZdZeddddddd	dd
dd
g ddddfdedede	de
e         dedededede	dedee         dede	dede
e	         fd„¦   «         Zed „ ¦   «         Zed!„ ¦   «         ZdGd"efd#„Z	 	 	 	 	 dHd$eedf         d%ee	ej        f         d&e
ee                  d'e
eedf                  de
eedf                  f
d(„Zd)ej        d*ej        fd+„Zd,„ Zd-„ Zd'ed.ed/ej        fd0„Zdd1œd2ej        d)ej        d*ej        fd3„Zddd4œd2ej        d)ej        d5ed*ej        fd6„Zdddd7œd8ej        d9ej        d:ej        d5ed*ej        f
d;„ZdId<„Z d=„ Z!	 	 dJd2ej        d>eeej        f         d)ej        d?ed*ee"e#f         f
d@„Z$d)ej        d*ej        fdA„Z%dBej        dCej        dDej&        d*ej        fdE„Z'dF„ Z(dS )KÚFlowUniPCMultistepScheduleruŒ  
    `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.

    Args:
        num_train_timesteps (`int`, defaults to 1000):
            The number of diffusion steps to train the model.
        solver_order (`int`, default `2`):
            The UniPC order which can be any positive integer. The effective order of accuracy is `solver_order + 1`
            due to the UniC. It is recommended to use `solver_order=2` for guided sampling, and `solver_order=3` for
            unconditional sampling.
        prediction_type (`str`, defaults to "flow_prediction"):
            Prediction type of the scheduler function; must be `flow_prediction` for this scheduler, which predicts
            the flow of the diffusion process.
        thresholding (`bool`, defaults to `False`):
            Whether to use the "dynamic thresholding" method. This is unsuitable for latent-space diffusion models such
            as Stable Diffusion.
        dynamic_thresholding_ratio (`float`, defaults to 0.995):
            The ratio for the dynamic thresholding method. Valid only when `thresholding=True`.
        sample_max_value (`float`, defaults to 1.0):
            The threshold value for dynamic thresholding. Valid only when `thresholding=True` and `predict_x0=True`.
        predict_x0 (`bool`, defaults to `True`):
            Whether to use the updating algorithm on the predicted x0.
        solver_type (`str`, default `bh2`):
            Solver type for UniPC. It is recommended to use `bh1` for unconditional sampling when steps < 10, and `bh2`
            otherwise.
        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.
        disable_corrector (`list`, default `[]`):
            Decides which step to disable the corrector to mitigate the misalignment between `epsilon_theta(x_t, c)`
            and `epsilon_theta(x_t^c, c)` which can influence convergence for a large guidance scale. Corrector is
            usually disabled during the first few steps.
        solver_p (`SchedulerMixin`, default `None`):
            Any other scheduler that if specified, the algorithm becomes `solver_p + UniC`.
        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 `True`,
            the sigmas are determined according to a sequence of noise levels {Ïƒi}.
        use_exponential_sigmas (`bool`, *optional*, defaults to `False`):
            Whether to use exponential sigmas for step sizes in the noise schedule during the sampling process.
        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](https://huggingface.co/papers/2305.08891) for more information.
        steps_offset (`int`, defaults to 0):
            An offset added to the inference steps, as required by some model families.
        final_sigmas_type (`str`, defaults to `"zero"`):
            The final `sigma` value for the noise schedule during the sampling process. If `"sigma_min"`, the final
            sigma is the same as the last sigma in the training schedule. If `zero`, the final sigma is set to 0.
    c                 ó   — g | ]	}|j         ‘Œ
S © )Úname)Ú.0Úes     úEC:\Users\johnn\OneDrive\Documents\AI\shared\utils\fm_solvers_unipc.pyú
<listcomp>z&FlowUniPCMultistepScheduler.<listcomp>I   s   € Ð>Ð>Ð>˜q�A”FÐ>Ð>Ð>ó    é   iè  é   Úflow_predictionç      ð?Fg×£p=
×ï?TÚbh2NÚlinspacer   ÚzeroÚnum_train_timestepsÚsolver_orderÚprediction_typeÚshiftÚthresholdingÚdynamic_thresholding_ratioÚsample_max_valueÚ
predict_x0Úsolver_typeÚlower_order_finalÚdisable_correctorÚsolver_pÚtimestep_spacingÚsteps_offsetÚfinal_sigmas_typec                 óþ  — |
dvr4|
dv r|                       d¬¦  «         nt          |
› d| j        › �¦  «        ‚|	| _        d | _        t          j        dd|z  |¦  «        d d d…                              ¦   «         }d|z
  }t          j	        |¦  «         
                    t          j        ¬	¦  «        }|s||z  d|dz
  |z  z   z  }|| _        ||z  | _        d g|z  | _        d g|z  | _        d
| _        || _        || _        d | _        d | _        d | _        | j         
                    d¦  «        | _        | j        d                              ¦   «         | _        | j        d
                              ¦   «         | _        d S )N)Úbh1r   )ÚmidpointÚheunÚlogrhor   )r'   z is not implemented for r   éÿÿÿÿr   ©Údtyper   Úcpu)r   ÚNotImplementedErrorÚ	__class__r&   Únum_inference_stepsÚnpr   ÚcopyÚtorchÚ
from_numpyÚtoÚfloat32ÚsigmasÚ	timestepsÚmodel_outputsÚtimestep_listÚlower_order_numsr)   r*   Úlast_sampleÚ_step_indexÚ_begin_indexÚitemÚ	sigma_minÚ	sigma_max)Úselfr   r    r!   r"   Úuse_dynamic_shiftingr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   Úalphasr@   s                      r   Ú__init__z$FlowUniPCMultistepScheduler.__init__L   s®  € ð* ˜nÐ,Ð,ØÐ<Ð<Ð<Ø×'Ò'°EÐ'Ñ:Ô:Ð:Ð:å)Ø"ÐLÐL¸D¼NÐLÐLñNô Nð Nð %ˆŒà#'ˆÔ Ý”˜Q Ð$7Ñ 7Ø0ñ2ô 2Ø26°$°B°$ô8ß8<º¹¼ð 	à�v‘ˆÝÔ! &Ñ)Ô)×,Ò,µ5´=Ð,ÑAÔAˆà#ð 	=à˜V‘^ qØ(-°©	°VÑ';ñ(<ñ =ˆFð ˆŒØÐ"5Ñ5ˆŒà"˜V lÑ2ˆÔØ"˜V lÑ2ˆÔØ !ˆÔØ!2ˆÔØ ˆŒØˆÔØˆÔØ ˆÔà”k—n’nØñô ˆŒàœ Rœ×-Ò-Ñ/Ô/ˆŒØœ Qœ×,Ò,Ñ.Ô.ˆŒˆˆr   c                 ó   — | j         S )zg
        The index counter for current timestep. It will increase 1 after each scheduler step.
        )rF   ©rK   s    r   Ú
step_indexz&FlowUniPCMultistepScheduler.step_index†   s   € ð
 ÔÐr   c                 ó   — | j         S )zq
        The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
        ©rG   rP   s    r   Úbegin_indexz'FlowUniPCMultistepScheduler.begin_index�   s   € ð
 Ô Ð r   rT   c                 ó   — || _         dS )zÜ
        Sets the begin index for the scheduler. This function should be run from pipeline before the inference.

        Args:
            begin_index (`int`):
                The begin index for the scheduler.
        NrS   )rK   rT   s     r   Úset_begin_indexz+FlowUniPCMultistepScheduler.set_begin_index•   s   € ð (ˆÔÐÐr   r9   Údevicer@   Úmuc                 óF  — | j         j        r|€t          d¦  «        ‚|€=t          j        | j        | j        |dz   ¦  «                             ¦   «         dd…         }| j         j        r|                      |d|¦  «        }n|€| j         j	        }||z  d|dz
  |z  z   z  }| j         j
        dk    r"d| j        d         z
  | j        d         z  dz  }n/| j         j
        d	k    rd}nt          d
| j         j
        › �¦  «        ‚|| j         j        z  }t          j        ||gg¦  «                             t          j        ¦  «        }t!          j        |¦  «        | _        t!          j        |¦  «                             |t           j        ¬¦  «        | _        t-          |¦  «        | _        dg| j         j        z  | _        d| _        d| _        | j        r!| j                             | j        |¬¦  «         d| _        d| _        | j                             d¦  «        | _        dS )a†  
        Sets the discrete timesteps used for the diffusion chain (to be run before inference).
        Args:
            num_inference_steps (`int`):
                Total number of the spacing of the time steps.
            device (`str` or `torch.device`, *optional*):
                The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
        NzR you have to pass a value for `mu` when `use_dynamic_shifting` is set to be `True`r   r3   r   rI   r   ç      à?r   zC`final_sigmas_type` must be one of 'zero', or 'sigma_min', but got ©rW   r5   ©rW   r6   ) ÚconfigrL   Ú
ValueErrorr:   r   rJ   rI   r;   Ú
time_shiftr"   r-   Úalphas_cumprodr   ÚconcatenateÚastyper?   r<   r=   r@   r>   Úint64rA   Úlenr9   r    rB   rD   rE   r*   Úset_timestepsrF   rG   )rK   r9   rW   r@   rX   r"   Ú
sigma_lastrA   s           r   re   z)FlowUniPCMultistepScheduler.set_timesteps    sP  € ð" Œ;Ô+ð 	°°
ÝØdñô ð ð ˆ>Ý”[ ¤°´Ø!4Ø!"ñ"#ñ$ô $ç$(¢D¡F¤F¨3¨B¨3ô0ˆFð Œ;Ô+ð 	=Ø—_’_ R¨¨fÑ5Ô5ˆFˆFàˆ}ØœÔ)�Ø˜V‘^ qØ(-°©	°VÑ';ñ(<ñ =ˆFð Œ;Ô(¨KÒ7Ð7Ø˜tÔ2°1Ô5Ñ5ØÔ-¨aÔ0ñ1Ø36ñ7ˆJˆJàŒ[Ô*¨fÒ4Ð4ØˆJˆJåØuÐVZÔVaÔVsÐuÐuñô ð ð ˜Tœ[Ô<Ñ<ˆ	Ý” ¨*¨ð !"ñ #ô #ß#)¢6­"¬*Ñ#5Ô#5ð 	õ Ô& vÑ.Ô.ˆŒÝÔ)¨)Ñ4Ô4×7Ò7Ø¥¤ð 8ñ .ô .ˆŒõ $' y¡>¤>ˆÔ ð ð
àŒKÔ$ñ%ˆÔð !"ˆÔØˆÔØŒ=ð 	QØŒM×'Ò'¨Ô(@ÈÐ'ÑPÔPÐPð  ˆÔØ ˆÔØ”k—n’nØñô ˆŒˆˆr   ÚsampleÚreturnc                 ó,  — |j         }|j        ^}}}|t          j        t          j        fvr|                     ¦   «         }|                     ||t          j        |¦  «        z  ¦  «        }| 	                    ¦   «         }t          j
        || j        j        d¬¦  «        }t          j        |d| j        j        ¬¦  «        }|                     d¦  «        }t          j        || |¦  «        |z  } |j        ||g|¢R Ž }|                     |¦  «        }|S )as  
        "Dynamic thresholding: At each sampling step we set s to a certain percentile absolute pixel value in xt0 (the
        prediction of x_0 at timestep t), and if s > 1, then we threshold xt0 to the range [-s, s] and then divide by
        s. Dynamic thresholding pushes saturated pixels (those near -1 and 1) inwards, thereby actively preventing
        pixels from saturation at each step. We find that dynamic thresholding results in significantly better
        photorealism as well as better image-text alignment, especially when using very large guidance weights."

        https://arxiv.org/abs/2205.11487
        r   ©Údim)ÚminÚmax)r5   Úshaper<   r?   Úfloat64ÚfloatÚreshaper:   ÚprodÚabsÚquantiler]   r$   Úclampr%   Ú	unsqueezer>   )rK   rg   r5   Ú
batch_sizeÚchannelsÚremaining_dimsÚ
abs_sampleÚss           r   Ú_threshold_samplez-FlowUniPCMultistepScheduler._threshold_sampleæ   s%  € ð ”ˆØ06´Ð-ˆ
�H˜~à�œ­¬Ð6Ð6Ð6Ø—\’\ñ ô ˆFð —’ 
¨Hµr´w¸~Ñ7NÔ7NÑ,NÑOÔOˆà—Z’Z‘\”\ˆ
åŒNØ˜œÔ>ÀAðGñ Gô GˆåŒKØ�1˜$œ+Ô6ð
ñ 
ô 
ˆð �KŠKØñô ˆå”Ø�Q�B˜ñ
ô 
àñˆð  �” 
¨HÐF°~ÐFÐFÐFˆØ—’˜5Ñ!Ô!ˆàˆr   c                 ó    — || j         j        z  S ©N©r]   r   ©rK   Úsigmas     r   Ú_sigma_to_tz'FlowUniPCMultistepScheduler._sigma_to_t  s   € Ø�t”{Ô6Ñ6Ð6r   c                 ó   — d|z
  |fS ©Nr   r   r€   s     r   Ú_sigma_to_alpha_sigma_tz3FlowUniPCMultistepScheduler._sigma_to_alpha_sigma_t  s   € Ø�5‰y˜%ÐÐr   r�   Útc                 ól   — t          j        |¦  «        t          j        |¦  «        d|z  dz
  |z  z   z  S r„   )ÚmathÚexp)rK   rX   r�   r†   s       r   r_   z&FlowUniPCMultistepScheduler.time_shift  s/   € ÝŒx˜‰|Œ|�tœx¨™|œ|¨q°1©u°q©y¸5Ñ.@Ñ@ÑAÐAr   ©rg   Úmodel_outputc                ó6  — t          |¦  «        dk    r|d         n|                     dd¦  «        }|€+t          |¦  «        dk    r	|d         }nt          d¦  «        ‚|�t          ddd¦  «         | j        | j                 }|                      |¦  «        \  }}| j        rk| j        j	        d	k    r| j        | j                 }|||z  z
  }	nt          d
| j        j	        › d�¦  «        ‚| j        j
        r|                      |	¦  «        }	|	S | j        j	        d	k    r| j        | j                 }|d|z
  |z  z
  }
nt          d
| j        j	        › d�¦  «        ‚| j        j
        r4| j        | j                 }|||z  z
  }	|                      |	¦  «        }	||	z   }
|
S )a  
        Convert the model output to the corresponding type the UniPC algorithm needs.

        Args:
            model_output (`torch.Tensor`):
                The direct output from the learned diffusion model.
            timestep (`int`):
                The current discrete timestep in the diffusion chain.
            sample (`torch.Tensor`):
                A current instance of a sample created by the diffusion process.

        Returns:
            `torch.Tensor`:
                The converted model output.
        r   ÚtimestepNr   z/missing `sample` as a required keyward argumentrA   ú1.0.0z‡Passing `timesteps` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`r   zprediction_type given as zi must be one of `epsilon`, `sample`, `v_prediction` or `flow_prediction` for the UniPCMultistepScheduler.)rd   Úpopr^   r   r@   rQ   r…   r&   r]   r!   r#   r|   )rK   r‹   rg   ÚargsÚkwargsr�   r�   Úalpha_tÚsigma_tÚx0_predÚepsilons              r   Úconvert_model_outputz0FlowUniPCMultistepScheduler.convert_model_output  sõ  € õ, " $™iœi¨!šm˜m�4˜”7�7°·²¸JÈÑ1MÔ1MˆØˆ>Ý�4‰yŒy˜1Š}ˆ}Ø˜aœ��å ØEñGô Gð GàÐÝØØð Zñô ð ð ”˜DœOÔ,ˆØ×7Ò7¸Ñ>Ô>Ñˆ�àŒ?ð 	ØŒ{Ô*Ð.?Ò?Ð?Øœ+ d¤oÔ6�Ø  7¨\Ñ#9Ñ9��å ð\°´Ô0Kð \ð \ð \ñô ð ð
 Œ{Ô'ð :Ø×0Ò0°Ñ9Ô9�àˆNàŒ{Ô*Ð.?Ò?Ð?Øœ+ d¤oÔ6�Ø  A¨¡K°<Ñ#?Ñ?��å ð\°´Ô0Kð \ð \ð \ñô ð ð
 Œ{Ô'ð 1Øœ+ d¤oÔ6�Ø  7¨\Ñ#9Ñ9�Ø×0Ò0°Ñ9Ô9�Ø&¨Ñ0�àˆNr   )rg   Úorderr—   c                óŒ	  — t          |¦  «        dk    r|d         n|                     dd¦  «        }|€+t          |¦  «        dk    r	|d         }nt          d¦  «        ‚|€+t          |¦  «        dk    r	|d         }nt          d¦  «        ‚|�t          ddd	¦  «         | j        }| j        d
         }|d
         }	|}
| j        r#| j                             |||
¦  «        j        }|S | j	        | j
        dz            | j	        | j
                 }}|                      |¦  «        \  }}|                      |¦  «        \  }}t          j        |¦  «        t          j        |¦  «        z
  }t          j        |¦  «        t          j        |¦  «        z
  }||z
  }|j        }g }g }t          d|¦  «        D ]œ}| j
        |z
  }||dz             }|                      | j	        |         ¦  «        \  }}t          j        |¦  «        t          j        |¦  «        z
  }||z
  |z  }|                     |¦  «         |                     ||	z
  |z  ¦  «         Œ�|                     d¦  «         t          j        ||¬¦  «        }g }g }| j        r| n|}t          j        |¦  «        } | |z  dz
  }!d}"| j        j        dk    r|}#n3| j        j        dk    rt          j        |¦  «        }#nt-          ¦   «         ‚t          d|dz   ¦  «        D ][}|                     t          j        ||dz
  ¦  «        ¦  «         |                     |!|"z  |#z  ¦  «         |"|dz   z  }"|!|z  d|"z  z
  }!Œ\t          j        |¦  «        }t          j        ||¬¦  «        }t          |¦  «        dk    ršt          j        |d¬¦  «        }|dk    rt          j        dg|
j        |¬¦  «        }$nbt          j                             |dd
…dd
…f         |dd
…         ¦  «                             |¦  «                             |
j        ¦  «        }$nd}| j        r8||z  |
z  || z  |	z  z
  }%|�t          j        d|$|¦  «        }&nd}&|%||#z  |&z  z
  }n7||z  |
z  || z  |	z  z
  }%|�t          j        d|$|¦  «        }&nd}&|%||#z  |&z  z
  }|                     |
j        ¦  «        }|S )aÂ  
        One step for the UniP (B(h) version). Alternatively, `self.solver_p` is used if is specified.

        Args:
            model_output (`torch.Tensor`):
                The direct output from the learned diffusion model at the current timestep.
            prev_timestep (`int`):
                The previous discrete timestep in the diffusion chain.
            sample (`torch.Tensor`):
                A current instance of a sample created by the diffusion process.
            order (`int`):
                The order of UniP at this timestep (corresponds to the *p* in UniPC-p).

        Returns:
            `torch.Tensor`:
                The sample tensor at the previous timestep.
        r   Úprev_timestepNr   z0 missing `sample` as a required keyward argumentr   z/ missing `order` as a required keyward argumentrŽ   z‹Passing `prev_timestep` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`r3   r   r\   r/   r   rj   rZ   ©r5   rW   úk,bkc...->bc...)rd   r�   r^   r   rB   rC   r*   ÚstepÚprev_sampler@   rQ   r…   r<   ÚlogrW   ÚrangeÚappendÚtensorr&   Úexpm1r]   r'   r7   ÚpowÚstackr5   ÚlinalgÚsolver>   Úeinsum)'rK   r‹   rg   r—   r�   r‘   r™   Úmodel_output_listÚs0Úm0ÚxÚx_tr“   Úsigma_s0r’   Úalpha_s0Úlambda_tÚ	lambda_s0ÚhrW   ÚrksÚD1sÚiÚsiÚmiÚalpha_siÚsigma_siÚ	lambda_siÚrkÚRÚbÚhhÚh_phi_1Úh_phi_kÚfactorial_iÚB_hÚrhos_pÚx_t_Úpred_ress'                                          r   Úmultistep_uni_p_bh_updatez5FlowUniPCMultistepScheduler.multistep_uni_p_bh_update^  s  € õ2 $' t¡9¤9¨q¢= =˜˜Qœ˜°f·j²jØ˜Tñ7#ô 7#ˆàˆ>Ý�4‰yŒy˜1Š}ˆ}Ø˜aœ��å ØFñHô Hð Hàˆ=Ý�4‰yŒy˜1Š}ˆ}Ø˜Qœ��å ØEñGô Gð GàÐ$ÝØØð ^ñô ð ð
 !Ô.ÐàÔ Ô#ˆØ˜rÔ"ˆØˆàŒ=ð 	Ø”-×$Ò$ \°2°qÑ9Ô9ÔEˆCØˆJà œK¨¬¸!Ñ(;Ô<¸d¼kØŒOô?�ˆà×7Ò7¸Ñ@Ô@Ñˆ�Ø!×9Ò9¸(ÑCÔCÑˆ�(å”9˜WÑ%Ô%­¬	°'Ñ(:Ô(:Ñ:ˆÝ”I˜hÑ'Ô'­%¬)°HÑ*=Ô*=Ñ=ˆ	à�yÑ ˆØ”ˆàˆØˆÝ�q˜%‘”ð 	'ð 	'ˆAØ” 1Ñ$ˆBØ" Q¨¡U 8Ô,ˆBØ!%×!=Ò!=¸d¼kÈ"¼oÑ!NÔ!NÑˆH�hÝœ	 (Ñ+Ô+­e¬i¸Ñ.AÔ.AÑAˆIØ˜iÑ'¨1Ñ,ˆBØ�JŠJ�r‰NŒNˆNØ�JŠJ˜˜R™ 2‘~Ñ&Ô&Ð&Ð&à�
Š
�3‰ŒˆÝŒl˜3 vÐ.Ñ.Ô.ˆàˆØˆà”?Ð)ˆaˆRˆR¨ˆÝ”+˜b‘/”/ˆØ˜B‘, Ñ"ˆàˆàŒ;Ô" eÒ+Ð+ØˆCˆCØŒ[Ô$¨Ò-Ð-Ý”+˜b‘/”/ˆCˆCå%Ñ'Ô'Ð'å�q˜% !™)Ñ$Ô$ð 	5ð 	5ˆAØ�HŠH•U”Y˜s A¨¡EÑ*Ô*Ñ+Ô+Ð+Ø�HŠH�W˜{Ñ*¨SÑ0Ñ1Ô1Ð1Ø˜1˜q™5Ñ ˆKØ ‘l Q¨¡_Ñ4ˆGˆGåŒK˜‰NŒNˆÝŒL˜ 6Ð*Ñ*Ô*ˆåˆs‰8Œ8�aŠ<ˆ<Ý”+˜c qÐ)Ñ)Ô)ˆCà˜ŠzˆzÝœ s e°1´7À6ÐJÑJÔJ��åœ×+Ò+¨A¨c¨r¨c°3°B°3¨h¬KØ,-¨c¨r¨c¬Fñ4ô 4ß46²B°v±J´J¿rºrÀ!Ä'¹{¼{ð �ð ˆCàŒ?ð 	2Ø˜XÑ%¨Ñ)¨G°gÑ,=ÀÑ,BÑBˆDØˆÝ œ<Ð(9¸6Ø(+ñ-ô -��ð �Ø˜ 3™¨Ñ1Ñ1ˆCˆCà˜XÑ%¨Ñ)¨G°gÑ,=ÀÑ,BÑBˆDØˆÝ œ<Ð(9¸6Ø(+ñ-ô -��ð �Ø˜ 3™¨Ñ1Ñ1ˆCà�fŠf�Q”W‰oŒoˆØˆ
r   )rE   Úthis_sampler—   Úthis_model_outputrE   rÆ   c                óÂ	  — t          |¦  «        dk    r|d         n|                     dd¦  «        }|€+t          |¦  «        dk    r	|d         }nt          d¦  «        ‚|€+t          |¦  «        dk    r	|d         }nt          d¦  «        ‚|€+t          |¦  «        dk    r	|d         }nt          d	¦  «        ‚|�t          dd
d¦  «         | j        }|d         }	|}
|}|}| j        | j                 | j        | j        dz
           }}|                      |¦  «        \  }}|                      |¦  «        \  }}t          j	        |¦  «        t          j	        |¦  «        z
  }t          j	        |¦  «        t          j	        |¦  «        z
  }||z
  }|j
        }g }g }t          d|¦  «        D ]Ÿ}| j        |dz   z
  }||dz             }|                      | j        |         ¦  «        \  }}t          j	        |¦  «        t          j	        |¦  «        z
  }||z
  |z  }|                     |¦  «         |                     ||	z
  |z  ¦  «         Œ |                     d¦  «         t          j        ||¬¦  «        }g }g }| j        r| n|} t          j        | ¦  «        }!|!| z  dz
  }"d}#| j        j        dk    r| }$n3| j        j        dk    rt          j        | ¦  «        }$nt%          ¦   «         ‚t          d|dz   ¦  «        D ][}|                     t          j        ||dz
  ¦  «        ¦  «         |                     |"|#z  |$z  ¦  «         |#|dz   z  }#|"| z  d|#z  z
  }"Œ\t          j        |¦  «        }t          j        ||¬¦  «        }t          |¦  «        dk    rt          j        |d¬¦  «        }nd}|dk    rt          j        dg|
j        |¬¦  «        }%nKt          j                             ||¦  «                             |¦  «                             |
j        ¦  «        }%| j        rQ||z  |
z  ||!z  |	z  z
  }&|�t          j        d|%dd…         |¦  «        }'nd}'||	z
  }(|&||$z  |'|%d         |(z  z   z  z
  }nP||z  |
z  ||!z  |	z  z
  }&|�t          j        d|%dd…         |¦  «        }'nd}'||	z
  }(|&||$z  |'|%d         |(z  z   z  z
  }|                     |
j        ¦  «        }|S )aÉ  
        One step for the UniC (B(h) version).

        Args:
            this_model_output (`torch.Tensor`):
                The model outputs at `x_t`.
            this_timestep (`int`):
                The current timestep `t`.
            last_sample (`torch.Tensor`):
                The generated sample before the last predictor `x_{t-1}`.
            this_sample (`torch.Tensor`):
                The generated sample after the last predictor `x_{t}`.
            order (`int`):
                The `p` of UniC-p at this step. The effective order of accuracy should be `order + 1`.

        Returns:
            `torch.Tensor`:
                The corrected sample tensor at the current timestep.
        r   Úthis_timestepNr   z4 missing`last_sample` as a required keyward argumentr   z4 missing`this_sample` as a required keyward argumenté   z. missing`order` as a required keyward argumentrŽ   z‹Passing `this_timestep` is deprecated and has no effect as model output conversion is now handled via an internal counter `self.step_index`r3   r   r\   r/   r   rj   rZ   rš   r›   )rd   r�   r^   r   rB   r@   rQ   r…   r<   rž   rW   rŸ   r    r¡   r&   r¢   r]   r'   r7   r£   r¤   r5   r¥   r¦   r>   r§   ))rK   rÇ   rE   rÆ   r—   r�   r‘   rÉ   r¨   rª   r«   r¬   Úmodel_tr“   r­   r’   r®   r¯   r°   r±   rW   r²   r³   r´   rµ   r¶   r·   r¸   r¹   rº   r»   r¼   r½   r¾   r¿   rÀ   rÁ   Úrhos_crÃ   Úcorr_resÚD1_ts)                                            r   Úmultistep_uni_c_bh_updatez5FlowUniPCMultistepScheduler.multistep_uni_c_bh_updateæ  sB  € õ8 $' t¡9¤9¨q¢= =˜˜Qœ˜°f·j²jØ˜Tñ7#ô 7#ˆàÐÝ�4‰yŒy˜1Š}ˆ}Ø" 1œg��å ØJñLô Lð LàÐÝ�4‰yŒy˜1Š}ˆ}Ø" 1œg��å ØJñLô Lð Làˆ=Ý�4‰yŒy˜1Š}ˆ}Ø˜Qœ��å ØDñFô Fð FàÐ$ÝØØð ^ñô ð ð !Ô.Ðà˜rÔ"ˆØˆØˆØ#ˆà œK¨¬Ô8¸$¼+ØŒO˜aÑô;!�ˆà×7Ò7¸Ñ@Ô@Ñˆ�Ø!×9Ò9¸(ÑCÔCÑˆ�(å”9˜WÑ%Ô%­¬	°'Ñ(:Ô(:Ñ:ˆÝ”I˜hÑ'Ô'­%¬)°HÑ*=Ô*=Ñ=ˆ	à�yÑ ˆØÔ#ˆàˆØˆÝ�q˜%‘”ð 	'ð 	'ˆAØ” A¨¡EÑ*ˆBØ" Q¨¡U 8Ô,ˆBØ!%×!=Ò!=¸d¼kÈ"¼oÑ!NÔ!NÑˆH�hÝœ	 (Ñ+Ô+­e¬i¸Ñ.AÔ.AÑAˆIØ˜iÑ'¨1Ñ,ˆBØ�JŠJ�r‰NŒNˆNØ�JŠJ˜˜R™ 2‘~Ñ&Ô&Ð&Ð&à�
Š
�3‰ŒˆÝŒl˜3 vÐ.Ñ.Ô.ˆàˆØˆà”?Ð)ˆaˆRˆR¨ˆÝ”+˜b‘/”/ˆØ˜B‘, Ñ"ˆàˆàŒ;Ô" eÒ+Ð+ØˆCˆCØŒ[Ô$¨Ò-Ð-Ý”+˜b‘/”/ˆCˆCå%Ñ'Ô'Ð'å�q˜% !™)Ñ$Ô$ð 	5ð 	5ˆAØ�HŠH•U”Y˜s A¨¡EÑ*Ô*Ñ+Ô+Ð+Ø�HŠH�W˜{Ñ*¨SÑ0Ñ1Ô1Ð1Ø˜1˜q™5Ñ ˆKØ ‘l Q¨¡_Ñ4ˆGˆGåŒK˜‰NŒNˆÝŒL˜ 6Ð*Ñ*Ô*ˆåˆs‰8Œ8�aŠ<ˆ<Ý”+˜c qÐ)Ñ)Ô)ˆCˆCàˆCð �AŠ:ˆ:Ý”\ 3 %¨q¬w¸vÐFÑFÔFˆFˆFå”\×'Ò'¨¨1Ñ-Ô-×0Ò0°Ñ8Ô8×;Ò;¸A¼GÑDÔDˆFàŒ?ð 	HØ˜XÑ%¨Ñ)¨G°gÑ,=ÀÑ,BÑBˆDØˆÝ œ<Ð(9¸6À#À2À#¼;ÈÑLÔL��à�Ø˜R‘<ˆDØ˜ 3™¨(°V¸B´ZÀ$Ñ5FÑ*FÑGÑGˆCˆCà˜XÑ%¨Ñ)¨G°gÑ,=ÀÑ,BÑBˆDØˆÝ œ<Ð(9¸6À#À2À#¼;ÈÑLÔL��à�Ø˜R‘<ˆDØ˜ 3™¨(°V¸B´ZÀ$Ñ5FÑ*FÑGÑGˆCØ�fŠf�Q”W‰oŒoˆØˆ
r   c                 ó¦   — |€| j         }||k                         ¦   «         }t          |¦  «        dk    rdnd}||                              ¦   «         S )Nr   r   )rA   Únonzerord   rH   )rK   r�   Úschedule_timestepsÚindicesÚposs        r   Úindex_for_timestepz.FlowUniPCMultistepScheduler.index_for_timestept  sW   € ØÐ%Ø!%¤Ðà%¨Ò1×:Ò:Ñ<Ô<ˆõ �w‘<”< !Ò#Ð#ˆaˆa¨ˆà�sŒ|× Ò Ñ"Ô"Ð"r   c                 óÖ   — | j         €Ut          |t          j        ¦  «        r|                     | j        j        ¦  «        }|                      |¦  «        | _        dS | j	        | _        dS )zF
        Initialize the step_index counter for the scheduler.
        N)
rT   Ú
isinstancer<   ÚTensorr>   rA   rW   rÕ   rF   rG   )rK   r�   s     r   Ú_init_step_indexz,FlowUniPCMultistepScheduler._init_step_indexƒ  sd   € ð
 ÔÐ#Ý˜(¥E¤LÑ1Ô1ð >Ø#Ÿ;š; t¤~Ô'<Ñ=Ô=�Ø#×6Ò6°xÑ@Ô@ˆDÔÐÐà#Ô0ˆDÔÐÐr   r�   Úreturn_dictc                 ó¸  — | j         €t          d¦  «        ‚| j        €|                      |¦  «         | j        dk    o| j        dz
  | j        vo| j        du}|                      ||¬¦  «        }|r#|                      || j        || j        ¬¦  «        }t          | j
        j        dz
  ¦  «        D ]2}| j        |dz            | j        |<   | j        |dz            | j        |<   Œ3|| j        d<   || j        d<   | j
        j        r5t          | j
        j        t!          | j        ¦  «        | j        z
  ¦  «        }	n| j
        j        }	t          |	| j        dz   ¦  «        | _        | j        dk    sJ ‚|| _        |                      ||| j        ¬¦  «        }
| j        | j
        j        k     r| xj        dz  c_        | xj        dz  c_        |s|
fS t+          |
¬	¦  «        S )
a™  
        Predict the sample from the previous timestep by reversing the SDE. This function propagates the sample with
        the multistep UniPC.

        Args:
            model_output (`torch.Tensor`):
                The direct output from learned diffusion model.
            timestep (`int`):
                The current discrete timestep in the diffusion chain.
            sample (`torch.Tensor`):
                A current instance of a sample created by the diffusion process.
            return_dict (`bool`):
                Whether or not to return a [`~schedulers.scheduling_utils.SchedulerOutput`] or `tuple`.

        Returns:
            [`~schedulers.scheduling_utils.SchedulerOutput`] or `tuple`:
                If return_dict is `True`, [`~schedulers.scheduling_utils.SchedulerOutput`] is returned, otherwise a
                tuple is returned where the first element is the sample tensor.

        NzaNumber of inference steps is 'None', you need to run 'set_timesteps' after creating the schedulerr   r   rŠ   )rÇ   rE   rÆ   r—   r3   )r‹   rg   r—   )r�   )r9   r^   rQ   rÙ   r)   rE   r–   rÏ   Ú
this_orderrŸ   r]   r    rB   rC   r(   rl   rd   rA   rD   rÅ   rF   r   )rK   r‹   r�   rg   rÚ   Ú	generatorÚuse_correctorÚmodel_output_convertr´   rÜ   r�   s              r   rœ   z FlowUniPCMultistepScheduler.step�  sE  € ð4 Ô#Ð+ÝØsñô ð ð Œ?Ð"Ø×!Ò! (Ñ+Ô+Ð+ð ŒO˜aÒð )ØŒO˜aÑ tÔ'=Ð=ð)àÔ DÐ(ð 	ð  $×8Ò8Ø ð  9ñ  )ô  )Ðàð 	Ø×3Ò3Ø"6Ø Ô,Ø"Ø”oð	 4ñ ô ˆFõ �t”{Ô/°!Ñ3Ñ4Ô4ð 	>ð 	>ˆAØ$(Ô$6°q¸1±uÔ$=ˆDÔ˜qÑ!Ø$(Ô$6°q¸1±uÔ$=ˆDÔ˜qÑ!Ð!à!5ˆÔ˜2ÑØ!)ˆÔ˜2ÑàŒ;Ô(ð 	2Ý˜Tœ[Ô5Ý  ¤Ñ0Ô0Ø!œ_ñ-ñ.ô .ˆJˆJð œÔ1ˆJå˜jØ"Ô3°aÑ7ñ9ô 9ˆŒàŒ Ò"Ð"Ð"Ð"à!ˆÔØ×4Ò4Ø%ØØ”/ð 5ñ 
ô 
ˆð Ô  4¤;Ô#;Ò;Ð;ØÐ!Ô! QÑ&Ð!Ô!ð 	ÐÔ˜AÑÐÔàð 	"Ø�>Ð!å¨;Ð7Ñ7Ô7Ð7r   c                 ó   — |S )a?  
        Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
        current timestep.

        Args:
            sample (`torch.Tensor`):
                The input sample.

        Returns:
            `torch.Tensor`:
                A scaled input sample.
        r   )rK   rg   r�   r‘   s       r   Úscale_model_inputz-FlowUniPCMultistepScheduler.scale_model_inputå  s	   € ð ˆr   Úoriginal_samplesÚnoiserA   c                 ó–  ‡ ‡
— ‰ j                              |j        |j        ¬¦  «        }|j        j        dk    rft          j        |¦  «        rR‰ j                             |j        t
          j        ¬¦  «        Š
|                     |j        t
          j        ¬¦  «        }n9‰ j                             |j        ¦  «        Š
|                     |j        ¦  «        }‰ j	        €ˆ
ˆ fd„|D ¦   «         }n4‰ j
        �‰ j
        g|j        d         z  }n‰ j	        g|j        d         z  }||                              ¦   «         }t          |j        ¦  «        t          |j        ¦  «        k     r?|                     d¦  «        }t          |j        ¦  «        t          |j        ¦  «        k     °?‰                      |¦  «        \  }}||z  ||z  z   }	|	S )Nr[   Úmpsr4   c                 ó<   •— g | ]}‰                      |‰¦  «        ‘ŒS r   )rÕ   )r   r†   rÒ   rK   s     €€r   r   z9FlowUniPCMultistepScheduler.add_noise.<locals>.<listcomp>  s:   ø€ ð ð ð àð ×'Ò'¨Ð+=Ñ>Ô>ðð ð r   r   r3   )r@   r>   rW   r5   Útyper<   Úis_floating_pointrA   r?   rT   rQ   rn   Úflattenrd   rv   r…   )rK   râ   rã   rA   r@   Ústep_indicesr�   r’   r“   Únoisy_samplesrÒ   s   `         @r   Ú	add_noisez%FlowUniPCMultistepScheduler.add_noiseö  sæ  øø€ ð ”—’Ø#Ô*Ð2BÔ2Hð  ñ Jô JˆàÔ"Ô'¨5Ò0Ð0µUÔ5LØñ6ô 6Ð0ð "&¤×!2Ò!2Ø Ô'­u¬}ð "3ñ ">ô ">Ðà!ŸšØ Ô'­u¬}ð %ñ >ô >ˆIˆIð "&¤×!2Ò!2Ð3CÔ3JÑ!KÔ!KÐØ!ŸšÐ%5Ô%<Ñ=Ô=ˆIð ÔÐ#ðð ð ð ð à"ðñ ô ˆLˆLð Œ_Ð(à œOÐ,¨y¬¸qÔ/AÑAˆLˆLð !Ô,Ð-°	´ÀÔ0BÑBˆLà�|Ô$×,Ò,Ñ.Ô.ˆÝ�%”+ÑÔ¥Ð%5Ô%;Ñ!<Ô!<Ò<Ð<Ø—O’O BÑ'Ô'ˆEõ �%”+ÑÔ¥Ð%5Ô%;Ñ!<Ô!<Ò<Ð<ð  ×7Ò7¸Ñ>Ô>Ñˆ�ØÐ"2Ñ2°W¸u±_ÑDˆØÐr   c                 ó   — | j         j        S r~   r   rP   s    r   Ú__len__z#FlowUniPCMultistepScheduler.__len__  s   € ØŒ{Ô.Ð.r   )r   )NNNNNr~   )TN))Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   Ú_compatiblesr—   r   ÚintÚstrr   rp   Úboolr   r
   rN   ÚpropertyrQ   rT   rV   r   r<   rW   re   rØ   r|   r‚   r…   r_   r–   rÅ   rÏ   rÕ   rÙ   r   r   rœ   rá   Ú	IntTensorrì   rî   r   r   r   r   r      sÆ  € € € € € ð2ð 2ðh ?Ð>Ð$=Ð>Ñ>Ô>€LØ€Eàð (,Ø !Ø#4Ø%(Ø!&Ø!&Ø05Ø&)Ø#Ø$Ø&*Ø+-Ø'+Ø$.Ø !Ø/5ð#7/ð 7/à!$ð7/ð ð7/ð !ð	7/ð
 ˜E”?ð7/ð ð7/ð ).ð7/ð $ð7/ð ð7/ð ð7/ð  $ð7/ð  $ Cœyð7/ð %ð7/ð "ð7/ð  ð!7/ð"  (¨œ}ð#7/ð 7/ð 7/ñ Ôð7/ðr ð ð  ñ „Xð ð ð!ð !ñ „Xð!ð(ð (¨3ð (ð (ð (ð (ð 15Ø+/Ø(,Ø+/Ø.2ðCð Cà" 3¨ 9Ô-ðCð �c˜5œ<Ð'Ô(ðCð ˜˜eœÔ%ð	Cð
 �U˜5 $˜;Ô'Ô(ðCð ˜˜e T˜kÔ*Ô+ðCð Cð Cð CðL$¨¬ð $¸¼ð $ð $ð $ð $ðN7ð 7ð 7ð ð  ð  ðB˜Uð B¨5ð B°U´\ð Bð Bð Bð Bð  $ð	Eð Eð Eà”lðEð ”ð	Eð 
ŒðEð Eð Eð EðV  $ØðFð Fð Fà”lðFð ”ð	Fð
 ðFð 
ŒðFð Fð Fð FðX %)Ø$(ØðLð Lð Là œ<ðLð ”\ð	Lð
 ”\ðLð ðLð 
ŒðLð Lð Lð Lð\#ð #ð #ð #ð
1ð 
1ð 
1ð  "&ØðT8ð T8Ø œ<ðT8à˜S %¤,Ð.Ô/ðT8ð ”\ðT8ð ð	T8ð
 !& o°uÐ&<Ô =ðT8ð T8ð T8ð T8ðl¨¬ð Ø',¤|ðð ð ð ð"'àœ,ð'ð Œ|ð'ð ”?ð	'ð
 
Œð'ð 'ð 'ð 'ðR/ð /ð /ð /ð /r   r   )rˆ   Útypingr   r   r   r   Únumpyr:   r<   Údiffusers.configuration_utilsr   r   Ú%diffusers.schedulers.scheduling_utilsr	   r
   r   Údiffusers.utilsr   r   Úscipy.statsÚscipyr   r   r   r   ú<module>r      s  ðð
 €€€Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /à Ð Ð Ð Ø €€€Ø IÐ IÐ IÐ IÐ IÐ IÐ IÐ IðDð Dð Dð Dð Dð Dð Dð Dð Dð Dð :Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9àÐÑÔð ØÐÐÐðL/ð L/ð L/ð L/ð L/ .°+ñ L/ô L/ð L/ð L/ð L/r   