
    +hn/                         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  SSKJrJr  SSK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)random   )ConfigMixinregister_to_config   )FlaxSchedulerMixinFlaxSchedulerOutputbroadcast_to_shape_from_leftc                       \ rS rSr% Sr\\R                     \S'   Sr	\\R                     \S'   Sr
\\R                     \S'   \S 5       rSrg)ScoreSdeVeSchedulerState   N	timestepsdiscrete_sigmassigmasc                     U " 5       $ N )clss    e/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/schedulers/scheduling_sde_ve_flax.pycreateScoreSdeVeSchedulerState.create$   s	    u    r   )__name__
__module____qualname____firstlineno__r   r   jnpndarray__annotations__r   r   classmethodr   __static_attributes__r   r   r   r   r      sO     (,Ix$+-1OXckk*1$(FHS[[!( r   r   c                   j    \ rS rSr% Sr\\S'   \R                  \S'   Sr	\
\R                     \S'   Srg)FlaxSdeVeOutput)   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'   r'   )   s-    
 $#.2hs{{+2r   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   4S jjr S S\S\R$                  S\R$                  S\R(                  S\S\\\4   4S jjrS rSrg)!FlaxScoreSdeVeScheduler<   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_stateT   s    r   num_train_timestepssnr	sigma_min	sigma_maxsampling_epscorrect_stepsc                     g r   r   )r2   r5   r6   r7   r8   r9   r:   s          r   __init__ FlaxScoreSdeVeScheduler.__init__X   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configr5   r7   r8   r9   )r2   r)   s     r   create_state$FlaxScoreSdeVeScheduler.create_stated   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@   r9   r!   linspacereplace)r2   r)   rC   rD   r9   r   s         r   set_timesteps%FlaxScoreSdeVeScheduler.set_timestepsn   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@   r7   r8   r9   r   rI   r!   exprG   logarrayrH   )	r2   r)   rC   r7   r8   r9   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   )r2   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'   )r2   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@   r6   rb   re   r   r'   )r2   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@   r5   r1   s    r   __len__FlaxScoreSdeVeScheduler.__len__  s    {{...r   )i  g333333?g{Gz?g     @gh㈵>r   )r   N)NNN)T)r   r   r   r    r,   propertyr3   r
   intfloatr<   rA   r   r   rI   r?   rT   r!   r"   jaxArrayboolr   r'   rm   ru   rx   r%   r   r   r   r.   r.   <   s   .    $(!"	 	 	 		
 	 	 	 	
 sw2-2DG2PU2jo2	!2.  "!M'!M !!M 	!M
 !M !M 
"!MFb !:h':h kk:h 	:h
 :h YY:h :h 
%	&:hD !2E'2E kk2E 	2E
 YY2E 2E 
%	&2Eh/r   r.   )dataclassesr   typingr   r   r   flaxr}   	jax.numpynumpyr!   r   configuration_utilsr	   r
   scheduling_utils_flaxr   r   r   structr   r'   r.   r   r   r   <module>r      su   " " ) )  
   A h h    3) 3 3$\/0+ \/r   