+
    ‚°ñjo  ã                   ób   € ^ RI t ^ RIHtHt ^ RItR R ltRR R lltRR R lltRR	 R
 lltR# )é    N)ÚUnionÚCallablec          
      ó�   € V ^8„  d   QhR\         P                  R\         P                  R\        R\        R\         P                  /# )é   ÚsÚtÚsolver_orderÚtau_tÚreturn)ÚtorchÚTensorÚintÚfloat)Úformats   "Ú?C:\Ai_Coding\apps\studio\ComfyUI\comfy\k_diffusion\sa_solver.pyÚ__annotate__r   
   s?   € ÷ (8ñ (8¤%§,¡,ð (8´5·<±<ð (8Ìsð (8Ô[`ð (8Ôej×eqÑeqñ (8ó    c                óª  € ^V^,          ,           pW,
          p\         P                  ! W P                  V P                  R7      pW,          W,          V) V,          P	                  4       ,          ,
          pVP                  ^4      VP                  ^ 4      ,
          pV^,           P                  4       p	V	P                  ^4      V	P                  ^ 4      ,
          p
V^ 8”  d$   W¨\        P                  ! V4      ,          ,
          p
\         P                  ! V^,          ^ 8H  RR4      pV
P	                  4       V,          P                  4       p
W§,          # )u¤  Compute (1 + tau^2) * integral of exp((1 + tau^2) * x) * x^p dx from s to t with exp((1 + tau^2) * t) factored out, using integration by parts.

Integral of exp((1 + tau^2) * x) * x^p dx
    = product_terms[p] - (p / (1 + tau^2)) * integral of exp((1 + tau^2) * x) * x^(p-1) dx,
with base case p=0 where integral equals product_terms[0].

where
    product_terms[p] = x^p * exp((1 + tau^2) * x) / (1 + tau^2).

Construct a recursive coefficient matrix following the above recursive relation to compute all integral terms up to p = (solver_order - 1).
Return coefficients used by the SA-Solver in data prediction mode.

Args:
    s: Start time s.
    t: End time t.
    solver_order: Current order of the solver.
    tau_t: Stochastic strength parameter in the SDE.

Returns:
    Exponential coefficients used in data prediction, with exp((1 + tau^2) * t) factored out, ordered from p=0 to p=solver_orderâˆ’1, shape (solver_order,).
)ÚdtypeÚdeviceç      ð?g      ð¿)r   Úaranger   r   ÚexpÚ	unsqueezeÚlgammaÚmathÚlogÚwhereÚtril)r   r   r	   r
   Útau_mulÚhÚpÚproduct_terms_factoredÚrecursive_depth_matÚlog_factorialÚrecursive_coeff_matÚsignss   &&&&        r   Úcompute_exponential_coeffsr(   
   s  € ð, �%˜1•*�n€GØ	�€AÜ�Š�\¯©¸¿¹ÔB€Að  �f q¥v°'°¸Aµ×0BÑ0BÓ0DÕ'DÕDÐð Ÿ+™+ a›.¨1¯;©;°q«>Õ9ÐØ˜•U—N‘NÓ$€MØ'×1Ñ1°!Ó4°}×7NÑ7NÈqÓ7QÕQÐØˆq„yØ1Ì4Ï8Ê8ÐT[ÓK\Õ5\Õ]ÐÜ�KŠKÐ+¨aÕ/°1Ñ4°c¸4Ó@€EØ.×2Ñ2Ó4°uÕ<×BÑBÓDÐàÕ7Ð7r   c                óÐ   € V ^8„  d   QhR\         P                  R\         P                  R\         P                  R\         P                  R\        R\        R\         P                  /# )r   Ú
sigma_nextÚcurr_lambdasÚlambda_sÚlambda_tr
   Úis_corrector_stepr   ©r   r   r   Úbool)r   s   "r   r   r   5   s   € ÷ #ñ #¼¿¹ð #ÔUZ×UaÑUað #Ômr×myÑmyð #ô  FK÷  FRñ  FRð #ô  [`ð #ô  uyð #ô  FK÷  FRñ  FRñ #r   c                óþ  € ^V^,          ,           pW2,
          pWP                  4       ,          pV'       dL   VRV,          V,          ,          p	W‡) V,          P                  4       P                  4       ,          V	,
          p
MfVRV,          V^,          ,          ,          VR,          V,
          ,          p
W‡) V,          P                  4       P                  4       ,          V
,
          p	\        P                  ! W©.4      # )z`Compute simple order-2 b coefficients from SA-Solver paper (Appendix D. Implementation Details).g      à?éþÿÿÿ)r   Úexpm1Únegr   Ústack)r*   r+   r,   r-   r
   r.   r    r!   Úalpha_tÚb_1Úb_2s   &&&&&&     r   Ú(compute_simple_stochastic_adams_b_coeffsr9   5   s¾   € à�%˜1•*�n€GØÕ€AØŸ<™<›>Õ)€Gßà˜˜w�¨Õ*Õ+ˆØ˜˜g�×,Ñ,Ó.×2Ñ2Ó4Õ4°sÕ:‰ð ˜˜w�¨¨a­Õ/Õ0°LÀÕ4DÀxÕ4OÕPˆØ˜˜g�×,Ñ,Ó.×2Ñ2Ó4Õ4°sÕ:ˆÜ�;Š;˜�zÓ"Ð"r   c                óÜ   € V ^8„  d   QhR\         P                  R\         P                  R\         P                  R\         P                  R\        R\        R\        R\         P                  /# )	r   r*   r+   r,   r-   r
   Úsimple_order_2r.   r   r/   )r   s   "r   r   r   E   sˆ   € ÷ (ñ (´%·,±,ð (ÌeÏlÉlð (Ôfk×frÑfrð (ô  D÷  Kñ  Kð (ô  TYð (ô  koð (ô  LPð (ô  ]b÷  ]iñ  ]iñ (r   c                ó6  € VP                   ^ ,          pV'       d   V^8X  d   \        WW#WF4      # \        W#Wt4      p\        P                  ! WRR7      P
                  p	\        P                  P                  W˜4      p
WP                  4       ,          pWº,          # )ay  Compute b_i coefficients for the SA-Solver (see eqs. 15 and 18).

The solver order corresponds to the number of input lambdas (half-logSNR points).

Args:
    sigma_next: Sigma at end time t.
    curr_lambdas: Lambda time points used to construct the Lagrange basis, shape (N,).
    lambda_s: Lambda at start time s.
    lambda_t: Lambda at end time t.
    tau_t: Stochastic strength parameter in the SDE.
    simple_order_2: Whether to enable the simple order-2 scheme.
    is_corrector_step: Flag for corrector step in simple order-2 mode.

Returns:
    b_i coefficients for the SA-Solver, shape (N,), where N is the solver order.
T)Ú
increasing)	Úshaper9   r(   r   ÚvanderÚTÚlinalgÚsolver   )r*   r+   r,   r-   r
   r;   r.   Únum_timestepsÚexp_integral_coeffsÚvandermonde_matrix_TÚlagrange_integralsr6   s   &&&&&&&     r   Ú!compute_stochastic_adams_b_coeffsrG   E   s…   € ð" !×&Ñ& qÕ)€Mç˜-¨1Ô,Ü7¸
ÐRZÐfkÓÐô 5°XÈÓ^ÐÜ Ÿ<š<¨ÐPTÔU×WÑWÐÜŸ™×+Ñ+Ð,@ÓVÐð
 Ÿ<™<›>Õ)€GØÕ'Ð'r   c                ó¢   € V ^8„  d   QhR\         R\         R\         R\        \        \        P                  \         3,          .\         3,          /# )r   Ústart_sigmaÚ	end_sigmaÚetar   )r   r   r   r   r   )r   s   "r   r   r   g   sV   € ÷ ñ ¤uð ¼ð ÄUð ÔU]Ô_dÔej×eqÑeqÔsxÐexÕ_yÐ^zô  }Bð  _Bõ  VCñ r   c                ó"   a aa€ R VVV 3R llpV# )a	  Return a function that controls the stochasticity of SA-Solver.

When eta = 0, SA-Solver runs as ODE. The official approach uses
time t to determine the SDE interval, while here we use sigma instead.

See:
    https://github.com/scxue/SA-Solver/blob/main/README.md
c                óf   € V ^8„  d   QhR\         \        P                  \        3,          R\        /# )r   Úsigmar   )r   r   r   r   )r   s   "r   r   Ú+get_tau_interval_func.<locals>.__annotate__q   s,   € ÷ Añ AœœeŸl™l¬EÐ1Õ2ð A´uñ Ar   c                 óž   <€ S^ 8:  d   R# \        V \        P                  4      '       d   V P                  4       p ST u;8¼  d	   S8¼  d   S#  R# )r   g        )Ú
isinstancer   r   Úitem)rN   rJ   rK   rI   s   &€€€r   Útau_funcÚ'get_tau_interval_func.<locals>.tau_funcq   sC   ø€ Ø�!Œ8Ùä�eœUŸ\™\×*Ò*Ø—J‘J“LˆEØ! UÖ7¨iÔ7ˆsÐ@Ð7¸SÐ@r   © )rI   rJ   rK   rS   s   fff r   Úget_tau_interval_funcrV   g   s   ú€ ÷Aò Að €Or   )F)FF)r   )	r   Útypingr   r   r   r(   r9   rG   rV   rU   r   r   Ú<module>rX      s+   ðó
 ß "Û õ(8÷V#÷ (÷Dñ r   