
    bi=                         d dl Z d dlmZ d dl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 ddlmZ dd	lmZ e G d
 de             Z	 	 ddZ G d dee      Zy)    N)	dataclass)OptionalTupleUnion   )ConfigMixinregister_to_config)
BaseOutput)randn_tensor   )SchedulerMixinc                   N    e Zd ZU dZej
                  ed<   ej
                  ed<   y)RePaintSchedulerOutputal  
    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. `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}) based on the model output from
             the current timestep. `pred_original_sample` can be used to preview progress or for guidance.
    prev_samplepred_original_sampleN)__name__
__module____qualname____doc__torchTensor__annotations__     b/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/schedulers/scheduling_repaint.pyr   r      s    
 ,,&r   r   c           
      $   |dk(  rd }n|dk(  rd }nt        d|       g }t        |       D ]<  }|| z  }|dz   | z  }|j                  t        d ||       ||      z  z
  |             > t	        j
                  |t        j                        S )a  
    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.


    Args:
        num_diffusion_timesteps (`int`): the number of betas to produce.
        max_beta (`float`): the maximum beta to use; use values lower than 1 to
                     prevent singularities.
        alpha_transform_type (`str`, *optional*, default to `cosine`): the type of noise schedule for alpha_bar.
                     Choose from `cosine` or `exp`

    Returns:
        betas (`np.ndarray`): the betas used by the scheduler to step the model outputs
    cosinec                 f    t        j                  | dz   dz  t         j                  z  dz        dz  S )NgMb?gT㥛 ?r   )mathcospits    r   alpha_bar_fnz)betas_for_alpha_bar.<locals>.alpha_bar_fnH   s-    88QY%/$''9A=>!CCr   expc                 2    t        j                  | dz        S )Ng      ()r   r%   r"   s    r   r$   z)betas_for_alpha_bar.<locals>.alpha_bar_fnM   s    88AI&&r   z"Unsupported alpha_transform_type: r   dtype)
ValueErrorrangeappendminr   tensorfloat32)num_diffusion_timestepsmax_betaalpha_transform_typer$   betasit1t2s           r   betas_for_alpha_barr6   /   s    . x'	D 
	&	' =>R=STUUE*+ M((!e..S\"-R0@@@(KLM <<U]]33r   c                      e Zd ZdZdZe	 	 	 	 	 	 	 d"dededededed	e	e
j                     d
efd       Zd#dej                  de	e   dej                  fdZ	 	 	 d$dedededeeej$                  f   fdZd Z	 	 d%dej                  dedej                  dej                  dej                  de	ej*                     dedeeef   fdZd#dZdej                  dej                  dej4                  dej                  fd Zd! Zy)&RePaintSchedulerah  
    `RePaintScheduler` is a scheduler for DDPM inpainting inside a given mask.

    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.
        beta_start (`float`, defaults to 0.0001):
            The starting `beta` value of inference.
        beta_end (`float`, defaults to 0.02):
            The final `beta` value.
        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`, `scaled_linear`, `squaredcos_cap_v2`, or `sigmoid`.
        eta (`float`):
            The weight of noise for added noise in diffusion step. If its value is between 0.0 and 1.0 it corresponds
            to the DDIM scheduler, and if its value is between -0.0 and 1.0 it corresponds to the DDPM scheduler.
        trained_betas (`np.ndarray`, *optional*):
            Pass an array of betas directly to the constructor to bypass `beta_start` and `beta_end`.
        clip_sample (`bool`, defaults to `True`):
            Clip the predicted sample between -1 and 1 for numerical stability.

    r   Nnum_train_timesteps
beta_startbeta_endbeta_scheduleetatrained_betasclip_samplec                    |t        j                  |      | _        n|dk(  r-t        j                  |||t         j                        | _        n|dk(  r6t        j                  |dz  |dz  |t         j                        dz  | _        np|dk(  rt        |      | _        nZ|dk(  r;t        j                  dd	|      }t        j                  |      ||z
  z  |z   | _        nt        | d
| j                         d| j                  z
  | _	        t        j                  | j                  d      | _        t        j                  d      | _        t        j                  d      | _        d| _        d | _        t        j                  t#        j$                  d|      d d d   j'                               | _        || _        y )Nlinearr'   scaled_linear      ?r   squaredcos_cap_v2sigmoidi   z is not implemented for       ?r   )dim)r   
from_numpyr2   linspacer.   r6   rE   NotImplementedError	__class__alphascumprodalphas_cumprodr-   onefinal_alpha_cumprodinit_noise_sigmanum_inference_stepsnparangecopy	timestepsr=   )	selfr9   r:   r;   r<   r=   r>   r?   r2   s	            r   __init__zRePaintScheduler.__init__x   sx    $))-8DJh&
H>QY^YfYfgDJo-
C3H[chcpcpquvvDJ11,-@ADJi'NN2q*=>Eu-J1FG*TDJ%7OPTP^P^O_&`aaDJJ&#mmDKKQ?<<$#(<<#4  !$ $( ))"))A7J*KDbD*Q*V*V*XYr   sampletimestepreturnc                     |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.
            timestep (`int`, *optional*):
                The current timestep in the diffusion chain.

        Returns:
            `torch.Tensor`:
                A scaled input sample.
        r   )rY   r[   r\   s      r   scale_model_inputz"RePaintScheduler.scale_model_input   s	     r   rT   jump_lengthjump_n_sampledevicec                 *   t        | j                  j                  |      }|| _        g }i }t	        d||z
  |      D ]
  }|dz
  ||<    |}|dk\  rb|dz
  }|j                  |       |j                  |d      dkD  r1||   dz
  ||<   t	        |      D ]  }	|dz   }|j                  |        |dk\  rbt        j                  |      | j                  j                  | j                  z  z  }t        j                  |      j                  |      | _        y)u  
        Sets the discrete timesteps used for the diffusion chain (to be run before inference).

        Args:
            num_inference_steps (`int`):
                The number of diffusion steps used when generating samples with a pre-trained model. If used,
                `timesteps` must be `None`.
            jump_length (`int`, defaults to 10):
                The number of steps taken forward in time before going backward in time for a single jump (“j” in
                RePaint paper). Take a look at Figure 9 and 10 in the paper.
            jump_n_sample (`int`, defaults to 10):
                The number of times to make a forward time jump for a given chosen time sample. Take a look at Figure 9
                and 10 in the paper.
            device (`str` or `torch.device`, *optional*):
                The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.

        r   r   N)r,   configr9   rT   r*   r+   getrU   arrayr   rJ   torX   )
rY   rT   r`   ra   rb   rX   jumpsjr#   _s
             r   set_timestepszRePaintScheduler.set_timesteps   s   0 "$++"A"ACVW#6 	q-;[I 	)A$q(E!H	)  1fAAQyyA" 8a<a{+ (AAA$$Q'( 1f HHY'4;;+J+JdNfNf+fg	)))477?r   c                     || j                   j                  | j                  z  z
  }| j                  |   }|dk\  r| j                  |   n| j                  }d|z
  }d|z
  }||z  d||z  z
  z  }|S )Nr   r   )rd   r9   rT   rP   rR   )rY   r#   prev_timestepalpha_prod_talpha_prod_t_prevbeta_prod_tbeta_prod_t_prevvariances           r   _get_variancezRePaintScheduler._get_variance   s    DKK;;t?W?WWW**1-BOSTBTD//>Z^ZrZr,&00 %{2q<J[;[7[\r   model_outputoriginal_imagemask	generatorreturn_dictc                    |}|| j                   j                  | j                  z  z
  }	| j                  |   }
|	dk\  r| j                  |	   n| j                  }d|
z
  }||dz  |z  z
  |
dz  z  }| j                   j
                  rt        j                  |dd      }|j                  }t        |j                  |||j                        }| j                  | j                  |      dz  z  }d}|dkD  r| j                  dkD  r||z  }d|z
  |dz  z
  dz  |z  }|dz  |z  |z   |z   }|dz  |z  d|z
  dz  |z  z   }||z  d|z
  |z  z   }|s||fS t        ||      S )	aO  
        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 (`int`):
                The current discrete timestep in the diffusion chain.
            sample (`torch.Tensor`):
                A current instance of a sample created by the diffusion process.
            original_image (`torch.Tensor`):
                The original image to inpaint on.
            mask (`torch.Tensor`):
                The mask where a value of 0.0 indicates which part of the original image to inpaint.
            generator (`torch.Generator`, *optional*):
                A random number generator.
            return_dict (`bool`, *optional*, defaults to `True`):
                Whether or not to return a [`~schedulers.scheduling_repaint.RePaintSchedulerOutput`] or `tuple`.

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

        r   r   rC   rI   rw   rb   r(   r   rG   )r   r   )rd   r9   rT   rP   rR   r?   r   clamprb   r   shaper(   r=   rs   r   )rY   rt   r\   r[   ru   rv   rw   rx   r#   rm   rn   ro   rp   r   rb   noise	std_dev_trr   pred_sample_directionprev_unknown_partprev_known_partpred_prev_samples                         r   stepzRePaintScheduler.step   s   H  4;;#B#BdF^F^#^^ **1-BOSTBTD//>Z^ZrZr,& !'c)9L)H HLZ]L]] ;;""#(;;/CR#K  $$\//9V[g[m[mnHHt11(;sBB	q5TXX\ 5(H "#%6!6A!E# MP\ \ .s25IILaadll -c1^CL]H]beGeinFnn  /1S4ZCT4TT $ 
 &2BYmnnr   c                    | j                   j                  | j                  z  }t        |      D ]  }| j                  ||z      }|j
                  j                  dk(  r>t        |j                  |j                  |      }|j                  |j
                        }n-t        |j                  ||j
                  |j                        }d|z
  dz  |z  |dz  |z  z   } |S )Nmps)r(   rw   rz   r   rC   )rd   r9   rT   r*   r2   rb   typer   r|   r(   rg   )rY   r[   r\   rw   nr3   betar}   s           r   	undo_stepzRePaintScheduler.undo_stepS  s    KK++t/G/GGq 
	DA::hl+D}}!!U*$V\\QZ[/$V\\Yv}}djdpdpq $h3&/$)e2CCF
	D r   original_samplesr}   rX   c                     t        d      )NzCUse `DDPMScheduler.add_noise()` to train for sampling with RePaint.)rL   )rY   r   r}   rX   s       r   	add_noisezRePaintScheduler.add_noised  s     ""ghhr   c                 .    | j                   j                  S N)rd   r9   )rY   s    r   __len__zRePaintScheduler.__len__l  s    {{...r   )i  g-C6?g{Gz?rA   g        NTr   )
   r   N)NT)r   r   r   r   orderr	   intfloatstrr   rU   ndarrayboolrZ   r   r   r_   r   rb   rk   rs   	Generatorr   r   r   r   	IntTensorr   r   r   r   r   r8   r8   [   s   4 E $("%.2 ( ( ( 	(
 ( (  

+( ( (T  Y^YeYe ( +/-@ -@ -@ 	-@
 c5<<'(-@^4 04 [oll[o [o 	[o
 [o ll[o EOO,[o [o 
%u,	-[oz"i,,i ||i ??	i
 
i/r   r8   )g+?r   )r   dataclassesr   typingr   r   r   numpyrU   r   configuration_utilsr   r	   utilsr
   utils.torch_utilsr   scheduling_utilsr   r   r6   r8   r   r   r   <module>r      s^     ! ) )   A  , , 'Z ' '( !)4XR/~{ R/r   