o
    ۷iu3                     @   s   d dl Z d dlmZ d dlmZ d dlZd dlZddlm	Z	m
Z
 ddlmZ ddlmZ eG dd	 d	eZG d
d dee	ZdS )    N)	dataclass)Literal   )ConfigMixinregister_to_config)SchedulerMixin)
BaseOutputc                   @   sL   e Zd ZU ejed< dZejdB ed< dZejdB ed< dZe	dB ed< dS )HeliosDMDSchedulerOutputprev_sampleNmodel_outputslast_sample
this_order)
__name__
__module____qualname__torchFloatTensor__annotations__r   r   r   int r   r   `/home/ubuntu/vllm_env/lib/python3.10/site-packages/diffusers/schedulers/scheduling_helios_dmd.pyr	      s
   
 
r	   c                   @   s  e Zd Zg ZdZedddg ddddd	d
f	dededededede	de
de
ded fddZdd Zdd Zedd Zedd ZdKd efd!d"Zd#d$ Z	%	%	%	%		dLd&ed'ed%B d(e	ejB d)e
d%B d*e
d%B d+e
fd,d-Zd*ed.ed/ejfd0d1Zd2d3 Zd4d5 Zd6d7 Zd8d9 Z	%	%	%			%	%	%	%dMd:ejd;eejB d<ejd=ejd%B d>e
d?ed@ejd%B dAejd%B dBejd%B dCejd%B dDee B fdEdFZ!dGdH Z"dIdJ Z#d%S )NHeliosDMDScheduler   i        ?   )r   UUUUUU?gUUUUUU?r   r   flow_predictionTFlinearnum_train_timestepsshiftstagesstage_rangegammaprediction_typeuse_flow_sigmasuse_dynamic_shiftingtime_shift_type)exponentialr   c
           
      C   sh   i | _ i | _i | _i | _i | _i | _|   | jd  | _	| jd  | _
|| _d | _d | _d | _d S )Nr   )timestep_ratiostimesteps_per_stagesigmas_per_stagestart_sigmas
end_sigmasori_start_sigmasinit_sigmas_for_each_stagesigmasitem	sigma_min	sigma_maxr"   r   _step_index_begin_index)
selfr   r   r    r!   r"   r#   r$   r%   r&   r   r   r   __init__'   s   
zHeliosDMDScheduler.__init__c                 C   s   | j j}| j j}tdd| |d }d| }t|| d|d |   dd  }t|}|| 	 }d| _
d| _|| _|d| _dS )z<
        initialize the global timesteps and sigmas
        r   r   Nr(   cpu)configr   r   nplinspaceflipcopyr   
from_numpycloner4   r5   	timestepstor0   )r6   r   r   alphasr0   r@   r   r   r   init_sigmasE   s   *
zHeliosDMDScheduler.init_sigmasc                 C   s8  |    g }| jj}| jj}| jj}t|D ]k}t|| | }t|d}t||d  | }t||}| j	| 
 }||k rF| j	| 
 nd}	|| j|< |dkrpd| }
| jj}dtdd|  d|
  |
  |
 }d| }|||	  || j|< |	| j|< qt|}t|D ]/}|dkrd}n
t|d| | }||d krd}nt|d|d  | }||f| j|< qt|D ][}| j| }t| jt|d |  d}| jtt|d | |d  }t|||d }t|tjr|dd nt|dd | j|< tdd|d }t|dd | j|< qdS )	z3
        Init the timesteps for each stage
        r   r   g        Ng?i  r(   g+?)rC   r9   r    r   r!   ranger   maxminr0   r1   r.   r"   mathsqrtappendr,   r-   sumr)   r@   r:   r;   
isinstancer   Tensorr>   r*   r+   )r6   stage_distancer    training_stepsr!   i_sstart_indice
end_indicestart_sigma	end_sigma	ori_sigmar"   corrected_sigmatot_distancestart_ratio	end_ratiotimestep_ratiotimestep_maxtimestep_minr@   stage_sigmasr   r   r   r/   W   sP   


&

 (z-HeliosDMDScheduler.init_sigmas_for_each_stagec                 C      | j S )zg
        The index counter for current timestep. It will increase 1 after each scheduler step.
        )r4   r6   r   r   r   
step_index      zHeliosDMDScheduler.step_indexc                 C   r]   )zq
        The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
        r5   r^   r   r   r   begin_index   r`   zHeliosDMDScheduler.begin_indexr   rb   c                 C   s
   || _ dS )z
        Sets the begin index for the scheduler. This function should be run from pipeline before the inference.

        Args:
            begin_index (`int`):
                The begin index for the scheduler.
        Nra   )r6   rb   r   r   r   set_begin_index   s   
z"HeliosDMDScheduler.set_begin_indexc                 C   s   || j j S Nr9   r   )r6   sigmar   r   r   _sigma_to_t   s   zHeliosDMDScheduler._sigma_to_tNnum_inference_stepsstage_indexdevicer0   muis_amplify_first_chunkc                 C   s  |r	|d d }n|d }|| _ |   | jjdkrV|du rHtdd| jj |d dd tj}| jj	dkrH| jj
r?J | | jj	d|}|| jj  }t|}n-| j| }t|d  |d  |}| j| }	t|	d  |	d  |}
t|
}t|j|d| _t|tdgj|d| _d| _|   | jdd | _t| jdd | jdd g| _| jj
r| jj	dksJ | |d| j| _| jjdkr| jdd | jj | _dS | j|  | jdd | j|  | j|     | _dS dS )	zA
        Setting the timesteps and sigmas for each stage
        r   r   Nr(   r   r   rj   )rh   rC   r9   r    r:   r;   r   astypefloat32r   r%   
time_shiftr=   r   r>   r*   r1   r+   rA   r@   catzerosr0   r4   reset_scheduler_historyrF   rE   )r6   rh   ri   rj   r0   rk   rl   r@   stage_timestepsr\   ratiosr   r   r   set_timesteps   sN   "




$
z HeliosDMDScheduler.set_timestepsrf   tc                 C   s8   | j jdkr| |||S | j jdkr| |||S dS )a  
        Apply time shifting to the sigmas.

        Args:
            mu (`float`):
                The mu parameter for the time shift.
            sigma (`float`):
                The sigma parameter for the time shift.
            t (`torch.Tensor`):
                The input timesteps.

        Returns:
            `torch.Tensor`:
                The time-shifted timesteps.
        r'   r   N)r9   r&   _time_shift_exponential_time_shift_linearr6   rk   rf   rx   r   r   r   rq      s
   zHeliosDMDScheduler.time_shiftc                 C   s$   t |t |d| d |   S Nr   )rG   expr{   r   r   r   ry     s   $z*HeliosDMDScheduler._time_shift_exponentialc                 C   s   ||d| d |   S r|   r   r{   r   r   r   rz     s   z%HeliosDMDScheduler._time_shift_linearc           	      C   sn   | |j}| |j}tj|d|d  dd}|| ddddd}d| | ||  }||S )Nr   r   dimr(   )rA   rj   r   argmin	unsqueezeabsreshapetype_as)	r6   original_samplesnoisetimestepr0   r@   timestep_idrf   sampler   r   r   	add_noise
  s   "
zHeliosDMDScheduler.add_noisec           
         s|   |j }|j  fdd||||fD \}}}}tj|d|d  dd}|| ddddd}|||  }	|	|S )Nc                 3   s    | ]
}|   V  qd S rd   )doublerA   ).0xrm   r   r   	<genexpr>  s    z=HeliosDMDScheduler.convert_flow_pred_to_x0.<locals>.<genexpr>r   r   r~   r(   )dtyperj   r   r   r   r   r   rA   )
r6   	flow_predxtr   r0   r@   original_dtyper   sigma_tx0_predr   rm   r   convert_flow_pred_to_x0  s   ""
z*HeliosDMDScheduler.convert_flow_pred_to_x0model_outputr   r   	generatorreturn_dictcur_sampling_stepdmd_noisy_tensor
dmd_sigmasdmd_timestepsall_timestepsreturnc              	   C   s   | j ||tj|jd f|tj|jd||	d}|t|
d k r;| j||tj|jd f|
|d  tj|jd||	d}n|}|sB|fS t|dS )Nr   )r   rj   )r   r   r   r0   r@   r   )r0   r@   )r
   )	r   r   fullshapelongrj   lenr   r	   )r6   r   r   r   r   r   r   r   r   r   r   pred_image_or_videor
   r   r   r   step  s0   


zHeliosDMDScheduler.stepc                 C   s   d | _ d | _d S rd   )r4   r5   r^   r   r   r   rt   F  s   
z*HeliosDMDScheduler.reset_scheduler_historyc                 C   s   | j jS rd   re   r^   r   r   r   __len__J  s   zHeliosDMDScheduler.__len__)r   )NNNNF)	NNNTr   NNNN)$r   r   r   _compatiblesorderr   r   floatliststrboolr   r7   rC   r/   propertyr_   rb   rc   rg   r   rj   rw   rL   rq   ry   rz   r   r   r   	Generatorr	   tupler   rt   r   r   r   r   r   r   #   s    	
<



>	

)r   )rG   dataclassesr   typingr   numpyr:   r   configuration_utilsr   r   schedulers.scheduling_utilsr   utilsr   r	   r   r   r   r   r   <module>   s   