
    
3ji0                     
   S SK Jr  S SKrS SKrS SKJ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  \R"                  " \5      r\R(                  R                   " S	 S
5      5       r\ " S S\5      5       r " S S\\	5      rg)    )	dataclassN)random   )ConfigMixinregister_to_config)logging   )FlaxSchedulerMixinFlaxSchedulerOutputbroadcast_to_shape_from_leftc                       \ rS rSr% Sr\R                  S-  \S'   Sr\R                  S-  \S'   Sr	\R                  S-  \S'   \
S 5       rSrg)ScoreSdeVeSchedulerState$   N	timestepsdiscrete_sigmassigmasc                     U " 5       $ N )clss    e/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/schedulers/scheduling_sde_ve_flax.pycreateScoreSdeVeSchedulerState.create+   s	    u    r   )__name__
__module____qualname____firstlineno__r   jnpndarray__annotations__r   r   classmethodr   __static_attributes__r   r   r   r   r   $   sO     %)Is{{T!(*.OS[[4'.!%FCKK$% r   r   c                   j    \ rS rSr% Sr\\S'   \R                  \S'   Sr	\R                  S-  \S'   Sr
g)FlaxSdeVeOutput0   a(  
Output class for the ScoreSdeVeScheduler's step function output.

Args:
    state (`ScoreSdeVeSchedulerState`):
    prev_sample (`jnp.ndarray` of shape `(batch_size, num_channels, height, width)` for images):
        Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the
        denoising loop.
    prev_sample_mean (`jnp.ndarray` of shape `(batch_size, num_channels, height, width)` for images):
        Mean averaged `prev_sample`. Same as `prev_sample`, only mean-averaged over previous timesteps.
stateprev_sampleNprev_sample_meanr   )r   r   r   r   __doc__r   r!   r   r    r)   r#   r   r   r   r%   r%   0   s-    
 $#+/ckkD(/r   r%   c                      \ rS rSrSr\S 5       r\      SS\S\	S\	S\	S\	S	\4S
 jj5       r
S r  SS\S\S\S\	S\4
S jjr   SS\S\S\	S\	S\	S\4S jjrS r S S\S\R$                  S\S\R$                  S\R(                  S\S\\-  4S jjr S S\S\R$                  S\R$                  S\R(                  S\S\\-  4S jjrS rSrg)!FlaxScoreSdeVeSchedulerC   a  
The variance exploding stochastic differential equation (SDE) scheduler.

For more information, see the original paper: https://huggingface.co/papers/2011.13456

[`~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.

Args:
    num_train_timesteps (`int`): number of diffusion steps used to train the model.
    snr (`float`):
        coefficient weighting the step from the model_output sample (from the network) to the random noise.
    sigma_min (`float`):
            initial noise scale for sigma sequence in sampling procedure. The minimum sigma should mirror the
            distribution of the data.
    sigma_max (`float`): maximum value used for the range of continuous timesteps passed into the model.
    sampling_eps (`float`): the end value of sampling, where timesteps decrease progressively from 1 to
    epsilon.
    correct_steps (`int`): number of correction steps performed on a produced sample.
c                     g)NTr   selfs    r   	has_state!FlaxScoreSdeVeScheduler.has_state[   s    r   num_train_timestepssnr	sigma_min	sigma_maxsampling_epscorrect_stepsc                 .    [         R                  S5        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warning)r0   r3   r4   r5   r6   r7   r8   s          r   __init__ FlaxScoreSdeVeScheduler.__init___   s     	[	
r   c                     [         R                  5       nU R                  UU R                  R                  U R                  R
                  U R                  R                  U R                  R                  5      $ r   )r   r   
set_sigmasconfigr3   r5   r6   r7   )r0   r'   s     r   create_state$FlaxScoreSdeVeScheduler.create_staten   sX    (//1KK++KK!!KK!!KK$$
 	
r   r   Nr'   num_inference_stepsshapereturnc                     Ub  UOU R                   R                  n[        R                  " SXB5      nUR	                  US9$ )a  
Sets the continuous timesteps used for the diffusion chain. Supporting function to be run before inference.

Args:
    state (`ScoreSdeVeSchedulerState`): the `FlaxScoreSdeVeScheduler` state data class instance.
    num_inference_steps (`int`):
        the number of diffusion steps used when generating samples with a pre-trained model.
    sampling_eps (`float`, optional):
        final timestep value (overrides value given at Scheduler instantiation).

r	   )r   )r@   r7   r   linspacereplace)r0   r'   rC   rD   r7   r   s         r   set_timesteps%FlaxScoreSdeVeScheduler.set_timestepsx   s;    $ (4'?|T[[E]E]LLLF	}}y}11r   c                 "   Ub  UOU R                   R                  nUb  UOU R                   R                  nUb  UOU R                   R                  nUR                  c  U R                  XU5      n[        R                  " [        R                  " [        R                  " U5      [        R                  " U5      U5      5      n[        R                  " UR                   Vs/ s H  osXC-  U-  -  PM     sn5      nUR                  XhS9$ s  snf )a  
Sets the noise scales used for the diffusion chain. Supporting function to be run before inference.

The sigmas control the weight of the `drift` and `diffusion` components of sample update.

Args:
    state (`ScoreSdeVeSchedulerState`): the `FlaxScoreSdeVeScheduler` state data class instance.
    num_inference_steps (`int`):
        the number of diffusion steps used when generating samples with a pre-trained model.
    sigma_min (`float`, optional):
        initial noise scale value (overrides value given at Scheduler instantiation).
    sigma_max (`float`, optional):
        final noise scale value (overrides value given at Scheduler instantiation).
    sampling_eps (`float`, optional):
        final timestep value (overrides value given at Scheduler instantiation).
)r   r   )r@   r5   r6   r7   r   rI   r   exprG   logarrayrH   )	r0   r'   rC   r5   r6   r7   r   tr   s	            r   r?   "FlaxScoreSdeVeScheduler.set_sigmas   s    0 "+!6IDKK<Q<Q	!*!6IDKK<Q<Q	'3'?|T[[E]E]??"&&u<PE''#,,swwy/A3779CUWj"kleoo^o)>1(DDo^_}}_}LL _s   #Dc                     [         R                  " US:H  [         R                  " U5      UR                  US-
     5      $ )Nr   r	   )r   where
zeros_liker   )r0   r'   r   rO   s       r   get_adjacent_sigma*FlaxScoreSdeVeScheduler.get_adjacent_sigma   s4    yya):E<Q<QR[^_R_<`aar   model_outputtimestepsamplekeyreturn_dictc                 ^   UR                   c  [        S5      eU[        R                  " UR                  S   5      -  nU[        UR                   5      S-
  -  R                  5       nUR                  U   nU R                  XU5      n	[        R                  " U5      n
US-  U	S-  -
  S-  nUR                  5       n[        XR                  5      nXS-  U-  -
  n
[        R                  " USS9n[        R                  " XTR                  S9nXJ-
  nXU-  -   nU(       d  XU4$ [        XUS9$ )	aK  
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 (`ScoreSdeVeSchedulerState`): the `FlaxScoreSdeVeScheduler` 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.
    generator: random number generator.
    return_dict (`bool`): option for returning tuple rather than FlaxSdeVeOutput class

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

Z`state.timesteps` is not set, you need to run 'set_timesteps' after creating the schedulerr   r	   r         ?numrY   rD   )r(   r)   r'   )r   
ValueErrorr   onesrD   lenlongr   rT   rS   flattenr   r   splitnormalr%   )r0   r'   rV   rW   rX   rY   rZ   r   sigmaadjacent_sigmadrift	diffusionnoiser)   r(   s                  r   	step_pred!FlaxScoreSdeVeScheduler.step_pred   s/   6 ??"l  chhLLO
 
 U__!5!9:@@B	%%i0008Lv&AX 11c9	 %%'	0LLI	1|33 ll3A&#\\:!>&U)::599;afggr   c                 <   UR                   c  [        S5      e[        R                  " USS9n[        R                  " XCR
                  S9n[        R                  R                  U5      n[        R                  R                  U5      nU R                  R                  U-  U-  S-  S-  n	U	[        R                  " UR
                  S   5      -  n	U	R                  5       n	[        XR
                  5      n	X9U-  -   n
XS-  S-  U-  -   nU(       d  X4$ [        XS9$ )	a  
Correct the predicted sample based on the output model_output of the network. This is often run repeatedly
after making the prediction for the previous timestep.

Args:
    state (`ScoreSdeVeSchedulerState`): the `FlaxScoreSdeVeScheduler` state data class instance.
    model_output (`jnp.ndarray`): direct output from learned diffusion model.
    sample (`jnp.ndarray`):
        current instance of sample being created by diffusion process.
    generator: random number generator.
    return_dict (`bool`): option for returning tuple rather than FlaxSdeVeOutput class

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

r\   r	   r^   r`   r   r   r]   )r(   r'   )r   ra   r   rf   rg   rD   r   linalgnormr@   r4   rb   re   r   r%   )r0   r'   rV   rX   rY   rZ   rl   	grad_norm
noise_norm	step_sizer)   r(   s               r   step_correct$FlaxScoreSdeVeScheduler.step_correct   s    2 ??"l  ll3A&#\\: JJOOL1	ZZ__U+
[[__z1I=!CaG	a 99	 %%'	0LLI	!$<<&q=S*@E)II'';DDr   c                 .    U R                   R                  $ r   )r@   r3   r/   s    r   __len__FlaxScoreSdeVeScheduler.__len__%  s    {{...r   )i  g333333?g{Gz?g     @gh㈵>r	   )r   N)NNN)T)r   r   r   r   r*   propertyr1   r   intfloatr<   rA   r   tuplerI   r?   rT   r   r    jaxArrayboolr%   rm   ru   rx   r#   r   r   r   r,   r,   C   s   .    $(!"
 
 
 	

 
 
 
 

 "2'2 !2 	2
 2 
"26  "!M'!M !!M 	!M
 !M !M 
"!MFb !:h':h kk:h 	:h
 :h YY:h :h 
5	 :hD !2E'2E kk2E 	2E
 YY2E 2E 
5	 2Eh/r   r,   )dataclassesr   flaxr~   	jax.numpynumpyr   r   configuration_utilsr   r   utilsr   scheduling_utils_flaxr
   r   r   
get_loggerr   r:   structr   r%   r,   r   r   r   <module>r      s   " "  
   A   
		H	%    0) 0 0$c/0+ c/r   