
    wi;                         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dS )    N)	dataclass)OptionalTupleUnion   )ConfigMixinregister_to_config)
BaseOutput)randn_tensor   )SchedulerMixinc                   <    e Zd ZU dZej        ed<   ej        ed<   dS )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__     w/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/diffusers/schedulers/scheduling_repaint.pyr   r      s:         
 
 ,&&&&&r   r   +?cosinec           
      F   |dk    rd }n|dk    rd }nt          d|           g }t          |           D ]J}|| z  }|dz   | z  }|                    t          d ||           ||          z  z
  |                     Kt	          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
    r   c                 \    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,    8QY%/$'9A=>>!CCr   expc                 0    t          j        | dz            S )Ng      ()r    r&   r#   s    r   r%   z)betas_for_alpha_bar.<locals>.alpha_bar_fnM   s    8A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_barr7   /   s    . x''	D 	D 	D 	D 
	&	&	' 	' 	' 	' T>RTTUUUE*++ M M((!e..S\\"--R0@0@@@(KKLLLL<U]33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j        dej        fd'Zd( ZdS )-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     -C6?{Gz?linear        NTnum_train_timesteps
beta_startbeta_endbeta_scheduleetatrained_betasclip_samplec                 h   |t          j        |          | _        n|dk    r(t          j        |||t           j                  | _        n|dk    r1t          j        |dz  |dz  |t           j                  dz  | _        ns|dk    rt          |          | _        nX|dk    r9t          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                                                   | _        || _        d S )Nr=   r(   scaled_linear      ?r   squaredcos_cap_v2sigmoidi   z is not implemented for       ?r   )dim)r   
from_numpyr3   linspacer/   r7   rJ   NotImplementedError	__class__alphascumprodalphas_cumprodr.   onefinal_alpha_cumprodinit_noise_sigmanum_inference_stepsnparangecopy	timestepsrC   )	selfr?   r@   rA   rB   rC   rD   rE   r3   s	            r   __init__zRePaintScheduler.__init__x   s    $)-88DJJh&&
H>QY^YfgggDJJo--
C3H[chcpqqquvvDJJ111,-@AADJJi''N2q*=>>Eu--J1FG*TDJJ%&`&`PTP^&`&`aaaDJ&#mDKQ???<$$#(<#4#4  !$ $( )")A7J*K*KDDbD*Q*V*V*X*XY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   )r^   r`   ra   s      r   scale_model_inputz"RePaintScheduler.scale_model_input   s	     r   
   rY   jump_lengthjump_n_sampledevicec                 0   t          | j        j        |          }|| _        g }i }t	          d||z
  |          D ]
}|dz
  ||<   |}|dk    rt|dz
  }|                    |           |                    |d          dk    r:||         dz
  ||<   t	          |          D ]}	|dz   }|                    |           |dk    tt          j        |          | j        j        | j        z  z  }t          j
        |                              |          | _        dS )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-   configr?   rY   r+   r,   getrZ   arrayr   rO   tor]   )
r^   rY   rf   rg   rh   r]   jumpsjr$   _s
             r   set_timestepszRePaintScheduler.set_timesteps   s9   0 "$+"ACVWW#6 	q-;[II 	) 	)A$q(E!HH1ffAAQyyA"" 8a<a{++ ( (AAA$$Q'''' 1ff HY''4;+JdNf+fg	))4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   )rj   r?   rY   rU   rW   )r^   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    DK;t?WWW*1-BOSTBTBTD/>>Z^Zr,&00 %{2q<J[;[7[\r   model_outputoriginal_imagemask	generatorreturn_dictc                 Z   |}|| 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        |                     |          dz  z  }d}|dk    r| j        dk    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   rH   rN   r}   rh   r)   r   rL   )r   r   )rj   r?   rY   rU   rW   rE   r   clamprh   r   shaper)   rC   ry   r   )r^   rz   ra   r`   r{   r|   r}   r~   r$   rs   rt   ru   rv   r   rh   noise	std_dev_trx   pred_sample_directionprev_unknown_partprev_known_partpred_prev_samples                         r   stepzRePaintScheduler.step   s   H  4;#BdF^#^^ *1-BOSTBTBTD/>>Z^Zr,& !'c)9L)H HLZ]L]] ;" 	L#(;/CR#K#K  $\/9V[g[mnnnHt11(;;sBB	q55TX\\ 5(H "#%6!6A!E# MP\ \ .s25IILaadll -c1^CL]H]beGeinFnn  /1S4ZCT4TT 	 $ 
 &2BYmnnnnr   c                 l   | j         j        | j        z  }t          |          D ]}| j        ||z            }|j        j        dk    r7t          |j        |j	        |          }|
                    |j                  }n"t          |j        ||j        |j	                  }d|z
  dz  |z  |dz  |z  z   }|S )Nmps)r)   r}   r   r   rH   )rj   r?   rY   r+   r3   rh   typer   r   r)   rm   )r^   r`   ra   r}   nr4   betar   s           r   	undo_stepzRePaintScheduler.undo_stepO  s    K+t/GGq 
	D 
	DA:hl+D}!U**$V\QZ[[[//$V\Yv}djdpqqq $h3&/$)e2CCFFr   original_samplesr   r]   c                      t          d          )NzCUse `DDPMScheduler.add_noise()` to train for sampling with RePaint.)rQ   )r^   r   r   r]   s       r   	add_noisezRePaintScheduler.add_noise`  s     ""ghhhr   c                     | j         j        S N)rj   r?   )r^   s    r   __len__zRePaintScheduler.__len__h  s    {..r   )r:   r;   r<   r=   r>   NTr   )re   re   N)NT)r   r   r   r   orderr	   intfloatstrr   rZ   ndarrayboolr_   r   r   rd   r   rh   rq   ry   	Generatorr   r   r   r   	IntTensorr   r   r   r   r   r9   r9   [   sd        4 E $("%.2 ( ( ( ( 	(
 ( (  
+( ( ( ( (T   Y^Ye    ( +/-@ -@ -@ -@ 	-@
 c5<'(-@ -@ -@ -@^  4 04 Wo WolWo Wo 	Wo
 Wo lWo EO,Wo Wo 
%u,	-Wo Wo Wo Wor   "i,i |i ?	i
 
i i i i/ / / / /r   r9   )r   r   )r    dataclassesr   typingr   r   r   numpyrZ   r   configuration_utilsr   r	   utilsr
   utils.torch_utilsr   scheduling_utilsr   r   r7   r9   r   r   r   <module>r      s6    ! ! ! ! ! ! ) ) ) ) ) ) ) ) ) )      A A A A A A A A       , , , , , , , , , , , , ' ' ' ' 'Z ' ' '( !)4 )4 )4 )4XN/ N/ N/ N/ N/~{ N/ N/ N/ N/ N/r   