
    
3ja4                        S SK Jr  S SKrS SKJr  SSKJrJr  SSK	J
r
  SSKJrJrJrJrJrJr  \
R$                  " \5      r\R*                  R                   " S S	5      5       r\ " S
 S\5      5       r " S S\\5      rg)    )	dataclassN   )ConfigMixinregister_to_config)logging   )CommonSchedulerStateFlaxKarrasDiffusionSchedulersFlaxSchedulerMixinFlaxSchedulerOutputadd_noise_commonget_velocity_commonc            	           \ rS rSr% \\S'   \R                  \S'   \R                  \S'   \R                  \S'   Sr\	\S'   \
S\S\R                  S\R                  S\R                  4S j5       rS	rg)
DDIMSchedulerState&   commonfinal_alpha_cumprodinit_noise_sigma	timestepsNnum_inference_stepsc                     U " UUUUS9$ )Nr   r   r   r    )clsr   r   r   r   s        c/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/schedulers/scheduling_ddim_flax.pycreateDDIMSchedulerState.create0   s      3-	
 	
    r   )__name__
__module____qualname____firstlineno__r	   __annotations__jnpndarrayr   intclassmethodr   __static_attributes__r   r   r   r   r   &   sw      $ kk!{{##
$
 ![[
 ++	

 ;;
 
r   r   c                        \ rS rSr% \\S'   Srg)FlaxDDIMSchedulerOutput@   stater   N)r   r    r!   r"   r   r#   r(   r   r   r   r*   r*   @   s    r   r*   c                      \ rS rSr% Sr\ V Vs/ s H  oR                  PM     snn r\R                  \
S'   \S 5       r\SSSSS	S
SS
SS\R                  4S\S\S\S\S\R$                  S	-  S\S\S\S\S\S\R                  4S jj5       rS/S\S	-  S\4S jjr S/S\S\R$                  S\S	-  S\R$                  4S jjr S0S\S \S!\S\4S" jjrS\4S# jr  S1S\S$\R$                  S\S\R$                  S%\S&\S\\-  4S' jjrS\S(\R$                  S)\R$                  S*\R$                  S\R$                  4
S+ jrS\S\R$                  S)\R$                  S*\R$                  S\R$                  4
S, jrS- r S.r!g	s  snn f )2FlaxDDIMSchedulerE   a  
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://huggingface.co/papers/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.
dtypec                     g)NTr   selfs    r   	has_stateFlaxDDIMScheduler.has_statep   s    r   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                 :    [         R                  S5        Xl        g )NzFlax classes are deprecated and will be removed in Diffusers v1.0.0. We recommend migrating to PyTorch classes or pinning your version of Diffusers.)loggerwarningr0   )r3   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   r0   s               r   __init__FlaxDDIMScheduler.__init__t   s     	[	
 
r   r   returnc                    Uc  [         R                  " U 5      nU R                  R                  (       a  [        R
                  " SU R                  S9OUR                  S   n[        R
                  " SU R                  S9n[        R                  " SU R                  R                  5      R                  5       S S S2   n[        R                  UUUUS9$ )Nr7   r0   r   r   )r	   r   configr@   r$   arrayr0   alphas_cumprodaranger9   roundr   )r3   r   r   r   r   s        r   create_stateFlaxDDIMScheduler.create_state   s    >)006F 150L0LCIIc,RXRgRghiRj 	
 99S

;JJq$++"A"ABHHJ4R4P	!(( 3-	 ) 
 	
r   r,   sampletimestepc                     U$ )z
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   )r3   r,   rS   rT   s       r   scale_model_input#FlaxDDIMScheduler.scale_model_input   s	     r   r   shapec                     U R                   R                  U-  n[        R                  " SU5      U-  R	                  5       SSS2   U R                   R
                  -   nUR                  UUS9$ )aM  
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   NrK   )r   r   )rL   r9   r$   rO   rP   rA   replace)r3   r,   r   rX   
step_ratior   s         r   set_timestepsFlaxDDIMScheduler.set_timesteps   sr     [[448KK
 ZZ#67*DKKMdPRdSVZVaVaVnVnn	}} 3  
 	
r   c                     UR                   R                  U   n[        R                  " US:  UR                   R                  U   UR                  5      nSU-
  nSU-
  nXv-  SXE-  -
  -  nU$ )Nr   r   )r   rN   r$   wherer   )	r3   r,   rT   prev_timestepalpha_prod_talpha_prod_t_prevbeta_prod_tbeta_prod_t_prevvariances	            r   _get_varianceFlaxDDIMScheduler._get_variance   sz    ||228<IIQLL''6%%

 ,&00$2q<;[7[\r   model_outputetareturn_dictc                    UR                   c  [        S5      eX0R                  R                  UR                   -  -
  nUR                  R
                  nUR                  n	X   n
[        R                  " US:  X   U	5      nSU
-
  nU R                  R                  S:X  a  XLS-  U-  -
  U
S-  -  nUnOU R                  R                  S:X  a  UnXJS-  U-  -
  US-  -  nO`U R                  R                  S:X  a#  U
S-  U-  US-  U-  -
  nU
S-  U-  US-  U-  -   nO#[        SU R                  R                   S	35      eU R                  R                  (       a;  UR                  U R                  R                  * U R                  R                  5      nU R                  XU5      nX_S-  -  nSU-
  US
-  -
  S-  U-  nUS-  U-  U-   nU(       d  UU4$ [        UUS9$ )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.

zaNumber of inference steps is 'None', you need to run 'set_timesteps' after creating the schedulerr   r   r8   g      ?rS   v_predictionzprediction_type given as z6 must be one of `epsilon`, `sample`, or `v_prediction`r   )prev_sampler,   )r   
ValueErrorrL   r9   r   rN   r   r$   r_   rB   r>   clipr?   rf   r*   )r3   r,   rh   rT   rS   ri   rj   r`   rN   r   ra   rb   rc   pred_original_samplepred_epsilonre   	std_dev_tpred_sample_directionrm   s                      r   stepFlaxDDIMScheduler.step   s'   4 $$,s   !;;#B#BeF_F_#__44#77 &/IImq&8.:WYlm,& ;;&&)3$*S-AL-P$PT`ehTi#i 'L[[((H4#/ "c%:=Q%QQU`ehUiiL[[((N:$0#$5#?;PSCSWcBc#c (#-=cAQU[@[[L+DKK,G,G+H I" "  ;;""#7#<#<...0M0M$  %%e}Es++	 "#%6!6A!E3 OR^ ^ (C03GGJ__''&;eLLr   original_samplesnoiser   c                 0    [        UR                  X#U5      $ N)r   r   )r3   r,   rv   rw   r   s        r   	add_noiseFlaxDDIMScheduler.add_noise3  s      .>yQQr   c                 0    [        UR                  X#U5      $ ry   )r   r   )r3   r,   rS   rw   r   s        r   get_velocityFlaxDDIMScheduler.get_velocity<  s     #5<<	JJr   c                 .    U R                   R                  $ ry   )rL   r9   r2   s    r   __len__FlaxDDIMScheduler.__len__E  s    {{...r   rJ   ry   )r   )g        T)"r   r    r!   r"   __doc__r
   name_compatiblesr$   r0   r#   propertyr4   r   float32r&   floatstrr%   boolrF   r	   r   rQ   rV   tupler\   rf   r*   rt   rz   r}   r   r(   ).0es   00r   r.   r.   E   s   $L %BB$AqFF$ABL99   $("%,0 #&!%(;;   	
  {{T)  !    yy (
#7$#> 
J\ 
8  $	!  *	
 
$ SU
'
>A
JO
	
,#5 (  [M![M kk[M 	[M
 [M [M [M 
!5	([MzR!R ++R {{	R
 ;;R 
RK!K K {{	K
 ;;K 
K/s Cs   E;r.   )dataclassesr   flax	jax.numpynumpyr$   configuration_utilsr   r   utilsr   scheduling_utils_flaxr	   r
   r   r   r   r   
get_loggerr   rD   structr   r*   r.   r   r   r   <module>r      s   $ "   A   
		H	% 
 
 
2 1  A/*K A/r   