o
    ۷ia4                     @   s   d dl mZ d dlZd dlmZ ddlmZmZ ddl	m
Z
 ddlmZmZmZmZmZmZ e
eZejjG dd	 d	ZeG d
d deZG dd deeZdS )    )	dataclassN   )ConfigMixinregister_to_config)logging   )CommonSchedulerStateFlaxKarrasDiffusionSchedulersFlaxSchedulerMixinFlaxSchedulerOutputadd_noise_commonget_velocity_commonc                	   @   sd   e Zd ZU eed< ejed< ejed< ejed< dZeed< e	dedejdejdejfddZ
dS )	DDIMSchedulerStatecommonfinal_alpha_cumprodinit_noise_sigma	timestepsNnum_inference_stepsc                 C   s   | ||||dS )Nr   r   r   r    )clsr   r   r   r   r   r   _/home/ubuntu/vllm_env/lib/python3.10/site-packages/diffusers/schedulers/scheduling_ddim_flax.pycreate0   s   zDDIMSchedulerState.create)__name__
__module____qualname__r   __annotations__jnpndarrayr   intclassmethodr   r   r   r   r   r   &   s    
 


r   c                   @   s   e Zd ZU eed< dS )FlaxDDIMSchedulerOutputstateN)r   r   r   r   r   r   r   r   r   r!   @   s   
 r!   c                   @   s  e Zd ZU dZdd eD Zejed< e	dd Z
eddd	d
ddddddejfdededededejdB dedededededejfddZd;dedB defddZ	d;d ed!ejd"edB dejfd#d$Z	%d<d ed&ed'edefd(d)Zd efd*d+Z	,	d=d ed-ejd"ed!ejd.ed/edeeB fd0d1Zd ed2ejd3ejd4ejdejf
d5d6Zd ed!ejd3ejd4ejdejf
d7d8Zd9d: ZdS )>FlaxDDIMSchedulera~	  
    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.
    c                 C   s   g | ]}|j qS r   )name).0er   r   r   
<listcomp>l   s    zFlaxDDIMScheduler.<listcomp>dtypec                 C   s   dS )NTr   selfr   r   r   	has_statep   s   zFlaxDDIMScheduler.has_statei  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                 C   s   t d || _d S )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(   )r*   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r(   r   r   r   __init__t   s   
zFlaxDDIMScheduler.__init__r   returnc                 C   st   |d u r	t | }| jjrtjd| jdn|jd }tjd| jd}td| jj	
 d d d }tj||||dS )Nr-   )r(   r   r   )r   r   configr6   r   arrayr(   alphas_cumprodaranger/   roundr   )r*   r   r   r   r   r   r   r   create_state   s   
 zFlaxDDIMScheduler.create_stater"   sampletimestepc                 C   s   |S )a  
        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   )r*   r"   rD   rE   r   r   r   scale_model_input   s   z#FlaxDDIMScheduler.scale_model_inputr   r   shapec                 C   s@   | j j| }td||  ddd | j j }|j||dS )a  
        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   Nr=   )r   r   )r>   r/   r   rA   rB   r7   replace)r*   r"   r   rG   
step_ratior   r   r   r   set_timesteps   s   &zFlaxDDIMScheduler.set_timestepsc           	      C   sP   |j j| }t|dk|j j| |j}d| }d| }|| d||   }|S )Nr   r   )r   r@   r   wherer   )	r*   r"   rE   prev_timestepalpha_prod_talpha_prod_t_prevbeta_prod_tbeta_prod_t_prevvariancer   r   r   _get_variance   s   
zFlaxDDIMScheduler._get_variance        model_outputetareturn_dictc                 C   sv  |j du r	td|| jj|j   }|jj}|j}	|| }
t|dk|| |	}d|
 }| jj	dkrA||d |  |
d  }|}n>| jj	dkrV|}||
d |  |d  }n)| jj	dkru|
d | |d |  }|
d | |d |  }n
td	| jj	 d
| jj
r|| jj | jj}| |||}||d  }d| |d  d | }|d | | }|s||fS t||dS )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.

        NzaNumber of inference steps is 'None', you need to run 'set_timesteps' after creating the schedulerr   r   r.   g      ?rD   v_predictionzprediction_type given as z6 must be one of `epsilon`, `sample`, or `v_prediction`r   )prev_sampler"   )r   
ValueErrorr>   r/   r   r@   r   r   rK   r8   r4   clipr5   rR   r!   )r*   r"   rT   rE   rD   rU   rV   rL   r@   r   rM   rN   rO   pred_original_samplepred_epsilonrQ   	std_dev_tpred_sample_directionrX   r   r   r   step   sB   
zFlaxDDIMScheduler.steporiginal_samplesnoiser   c                 C      t |j|||S N)r   r   )r*   r"   r`   ra   r   r   r   r   	add_noise3     zFlaxDDIMScheduler.add_noisec                 C   rb   rc   )r   r   )r*   r"   rD   ra   r   r   r   r   get_velocity<  re   zFlaxDDIMScheduler.get_velocityc                 C   s   | j jS rc   )r>   r/   r)   r   r   r   __len__E  s   zFlaxDDIMScheduler.__len__rc   )r   )rS   T)r   r   r   __doc__r	   _compatiblesr   r(   r   propertyr+   r   float32r   floatstrr   boolr;   r   r   rC   rF   tuplerJ   rR   r!   r_   rd   rf   rg   r   r   r   r   r#   E   s   
 &

	



]
	
	r#   )dataclassesr   flax	jax.numpynumpyr   configuration_utilsr   r   utilsr   scheduling_utils_flaxr   r	   r
   r   r   r   
get_loggerr   r9   structr   r!   r#   r   r   r   r   <module>   s    

