
    wi0                         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
 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<   dZee	         ed<   e
dedej        dej        fd            ZdS )DDPMSchedulerStatecommoninit_noise_sigma	timestepsNnum_inference_stepsc                      | |||          S )Nr   r   r    )clsr   r   r   s       y/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/diffusers/schedulers/scheduling_ddpm_flax.pycreatezDDPMSchedulerState.create,   s    s&3CyYYYY    )__name__
__module____qualname__r   __annotations__jnpndarrayr   r   intclassmethodr   r   r   r   r   r   #   s              k!!!{)-#---Z0 ZCK Z\_\g Z Z Z [Z Z Zr   r   c                       e Zd ZU eed<   dS )FlaxDDPMSchedulerOutputstateN)r   r   r    r   r!   r   r   r   r'   r'   1   s         r   r'   c                   F   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ej        f	dededededeej                 dedededej        fd            Zd,dee         defdZ	 d,dedej        dee         dej        fdZ	 d-dedededefd Zd.defd!Z	 	 d/ded"ej        dedej        d#eej                 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 )0FlaxDDPMSchedulera  
    Denoising diffusion probabilistic models (DDPMs) explores the connections between denoising score matching and
    Langevin dynamics sampling.

    [`~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/2006.11239

    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 (`np.ndarray`, optional):
            option to pass an array of betas directly to the constructor to bypass `beta_start`, `beta_end` etc.
        variance_type (`str`):
            options to clip the variance used when adding noise to the denoised sample. Choose from `fixed_small`,
            `fixed_small_log`, `fixed_large`, `fixed_large_log`, `learned` or `learned_range`.
        clip_sample (`bool`, default `True`):
            option to clip predicted sample between -1 and 1 for numerical stability.
        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FlaxDDPMScheduler.<listcomp>W   s    BBBqAFBBBr   dtypec                     dS NTr   selfs    r   	has_statezFlaxDDPMScheduler.has_state[   s    tr   i  g-C6?g{Gz?linearNfixed_smallTepsilonnum_train_timesteps
beta_startbeta_endbeta_scheduletrained_betasvariance_typeclip_sampleprediction_typec
                     |	| _         d S Nr0   )
r4   r9   r:   r;   r<   r=   r>   r?   r@   r0   s
             r   __init__zFlaxDDPMScheduler.__init___   s     


r   r   returnc                    |t          j        |           }t          j        d| j                  }t          j        d| j        j                                                  d d d         }t                              |||          S )N      ?rC   r   r   )
r   r   r"   arrayr0   arangeconfigr9   roundr   )r4   r   r   r   s       r   create_statezFlaxDDPMScheduler.create_staten   s    >)066F 9S
;;;Jq$+"ABBHHJJ44R4P	!((- ) 
 
 	
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(   rN   rO   s       r   scale_model_inputz#FlaxDDPMScheduler.scale_model_input}   s	     r   r   r   shapec                     | j         j        |z  }t          j        d|          |z                                  ddd         }|                    ||          S )a  
        Sets the discrete timesteps used for the diffusion chain. Supporting function to be run before inference.

        Args:
            state (`DDIMSchedulerState`):
                the `FlaxDDPMScheduler` state data class instance.
            num_inference_steps (`int`):
                the number of diffusion steps used when generating samples with a pre-trained model.
        r   NrH   )r   r   )rK   r9   r"   rJ   rL   replace)r4   r(   r   rR   
step_ratior   s         r   set_timestepszFlaxDDPMScheduler.set_timesteps   sf     [48KK
 Z#677*DKKMMddPRdS	}} 3  
 
 	
r   c                    |j         j        |         }t          j        |dk    |j         j        |dz
           t          j        d| j                            }d|z
  d|z
  z  |j         j        |         z  }|| j        j        }|dk    rt          j	        |d          }n|dk    r)t          j
        t          j	        |d                    }n||d	k    r|j         j        |         }nc|d
k    r%t          j
        |j         j        |                   }n8|dk    r|S |dk    r*|}|j         j        |         }	|dz   dz  }
|
|	z  d|
z
  |z  z   }|S )Nr   r
   rG   rC   r7   g#B;)a_minfixed_small_logfixed_largefixed_large_loglearnedlearned_ranger   )r   alphas_cumprodr"   whererI   r0   betasrK   r>   cliplog)r4   r(   tpredicted_variancer>   alpha_prod_talpha_prod_t_prevvariancemin_logmax_logfracs              r   _get_variancezFlaxDDPMScheduler._get_variance   sq   |215Ia!eU\-HQ-OQTQZ[^fjfpQqQqQqrr
 ))a,.>?%,BTUVBWW  K5M M))x666HH///wsx>>>??HHm++|)!,HH///wu|1!455HHi''%%o--Gl(+G&*a/Dg~TW(<<Hr   model_outputkeyreturn_dictc                 l    |t           j                            d          t          j                  dk    rSj        d         |j        d         dz  k    r4 j        j        dv r&t          j        |j        d         d          \  ndj	        j
                 }t          j        dk    j	        j
        dz
           t          j        d j                            }d|z
  }	d|z
  }
 j        j        d	k    r||	d
z  z  z
  |d
z  z  }nR j        j        dk    r}n? j        j        dk    r|d
z  |z  |	d
z  z  z
  }nt          d j        j         d           j        j        rt          j        |dd          }|d
z  j	        j                 z  |	z  }j	        j                 d
z  |
z  |	z  }||z  ||z  z   } fd}t          j        dk     |            t          j        j         j                            }||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 (`DDPMSchedulerState`): the `FlaxDDPMScheduler` 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.
            key (`jax.Array`): a PRNG key.
            return_dict (`bool`): option for returning tuple rather than FlaxDDPMSchedulerOutput class

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

        Nr   r
   r   )r\   r]   )axisrG   rC   r8         ?rN   v_predictionzprediction_type given as z? must be one of `epsilon`, `sample`  for the FlaxDDPMScheduler.rH   c                      t           j                            d          d         } t           j                            | j        j                  }                              dz  |z  S )Nr
   )numr   )rR   r0   )rd   rq   )jaxrandomsplitnormalrR   r0   rk   )	split_keynoiserm   rl   rd   r4   r(   rc   s     r   random_variancez/FlaxDDPMScheduler.step.<locals>.random_variance  sm    
((!(44Q7IJ%%i|7IQUQ[%\\E&&uaDV&WW[^^bgggr   )prev_sampler(   )ru   rv   rm   lenrR   rK   r>   r"   rw   r   r^   r_   rI   r0   r@   
ValueErrorr?   ra   r`   alphaszerosr'   )r4   r(   rl   rO   rN   rm   rn   re   rf   beta_prod_tbeta_prod_t_prevpred_original_samplepred_original_sample_coeffcurrent_sample_coeffpred_prev_sampler{   rg   rd   rc   s   ```  `           @@r   stepzFlaxDDPMScheduler.step   s   6 ;*..##C "##a''"1%a1)<<<)-III/2yv|TU]^/_/_/_,L,,!% |215Ia!eU\-HQ-OQTQZ[^fjfpQqQqQqrr,&00 ;&)33$*[S-AL-P$PT`ehTi#i  [(H44#/  [(N::$0#$5#?;PSCSWcBc#c  .DK,G . . .   ;" 	I#&8,@"a#H#H  '8C&@5<CUVWCX&X\g%g"$|215#>AQQT__ 68LLOcflOll	h 	h 	h 	h 	h 	h 	h 	h 	h 	h
 9QUOO$5$5syAS[_[e7f7f7fgg+h6 	-$e,,&3C5QQQQr   original_samplesrz   r   c                 0    t          |j        |||          S rB   )r   r   )r4   r(   r   rz   r   s        r   	add_noisezFlaxDDPMScheduler.add_noise  s      .>yQQQr   c                 0    t          |j        |||          S rB   )r   r   )r4   r(   rN   rz   r   s        r   get_velocityzFlaxDDPMScheduler.get_velocity%  s     #5<	JJJr   c                     | j         j        S rB   )rK   r9   r3   s    r   __len__zFlaxDDPMScheduler.__len__.  s    {..r   rB   )r   )NNr2   )#r   r   r    __doc__r   _compatiblesr"   r0   r!   propertyr5   r	   float32r$   floatstrr   r#   boolrD   r   r   rM   rQ   r   rV   rk   ru   Arrayr   r'   r   r   r   r   r   r   r   r*   r*   6   s+         @ CB$ABBBL9  X  $("%/3* (;    	
   ,    y   
 
8,@#A 
M_ 
 
 
 
  Y] '14HPQT	    SU
 
'
>A
JO
	
 
 
 
. #5    N $( WR WR!WR kWR 	WR
 WR ci WR WR 
&-	.WR WR WR WRrR!R +R {	R
 ;R 
R R R RK!K K {	K
 ;K 
K K K K/ / / / /r   r*   )dataclassesr   typingr   r   r   flaxru   	jax.numpynumpyr"   configuration_utilsr   r	   scheduling_utils_flaxr   r   r   r   r   r   structr   r'   r*   r   r   r   <module>r      s}  " " ! ! ! ! ! ) ) ) ) ) ) ) ) ) )  



       A A A A A A A A                
Z 
Z 
Z 
Z 
Z 
Z 
Z 
Z     1   y/ y/ y/ y/ y/*K y/ y/ y/ y/ y/r   