
    
3j02                     
   S SK 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  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'   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/wildlama/miniconda3/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   intclassmethodr   __static_attributes__r   r   r   r   r   &   sd       kk!{{##Z$Z ++Z ;;	Z Zr   r   c                        \ rS rSr% \\S'   Srg)FlaxDDPMSchedulerOutput9   stater   N)r   r   r    r!   r   r"   r'   r   r   r   r)   r)   9   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\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 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#\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 )0FlaxDDPMScheduler>   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_statec   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
                 :    [         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warningr/   )
r3   r9   r:   r;   r<   r=   r>   r?   r@   r/   s
             r   __init__FlaxDDPMScheduler.__init__g   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      ?r/   r   r   )
r	   r   r#   arrayr/   arangeconfigr9   roundr   )r3   r   r   r   s       r   create_stateFlaxDDPMScheduler.create_statez   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   )r3   r+   rQ   rR   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   NrJ   )r   r   )rM   r9   r#   rL   rN   replace)r3   r+   r   rV   
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   rH   rI   r7   g#B;)a_minfixed_small_logfixed_largefixed_large_loglearnedlearned_ranger   )r   alphas_cumprodr#   whererK   r/   betasrM   r>   cliplog)r3   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   )ra   rb   )axisrH   rI   r8         ?rQ   v_predictionzprediction_type given as z? must be one of `epsilon`, `sample`  for the FlaxDDPMScheduler.rJ   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   )rV   r/   )ri   rw   )jaxrandomsplitnormalrV   r/   rp   )	split_keynoisers   rr   ri   r3   r+   rh   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|   rs   lenrV   rM   r>   r#   r}   r   rc   rd   rK   r/   r@   
ValueErrorr?   rf   re   alphaszerosr)   )r3   r+   rr   rR   rQ   rs   rt   rj   rk   beta_prod_tbeta_prod_t_prevpred_original_samplepred_original_sample_coeffcurrent_sample_coeffpred_prev_sampler   rl   ri   rh   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      $ N)r   r   )r3   r+   r   r   r   s        r   	add_noiseFlaxDDPMScheduler.add_noise+  s      .>yQQr   c                 0    [        UR                  X#U5      $ r   )r   r   )r3   r+   rQ   r   r   s        r   get_velocityFlaxDDPMScheduler.get_velocity4  s     #5<<	JJr   c                 .    U R                   R                  $ r   )rM   r9   r2   s    r   __len__FlaxDDPMScheduler.__len__=  s    {{...r   rI   r   )r   )NNr1   )$r   r   r    r!   __doc__r
   name_compatiblesr#   r/   r"   propertyr4   r   float32r%   floatstrr$   boolrD   r	   r   rO   rT   tuplerZ   rp   r{   Arrayr)   r   r   r   r   r'   ).0es   00r   r-   r-   >   s   @ %BB$AqFF$ABL99   $("%,0* (;;   	
  {{T)    yy $
#7$#> 
J\ 
&  $	!  *	
 
$ SU
'
>A
JO
	
.#5 N !% WR!WR kkWR 	WR
 WR YYWR WR 
!5	(WRrR!R ++R {{	R
 ;;R 
RK!K K {{	K
 ;;K 
K/} Cs   Fr-   )dataclassesr   flaxr{   	jax.numpynumpyr#   configuration_utilsr   r   utilsr   scheduling_utils_flaxr	   r
   r   r   r   r   
get_loggerr   rB   structr   r)   r-   r   r   r   <module>r      s   " "  
  A   
		H	% Z Z Z$ 1  @/*K @/r   