
    +j                         d dl Zd dlZd dlZd dlmZ d dlmZmZ  G d dej	                  Z
 G d dej	                  Z G d d	e          Zd
efdZdS )    N)override)ComfyExtensionioc                   T    e Zd ZdZed             Zeddej        fd            ZeZ	dS )	SkipLayerGuidanceDiTa
  
    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                 L   t          j        ddddt           j                            d          t           j                            ddd          t           j                            d	dd          t           j                            d
dddd          t           j                            dddddd          t           j                            dddddd          t           j                            ddddd          gt           j                                        g          S )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_schemaz"SkipLayerGuidanceDiT.define_schema   s    y*(h w''	TRR	TRRw$SQQ#3UZeijj}dSXcghh0#3DW[\\ !!
 
 
 	
     r   returnc           	      $  
 d |                     d          

                              
                    |          t          j        d          d D             t          j        d          d D             t	                    dk    r't	                    dk    rt          j        |          S 
f	d}|                                }	|	                    |           t          j        |	          S )Nc                     | S N args
extra_argss     r,   skipz*SkipLayerGuidanceDiT.execute.<locals>.skip'       Kr.   model_sampling\d+c                 ,    g | ]}t          |          S r4   int.0is     r,   
<listcomp>z0SkipLayerGuidanceDiT.execute.<locals>.<listcomp>/       777AQ777r.   c                 ,    g | ]}t          |          S r4   r=   r?   s     r,   rB   z0SkipLayerGuidanceDiT.execute.<locals>.<listcomp>2   rC   r.   r   c                   	 | d         }| d         }| d         }| d         }| d         }| d         }| d                                          }D ]%}t          j                            |dd	|          }&D ]%}t          j                            |dd
|          }&                               |d                                         }	dk    r||	k    rv|	k    rpt          j                            ||g|||          \  }
|||
z
  z  z   }dk    r9|                                |                                z  }|z  dz
  z   }||z  }|S )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)r6   r
   	cond_predrG   
cfg_resultrI   xrK   layersigma_slgfactorr   r:   r   r   	sigma_endsigma_startr   r8   r   s               r,   post_cfg_functionz7SkipLayerGuidanceDiT.execute.<locals>.post_cfg_function7   s   ME_-I<Dj)JMEWA 16688M& G G % 3 S STacginp~  AF  !G  !G& G G % 3 S STacginp~  AF  !G  !G++M:::1X]]__FqyyVy00V{5J5J77vq%Q^__'9s?e*CC
"a''&]]__z~~/?/??F,v5_9LMF&(Jr.   )	get_model_objectrT   refindalllenr   
NodeOutputclone#set_model_sampler_post_cfg_function)r+   r
   r   r   r   r   r   r   rb   mr:   r`   ra   r8   s     `` ```  @@@@r,   executezSkipLayerGuidanceDiT.execute$   sH   	 	 	 //0@AA$55mDD"33K@@	
6=9977777
6=9977777}""s='9'9Q'>'>='''	 	 	 	 	 	 	 	 	 	 	 	 	8 KKMM	--.?@@@}Qr.   N)r/   r/   r   
__name__
__module____qualname____doc__classmethodr-   r   rg   rk   skip_guidancer4   r.   r,   r   r      so          
 
 [
( 1  1 y{  zG 1  1  1  [1 f MMMr.   r   c                   T    e Zd ZdZed             Zeddej        fd            ZeZ	dS )SkipLayerGuidanceDiTSimplez]
    Simple version of the SkipLayerGuidanceDiT node that only modifies the uncond pass.
    c                    t          j        ddddt           j                            d          t           j                            ddd          t           j                            d	dd          t           j                            d
ddddd          t           j                            dddddd          gt           j                                        g          S )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-   z(SkipLayerGuidanceDiTSimple.define_schema_   s    y0(m w''	TRR	TRR#TYdhii}csRWbfgg !!
 
 
 	
r.   r/   r0   c                   	
 d |                     d          }|                    |          
|                    |          	t          j        d          d D             t          j        d          d D             t	                    dk    r't	                    dk    rt          j        |          S 	
fd}|                                }|                    |           t          j        |          S )Nc                     | S r3   r4   r5   s     r,   r8   z0SkipLayerGuidanceDiTSimple.execute.<locals>.skipt   r9   r.   r:   r;   c                 ,    g | ]}t          |          S r4   r=   r?   s     r,   rB   z6SkipLayerGuidanceDiTSimple.execute.<locals>.<listcomp>|   rC   r.   c                 ,    g | ]}t          |          S r4   r=   r?   s     r,   rB   z6SkipLayerGuidanceDiTSimple.execute.<locals>.<listcomp>   rC   r.   r   c                 j   | d         }| d         }| d         }| d         }| d         }|                                 }D ]%}t          j                            |dd|          }&D ]%}t          j                            |dd|          }&|\  }}	|d	                                         }
|
k    r]|
k    rW|	Ut          j                            ||d g|||          \  }}t          j                            |d |	g|||          \  }}||g}n#t          j                            |||||          }|S )
NrJ   r
   condsrI   rK   rL   rM   rN   r   )rP   rQ   rR   rS   rU   rV   rW   )r6   r[   r
   r{   rI   rK   slg_model_optionsr\   rG   uncondr]   cond_out_
uncond_outoutr   r`   ra   r   r8   s                  r,   calc_cond_batch_functionzDSkipLayerGuidanceDiTSimple.execute.<locals>.calc_cond_batch_function   s   WAMEMEME 1M - 2 2 4 4& O O$)$7$W$WXikoqv  yG  IN  %O  %O!!& O O$)$7$W$WXikoqv  yG  IN  %O  %O!! LD&1X]]__F""v'<'<AS#n<<UT4LRSUZ\ijj! % > >utVnVWY^`q r r:,n44UE1e][[Jr.   )	rc   rT   rd   re   rf   r   rg   rh   *set_model_sampler_calc_cond_batch_function)r+   r
   r   r   r   r   r:   r   rj   r`   ra   r8   s       ``   @@@r,   rk   z"SkipLayerGuidanceDiTSimple.executer   s0   	 	 	 //0@AA$55mDD"33K@@	
6=9977777
6=9977777}""s='9'9Q'>'>='''	 	 	 	 	 	 	 	 	4 KKMM	445MNNN}Qr.   N)r/   r/   rl   r4   r.   r,   rt   rt   [   sk          
 
 [
$ .  . _a_l .  .  .  [. ` MMMr.   rt   c                   L    e Zd Zedeeej                          fd            ZdS )SkipLayerGuidanceExtensionr0   c                 "   K   t           t          gS r3   )r   rt   )selfs    r,   get_node_listz(SkipLayerGuidanceExtension.get_node_list   s       !&
 	
r.   N)	rm   rn   ro   r   listtyper   	ComfyNoder   r4   r.   r,   r   r      sE        
T$r|*<%= 
 
 
 X
 
 
r.   r   r0   c                  "   K   t                      S r3   )r   r4   r.   r,   comfy_entrypointr      s      %'''r.   )comfy.model_patcherrQ   comfy.samplersrd   typing_extensionsr   comfy_api.latestr   r   r   r   rt   r   r   r4   r.   r,   <module>r      s
           				 & & & & & & / / / / / / / /P P P P P2< P P PfH H H H H H H HV
 
 
 
 
 
 
 
( : ( ( ( ( ( (r.   