
    +h1                         S SK Jr  S SKJrJrJr  S SKrS SKrS SKJ	r
  SSKJrJr  SSKJrJrJrJrJrJr  \R*                  R                   " S S	5      5       r\ " S
 S\5      5       r " S S\\5      rg)    )	dataclass)OptionalTupleUnionN   )ConfigMixinregister_to_config   )CommonSchedulerStateFlaxKarrasDiffusionSchedulersFlaxSchedulerMixinFlaxSchedulerOutputadd_noise_commonget_velocity_commonc                       \ rS rSr% \\S'   \R                  \S'   \R                  \S'   Sr\	\
   \S'   \S\S\R                  S\R                  4S j5       rSrg)	DDPMSchedulerState#   commoninit_noise_sigma	timestepsNnum_inference_stepsc                     U " XUS9$ )Nr   r   r    )clsr   r   r   s       c/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/schedulers/scheduling_ddpm_flax.pycreateDDPMSchedulerState.create,   s    &yYY    r   )__name__
__module____qualname____firstlineno__r   __annotations__jnpndarrayr   r   intclassmethodr   __static_attributes__r   r   r   r   r   #   se       kk!{{)-#-Z0 ZCKK Z\_\g\g Z Zr   r   c                        \ rS rSr% \\S'   Srg)FlaxDDPMSchedulerOutput1   stater   N)r    r!   r"   r#   r   r$   r)   r   r   r   r+   r+   1   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\R                  4	S\S\S\S\S\\R&                     S\S\S\S\R                  4S jj5       rS,S\\   S\4S jjr S,S\S\R&                  S\\   S\R&                  4S jjr S-S\S\S\S\4S jjrS.S\4S  jjr  S/S\S!\R&                  S\S\R&                  S"\\R<                     S#\S\\ \4   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 )0FlaxDDPMScheduler6   a7  
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://huggingface.co/papers/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.
dtypec                     gNTr   selfs    r   	has_stateFlaxDDPMScheduler.has_state[   s    r   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
                     Xl         g Nr1   )
r5   r;   r<   r=   r>   r?   r@   rA   rB   r1   s
             r   __init__FlaxDDPMScheduler.__init___   s	     
r   r   returnc                    Uc  [         R                  " U 5      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S9$ )N      ?rE   r   r   )
r   r   r%   arrayr1   arangeconfigr;   roundr   )r5   r   r   r   s       r   create_stateFlaxDDPMScheduler.create_staten   s{    >)006F 99S

;JJq$++"A"ABHHJ4R4P	!((- ) 
 	
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   )r5   r-   rR   rS   s       r   scale_model_input#FlaxDDPMScheduler.scale_model_input}   s	     r   r   shapec                     U R                   R                  U-  n[        R                  " SU5      U-  R	                  5       SSS2   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 `FlaxDDPMScheduler` 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   )rN   r;   r%   rM   rO   replace)r5   r-   r   rW   
step_ratior   s         r   set_timestepsFlaxDDPMScheduler.set_timesteps   s`     [[448KK
 ZZ#67*DKKMdPRdS	}} 3  
 	
r   c           	         UR                   R                  U   n[        R                  " US:  UR                   R                  US-
     [        R                  " SU R
                  S95      nSU-
  SU-
  -  UR                   R                  U   -  nUc  U R                  R                  nUS:X  a  [        R                  " USS9nU$ US:X  a+  [        R                  " [        R                  " USS95      nU$ US	:X  a  UR                   R                  U   nU$ US
:X  a/  [        R                  " UR                   R                  U   5      nU$ US:X  a  U$ US:X  a0  UnUR                   R                  U   n	US-   S-  n
X-  SU
-
  U-  -   nU$ )Nr   r
   rJ   rE   r9   g#B;)a_minfixed_small_logfixed_largefixed_large_loglearnedlearned_ranger   )r   alphas_cumprodr%   whererL   r1   betasrN   r@   cliplog)r5   r-   tpredicted_variancer@   alpha_prod_talpha_prod_t_prevvariancemin_logmax_logfracs              r   _get_varianceFlaxDDPMScheduler._get_variance   s   ||2215IIa!eU\\-H-HQ-OQTQZQZ[^fjfpfpQqr
 ))a,.>?%,,BTBTUVBWW  KK55M M)xx6H"  //wwsxx>?H  m+||))!,H  //wwu||11!45H  i'%%o-Gll((+G&*a/D~TW(<<Hr   model_outputkeyreturn_dictc           	      D  ^ ^^^^^ UmTc  [         R                  R                  S5      m[        TR                  5      S:  ad  TR                  S   UR                  S   S-  :X  aA  T R
                  R                  S;   a'  [        R                  " TUR                  S   SS9u  mmOSmTR                  R                  T   n[        R                  " TS:  TR                  R                  TS-
     [        R                  " ST R                  S95      nSU-
  n	SU-
  n
T R
                  R                  S	:X  a  XIS
-  T-  -
  US
-  -  nOlT R
                  R                  S:X  a  TnOOT R
                  R                  S:X  a  US
-  U-  U	S
-  T-  -
  nO#[        ST R
                  R                   S35      eT R
                  R                   (       a  [        R"                  " USS5      nUS
-  TR                  R$                  T   -  U	-  nTR                  R&                  T   S
-  U
-  U	-  nX-  X-  -   nUUUU UU4S jn[        R                  " TS:  U" 5       [        R(                  " TR                  T R                  S95      nUU-   nU(       d  UT4$ [+        UTS9$ )aR  
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   )rb   rc   )axisrJ   rE   r:         ?rR   v_predictionzprediction_type given as z? must be one of `epsilon`, `sample`  for the FlaxDDPMScheduler.rK   c                     > [         R                  R                  TSS9S   n [         R                  R                  U TR                  TR
                  S9nTR                  TTTS9S-  U-  $ )Nr
   )numr   )rW   r1   )rj   rx   )jaxrandomsplitnormalrW   r1   rq   )	split_keynoisert   rs   rj   r5   r-   ri   s     r   random_variance/FlaxDDPMScheduler.step.<locals>.random_variance  sn    

((!(4Q7IJJ%%i|7I7IQUQ[Q[%\E&&uaDV&W[^^bgggr   )prev_sampler-   )r|   r}   rt   lenrW   rN   r@   r%   r~   r   rd   re   rL   r1   rB   
ValueErrorrA   rg   rf   alphaszerosr+   )r5   r-   rs   rS   rR   rt   ru   rk   rl   beta_prod_tbeta_prod_t_prevpred_original_samplepred_original_sample_coeffcurrent_sample_coeffpred_prev_sampler   rm   rj   ri   s   ```  `           @@r   stepFlaxDDPMScheduler.step   s   6 ;**..#C ""#a'""1%a1)<<))-II/2yyv||TU]^/_,L,!% ||2215IIa!eU\\-H-HQ-OQTQZQZ[^fjfpfpQqr,&00 ;;&&)3$*S-AL-P$PT`ehTi#i [[((H4#/ [[((N:$0#$5#?;PSCSWcBc#c +DKK,G,G+H I. .  ;;""#&88,@"a#H  '8C&@5<<CUCUVWCX&X\g%g"$||2215#>AQQT__ 6LOcOll	h 	h
 99QUO$5syyASAS[_[e[e7fg+h6$e,,&3C5QQr   original_samplesr   r   c                 0    [        UR                  X#U5      $ rD   )r   r   )r5   r-   r   r   r   s        r   	add_noiseFlaxDDPMScheduler.add_noise  s      .>yQQr   c                 0    [        UR                  X#U5      $ rD   )r   r   )r5   r-   rR   r   r   s        r   get_velocityFlaxDDPMScheduler.get_velocity%  s     #5<<	JJr   c                 .    U R                   R                  $ rD   )rN   r;   r4   s    r   __len__FlaxDDPMScheduler.__len__.  s    {{...r   rE   rD   )r   )NNr3   )&r    r!   r"   r#   __doc__r   name_compatiblesr%   r1   r$   propertyr6   r	   float32r'   floatstrr   r&   boolrF   r   r   rP   rU   r   r[   rq   r|   Arrayr   r+   r   r   r   r   r)   ).0es   00r   r/   r/   6   s   @ %BB$AqFF$ABL99   $("%/3* (;;   	
   ,    yy 
8,@#A 
M_ 
  Y]'14HPQT	 SU
'
>A
JO
	
.#5 N $( WR!WR kkWR 	WR
 WR cii WR WR 
&-	.WRrR!R ++R {{	R
 ;;R 
RK!K K {{	K
 ;;K 
K/o Cs   Fr/   )dataclassesr   typingr   r   r   flaxr|   	jax.numpynumpyr%   configuration_utilsr   r	   scheduling_utils_flaxr   r   r   r   r   r   structr   r+   r/   r   r   r   <module>r      sy   " " ) )  
  A  
Z 
Z 
Z 1  y/*K y/r   