
    
3ju3                         S SK r S SKJr  S SKJr  S SKrS SKrSSKJ	r	J
r
  SSKJr  SSKJr  \ " S S	\5      5       r " S
 S\\	5      rg)    N)	dataclass)Literal   )ConfigMixinregister_to_config)SchedulerMixin)
BaseOutputc                       \ rS rSr% \R
                  \S'   Sr\R
                  S-  \S'   Sr\R
                  S-  \S'   Sr	\
S-  \S'   Srg)HeliosDMDSchedulerOutput   prev_sampleNmodel_outputslast_sample
this_order )__name__
__module____qualname____firstlineno__torchFloatTensor__annotations__r   r   r   int__static_attributes__r       d/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/schedulers/scheduling_helios_dmd.pyr   r      sF    """.2M5$$t+2,0K""T)0!Jd
!r   r   c                   x   \ rS rSr/ rSr\SSS/ SQSSS	S
S4	S\S\S\S\	S\S\
S\S\S\S   4S jj5       rS rS r\S 5       r\S 5       rS<S\4S jjrS r     S=S\S \S-  S!\
\R,                  -  S"\S-  S#\S-  S$\4S% jjrS#\S&\S'\R0                  4S( jrS) rS* rS+ rS, r         S>S-\R<                  S.\\R<                  -  S/\R<                  S0\R>                  S-  S1\S2\S3\R<                  S-  S4\R<                  S-  S5\R<                  S-  S6\R<                  S-  S7\ \!-  4S8 jjr"S9 r#S: r$S;r%g)?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
                 6   0 U l         0 U l        0 U l        0 U l        0 U l        0 U l        U R                  5         U R                  S   R                  5       U l	        U R                  S   R                  5       U l
        XPl        S U l        S U l        S U l        g )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.   s
             r   __init__HeliosDMDScheduler.__init__'   s      "#%  " " 	'')R--/Q,,.
 r   c                    U R                   R                  nU R                   R                  n[        R                  " SSU-  US-   5      nSU-
  n[        R
                  " X$-  SUS-
  U-  -   -  5      SS R                  5       n[        R                  " U5      nXA-  R                  5       nSU l
        SU l        XPl        UR                  S5      U l        g)z,
initialize the global timesteps and sigmas
r    r!   Nr1   cpu)configr&   r'   nplinspaceflipcopyr   
from_numpycloner=   r>   	timestepstor9   )r?   r&   r'   alphasr9   rK   s         r   init_sigmasHeliosDMDScheduler.init_sigmasE   s     #kk==!!Q$7 79Lq9PQv1	V/C+CDEcrJOOQ!!&)188:	 "ii&r   c                    U R                  5         / nU R                  R                  nU R                  R                  nU R                  R                  n[        U5       GH  n[        XE   U-  5      n[        US5      n[        XES-      U-  5      n[        Xs5      nU R                  U   R                  5       nXs:  a  U R                  U   R                  5       OSn	XR                  U'   US:w  aK  SU-
  n
U R                  R                  nS[        R                  " SSU-  -   5      SU
-
  -  U
-   -  U
-  nSU-
  nUR                  X-
  5        XR                   U'   XR"                  U'   GM     [%        U5      n[        U5       HL  nUS:X  a  SnO[%        USU 5      U-  nXRS-
  :X  a  SnO[%        USUS-    5      U-  nX4U R&                  U'   MN     [        U5       GH  nU R&                  U   n[        U R(                  [        US   U-  5         S5      nU R(                  [        [        US   U-  5      US-
  5         n[*        R,                  " UUUS-   5      n[/        U[0        R2                  5      (       a  USS O[0        R4                  " USS 5      U R6                  U'   [*        R,                  " SSUS-   5      n[0        R4                  " USS 5      U R8                  U'   GM     g)	z#
Init the timesteps for each stage
r   r    g        Ng?i  r1   g+?)rN   rD   r(   r&   r)   ranger   maxminr9   r:   r7   r*   mathsqrtappendr5   r6   sumr2   rK   rE   rF   
isinstancer   TensorrI   r3   r4   )r?   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_minrK   stage_sigmass                        r   r8   -HeliosDMDScheduler.init_sigmas_for_each_stageW   s    	##88kk-- =C{/.@AL|Q/L[q1NBCJZ8J++l388:K:D:UJ/446[^I)4!!#&axO	))#$		!q5y/(Ba)m(TW`(`#aen"n/1!!+"9:%0c"#,OOC ' !, >*=Cax!!.#"67,Fqj .	yq 9:\I	)4(@D  % ! =C!11#6Nt~~c.2Cn2T.UVX[\L>>#c.2Cn2T.UWehiWi*jkLL,QR@RSI",Y"E"E	#25K[K[\efigi\jKk $$S) ;;ua!1CDL).)9)9,s:K)LD!!#& !r   c                     U R                   $ )zW
The index counter for current timestep. It will increase 1 after each scheduler step.
)r=   r?   s    r   
step_indexHeliosDMDScheduler.step_index   s    
 r   c                     U R                   $ )za
The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
r>   rl   s    r   begin_indexHeliosDMDScheduler.begin_index   s    
    r   rq   c                     Xl         g)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.
Nrp   )r?   rq   s     r   set_begin_index"HeliosDMDScheduler.set_begin_index   s
     (r   c                 2    XR                   R                  -  $ NrD   r&   )r?   sigmas     r   _sigma_to_tHeliosDMDScheduler._sigma_to_t   s    {{6666r   Nnum_inference_stepsstage_indexdevicer9   muis_amplify_first_chunkc                 j   U(       a	  US-  S-   nOUS-   nXl         U R                  5         U R                  R                  S:X  a  Uc  [        R
                  " SSU R                  R                  -  US-   5      SS R                  [        R                  5      nU R                  R                  S:w  aD  U R                  R                  (       a   eU R                  U R                  R                  SU5      nX@R                  R                  -  R                  5       n[        R                  " U5      nOU R                  U   n[        R
                  " US   R!                  5       US   R!                  5       U5      nU R"                  U   n	[        R
                  " U	S   R!                  5       U	S   R!                  5       U5      n
[        R                  " U
5      n[        R                  " U5      R%                  US9U l        [        R(                  " U[        R*                  " S5      /5      R%                  US9U l        SU l        U R1                  5         U R&                  SS U l        [        R(                  " U R,                  SS U R,                  SS /5      U l        U R                  R                  (       a  U R                  R                  S:X  d   eU R                  USU R,                  5      U l        U R                  R                  S:X  a,  U R,                  SS U R                  R                  -  U l        gU R                  U   R3                  5       U R,                  SS U R                  U   R5                  5       U R                  U   R3                  5       -
  -  -   U l        gg)	z1
Setting the timesteps and sigmas for each stage
r   r    Nr1   r!   r   )r~   )r|   rN   rD   r(   rE   rF   r&   astypefloat32r'   r-   
time_shiftrH   r   rI   r3   r:   r4   rL   rK   catzerosr9   r=   reset_scheduler_historyrS   rR   )r?   r|   r}   r~   r9   r   r   rK   stage_timestepsri   ratioss              r   set_timesteps HeliosDMDScheduler.set_timesteps   s    ""5"9A"="5"9#6 ;;"~QDKK,K,K(KM`cdMdefigijqqJJ ;;$$+#{{????!__T[[->->VLF++"A"AAGGII%%f-F"66{CO"'')#((*#I  00=L[[a!5!5!7b9I9N9N9PRefF%%f-F)))477v7FiiQ 89<<F<K$$&,iiSb!14;;rs3C DE;;++;;$$+++//"c4;;?DK{{!!Q&!%Sb!1DKK4S4S!S!%!9!9+!F!J!J!Lt{{[^\^O_,,[9==?$BZBZ[fBgBkBkBmmP " ,r   ry   tc                     U R                   R                  S:X  a  U R                  XU5      $ U R                   R                  S:X  a  U R                  XU5      $ g)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)rD   r.   _time_shift_exponential_time_shift_linearr?   r   ry   r   s       r   r   HeliosDMDScheduler.time_shift   sS      ;;&&-7//1==[[((H4**2a88 5r   c                 t    [         R                  " U5      [         R                  " U5      SU-  S-
  U-  -   -  $ Nr    )rT   expr   s       r   r   *HeliosDMDScheduler._time_shift_exponential  s/    xx|txx|q1uqyU.BBCCr   c                 "    XSU-  S-
  U-  -   -  $ r   r   r   s       r   r   %HeliosDMDScheduler._time_shift_linear  s    1q519..//r   c                 `   UR                  UR                  5      nUR                  UR                  5      n[        R                  " UR	                  S5      UR	                  S5      -
  R                  5       SS9nXF   R                  SSSSS5      nSU-
  U-  Xr-  -   nUR                  U5      $ )Nr   r    dimr1   )rL   r~   r   argmin	unsqueezeabsreshapetype_as)	r?   original_samplesnoisetimestepr9   rK   timestep_idry   samples	            r   	add_noiseHeliosDMDScheduler.add_noise
  s    5<<(LL.	llI$7$7$:X=O=OPQ=R$R#W#W#Y_`a#++B1a;e)//%-?~~e$$r   c                 <  ^
 UR                   nUR                  m
U
4S jXXE4 5       u  ppE[        R                  " UR	                  S5      UR	                  S5      -
  R                  5       SS9nXG   R                  SSSSS5      nX(U-  -
  n	U	R                  U5      $ )Nc              3   `   >#    U  H#  oR                  5       R                  T5      v   M%     g 7frw   )doublerL   ).0xr~   s     r   	<genexpr>=HeliosDMDScheduler.convert_flow_pred_to_x0.<locals>.<genexpr>  s#     +nKmaHHJMM&,A,AKms   +.r   r    r   r1   )dtyper~   r   r   r   r   r   rL   )r?   	flow_predxtr   r9   rK   original_dtyper   sigma_tx0_predr~   s             @r   convert_flow_pred_to_x0*HeliosDMDScheduler.convert_flow_pred_to_x0  s    "!!+nI[aKm+n(	vllI$7$7$:X=O=OPQ=R$R#W#W#Y_`a%--b!Q1=**zz.))r   model_outputr   r   	generatorreturn_dictcur_sampling_stepdmd_noisy_tensor
dmd_sigmasdmd_timestepsall_timestepsreturnc                    U R                  UU[        R                  " UR                  S   4U[        R                  UR
                  S9UU	S9nU[        U
5      S-
  :  aT  U R                  UU[        R                  " UR                  S   4XS-      [        R                  UR
                  S9UU	S9nOUnU(       d  U4$ [        US9$ )Nr   )r   r~   )r   r   r   r9   rK   r    )r9   rK   )r   )	r   r   fullshapelongr~   lenr   r   )r?   r   r   r   r   r   r   r   r   r   r   pred_image_or_videor   s                r   stepHeliosDMDScheduler.step  s     #::"ZZ!3!3A!6 8(%**]i]p]pq# ; 
 s=1A55..# 

!''*,!a"78**'..	 "' ) K .K>!'K@@r   c                      S U l         S U l        g rw   )r=   r>   rl   s    r   r   *HeliosDMDScheduler.reset_scheduler_historyF  s     r   c                 .    U R                   R                  $ rw   rx   rl   s    r   __len__HeliosDMDScheduler.__len__J  s    {{...r   )r>   r=   r6   r*   r   r|   r7   r<   r;   r9   r4   r5   r2   rK   r3   )r   )NNNNF)	NNNTr   NNNN)&r   r   r   r   _compatiblesorderr   r   floatliststrboolr   r@   rN   r8   propertyrm   rq   rt   rz   r   r~   r   rY   r   r   r   r   r   r   	Generatorr   tupler   r   r   r   r   r   r   r   r   #   sm   LE $(00 $%*<D! ! ! 	!
 ! ! ! ! #! !!89! !:'$:Mx     ! !(3 (7 #'%)"',; ; 4Z; ell"	;
 t; 4K; !%;|9U 95 9U\\ 9,D0%	* /3$(,0 !"59/32626'A'''A %+++'A !!	'A
 ??T)'A 'A 'A  ++d2'A %%,'A ((4/'A ((4/'A 
"E	)'AR!/r   r   )rT   dataclassesr   typingr   numpyrE   r   configuration_utilsr   r   schedulers.scheduling_utilsr   utilsr	   r   r   r   r   r   <module>r      sJ     !    A 8  "z " "h/ h/r   