
    +j                     &   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Zd dlZd dlmZ d dlmZ d dlmZmZ d dlmZmZ  ej        d          Z G d dej                  Z G d	 d
ej                  Z G d dej                  Z G d dej                  Zd0dZd1dZd Zd1dZ  G d dej                  Z! G d dej                  Z" G d dej                  Z# G d dej                  Z$ G d dej                  Z%dej&        fdZ'd  Z(d2d"ej)        fd#Z* G d$ d%ej                  Z+d dl,Z G d& d'ej                  Z- G d( d)ej                  Z. G d* d+ej                  Z/ G d, d-e          Z0d.e0fd/Z1dS )3    N)BytesIO)override)SymmetricPatchifierlatent_to_pixel_coords)ComfyExtensionioIC_LORA_PARAMETERSc                   J    e Zd Zed             Zedej        fd            ZdS )GetICLoRAParametersc                     t          j        ddddg dt           j                            dd          gt                              d	d
          g          S )Nr   zGet IC-LoRA ParameterszExtracts IC-LoRA parameters from the safetensors metadata of a LoRA-loaded model and outputs them for LTXVAddGuide (eg. reference_downscale_factor).model/conditioning/video_models)zic-lorazic loraiclorazdownscale factorzreference downscaleiclora_modelz]Direct output from a LoRA Loader for the specific IC-LoRA from which to extract the metadata.tooltipiclora_parameterszIC-LoRA parameters extracted from the LoRA metadata (eg. reference_downscale_factor). Connect to LTXVAddGuide if the LoRA requires special handling of the guides.)node_iddisplay_namedescriptioncategorysearch_aliasesinputsoutputs)r   SchemaModelInputICLoRAParametersOutputclss    5/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_lt.pydefine_schemaz!GetICLoRAParameters.define_schema   s    y)1d6fff"B    !'''S (  
 
 
 	
    returnc                    |                     d          }d}|rY	 t          dt          t          |                    dd                                        }n# t
          t          f$ r d}Y nw xY wd|i}t          j        |          S )Nlora_metadata   reference_downscale_factor)	get_attachmentmaxroundfloatget	TypeError
ValueErrorr   
NodeOutput)r    r   metadatafactor
parameterss        r!   executezGetICLoRAParameters.execute/   s    ..?? 	QeHLL9UWX,Y,Y&Z&Z [ [\\z*   2F;
}Z(((s   >A A0/A0N__name__
__module____qualname__classmethodr"   r   r0   r4    r#   r!   r   r      sV        
 
 [
2 	)bm 	) 	) 	) [	) 	) 	)r#   r   c                   P    e Zd Zed             Zeddej        fd            ZeZdS )EmptyLTXVLatentVideoc                    t          j        ddt           j                            dddt          j        d          t           j                            dd	dt          j        d          t           j                            d
ddt          j        d          t           j                            dddd          gt           j                                        g          S )Nr<   model/latent/video/ltxvwidth   @       defaultminr*   stepheight   lengtha   r'      
batch_size   rD   rE   r*   r   r   r   r   )r   r   Intr   nodesMAX_RESOLUTIONLatentr   r   s    r!   r"   z"EmptyLTXVLatentVideo.define_schema=   s    y*.Wcru?SZ\]]Xs@T[]^^Xrqe>RYZ[[\1!FF	 	  ""
 
 
 	
r#   r'   r$   c                     t          j        |d|dz
  dz  dz   |dz  |dz  gt          j                                                  }t          j        |dd          S )N   r'   rK   rB   device)samplesdownscale_ratio_spacial)torchzeroscomfymodel_managementintermediate_devicer   r0   )r    r?   rG   rI   rL   latents         r!   r4   zEmptyLTXVLatentVideo.executeM   s    j#!0AQ/FRTV[_aVabkp  lB  lV  lV  lX  lX  Y  Y  Y}BOOPPPr#   Nr'   	r6   r7   r8   r9   r"   r   r0   r4   generater:   r#   r!   r<   r<   <   sb        
 
 [
 Q QR] Q Q Q [Q HHHr#   r<   c                   N    e Zd Zed             Zedej        fd            ZeZdS )LTXVImgToVideoc                 r   t          j        ddt           j                            d          t           j                            d          t           j                            d          t           j                            d          t           j                            ddd	t          j        d
          t           j                            ddd	t          j        d
          t           j                            dddt          j        d          t           j                            dddd          t           j	                            dddd          g	t           j        
                    d          t           j        
                    d          t           j        
                    d          g          S )Nrd   r   positivenegativevaeimager?   r@   rA   rB   rC   rG   rH   rI   rJ   	   rK   rL   r'   rM   rN   strength      ?        r   r_   rO   )r   r   Conditioningr   VaeImagerP   rQ   rR   Floatr   rS   r   s    r!   r"   zLTXVImgToVideo.define_schemaU   sF   y$6%%j11%%j11U##w''Wcru?SZ\]]Xs@T[]^^Xrqe>RYZ[[\1!FFz3CSII
 &&J&??&&J&??	  h 77
 
 
 	
r#   r$   c
                 ~   t           j                            |                    dd          ||dd                              dd          }
|
d d d d d d d df         }|                    |          }t          j        |d|dz
  dz  dz   |dz  |dz  gt           j                                        	          }||d d d d d |j	        d
         f<   t          j
        |d|j	        d
         ddft
          j        |j                  }d|	z
  |d d d d d |j	        d
         f<   t          j        ||||d          S )Nr'   bilinearcenter   rU   rK   rB   rV      dtyperW   rl   rX   
noise_mask)r\   utilscommon_upscalemovedimencoderZ   r[   r]   r^   shapeonesfloat32rW   r   r0   )r    rf   rg   ri   rh   r?   rG   rI   rL   rk   pixelsencode_pixelstr_   conditioning_latent_frames_masks                  r!   r4   zLTXVImgToVideo.executel   sz   ++EMM"a,@,@%Q[]effnnoprtuuqqq!!!QQQ{+JJ}%%j#!0AQ/FRTV[_aVabkp  lB  lV  lV  lX  lX  Y  Y  Y$%qqq!!![agaj[ !*/*FLOQ2-=+
 +
 +
'
 >A8^'111kqwqzk(9:}XxVSr1s1stttr#   Nra   r:   r#   r!   rd   rd   T   s^        
 
 [
, ueger u u u [u" HHHr#   rd   c                   P    e Zd Zed             Zeddej        fd            ZeZdS )LTXVImgToVideoInplacec                    t          j        ddt           j                            d          t           j                            d          t           j                            d          t           j                            dddd	          t           j                            d
dd          gt           j                            d          g          S )Nr   r   rh   ri   r_   rk   rl   rm   rN   bypassFzBypass the conditioning.)rD   r   rn   rO   )	r   r   rp   r   rq   rS   rr   Booleanr   r   s    r!   r"   z#LTXVImgToVideoInplace.define_schema   s    y+6U##w''	))z3CSII
  5B\ ]] 	  h 77
 
 
 	
r#   Fr$   c                 l   |r|fS |d                                          }|j        \  }}}	|j        \  }}}}
}||	z  }|
|z  }|j        d         |k    s|j        d         |k    rLt          j                            |                    dd          ||dd                              dd          }n|}|d d d d d d d df         }|                    |          }||d d d d d |j        d         f<   t          |          }d|z
  |d d d d d |j        d         f<   t          j
        ||d	          S )
NrX   r'   rx   rt   ru   rv   rw   rl   r{   )clonedownscale_index_formular   r\   r}   r~   r   r   get_noise_maskr   r0   )r    rh   ri   r_   rk   r   rX   _height_scale_factorwidth_scale_factorlatent_heightlatent_widthr?   rG   r   r   r   r   s                     r!   r4   zLTXVImgToVideoInplace.execute   sq    	9#))++' 	3 2 07},1a11!44;q>V##u{1~'>'>[//b!0D0DeVU_aijjrrstvxyyFFFqqq!!!QQQ{+JJ}%%%&111kqwqzk!"*8*@*@'=@8^'111kqwqzk(9:}@_``aaar#   N)Fra   r:   r#   r!   r   r      sb        
 
 [
  b bBM b b b [b6 HHHr#   r   rl   c                 H   |||(|                     d                               d          nd|d}g }| |fD ][}g }	|D ]$}
|
d                             dd          }||}	 n%g |	|}|                    t          j        |d|i                     \|d         |d         fS )zAppend a guide_attention_entry to both positive and negative conditioning.

    Each entry tracks one guide reference for per-reference attention control.
    Entries are derived independently from each conditioning to avoid cross-contamination.
    Nr   )pre_filter_countrk   
pixel_masklatent_shaper'   guide_attention_entries)	unsqueezer-   appendnode_helpersconditioning_set_values)rf   rg   r   r   rk   attention_mask	new_entryresultscondexistingr   foundentriess                r!   _append_guide_attention_entryr      s     -BPB\n..q11;;A>>>bf$	 I G8$   	 	AaDHH6==E   ! )H(i(|;,g6
 
 	 	 	 	 1:wqz!!r#   c                 D    | D ]}||d         v r|d         |         c S |S )Nr'   r:   )conditioningkeyrD   r   s       r!   conditioning_get_any_valuer      s<      !A$;;Q49 Nr#   c                     |                      dd           }| d         }|9|j        \  }}}}}t          j        |d|ddft          j        |j                  }n|                                }|S )Nr|   rX   r'   ry   )r-   r   rZ   r   r   rW   r   )r_   r|   latent_imagerL   r   latent_lengths         r!   r   r      s    L$//J)$L-9-?*
A}aZM1a0-&
 
 


  %%''
r#   c                 b   t          | dd           }|dS |8t          |          dk    r%|d         |d         z  }|j        d         |z  }||fS t          | dd           }|rt          d |D                       }||fS t	          j        |d d d	d d d	f                   j        d	         }||fS )
Nkeyframe_idxsNr      rt   rx   r   c              3   2   K   | ]}|d          d         V  dS )r   r   Nr:   ).0es     r!   	<genexpr>z$get_keyframe_idxs.<locals>.<genexpr>   s,      BBQAn-a0BBBBBBr#   r   )r   lenr   sumrZ   unique)r   r   r   tokens_per_framenum_keyframesr   s         r!   get_keyframe_idxsr      s    .t_dKKMwC$5$5$:$:'+l2.>>%+A.2BBm++(/H$OOG ,BB'BBBBBm++ Lqqq!QQQz!:;;A!DM-''r#   c                      e Zd Z edd          Zed             Zedd            Zed             Zed             Z	edd	            Z
edd
            Zedd            Zed             Zeddej        fd            ZeZdS )LTXVAddGuider'   T)	start_endc                 "   t          j        ddt           j                            d          t           j                            d          t           j                            d          t           j                            d          t           j                            dd	          t           j                            d
dddd          t           j                            ddddd          t           j	                            ddd          t                              ddd          g	t           j                            d          t           j                            d          t           j                            d          g          S )Nr   r   rf   rg   rh   r_   ri   zImage or video to condition the latent video on. Must be 8*n + 1 frames. If the video is not 8*n + 1 frames, it will be cropped to the nearest 8*n + 1 frames.r   	frame_idxr   ii'  a3  Frame index to start the conditioning at. For single-frame images or videos with 1-8 frames, any frame_idx value is acceptable. For videos with 9+ frames, frame_idx must be divisible by 8, otherwise it will be rounded down to the nearest multiple of 8. Negative values are counted from the end of the video.)rD   rE   r*   r   rk   rl   rm   g      $@{Gz?rC   r   TzyOptional pixel-space spatial mask. Controls per-region conditioning influence via self-attention, multiplied by strength.)optionalr   r   a   Optional IC-LoRA parameters from a Get IC-LoRA Parameters node. Used for adjusting guide processing as required by certain IC-LoRAs (eg. those with a reference_downscale_factor > 1). When chained, each LTXVAddGuide uses only the parameters connected to it.rn   rO   )r   r   ro   r   rp   rS   rq   rP   rr   Maskr   r   r   s    r!   r"   zLTXVAddGuide.define_schema   sn   y"6%%j11%%j11U##	))t   
 x  	 	 z3CTPTUU$!a    !&&'!h '  7#J &&J&??&&J&??	  h 77O,
 ,
 ,
 ,	
r#   c                    |\  }}}	|d |j         d         dz
  |z  |z  dz            }t          ||z  |z            }
t          ||	z  |z            }t          j                            |                    dd          |
|dd                              dd          }|d d d d d d d df         }|                    |          }||fS )Nr   r'   rt   ru   rv   )croprw   )r   intr\   r}   r~   r   r   )r    rh   r   r   imagesscale_factorslatent_downscale_factortime_scale_factorr   r   target_widthtarget_heightr   r   r   s                  r!   r   zLTXVAddGuide.encode*  s   ERB-/B[&,q/A-2CCFWWZ[[[\<*<<?VVWWM,??BYYZZ++FNN2q,A,A<Q^`jqy+zz  C  C  DE  GI  J  Jqqq!!!QQQ{+JJ}%%ar#   c                    |dk    r|d fS t          |          }|j        d d         |j        d         |z  |j        d         |z  fz   }t          j        ||j        |j                  }||dd d |d d |f<   t          j        |j        d         d|j        d         |j        d         |j        d         fd|j        |j                  }d	|dd d |d d |f<   ||fS )
Nr'   rw      )rW   rz   .r   rx   g      rl   )r   r   rZ   r[   rW   rz   full)r    guide_latentr   scaledilated_shapedilateddilated_masks          r!   dilate_latentzLTXVAddGuide.dilate_latent5  s   "a''%%+,,$*2A2.,2DQ2G%2OQ]QcdeQfinQn1oo+mL4G|Oabbb)5WWuWggg%&z]1q'-"2GM!4DgmTUFVW,L4F
 
 
 /2S''E'77U7*+$$r#   c                     |sdS 	 t          dt          t          |                    dd                                        }n# t          t
          f$ r d}Y nw xY w|S )Nr'   r(   )r*   r+   r,   r-   r.   r/   )r    r   r2   s      r!   get_reference_downscale_factorz+LTXVAddGuide.get_reference_downscale_factorD  sv      	1	E%(9(=(=>Z\](^(^"_"_``aaFF:& 	 	 	FFF	s   >A AANc                     |\  }}}t          ||          \  }}	||	z
  }
|dk    r|nt          |
dz
  |z  dz   |z   d          }|dk    r|dk    r|dz
  |z  |z  dz   }||z   dz
  |z  }||fS )Nr   r'   )r   r*   )r    r   r   guide_lengthr   r   r   r   r   r   latent_count
latent_idxs               r!   get_latent_indexzLTXVAddGuide.get_latent_indexN  s    "/1a,T<@@=$}4!*aIIS,:JN_9_bc9cfo9oqr5s5s	!	Q"Q+<<?PPSTTI"33a7<MM
*$$r#   c                 ,   t          |          \  }}| j                            |          \  }}	||dk    p|j        d         dk    }t	          |	||          }
|
d d dfxx         |z  cc<   |dz
  t          j        |dd          |
j                                      dddd          z  }|
d d dd d d dd fxx         |	                    |
j
                  z  cc<   ||
}nt          j        ||
gd          }t          j        |d|i          S )	Nr   rx   r'   
causal_fixrV   rt   dimr   )r   
PATCHIFIERpatchifyr   r   rZ   tensorrW   viewtorz   catr   r   )r    r   r   guiding_latentr   r   r   r   r   latent_coordspixel_coordsspatial_end_offsets               r!   add_keyframe_indexzLTXVAddGuide.add_keyframe_index[  sZ   ,T22q>22>BB="aG>+?+Ba+GJ-m]WabbbQQQTi' 69U\!""&>
 >
 >
 $q"a

 	QQQAAAqrr\"""&8&;&;L<N&O&OO""" (MM!I}l&CKKKM3D?M:Z[[[r#   rU   c                 d   |j         d         |
k    s|j         d         |
k    rt          d          |                     ||||||          }|                     ||||||          }|	t          |j         d         |	j         d                   }t          |j         d         |	j         d                   }|j         d         dk    s|j         d         dk    r|                    ddd||          }|	j         d         dk    s|	j         d         dk    r|	                    ddd||          }	|	|z
  }ndt          j        |j         d         d|j         d         |j         d         |j         d         ft          d	d
|z
            |j        |j                  }|j         d         |j         d         k    rJ|j         d         |j         d         z
  }t
          j	        j
                            |ddddddd|fd          }t          j        ||gd          }t          j        ||gd          }||||fS )Nr'   z6Adding guide to a combined AV latent is not supported.r   rw   r   rt   r   rx   rm   rl   ry   )padvaluer   )r   r/   r   r*   expandrZ   r   rz   rW   nn
functionalr   r   )r    rf   rg   r   r   r|   r   rk   r   
guide_maskin_channelsr   r   target_htarget_wmaskpad_lens                    r!   append_keyframezLTXVAddGuide.append_keyframev  sq   a K//>3G3Jk3Y3YUVVV))(I~}^u  CM)  N  N))(I~}^u  CM)  N  N!:+A.
0@0CDDH:+A.
0@0CDDH"a'':+;A+>!+C+C'..r2r8XNN
"a'':+;A+>!+C+C'..r2r8XNN
(DD:!!$a)=a)@*BRSTBUWaWghiWjkCx(( &!(	  D a >#7#:::"(+n.B1.EEG"X044^!QPQSTVWYZ\]_fIgop4qqNy,!?QGGGY
D1q999
<;;r#   c                 z   |j         d         }|j         d         ||z   k    s
J d            t          j        |j         d         d|ddft          dd|z
            |j        |j                  }|                                }|                                }||d d d d |||z   f<   ||d d d d |||z   f<   ||fS )Nrx   =Conditioning frames exceed the length of the latent sequence.r   r'   rm   rl   ry   )r   rZ   r   r*   rz   rW   r   )r    r   r|   r   r   rk   cond_lengthr   s           r!   replace_latent_framesz"LTXVAddGuide.replace_latent_frames  s    $*1-!!$
[(@@@@  CB@@@za ![!Q7S8^$$"$	
 
 
 $))++%%''
DRQQQ:
[(@@@ABF
111aaaj;&>>>?Z''r#   r$   c
                    |j         }
|d         }t          |          }|j        \  }}}}}|                     |	          }|dk    r+||z  dk    s	||z  dk    rt	          d| d| d| d          |
d         }|j        d         dz
  |z  |z  dz   }|}|dk     r7t          ||j                  \  }}t          ||z
  dz
  |z  dz   |z   d          }|dk    p|dk    }|s t          j        |d d         |gd          }| 	                    |||||
|          \  }}|s!|d d d d dd d d d d f         }|dd          }t          |j        d	d                    }d }|dk    r|                     ||          \  }}|                     ||t          |          ||
|j        
          \  }}||j        d	         z   |k    s
J d            |                     ||||||||
|||          \  }}}}|j        d	         |j        d         z  |j        d         z  }t          ||||||          \  }}t!          j        ||||d          S )NrX   r'   r   zLatent spatial size xz1 must be divisible by reference_downscale_factor z from the IC-LoRA parameters.r   rx   )r   r   )r   r   r   rw   r   )rk   r   r{   )r   r   r   r   r/   r   r*   rZ   r   r   listr   r   r   r   r   r   r0   )r    rf   rg   rh   r_   ri   r   rk   r   r   r   r   r|   r   r   r   r   r   r   num_frames_to_keepresolved_frame_idxr   r   r   guide_latent_shaper   r   r   s                               r!   r4   zLTXVAddGuide.execute  s5   3i(#F++
;G;M81m]L"%"D"DEV"W"W"Q&&55::mNe>eij>j>j i< i i- i i2Ii i i   *!,${1~16GGK\\_``&q==0<;MNNA}!$mm&Ca&GK\%\_`%`cl%lno!p!p'1,G0Ba0G
 	9IuRaRy%0a888E::c<}Vmnnq 	!!!QQQAAAqqq.!A!""IE!!'!""+..
"Q&&--a1HIIMAz # 4 4X}cRWjjZcer  BN  BT 4  !U  !U	:AGAJ&-7779x7777:7J7J!$;! 8K 8
 8
4(L* 71:
2QWQZ?:h 02Dx)
 
 
(
 }Xx\Yc1d1deeer#   r`   N)r'   N)NrU   r'   N)NN)r6   r7   r8   r   r   r9   r"   r   r   r   r   r   r   r   r   r0   r4   rb   r:   r#   r!   r   r      sY       $$Q$777J-
 -
 [-
^       [  % % [%   [ 
% 
% 
% [
% \ \ \ [\4 < < < [<B ( ( [(& >f >f  CE  CP >f >f >f [>f@ HHHr#   r   c                   N    e Zd Zed             Zedej        fd            ZeZdS )LTXVCropGuidesc           
         t          j        ddt           j                            d          t           j                            d          t           j                            d          gt           j                            d          t           j                            d          t           j                            d          g          S )Nr  r   rf   rg   r_   rn   rO   )r   r   ro   r   rS   r   r   s    r!   r"   zLTXVCropGuides.define_schema  s    y$6%%j11%%j11	)) &&J&??&&J&??	  h 77
 
 
 	
r#   r$   c                    |d                                          }t          |          }t          ||j                  \  }}|dk    rt	          j        ||||d          S |d d d d d | f         }|d d d d d | f         }t          j        |d d d          }t          j        |d d d          }t	          j        ||||d          S )NrX   r   r{   )r   r   )r   r   r   r   r   r0   r   r   )r    rf   rg   r_   r   r|   r   r   s           r!   r4   zLTXVCropGuides.execute  s   i(..00#F++
,X|7IJJ=A=8]g5h5hjjj#AAAqqq/M>/$9:111o~o 56
7!'+C
 C
    7!'+C
 C
  
 }Xx\Yc1d1deeer#   N)	r6   r7   r8   r9   r"   r   r0   r4   r   r:   r#   r!   r  r    s\        
 
 [
  fBM f f f [f, DDDr#   r  c                   J    e Zd Zed             Zedej        fd            ZdS )LTXVConditioningc                 l   t          j        ddt           j                            d          t           j                            d          t           j                            ddddd	
          gt           j                            d          t           j                            d          g          S )Nr  r   rf   rg   
frame_rateg      9@rm   g     @@r   rC   rn   rO   )r   r   ro   r   rr   r   r   s    r!   r"   zLTXVConditioning.define_schema  s    y&6%%j11%%j11|TsUYZZ &&J&??&&J&??
 
 
 	
r#   r$   c                     t          j        |d|i          }t          j        |d|i          }t          j        ||          S )Nr  )r   r   r   r0   )r    rf   rg   r  s       r!   r4   zLTXVConditioning.execute-  sD    7<Q[B\]]7<Q[B\]]}Xx000r#   Nr5   r:   r#   r!   r  r    sV        
 
 [
 1 1 1 1 [1 1 1r#   r  c                   L    e Zd Zed             Zeddej        fd            ZdS )ModelSamplingLTXVc                 t   t          j        ddt           j                            d          t           j                            ddddd	          t           j                            d
dddd	          t           j                            dd          gt           j                                        g          S )Nr  zadvanced/modelmodel	max_shiftffffff @rm         Y@r   rC   
base_shiftffffff?r_   Tr   rO   )r   r   r   r   rr   rS   r   r   s    r!   r"   zModelSamplingLTXV.define_schema5  s    y'%w''{DcuSWXX|TsTXYY	488	 !!
 
 
 	
r#   Nr$   c                    |                                 }|d}n't          j        |d         j        dd                    }d}d}||z
  ||z
  z  }	||	|z  z
  }
||	z  |
z   }t          j        j        }t          j        j        } G d d||          } ||j        j	                  }|
                    |           |                    d|           t          j        |          S )	NrM   rX   rx      c                       e Zd ZdS )8ModelSamplingLTXV.execute.<locals>.ModelSamplingAdvancedN)r6   r7   r8   r:   r#   r!   ModelSamplingAdvancedr  W  s        Dr#   r  )shiftmodel_sampling)r   mathprodr   r\   r  ModelSamplingFluxCONSTr  model_configset_parametersadd_object_patchr   r0   )r    r  r  r  r_   mtokensx1x2mmbr  sampling_basesampling_typer  r  s                   r!   r4   zModelSamplingLTXV.executeE  s   KKMM>FFYvi06qrr:;;F*$b1b 2!,>,2	 	 	 	 	M= 	 	 	 /.u{/GHH%%E%222	+^<<<}Qr#   r  r5   r:   r#   r!   r  r  4  s[        
 
 [
    2=       [     r#   r  c                   L    e Zd Zed             Zeddej        fd            ZdS )LTXVSchedulerc                 
   t          j        ddt           j                            dddd          t           j                            dd	d
dd          t           j                            ddd
dd          t           j                            dddd          t           j                            ddd
dddd          t           j                            dd          gt           j                                        g          S )Nr0  zmodel/sampling/schedulerssteps   r'   i'  rN   r  r  rm   r  r   rC   r  r  stretchTz4Stretch the sigmas to be in the range [terminal, 1].)idrD   r   advancedterminalg?gGz?z2The terminal value of the sigmas after stretching.)r5  rD   rE   r*   rF   r   r6  r_   r  rO   )	r   r   rP   r   rr   r   rS   Sigmasr   r   s    r!   r"   zLTXVScheduler.define_schemab  s    y#0WbaUCC{DcuSWXX|TsTXYY
    R!	 !   !P!    	488', 	  ""1
 
 
 	
r#   Nr$   c                    |d}n't          j        |d         j        dd                    }t          j        dd|dz             }d}	d}
||z
  |
|	z
  z  }|||	z  z
  }||z  |z   }d}t          j        |dk    t          j        |          t          j        |          d|z  dz
  |z  z   z  d          }|r.|dk    }||         }d|z
  }|d	         d|z
  z  }d||z  z
  }|||<   t          j        |          S )
NrM   rX   rx   rl   rm   r'   r  r   rt   )	r   r!  r   rZ   linspacewhereexpr   r0   )r    r2  r  r  r4  r7  r_   r(  sigmasr)  r*  r+  r,  sigma_shiftpowernon_zero_masknon_zero_sigmasone_minus_zscale_factor	stretcheds                       r!   r4   zLTXVScheduler.execute  s/   >FFYvi06qrr:;;FS%!)44*$b1b ma'aKH[!!TXk%:%:a&j1nQV=V%VW
 
  	."aKM$]3O/K&r?cHn=L{\9:I$-F=!}V$$$r#   r  r5   r:   r#   r!   r0  r0  a  s]        
 
 [
< % %VXVc % % % [% % %r#   r0  image_arrayc                 0   t          j        | dd          }	 |                    ddt          |          dd          }|j        d	         |_        |j        d         |_        t           j                            |d
          	                    d          }|
                    |                    |                     |
                    |                                           |                                 d S # |                                 w xY w)Nwmp4formatlibx264r'   veryfast)crfpreset)rateoptionsr   rgb24yuv420p)avopen
add_streamstrr   rG   r?   
VideoFramefrom_ndarrayreformatmuxr   close)output_filerE  rM  	containerstreamav_frames         r!   encode_single_framer`    s   S777I%%As3xx:'N'N & 
 
 $)!,"(+=--k'-JJSS T 
 
 	fmmH--...fmmoo&&&	s   CC? ?Dc                 4   t          j        |           }	 t          d |j        D                       }t          |                    |                    }|                                 n# |                                 w xY w|                    d          S )Nc              3   0   K   | ]}|j         d k    |V  dS )videoN)type)r   ss     r!   r   z&decode_single_frame.<locals>.<genexpr>  s.      HHAaf6G6Ga6G6G6G6GHHr#   rQ  rI  )rS  rT  nextstreamsdecoder[  
to_ndarray)
video_filer]  r^  frames       r!   decode_single_framerl    s    
##IHH!2HHHHHY%%f--..	7+++s   A A+ +B   ri   c                 2   |dk    r| S | d | j         d         dz  dz  d | j         d         dz  dz  f         dz                                                                                                  }t	                      5 }t          |||           |                                }d d d            n# 1 swxY w Y   t	          |          5 }t          |          }d d d            n# 1 swxY w Y   t          j	        || j
        | j                  dz  }|S )Nr   rx   r'   g     o@ry   )r   bytecpunumpyr   r`  getvaluerl  rZ   r   rz   rW   )ri   rM  rE  r\  video_bytesrj  r   s          r!   
preprocessrt    s   
axx35;q>Q.!335Ou{1~7Ja6O5OOPSXX^^``ddffllnnK	 -kKc:::!**,,- - - - - - - - - - - - - - - 
		 6)*556 6 6 6 6 6 6 6 6 6 6 6 6 6 6\+U[NNNQVVFMs$    &B22B69B6C((C,/C,c                   N    e Zd Zed             Zedej        fd            ZeZdS )LTXVPreprocessc                     t          j        dddt           j                            d          t           j                            ddddd	
          gt           j                            d          g          S )Nrv  zLTXV Preprocesszvideo/preprocessorsri   img_compression#   r   d   z(Amount of compression to apply on image.)r5  rD   rE   r*   r   output_imagern   )r   r   r   r   r   )r   r   rq   r   rP   r   r   s    r!   r"   zLTXVPreprocess.define_schema  sx    y$**w''("!Nx    ^<<
 
 
 	
r#   r$   c                     g }t          |j        d                   D ]+}|                    t          ||         |                     ,t	          j        t          j        |                    S r   )ranger   r   rt  r   r0   rZ   stack)r    ri   rx  output_imagesis        r!   r4   zLTXVPreprocess.execute  se    u{1~&& 	H 	HA  E!Ho!F!FGGGG}U[77888r#   N)	r6   r7   r8   r9   r"   r   r0   r4   rt  r:   r#   r!   rv  rv    sW        
 
 [
  9 9 9 9 [9 JJJr#   rv  c                   J    e Zd Zed             Zedej        fd            ZdS )LTXVConcatAVLatentc                     t          j        ddt           j                            d          t           j                            d          gt           j                            d          g          S )Nr  r>   video_latentaudio_latentr_   rn   rO   r   r   rS   r   r   r   s    r!   r"   z LTXVConcatAVLatent.define_schema  se    y(.	//	//
 	  h 77

 

 

 
	
r#   r$   c                    i }|                     |           |                     |           |                    dd           }|                    dd           }||\|t          j        |d                   }|t          j        |d                   }t          j                            ||f          |d<   t          j                            |d         |d         f          |d<   t          j        |          S )Nr|   rX   )	updater-   rZ   	ones_liker\   nested_tensorNestedTensorr   r0   )r    r  r  outputvideo_noise_maskaudio_noise_masks         r!   r4   zLTXVConcatAVLatent.execute  s    l###l###'++L$??'++L$??'+;+G'#(?<	3J#K#K '#(?<	3J#K#K #(#6#C#CEUWgDh#i#iF< !/<<l9>UWcdmWn=oppy}V$$$r#   Nr5   r:   r#   r!   r  r    sV        
 
 [
 %BM % % % [% % %r#   r  c                   J    e Zd Zed             Zedej        fd            ZdS )LTXVSeparateAVLatentc           
          t          j        dddt           j                            d          gt           j                            d          t           j                            d          g          S )	Nr  r>   zLTXV Separate AV Latent	av_latentr  rn   r  )r   r   r   r   r   r  r   s    r!   r"   z"LTXVSeparateAVLatent.define_schema  sm    y*.1	,, 	  n ==	  n ==
 
 
 	
r#   r$   c                 L   |d                                          }|                                }|d         |d<   |                                }|d         |d<   d|v r4|d         }|*|                                 }|d         |d<   |d         |d<   t          j        ||          S )NrX   r   r'   r|   )unbindcopyr   r0   )r    r  latentsr  r  maskss         r!   r4   zLTXVSeparateAVLatent.execute  s    I&--// ~~''")!*Y ~~''")!*Y9$$l+E -21X\*-21X\*}\<888r#   Nr5   r:   r#   r!   r  r    sV        
 
 [
 92= 9 9 9 [9 9 9r#   r  c                   Z    e Zd Zedej        fd            Zedej        fd            ZdS )LTXVReferenceAudior$   c                     t          j        ddddt           j                            d          t           j                            d          t           j                            d          t           j                            dd	
          t           j                            ddd          t           j                            ddddddd          t           j                            ddddddd          t           j                            ddddddd          gt           j                                        t           j                            d          t           j                            d          g          S ) Nr  zLTXV Reference Audio (ID-LoRA)zmodel/conditioning/audiozSet reference audio for ID-LoRA speaker identity transfer. Encodes a reference audio clip into the conditioning and optionally patches the model with identity guidance (extra forward pass without reference, amplifying the speaker identity effect).r  rf   rg   reference_audiozReference audio clip whose speaker identity to transfer. ~5 seconds recommended (training duration). Shorter or longer clips may degrade voice identity transfer.r   	audio_vaez	Audio VAEzLTXV Audio VAE for encoding.)r5  r   r   identity_guidance_scaleg      @rm   r  r   zStrength of identity guidance. Runs an extra forward pass without reference each step to amplify speaker identity. Set to 0 to disable (no extra pass).)rD   rE   r*   rF   r+   r   start_percentrl   gMbP?Tz;Start of the sigma range where identity guidance is active.)rD   rE   r*   rF   r6  r   end_percentz9End of the sigma range where identity guidance is active.rn   )r   r   r   r   r   r   )	r   r   r   r   ro   Audiorp   rr   r   r   s    r!   r"   z LTXVReferenceAudio.define_schema,  sf   y(9/ Rw''%%j11%%j110  ;^  _  _+Omnn8#3TY`dlp  {T  U  U#TYdh  sp  q  q}csRWbf  ql  m  m	 !!&&J&??&&J&??
 
 
 	
r#   c	                    |d         }	t          |dd          }
|
|	k    r(t          j                            |d         |	|
          }n|d         }|                    |                    dd                    }|j        \  }}}}|                    dddd	                              ||||z            }d
|i}t          j
        |d|i          }t          j
        |d|i          }|                                }||                    d          }|                    |          |                    |          fd}|                    |           t          j        |||          S )Nsample_rateaudio_sample_rateiD  waveformr'   rt   r   rx   rw   r(  	ref_audior  c                 T   dk    r| d         S | d         }|d                                          }|k    s|k     r| d         S | d         }| d         }| d         }| d                                         }| d         }g }|D ]n}	|	                                }
|
                    di                                           }|                    d	d            ||
d<   |                    |
           ot
          j                            | d
         |g|||          \  }|||z
  z  z   S )Nr   denoisedsigmacond_denoisedr   model_optionsinputmodel_condsr  r  )itemr  r-   popr   r\   samplerscalc_cond_batch)argsr  sigma_	cond_predr   
cfg_resultr  r   
noref_condentryr   mc
pred_norefr   	sigma_endsigma_starts                r!   post_cfg_functionz5LTXVReferenceAudio.execute.<locals>.post_cfg_function]  sE   zzJ''ME1X]]__F##v	'9'9J''_-I<Dj)J 16688MWA J - -!JJLL	]]="55::<<{D)))+-	-(!!),,,,!N::W
|Q} MZ Z!75 @@@r#   )getattr
torchaudior   resampler   r   r   permutereshaper   r   r   get_model_objectpercent_to_sigma#set_model_sampler_post_cfg_functionr   r0   )r    r  rf   rg   r  r  r  r  r  r  vae_sample_rater  audio_latentsr,  cr   f
ref_tokensr  r'  r  r  r   r  r  s                         @@@r!   r4   zLTXVReferenceAudio.executeD  s    &m4!)-@%HHk))!,55oj6QS^`oppHH&z2H!(()9)9!R)@)@AA"(
1a"**1aA66>>q!QUKK
z*	7;PYBZ[[7;PYBZ[[ KKMM'++,<==$55mDD"33K@@		A 	A 	A 	A 	A 	A 	A< 	
--.?@@@}Q(333r#   N)	r6   r7   r8   r9   r   r   r"   r0   r4   r:   r#   r!   r  r  +  sn        
bi 
 
 
 [
. 84  EG  ER 84 84 84 [84 84 84r#   r  c                   L    e Zd Zedeeej                          fd            ZdS )LtxvExtensionr$   c                    K   t           t          t          t          t          t
          t          t          t          t          t          t          t          gS r  )r<   rd   r   r  r  r0  r   r   rv  r  r  r  r  )selfs    r!   get_node_listzLtxvExtension.get_node_list  s9       !! 
 	
r#   N)	r6   r7   r8   r   r  rd  r   	ComfyNoder  r:   r#   r!   r  r    sE        
T$r|*<%= 
 
 
 X
 
 
r#   r  r$   c                  "   K   t                      S r  )r  r:   r#   r!   comfy_entrypointr    s      ??r#   )rl   Nr  )rm  )2rQ   r   rZ   r  comfy.model_managementr\   comfy.model_samplingcomfy.samplerscomfy.utilsr   rq  nprS  r   r   typing_extensionsr   )comfy.ldm.lightricks.symmetric_patchifierr   r   comfy_api.latestr   Customr   r  r   r<   rd   r   r   r   r   r   r   r  r  r  r0  ndarrayr`  rl  Tensorrt  rv  comfy.nested_tensorr  r  r  r  r  r:   r#   r!   <module>r     s0                                  				       & & & & & & a a a a a a a a / / / / / / / /29122 %) %) %) %) %)", %) %) %)P    2<   0* * * * *R\ * * *Z. . . . .BL . . .b" " " "<     ( ( ( ($v v v v v2< v v vr) ) ) ) )R\ ) ) )X1 1 1 1 1r| 1 1 10*  *  *  *  *  *  *  * Z?% ?% ?% ?% ?%BL ?% ?% ?%B"*    ", , , el        R\   8     %  %  %  %  %  %  %  %F9 9 9 9 92< 9 9 9@R4 R4 R4 R4 R4 R4 R4 R4j
 
 
 
 
N 
 
 
(      r#   