
    +j                         S SK rS SKrS SKrS SKJr  S SKJrJr   " S S\R                  5      r
 " S S\R                  5      r " S S	\5      rS
\4S jrg)    N)override)ComfyExtensionioc                   \    \ rS rSrSr\S 5       r\SS\R                  4S jj5       r	\	r
Srg)	SkipLayerGuidanceDiT   z
Enhance guidance towards detailed dtructure by having another set of CFG negative with skipped layers.
Inspired by Perturbed Attention Guidance (https://arxiv.org/abs/2403.17377)
Original experimental implementation for SD3 by Dango233@StabilityAI.
c                 &   [         R                  " SSSS[         R                  R                  S5      [         R                  R                  SSSS9[         R                  R                  S	SSS9[         R
                  R                  S
SSSSS9[         R
                  R                  SSSSSSS9[         R
                  R                  SSSSSSS9[         R
                  R                  SSSSSS9/[         R                  R                  5       /S9$ )Nr   advanced/guidancezNGeneric version of SkipLayerGuidance node that can be used on every DiT model.Tmodeldouble_layers7, 8, 9defaultadvancedsingle_layersscaleg      @        g      $@g?)r   minmaxstepstart_percentg{Gz?      ?MbP?r   r   r   r   r   end_percentg333333?rescaling_scalenode_idcategorydescriptionis_experimentalinputsoutputsr   SchemaModelInputStringFloatOutputclss    6/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_slg.pydefine_schema"SkipLayerGuidanceDiT.define_schema   s    yy*(h w'		TR		TRw$SQ#3UZeij}dSXcgh0#3DW[\ !
 	
    returnc           	      R  ^^^^^^^^^ S mUR                  S5      mTR                  T5      mTR                  U5      m[        R                  " ST5      mT Vs/ s H  n[	        U5      PM     snm[        R                  " ST5      mT Vs/ s H  n[	        U5      PM     snm[        T5      S:X  a%  [        T5      S:X  a  [        R                  " U5      $ UUUUUUUUU4	S jn	UR                  5       n
U
R                  U	5        [        R                  " U
5      $ s  snf s  snf )Nc                     U $ N args
extra_argss     r-   skip*SkipLayerGuidanceDiT.execute.<locals>.skip'       Kr0   model_sampling\d+r   c                 N  >	 U S   nU S   nU S   nU S   nU S   nU S   nU S   R                  5       nT H&  n[        R                  R                  UTSS	U5      nM(     T H&  n[        R                  R                  UTSS
U5      nM(     TR	                  T5        US   R                  5       n	TS:  ap  U	T:  aj  U	T::  ad  [        R                  R                  X/XeU5      u  n
XBU
-
  T-  -   nTS:w  a0  UR                  5       UR                  5       -  nTU-  ST-
  -   nXK-  nU$ )Nr   cond_denoisedconddenoisedsigmainputmodel_optionsditdouble_blocksingle_blockr      )	copycomfymodel_patcherset_model_options_patch_replacepercent_to_sigmaitemsamplerscalc_cond_batchstd)r7   r   	cond_predr@   
cfg_resultrB   xrD   layersigma_slgfactorr   r<   r   r   	sigma_endsigma_startr   r9   r   s               r-   post_cfg_function7SkipLayerGuidanceDiT.execute.<locals>.post_cfg_function7   s[   ME_-I<Dj)JMEWA 1668M& % 3 3 S STacginp~  AF  !G ' ' % 3 3 S STacginp~  AF  !G ' ++M:1X]]_FqyVy0V{5J77vqQ^_'s?e*CC
"a'&]]_z~~/??F,v5_9LMF(Jr0   )
get_model_objectrM   refindallintlenr   
NodeOutputclone#set_model_sampler_post_cfg_function)r,   r   r   r   r   r   r   r   ir[   mr<   rY   rZ   r9   s     `` ```   @@@@r-   executeSkipLayerGuidanceDiT.execute$   s    	 //0@A$55mD"33K@	

6=9)67AQ7

6=9)67AQ7}"s='9Q'>==''	 	8 KKM	--.?@}}QO 8 8s   DD$r5   N) ri   r   __name__
__module____qualname____firstlineno____doc__classmethodr.   r   rb   rg   skip_guidance__static_attributes__r5   r0   r-   r   r      sM     
 
( 1 y{  zG  zG 1  1 f Mr0   r   c                   \    \ rS rSrSr\S 5       r\SS\R                  4S jj5       r	\	r
Srg)	SkipLayerGuidanceDiTSimple[   zU
Simple version of the SkipLayerGuidanceDiT node that only modifies the uncond pass.
c                    [         R                  " SSSS[         R                  R                  S5      [         R                  R                  SSSS9[         R                  R                  S	SSS9[         R
                  R                  S
SSSSSS9[         R
                  R                  SSSSSSS9/[         R                  R                  5       /S9$ )Nrt   r
   zSSimple version of the SkipLayerGuidanceDiT node that only modifies the uncond pass.Tr   r   r   r   r   r   r   r   r   r   r   r   r$   r+   s    r-   r.   (SkipLayerGuidanceDiTSimple.define_schema_   s    yy0(m w'		TR		TR#TYdhi}csRWbfg !
 	
r0   r1   c                 B  ^^^
^^ S mUR                  S5      nUR                  U5      mUR                  U5      m
[        R                  " ST5      mT Vs/ s H  n[	        U5      PM     snm[        R                  " ST5      mT Vs/ s H  n[	        U5      PM     snm[        T5      S:X  a%  [        T5      S:X  a  [        R                  " U5      $ UU
UUU4S jnUR                  5       n	U	R                  U5        [        R                  " U	5      $ s  snf s  snf )Nc                     U $ r4   r5   r6   s     r-   r9   0SkipLayerGuidanceDiTSimple.execute.<locals>.skipt   r;   r0   r<   r=   r   c                 8  > U S   nU S   nU S   nU S   nU S   nUR                  5       nT H&  n[        R                  R                  UTSSU5      nM(     T H&  n[        R                  R                  UTSSU5      nM(     Uu  pUS	   R	                  5       n
U
T:  aY  U
T::  aS  U	bP  [        R
                  R                  X(S /XU5      u  p[        R
                  R                  US U	/XU5      u  pX/nU$ [        R
                  R                  X#XU5      nU$ )
NrC   r   condsrB   rD   rE   rF   rG   r   )rI   rJ   rK   rL   rN   rO   rP   )r7   rT   r   r|   rB   rD   slg_model_optionsrU   r@   uncondrV   cond_out_
uncond_outoutr   rY   rZ   r   r9   s                  r-   calc_cond_batch_functionDSkipLayerGuidanceDiTSimple.execute.<locals>.calc_cond_batch_function   sP   WAMEMEME 1M - 2 2 4&$)$7$7$W$WXikoqv  yG  IN  %O! ' '$)$7$7$W$WXikoqv  yG  IN  %O! ' !LD1X]]_F"v'<AS#nn<<U4LRS\ij % > >utVnVW`q r, J nn44U1][Jr0   )
r]   rM   r^   r_   r`   ra   r   rb   rc   *set_model_sampler_calc_cond_batch_function)r,   r   r   r   r   r   r<   re   r   rf   rY   rZ   r9   s       ``    @@@r-   rg   "SkipLayerGuidanceDiTSimple.executer   s    	 //0@A$55mD"33K@	

6=9)67AQ7

6=9)67AQ7}"s='9Q'>==''	 	4 KKM	445MN}}QK 8 8s   D
Dr5   N)ri   ri   rj   r5   r0   r-   rt   rt   [   sE     
 
$ . _a_l_l .  . ` Mr0   rt   c                   L    \ rS rSr\S\\\R                        4S j5       r	Sr
g)SkipLayerGuidanceExtension   r1   c                 "   #    [         [        /$ 7fr4   )r   rt   )selfs    r-   get_node_list(SkipLayerGuidanceExtension.get_node_list   s      !&
 	
s   r5   N)rk   rl   rm   rn   r   listtyper   	ComfyNoder   rr   r5   r0   r-   r   r      s)    
T$r||*<%= 
 
r0   r   r1   c                     #    [        5       $ 7fr4   )r   r5   r0   r-   comfy_entrypointr      s     %''s   )comfy.model_patcherrJ   comfy.samplersr^   typing_extensionsr   comfy_api.latestr   r   r   r   rt   r   r   r5   r0   r-   <module>r      sS      	 & /P2<< PfH HV
 
( : (r0   