+
    „°ñj©  ã                   ó°   € ^ RI t^ RIt^ RIt^ RIHt ^ RIHtHt  ! R R]P                  4      t
 ! R R]P                  4      t ! R R	]4      tR
 R ltR# )é    N)Úoverride)ÚComfyExtensionÚioc                   óX   a € ] tR t^t o Rt]R 4       t]RV 3R lR ll4       t]tRt	V t
R# )ÚSkipLayerGuidanceDiTzú
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                óP  € \         P                  ! R RRR\         P                  P                  R4      \         P                  P                  RRRR7      \         P                  P                  RRRR7      \         P
                  P                  R	R
RRRR7      \         P
                  P                  RRRRRRR7      \         P
                  P                  RRRRRRR7      \         P
                  P                  RRRRRR7      .\         P                  P                  4       .R7      # )r   ú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{®Gáz„?ç      ð?çü©ñÒ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   &Ú:C:\Ai_Coding\apps\studio\ComfyUI\comfy_extras\nodes_slg.pyÚdefine_schemaÚ"SkipLayerGuidanceDiT.define_schema   sç   € ä�yŠyØ*Ø(ØhØ ä—‘—‘˜wÓ'Ü—	‘	—‘ ¸ÈT�ÓRÜ—	‘	—‘ ¸ÈT�ÓRÜ—‘—‘˜w°¸À$ÈS�ÓQÜ—‘—‘˜¸À#È3ÐUZÐei�ÓjÜ—‘—‘˜}°dÀÈÐSXÐcg�ÓhÜ—‘—‘Ð0¸#À3ÈDÐW[�Ó\ðô —‘—‘Ó!ðô
ð 	
ó    c                ó4   <€ V ^8„  d   QhRS[ P                  /# ©é   Úreturn©r   Ú
NodeOutput)ÚformatÚ__classdict__s   "€r,   Ú__annotate__Ú!SkipLayerGuidanceDiT.__annotate__%   s#   ø€ ÷ 1 ñ 1 Ñy{÷  zGñ  zGñ 1 r/   c           	     óR  aaaaaaaaa€ R  oVP                  R4      oSP                  S4      oSP                  V4      o\        P                  ! RS4      oS Uu. uF  p\	        V4      NK  	  upo\        P                  ! RS4      oS Uu. uF  p\	        V4      NK  	  upo\        S4      ^ 8X  d'   \        S4      ^ 8X  d   \        P                  ! V4      # VVVVVVVVV3	R lp	VP                  4       p
V
P                  V	4       \        P                  ! V
4      # u upi u upi )c                 ó   € V # ©N© ©ÚargsÚ
extra_argss   &&r,   ÚskipÚ*SkipLayerGuidanceDiT.execute.<locals>.skip'   ó   € ØˆKr/   Úmodel_samplingú\d+c                 óÖ  <	€ V R ,          pV R,          pV R,          pV R,          pV R,          pV R,          pV R,          P                  4       pS F&  p\        P                  P                  VSRRV4      pK(  	  S F&  p\        P                  P                  VSRR	V4      pK(  	  SP	                  S4       V^ ,          P                  4       p	S^ 8”  d”   V	S8¼  d�   V	S8:  d†   \        P                  P                  W.WeV4      w  p
WBV
,
          S,          ,           pS^ 8w  dE   VP                  4       VP                  4       ,          pSV,          ^S,
          ,           pWK,          pV# )
r
   Úcond_denoisedÚcondÚdenoisedÚsigmaÚinputÚmodel_optionsÚditÚdouble_blockÚsingle_block)	ÚcopyÚcomfyÚmodel_patcherÚset_model_options_patch_replaceÚpercent_to_sigmaÚitemÚsamplersÚcalc_cond_batchÚstd)r?   r
   Ú	cond_predrH   Ú
cfg_resultrJ   ÚxrL   ÚlayerÚsigma_ÚslgÚfactorr   rD   r   r   Ú	sigma_endÚsigma_startr   rA   r   s   &           €€€€€€€€€r,   Úpost_cfg_functionÚ7SkipLayerGuidanceDiT.execute.<locals>.post_cfg_function7   s[  ø€ Ø˜•MˆEØ˜_Õ-ˆIØ˜•<ˆDØ˜jÕ)ˆJØ˜•MˆEØ�W•ˆAØ  Õ1×6Ñ6Ó8ˆMã&�Ü %× 3Ñ 3× SÑ SÐTaÐcgÐinÐp~ð  AFó  !G’ñ 'ó '�Ü %× 3Ñ 3× SÑ SÐTaÐcgÐinÐp~ð  AFó  !G’ñ 'ð ×+Ñ+¨MÔ:à˜1•X—]‘]“_ˆFØ�qŒy˜V yÔ0°V¸{Ô5JÜŸ™×7Ñ7¸¸vÀqÐQ^Ó_‘�Ø'°s­?¸eÕ*CÕC�
Ø" aÔ'Ø&Ÿ]™]›_¨z¯~©~Ó/?Õ?�FØ,¨vÕ5¸¸_Õ9LÕM�FØÕ(�JàÐr/   )
Úget_model_objectrT   ÚreÚfindallÚintÚlenr   r5   ÚcloneÚ#set_model_sampler_post_cfg_function)r+   r
   r   r   r   r   r   r   Úirb   ÚmrD   r`   ra   rA   s   &&ff&fff   @@@@r,   ÚexecuteÚSkipLayerGuidanceDiT.execute$   sô   ÿø€ ò	ð ×/Ñ/Ð0@ÓAˆØ$×5Ñ5°mÓDˆØ"×3Ñ3°KÓ@ˆ	äŸ
š
 6¨=Ó9ˆÙ)6Ó7© Aœ˜Qž©Ñ7ˆäŸ
š
 6¨=Ó9ˆÙ)6Ó7© Aœ˜Qž©Ñ7ˆäˆ}Ó Ô"¤s¨=Ó'9¸QÔ'>Ü—=’= Ó'Ð'÷	õ 	ð8 �K‰K‹MˆØ	×-Ñ-Ð.?Ô@ä�}Š}˜QÓÐùòO 8ùò 8s   ÁDÂD$r=   N)Ú ro   r   ©Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Úclassmethodr-   rm   Úskip_guidanceÚ__static_attributes__Ú__classdictcell__©r7   s   @r,   r   r      s?   ø‡ € ñð ñ
ó ð
ð( ÷1 ñ 1 ó ð1 ðf †Mr/   r   c                   óX   a € ] tR t^[t o Rt]R 4       t]RV 3R lR ll4       t]tRt	V t
R# )ÚSkipLayerGuidanceDiTSimplezU
Simple version of the SkipLayerGuidanceDiT node that only modifies the uncond pass.
c                óÄ  € \         P                  ! R RRR\         P                  P                  R4      \         P                  P                  RRRR7      \         P                  P                  RRRR7      \         P
                  P                  R	R
R
RRRR7      \         P
                  P                  RRR
RRRR7      .\         P                  P                  4       .R7      # )r|   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±   € ä�yŠyØ0Ø(ØmØ ä—‘—‘˜wÓ'Ü—	‘	—‘ ¸ÈT�ÓRÜ—	‘	—‘ ¸ÈT�ÓRÜ—‘—‘˜¸ÀÈ#ÐTYÐdh�ÓiÜ—‘—‘˜}°c¸sÈÐRWÐbf�Ógðô —‘—‘Ó!ðô
ð 	
r/   c                ó4   <€ V ^8„  d   QhRS[ P                  /# r1   r4   )r6   r7   s   "€r,   r8   Ú'SkipLayerGuidanceDiTSimple.__annotate__s   s   ø€ ÷ . ñ . Ñ_a×_lÑ_lñ . r/   c                óB  aaa
aa€ R  oVP                  R4      pVP                  V4      oVP                  V4      o
\        P                  ! RS4      oS Uu. uF  p\	        V4      NK  	  upo\        P                  ! RS4      oS Uu. uF  p\	        V4      NK  	  upo\        S4      ^ 8X  d'   \        S4      ^ 8X  d   \        P                  ! V4      # VV
VVV3R lpVP                  4       p	V	P                  V4       \        P                  ! V	4      # u upi u upi )c                 ó   € V # r<   r=   r>   s   &&r,   rA   Ú0SkipLayerGuidanceDiTSimple.execute.<locals>.skipt   rC   r/   rD   rE   c                 ón  <€ V R ,          pV R,          pV R,          pV R,          pV R,          pVP                  4       pS F&  p\        P                  P                  VSRRV4      pK(  	  S F&  p\        P                  P                  VSRRV4      pK(  	  Vw  r‰V^ ,          P	                  4       p
V
S8¼  d\   V
S8:  dU   V	eQ   \        P
                  P                  W(R.WV4      w  r¼\        P
                  P                  VRV	.WV4      w  rÍW½.pV# \        P
                  P                  W#WV4      pV# )	rK   r
   ÚcondsrJ   rL   rM   rN   rO   N)rP   rQ   rR   rS   rU   rV   rW   )r?   r[   r
   r…   rJ   rL   Úslg_model_optionsr\   rH   Úuncondr]   Úcond_outÚ_Ú
uncond_outÚoutr   r`   ra   r   rA   s   &              €€€€€r,   Úcalc_cond_batch_functionÚDSkipLayerGuidanceDiTSimple.execute.<locals>.calc_cond_batch_function„   sP  ø€ Ø�W•ˆAØ˜•MˆEØ˜•MˆEØ˜•MˆEà  Õ1ˆMØ -× 2Ñ 2Ó 4Ðã&�Ü$)×$7Ñ$7×$WÑ$WÐXiÐkoÐqvð  yGð  INó  %OÒ!ñ 'ó '�Ü$)×$7Ñ$7×$WÑ$WÐXiÐkoÐqvð  yGð  INó  %OÒ!ñ 'ð !‰LˆDØ˜1•X—]‘]“_ˆFØ˜Ô" v°Ô'<ÀÒASÜ#Ÿn™n×<Ñ<¸UÈ4ÀLÐRSÐ\iÓj‘�Ü %§¡× >Ñ >¸uÀtÈVÀnÐVWÐ`qÓ r‘�ØÐ,�ð ˆJô —n‘n×4Ñ4°UÀ1È]Ó[�àˆJr/   )
rd   rT   re   rf   rg   rh   r   r5   ri   Ú*set_model_sampler_calc_cond_batch_function)r+   r
   r   r   r   r   rD   rk   rŒ   rl   r`   ra   rA   s   &&&&ff    @@@r,   rm   Ú"SkipLayerGuidanceDiTSimple.executer   só   ü€ ò	ð ×/Ñ/Ð0@ÓAˆØ$×5Ñ5°mÓDˆØ"×3Ñ3°KÓ@ˆ	äŸ
š
 6¨=Ó9ˆÙ)6Ó7© Aœ˜Qž©Ñ7ˆäŸ
š
 6¨=Ó9ˆÙ)6Ó7© Aœ˜Qž©Ñ7ˆäˆ}Ó Ô"¤s¨=Ó'9¸QÔ'>Ü—=’= Ó'Ð'÷	ñ 	ð4 �K‰K‹MˆØ	×4Ñ4Ð5MÔNä�}Š}˜QÓÐùòK 8ùò 8s   ÁDÂ	Dr=   N)ro   ro   rp   rz   s   @r,   r|   r|   [   s?   ø‡ € ñð ñ
ó ð
ð$ ÷. ñ . ó ð. ð` †Mr/   r|   c                   ó<   a € ] tR t^¦t o ]V 3R lR l4       tRtV tR# )ÚSkipLayerGuidanceExtensionc                óT   <€ V ^8„  d   QhRS[ S[S[P                  ,          ,          /# r1   )ÚlistÚtyper   Ú	ComfyNode)r6   r7   s   "€r,   r8   Ú'SkipLayerGuidanceExtension.__annotate__¨   s"   ø€ ÷ 
ñ 
¡T©$©r¯|©|Õ*<Õ%=ñ 
r/   c              ƒ  ó"   "  € \         \        .# 5ir<   )r   r|   )Úselfs   &r,   Úget_node_listÚ(SkipLayerGuidanceExtension.get_node_list§   s   é € ô !Ü&ð
ð 	
ùs   ‚r=   N)rq   rr   rs   rt   r   r™   rx   ry   rz   s   @r,   r‘   r‘   ¦   s   ø‡ € Ø÷
ó ö
r/   r‘   c                ó$   € V ^8„  d   QhR\         /# r1   ©r‘   )r6   s   "r,   r8   r8   ¯   s   € ÷ (ñ (Ô :ñ (r/   c               ƒ   ó   "  € \        4       # 5ir<   rœ   r=   r/   r,   Úcomfy_entrypointrž   ¯   s   é € Ü%Ó'Ð'ùs   ‚)Úcomfy.model_patcherrQ   Úcomfy.samplersre   Útyping_extensionsr   Úcomfy_api.latestr   r   r•   r   r|   r‘   rž   r=   r/   r,   Ú<module>r£      sK   ðÛ Û Û 	Ý &ß /ôP˜2Ÿ<™<ô PôfH §¡ô HôV
 ô 
÷(r/   