
    wiB3                         d dl mZ d dlmZmZmZ d dlZd dlmZ	 ddl
mZmZ ddlmZmZmZmZmZmZ ej        j         G d d	                      Ze G d
 de                      Z G d dee          ZdS )    )	dataclass)OptionalTupleUnionN   )ConfigMixinregister_to_config   )CommonSchedulerStateFlaxKarrasDiffusionSchedulersFlaxSchedulerMixinFlaxSchedulerOutputadd_noise_commonget_velocity_commonc            	           e Zd ZU eed<   ej        ed<   ej        ed<   ej        ed<   dZee	         ed<   e
dedej        dej        dej        fd            ZdS )DDIMSchedulerStatecommonfinal_alpha_cumprodinit_noise_sigma	timestepsNnum_inference_stepsc                       | ||||          S )Nr   r   r   r    )clsr   r   r   r   s        y/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/diffusers/schedulers/scheduling_ddim_flax.pycreatezDDIMSchedulerState.create-   s)     s 3-	
 
 
 	
    )__name__
__module____qualname__r   __annotations__jnpndarrayr   r   intclassmethodr   r   r   r   r   r   #   s             $$$ k!!!{)-#---
$
 ![
 +	

 ;
 
 
 [
 
 
r   r   c                       e Zd ZU eed<   dS )FlaxDDIMSchedulerOutputstateN)r   r    r!   r   r"   r   r   r   r(   r(   =   s         r   r(   c                   :   e Zd ZU dZd eD             Zej        ed<   e	d             Z
eddddd	d
dd
ddej        fdededededeej                 dedededededej        fd            Zd0dee         defdZ	 d0dedej        dee         dej        fdZ	 d1ded!ed"edefd#Zdefd$Z	 	 d2ded&ej        dedej        d'ed(edeeef         fd)Zded*ej        d+ej        d,ej        dej        f
d-Zdedej        d+ej        d,ej        dej        f
d.Zd/ Z d	S )3FlaxDDIMSchedulerav	  
    Denoising diffusion implicit models is a scheduler that extends the denoising procedure introduced in denoising
    diffusion probabilistic models (DDPMs) with non-Markovian guidance.

    [`~ConfigMixin`] takes care of storing all config attributes that are passed in the scheduler's `__init__`
    function, such as `num_train_timesteps`. They can be accessed via `scheduler.config.num_train_timesteps`.
    [`SchedulerMixin`] provides general loading and saving functionality via the [`SchedulerMixin.save_pretrained`] and
    [`~SchedulerMixin.from_pretrained`] functions.

    For more details, see the original paper: https://arxiv.org/abs/2010.02502

    Args:
        num_train_timesteps (`int`): number of diffusion steps used to train the model.
        beta_start (`float`): the starting `beta` value of inference.
        beta_end (`float`): the final `beta` value.
        beta_schedule (`str`):
            the beta schedule, a mapping from a beta range to a sequence of betas for stepping the model. Choose from
            `linear`, `scaled_linear`, or `squaredcos_cap_v2`.
        trained_betas (`jnp.ndarray`, optional):
            option to pass an array of betas directly to the constructor to bypass `beta_start`, `beta_end` etc.
        clip_sample (`bool`, default `True`):
            option to clip predicted sample between for numerical stability. The clip range is determined by
            `clip_sample_range`.
        clip_sample_range (`float`, default `1.0`):
            the maximum magnitude for sample clipping. Valid only when `clip_sample=True`.
        set_alpha_to_one (`bool`, default `True`):
            each diffusion step uses the value of alphas product at that step and at the previous one. For the final
            step there is no previous alpha. When this option is `True` the previous alpha product is fixed to `1`,
            otherwise it uses the value of alpha at step 0.
        steps_offset (`int`, default `0`):
            An offset added to the inference steps, as required by some model families.
        prediction_type (`str`, default `epsilon`):
            indicates whether the model predicts the noise (epsilon), or the samples. One of `epsilon`, `sample`.
            `v-prediction` is not supported for this scheduler.
        dtype (`jnp.dtype`, *optional*, defaults to `jnp.float32`):
            the `dtype` used for params and computation.
    c                     g | ]	}|j         
S r   )name).0es     r   
<listcomp>zFlaxDDIMScheduler.<listcomp>i   s    BBBqAFBBBr   dtypec                     dS )NTr   selfs    r   	has_statezFlaxDDIMScheduler.has_statem   s    tr   i  g-C6?g{Gz?linearNT      ?r   epsilonnum_train_timesteps
beta_startbeta_endbeta_scheduletrained_betasclip_sampleclip_sample_rangeset_alpha_to_onesteps_offsetprediction_typec                     || _         d S Nr1   )r4   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   r1   s               r   __init__zFlaxDDIMScheduler.__init__q   s     


r   r   returnc                 |   |t          j        |           }| j        j        rt	          j        d| j                  n|j        d         }t	          j        d| j                  }t	          j        d| j        j	                  
                                d d d         }t                              ||||          S )Nr7   rE   r   r   )r   r   configr@   r#   arrayr1   alphas_cumprodaranger9   roundr   )r4   r   r   r   r   s        r   create_statezFlaxDDIMScheduler.create_state   s    >)066F 150LjCIc,,,,RXRghiRj 	
 9S
;;;Jq$+"ABBHHJJ44R4P	!(( 3-	 ) 
 
 	
r   r)   sampletimestepc                     |S )a  
        Args:
            state (`PNDMSchedulerState`): the `FlaxPNDMScheduler` state data class instance.
            sample (`jnp.ndarray`): input sample
            timestep (`int`, optional): current timestep

        Returns:
            `jnp.ndarray`: scaled input sample
        r   )r4   r)   rP   rQ   s       r   scale_model_inputz#FlaxDDIMScheduler.scale_model_input   s	     r   r   r   shapec                     | j         j        |z  }t          j        d|          |z                                  ddd         | j         j        z   }|                    ||          S )a  
        Sets the discrete timesteps used for the diffusion chain. Supporting function to be run before inference.

        Args:
            state (`DDIMSchedulerState`):
                the `FlaxDDIMScheduler` state data class instance.
            num_inference_steps (`int`):
                the number of diffusion steps used when generating samples with a pre-trained model.
        r   NrI   )r   r   )rJ   r9   r#   rM   rN   rA   replace)r4   r)   r   rT   
step_ratior   s         r   set_timestepszFlaxDDIMScheduler.set_timesteps   sr     [48KK
 Z#677*DKKMMddPRdSVZVaVnn	}} 3  
 
 	
r   c                     |j         j        |         }t          j        |dk    |j         j        |         |j                  }d|z
  }d|z
  }||z  d||z  z
  z  }|S )Nr   r
   )r   rL   r#   wherer   )	r4   r)   rQ   prev_timestepalpha_prod_talpha_prod_t_prevbeta_prod_tbeta_prod_t_prevvariances	            r   _get_variancezFlaxDDIMScheduler._get_variance   ss    |28<IQ ;M JELe
 
 ,&00${2q<J[;[7[\r           model_outputetareturn_dictc                    |j         t          d          || j        j        |j         z  z
  }|j        j        }|j        }	||         }
t          j        |dk    ||         |	          }d|
z
  }| j        j	        dk    r||dz  |z  z
  |
dz  z  }|}nt| j        j	        dk    r|}||
dz  |z  z
  |dz  z  }nP| j        j	        dk    r#|
dz  |z  |dz  |z  z
  }|
dz  |z  |dz  |z  z   }nt          d	| j        j	         d
          | j        j
        r+|                    | j        j         | j        j                  }|                     |||          }||dz  z  }d|z
  |dz  z
  dz  |z  }|dz  |z  |z   }|s||fS t          ||          S )a  
        Predict the sample at the previous timestep by reversing the SDE. Core function to propagate the diffusion
        process from the learned model outputs (most often the predicted noise).

        Args:
            state (`DDIMSchedulerState`): the `FlaxDDIMScheduler` state data class instance.
            model_output (`jnp.ndarray`): direct output from learned diffusion model.
            timestep (`int`): current discrete timestep in the diffusion chain.
            sample (`jnp.ndarray`):
                current instance of sample being created by diffusion process.
            return_dict (`bool`): option for returning tuple rather than FlaxDDIMSchedulerOutput class

        Returns:
            [`FlaxDDIMSchedulerOutput`] or `tuple`: [`FlaxDDIMSchedulerOutput`] if `return_dict` is True, otherwise a
            `tuple`. When returning a tuple, the first element is the sample tensor.

        NzaNumber of inference steps is 'None', you need to run 'set_timesteps' after creating the schedulerr   r
   r8   g      ?rP   v_predictionzprediction_type given as z6 must be one of `epsilon`, `sample`, or `v_prediction`r   )prev_sampler)   )r   
ValueErrorrJ   r9   r   rL   r   r#   rZ   rB   r>   clipr?   ra   r(   )r4   r)   rc   rQ   rP   rd   re   r[   rL   r   r\   r]   r^   pred_original_samplepred_epsilonr`   	std_dev_tpred_sample_directionrh   s                      r   stepzFlaxDDIMScheduler.step   s4   4 $,s    !4;#BeF_#__4#7 &h/Imq&8.:WYlmm,& ;&)33$*[S-AL-P$PT`ehTi#i 'LL[(H44#/ "\c%:=Q%QQU`ehUiiLL[(N::$0#$5#?;PSCSWcBc#c (#-=cAQU[@[[LL"DK,G " " "   ;" 	#7#<#<..0M$ $  %%eX}EE(s++	 "#%6!6A!E3 OR^ ^ (C03GGJ__ 	(''&;eLLLLr   original_samplesnoiser   c                 0    t          |j        |||          S rD   )r   r   )r4   r)   rp   rq   r   s        r   	add_noisezFlaxDDIMScheduler.add_noise'  s      .>yQQQr   c                 0    t          |j        |||          S rD   )r   r   )r4   r)   rP   rq   r   s        r   get_velocityzFlaxDDIMScheduler.get_velocity0  s     #5<	JJJr   c                     | j         j        S rD   )rJ   r9   r3   s    r   __len__zFlaxDDIMScheduler.__len__9  s    {..r   rD   )r   )rb   T)!r   r    r!   __doc__r   _compatiblesr#   r1   r"   propertyr5   r	   float32r%   floatstrr   r$   boolrF   r   r   rO   rS   r   rX   ra   r   r(   ro   rs   ru   rw   r   r   r   r+   r+   B   s8        $ $L CB$ABBBL9  X  $("%/3 #&!%(;    	
   ,  !    y    
 
8,@#A 
M_ 
 
 
 
2 Y] '14HPQT	    SU
 
'
>A
JO
	
 
 
 
,
#5 
 
 
 
$  [M [M![M k[M 	[M
 [M [M [M 
&-	.[M [M [M [MzR!R +R {	R
 ;R 
R R R RK!K K {	K
 ;K 
K K K K/ / / / /r   r+   )dataclassesr   typingr   r   r   flax	jax.numpynumpyr#   configuration_utilsr   r	   scheduling_utils_flaxr   r   r   r   r   r   structr   r(   r+   r   r   r   <module>r      sl  $ " ! ! ! ! ! ) ) ) ) ) ) ) ) ) )        A A A A A A A A                
 
 
 
 
 
 
 
2     1   x/ x/ x/ x/ x/*K x/ x/ x/ x/ x/r   