
    3j                     x   S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
rS SKrS SKJr  S SKJr  S SKJrJr  S SKJrJr  \R,                  " S5      r " S S\R0                  5      r " S	 S
\R0                  5      r " S S\R0                  5      r " S S\R0                  5      rS.S jrS/S jrS rS/S jr  " S S\R0                  5      r! " S S\R0                  5      r" " S S\R0                  5      r# " S S\R0                  5      r$ " S S\R0                  5      r%S\RL                  4S jr'S r(S0S \RR                  4S! jjr* " S" S#\R0                  5      r+S SK,r " S$ S%\R0                  5      r- " S& S'\R0                  5      r. " S( S)\R0                  5      r/ " S* S+\5      r0S,\04S- jr1g)1    N)BytesIO)override)SymmetricPatchifierlatent_to_pixel_coords)ComfyExtensionioIC_LORA_PARAMETERSc                   P    \ rS rSr\S 5       r\S\R                  4S j5       rSr	g)GetICLoRAParameters   c                     [         R                  " SSSS/ SQ[         R                  R                  SSS9/[        R                  S	S
S9/S9$ )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/ltxv)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_schema!GetICLoRAParameters.define_schema   sk    yy)1d.f"B   !'''S ( 
 	
    returnc                     UR                  S5      nSnU(       a/   [        S[        [        UR	                  SS5      5      5      5      nSU0n[        R                  " U5      $ ! [
        [        4 a    Sn N.f = f)Nlora_metadata   reference_downscale_factor)	get_attachmentmaxroundfloatget	TypeError
ValueErrorr   
NodeOutput)r!   r   metadatafactor
parameterss        r"   executeGetICLoRAParameters.execute/   sx    ..?QeHLL9UWX,Y&Z [\ 3F;
}}Z(( z* s   .A$ $A98A9 N
__name__
__module____qualname____firstlineno__classmethodr#   r   r2   r6   __static_attributes__r8   r%   r"   r   r      s3    
 
2 	)bmm 	) 	)r%   r   c                   X    \ rS rSr\S 5       r\SS\R                  4S jj5       r\r	Sr
g)EmptyLTXVLatentVideo<   c                    [         R                  " SS[         R                  R                  SSS[        R
                  SS9[         R                  R                  SS	S[        R
                  SS9[         R                  R                  S
SS[        R
                  SS9[         R                  R                  SSSSS9/[         R                  R                  5       /S9$ )NrA   model/latent/ltxvwidth   @       defaultminr,   stepheight   lengtha   r)      
batch_size   rJ   rK   r,   r   r   r   r   )r   r   Intr   nodesMAX_RESOLUTIONLatentr   r    s    r"   r#   "EmptyLTXVLatentVideo.define_schema=   s    yy*(Wcru?S?SZ\]Xs@T@T[]^Xrqe>R>RYZ[\1!F	 		  "
 	
r%   r&   c                     [         R                  " USUS-
  S-  S-   US-  US-  /[        R                  R	                  5       S9n[
        R                  " USS.5      $ )N   r)   rQ   rH   device)samplesdownscale_ratio_spacial)torchzeroscomfymodel_managementintermediate_devicer   r2   )r!   rE   rM   rO   rR   latents         r"   r6   EmptyLTXVLatentVideo.executeM   sr    j#!0AQ/FRTV[_aVabkp  lB  lB  lV  lV  lX  Y}}BOPPr%   r8   Nr)   r:   r;   r<   r=   r>   r#   r   r2   r6   generater?   r8   r%   r"   rA   rA   <   s=    
 
 QR]] Q Q Hr%   rA   c                   T    \ rS rSr\S 5       r\S\R                  4S j5       r\r	Sr
g)LTXVImgToVideoT   c                 X   [         R                  " SS[         R                  R                  S5      [         R                  R                  S5      [         R                  R                  S5      [         R
                  R                  S5      [         R                  R                  SSS	[        R                  S
S9[         R                  R                  SSS	[        R                  S
S9[         R                  R                  SSS[        R                  SS9[         R                  R                  SSSSS9[         R                  R                  SSSSS9/	[         R                  R                  SS9[         R                  R                  SS9[         R                  R                  SS9/S9$ )Nrl   r   positivenegativevaeimagerE   rF   rG   rH   rI   rM   rN   rO   rP   	   rQ   rR   r)   rS   rT   strength      ?        r   rf   rU   )r   r   Conditioningr   VaeImagerV   rW   rX   Floatr   rY   r    s    r"   r#   LTXVImgToVideo.define_schemaU   s;   yy$.%%j1%%j1U#w'Wcru?S?SZ\]Xs@T@T[]^Xrqe>R>RYZ[\1!Fz3CSI
 &&J&?&&J&?		  h 7
 	
r%   r&   c
                 v   [         R                  R                  UR                  SS5      XVSS5      R                  SS5      n
U
S S 2S S 2S S 2S S24   nUR	                  U5      n[
        R                  " USUS-
  S-  S-   US-  US-  /[         R                  R                  5       S	9nXS S 2S S 2S UR                  S
   24'   [
        R                  " USUR                  S
   SS4[
        R                  UR                  S9nSU	-
  US S 2S S 2S UR                  S
   24'   [        R                  " XXS.5      $ )Nr)   bilinearcenter   r\   rQ   rH   r]      dtyper^   ru   r_   
noise_mask)rc   utilscommon_upscalemovedimencodera   rb   rd   re   shapeonesfloat32r^   r   r2   )r!   ro   rp   rr   rq   rE   rM   rO   rR   rt   pixelsencode_pixelstrf   conditioning_latent_frames_masks                  r"   r6   LTXVImgToVideo.executel   s?   ++EMM"a,@%Q[]efnnoprtuq!Q{+JJ}%j#!0AQ/FRTV[_aVabkp  lB  lB  lV  lV  lX  Y$%q![aggaj[ !*/**FLLOQ2--==+
'
 >A8^'1kqwwqzk(9:}}XV1sttr%   r8   Nri   r8   r%   r"   rl   rl   T   s@    
 
, uegerer u u" Hr%   rl   c                   X    \ rS rSr\S 5       r\SS\R                  4S jj5       r\r	Sr
g)LTXVImgToVideoInplace   c                    [         R                  " SS[         R                  R                  S5      [         R                  R                  S5      [         R
                  R                  S5      [         R                  R                  SSSSS	9[         R                  R                  S
SSS9/[         R
                  R                  SS9/S9$ )Nr   r   rq   rr   rf   rt   ru   rv   rT   bypassFzBypass the conditioning.)rJ   r   rw   rU   )	r   r   ry   r   rz   rY   r{   Booleanr   r    s    r"   r#   #LTXVImgToVideoInplace.define_schema   s    yy+.U#w'		)z3CSI

  5B\ ] 		  h 7
 	
r%   r&   c                 `   U(       a  U4$ US   R                  5       nUR                  u  pxn	UR                  u      pznX-  nX-  nUR                  S   U:w  d  UR                  S   U:w  aC  [        R                  R                  UR                  SS5      XSS5      R                  SS5      nOUnUS S 2S S 2S S 2S S24   nUR                  U5      nUUS S 2S S 2S UR                  S   24'   [        U5      nSU-
  US S 2S S 2S UR                  S   24'   [        R                  " UUS	.5      $ )
Nr_   r)   r   r~   r   r   r   ru   r   )clonedownscale_index_formular   rc   r   r   r   r   get_noise_maskr   r2   )r!   rq   rr   rf   rt   r   r_   _height_scale_factorwidth_scale_factorlatent_heightlatent_widthrE   rM   r   r   r   r   s                     r"   r6   LTXVImgToVideoInplace.execute   s4   9#))+'' 	3 2 07}},1a14;;q>V#u{{1~'>[[//b!0DeU_aijrrstvxyFFq!Q{+JJ}%%&1kqwwqzk!"*8*@'=@8^'1kqwwqzk(9:}}@_`aar%   r8   N)Fri   r8   r%   r"   r   r      s=    
 
  bBMM b b6 Hr%   r   c                 ,   UUUb   UR                  S5      R                  S5      OSUS.n/ nX4 HX  n/ n	U H  n
U
S   R                  SS5      nUc  M  Un	  O   / U	QUPnUR                  [        R                  " USU05      5        MZ     US   US   4$ )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_countrt   
pixel_masklatent_shaper)   guide_attention_entries)	unsqueezer/   appendnode_helpersconditioning_set_values)ro   rp   r   r   rt   attention_mask	new_entryresultscondexistingr   foundentriess                r"   _append_guide_attention_entryr      s     -BPB\n..q1;;A>bf$	I G$AaDHH6=E  	  )H(i(|;;,g6
 	 % 1:wqz!!r%   c                 <    U  H  nXS   ;   d  M  US   U   s  $    U$ )Nr)   r8   )conditioningkeyrJ   r   s       r"   conditioning_get_any_valuer      s*    A$;Q49  Nr%   c                     U R                  SS 5      nU S   nUcF  UR                  u  p4n  n[        R                  " USUSS4[        R                  UR
                  S9nU$ UR                  5       nU$ )Nr   r_   r)   r   )r/   r   ra   r   r   r^   r   )rf   r   latent_imagerR   r   latent_lengths         r"   r   r      s    L$/J)$L-9-?-?*
}aZZM1a0--&&

   %%'
r%   c                 <   [        U SS 5      nUc  gUb/  [        U5      S:X  a   US   US   -  nUR                  S   U-  nX$4$ [        U SS 5      nU(       a  [        S U 5       5      nX$4$ [        R
                  " US S 2S	S S 2S	4   5      R                  S	   nX$4$ )
Nkeyframe_idxsNr      r~   r   r   c              3   0   #    U  H  oS    S   v   M     g7f)r   r   Nr8   ).0es     r"   	<genexpr>$get_keyframe_idxs.<locals>.<genexpr>   s     B'Qn-a0's   r   )r   lenr   sumra   unique)r   r   r   tokens_per_framenum_keyframesr   s         r"   get_keyframe_idxsr      s    .t_dKMC$5$:'+l2.>>%++A.2BB++(/H$OGB'BB++ LLq!Qz!:;AA!DM''r%   c                       \ rS rSr\" SSS9r\S 5       r\SS j5       r\S 5       r	\S 5       r
\SS
 j5       r\SS j5       r\SS j5       r\S 5       r\SS\R                   4S jj5       r\rSrg	)LTXVAddGuide   r)   T)	start_endc                    [         R                  " SS[         R                  R                  S5      [         R                  R                  S5      [         R                  R                  S5      [         R
                  R                  S5      [         R                  R                  SSS	9[         R                  R                  S
SSSSS9[         R                  R                  SSSSSS9[         R                  R                  SSSS9[        R                  SSSS9/	[         R                  R                  SS9[         R                  R                  SS9[         R
                  R                  SS9/S9$ )Nr   r   ro   rp   rq   rf   rr   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.)rJ   rK   r,   r   rt   ru   rv   g      $@{Gz?rI   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.rw   rU   )r   r   rx   r   ry   rY   rz   rV   r{   Maskr   r   r    s    r"   r#   LTXVAddGuide.define_schema   sP   yy".%%j1%%j1U#		)t  
 x  	 z3CTPTU$!a   !&&'!h ' 7#J &&J&?&&J&?		  h 7O,
 ,	
r%   c                 R   Uu  pxn	US UR                   S   S-
  U-  U-  S-    n[        X(-  U-  5      n
[        X9-  U-  5      n[        R                  R	                  UR                  SS5      XSSS9R                  SS5      nUS S 2S S 2S S 2S S24   nUR                  U5      nX4$ )Nr   r)   r~   r   r   )cropr   )r   intrc   r   r   r   r   )r!   rq   r   r   imagesscale_factorslatent_downscale_factortime_scale_factorr   r   target_widthtarget_heightr   r   r   s                  r"   r   LTXVAddGuide.encode*  s    ERB/B[&,,q/A-2CCFWWZ[[\<<?VVWM?BYYZ++FNN2q,A<`jqy+z  C  C  DE  GI  Jq!Q{+JJ}%r%   c                    US::  a  US 4$ [        U5      nUR                  S S UR                  S   U-  UR                  S   U-  4-   n[        R                  " XAR                  UR
                  S9nXSS S U2S S U24'   [        R                  " UR                  S   SUR                  S   UR                  S   UR                  S   4SUR                  UR
                  S9nS	USS S U2S S U24'   XV4$ )
Nr)   r      )r^   r   .r   r   g      ru   )r   r   ra   rb   r^   r   full)r!   guide_latentr   scaledilated_shapedilateddilated_masks          r"   dilate_latentLTXVAddGuide.dilate_latent5  s
   "a'%%+,$**2A.,2D2DQ2G%2OQ]QcQcdeQfinQn1oo++m4G4G|OaOab)5WuWgg%&zz]]1q'--"2GMM!4DgmmTUFVW,,L4F4F
 /2S'E'7U7*+$$r%   c                     U(       d  g [        S[        [        UR                  SS5      5      5      5      nU$ ! [        [
        4 a    Sn U$ f = f)Nr)   r*   )r,   r-   r.   r/   r0   r1   )r!   r   r4   s      r"   get_reference_downscale_factor+LTXVAddGuide.get_reference_downscale_factorD  sZ     	E%(9(=(=>Z\](^"_`aF  :& 	F	s   .: AANc                     Uu  n  n[        X5      u  pX)-
  n
US:  a  UO[        U
S-
  U-  S-   U-   S5      nUS:  a  US:w  a  US-
  U-  U-  S-   nXG-   S-
  U-  nXK4$ )Nr   r)   )r   r,   )r!   r   r   guide_lengthr   r   r   r   r   r   latent_count
latent_idxs               r"   get_latent_indexLTXVAddGuide.get_latent_indexN  s    "/1a,T@$4!*aIS,:JN_9_bc9cfo9oqr5s	!	Q"Q+<<?PPSTTI3a7<MM
$$r%   c                    [        U5      u  pxU R                  R                  U5      u  pUc  US:H  =(       d    UR                  S   S:H  n[	        XUS9n
U
S S 2S4==   U-  ss'   US-
  [
        R                  " USS  U
R                  S9R                  SSSS5      -  nU
S S 2SS 2S S 2SS 24==   UR                  U
R                  5      -  ss'   Uc  U
nO[
        R                  " Xz/SS9n[        R                  " USU05      $ )	Nr   r   r)   
causal_fixr]   r~   dimr   )r   
PATCHIFIERpatchifyr   r   ra   tensorr^   viewtor   catr   r   )r!   r   r   guiding_latentr   r   r   r   r   latent_coordspixel_coordsspatial_end_offsets               r"   add_keyframe_indexLTXVAddGuide.add_keyframe_index[  s   ,T2>>22>B"aG>+?+?+Ba+GJ-mWabQTi' 69U\\!"&&>
 $q"a
 	QAqr\"&8&;&;L<N<N&OO" (M!II}&CKM33D?M:Z[[r%   c                 Z   UR                   S   U
:w  d  UR                   S   U
:w  a  [        S5      eU R                  XXhXS9nU R                  X#XhXS9nU	b  [        UR                   S   U	R                   S   5      n[        UR                   S   U	R                   S   5      nUR                   S   S:X  d  UR                   S   S:X  a  UR	                  SSSX5      nU	R                   S   S:X  d  U	R                   S   S:X  a  U	R	                  SSSX5      n	X-
  nOq[
        R                  " UR                   S   SUR                   S   UR                   S   UR                   S   4[        S	S
U-
  5      UR                  UR                  S9nUR                   S   UR                   S   :  aP  UR                   S   UR                   S   -
  n[
        R                  R                  R                  USSSSSSSU4SS9n[
        R                  " XF/SS9n[
        R                  " X_/SS9nXXE4$ )Nr)   z6Adding guide to a combined AV latent is not supported.r   r   r   r~   r   r   rv   ru   r   )padvaluer   )r   r1   r  r,   expandra   r   r   r^   nn
functionalr  r   )r!   ro   rp   r   r   r   r   rt   r   
guide_maskin_channelsr   r   target_htarget_wmaskpad_lens                    r"   append_keyframeLTXVAddGuide.append_keyframev  sF   a K/>3G3G3Jk3YUVV))(~^u)  N))(~^u)  N!:++A.
0@0@0CDH:++A.
0@0@0CDH"a':+;+;A+>!+C'..r2r8N
"a':+;+;A+>!+C'..r2r8N
(D::!!!$a)=)=a)@*BRBRSTBUWaWgWghiWjkCx( &&!((	D a >#7#7#::"((+n.B.B1.EEG"XX0044^!QPQSTVWYZ\]_fIgop4qNyy,!?QGYY
1q9
<;;r%   c                 n   UR                   S   nUR                   S   XF-   :  d   S5       e[        R                  " UR                   S   SUSS4[        SSU-
  5      UR                  UR
                  S9nUR                  5       nUR                  5       nX1S S 2S S 2XDU-   24'   XrS S 2S S 2XDU-   24'   X4$ )Nr   =Conditioning frames exceed the length of the latent sequence.r   r)   rv   ru   r   )r   ra   r   r,   r   r^   r   )r!   r   r   r   r   rt   cond_lengthr  s           r"   replace_latent_frames"LTXVAddGuide.replace_latent_frames  s    $**1-!!!$
(@@  	B  CB  	B@zza ![!Q7S8^$""$$	
 $))+%%'
DRQ:[(@@@ABF1a;&>>>?''r%   r&   c
                 <   UR                   n
US   n[        U5      nUR                  u    pnnU R                  U	5      nUS:  a'  UU-  S:w  d	  UU-  S:w  a  [	        SU SU SU S35      eU
S   nUR                  S   S-
  U-  U-  S-   nUnUS:  a3  [        XR                  5      u  nn[        UU-
  S-
  U-  S-   U-   S5      nUS:H  =(       d    US:H  nU(       d  [        R                  " US S U/SS9nU R                  UUXU
U5      u  nnU(       d  US S 2S S 2SS 2S S 2S S 24   nUSS  n[        UR                  S	S  5      nS nUS:  a  U R                  UU5      u  nnU R                  X[        U5      XjUR                  S
9u  nnUUR                  S	   -   U::  d   S5       eU R                  UUUUUUUU
UUUS9u  ppUR                  S	   UR                  S   -  UR                  S   -  n[        XUUUUS9u  p[         R"                  " XXS.5      $ )Nr_   r)   r   zLatent spatial size xz1 must be divisible by reference_downscale_factor z from the IC-LoRA parameters.r   r   )r   r  )r
  r   r   r   r   )rt   r   r   )r   r   r   r   r1   r   r,   ra   r   r   listr   r   r   r  r   r   r2   )r!   ro   rp   rq   rf   rr   r   rt   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"   r6   LTXVAddGuide.execute  s   33i(#F+
;G;M;M81]L"%"D"DEV"W"Q&55:mNe>eij>j *<.- I22I1JJgi  *!,${{1~16GGK\\_``&q=0;M;MNA}!$mm&Ca&GK\%\_`%`cl%lno!p'1,G0Ba0G
IIuRay%0a8E::c<}Vmnq!QAq.!A!"IE!!''!"+.
"Q&--a1HIMAz # 4 4XcRWjZc  BN  BT  BT 4  !U	:AGGAJ&-7x9xx77:7J7J!$;! 8K 8
4L 771:
2QWWQZ?: 02Dx)

 }}X\1deer%   r8   rh   N)r)   N)Nr\   r)   N)NN)r:   r;   r<   r=   r   r   r>   r#   r   r   r   r   r  r  r  r   r2   r6   rj   r?   r8   r%   r"   r   r      s    $Q$7J-
 -
^     % %   
% 
% \ \4 < <B ( (& >f  CE  CP  CP >f >f@ Hr%   r   c                   T    \ rS rSr\S 5       r\S\R                  4S j5       r\r	Sr
g)LTXVCropGuidesi  c                    [         R                  " SS[         R                  R                  S5      [         R                  R                  S5      [         R                  R                  S5      /[         R                  R                  SS9[         R                  R                  SS9[         R                  R                  SS9/S9$ )Nr   r   ro   rp   rf   rw   rU   )r   r   rx   r   rY   r   r    s    r"   r#   LTXVCropGuides.define_schema  s    yy$.%%j1%%j1		) &&J&?&&J&?		  h 7
 	
r%   r&   c                    US   R                  5       n[        U5      n[        XR                  5      u  pgUS:X  a  [        R
                  " XXES.5      $ US S 2S S 2S U* 24   nUS S 2S S 2S U* 24   n[        R                  " US S S.5      n[        R                  " US S S.5      n[        R
                  " XXES.5      $ )Nr_   r   r   )r   r   )r   r   r   r   r   r2   r   r   )r!   ro   rp   rf   r   r   r   r   s           r"   r6   LTXVCropGuides.execute  s    i(..0#F+
,X7I7IJA==5hjj#Aq/M>/$9:1o~o 56
77!'+C
   77!'+C
 
 }}X\1deer%   r8   N)r:   r;   r<   r=   r>   r#   r   r2   r6   r   r?   r8   r%   r"   r   r     s=    
 
  fBMM f f, Dr%   r   c                   P    \ rS rSr\S 5       r\S\R                  4S j5       rSr	g)LTXVConditioningi  c                 X   [         R                  " SS[         R                  R                  S5      [         R                  R                  S5      [         R                  R                  SSSSS	S
9/[         R                  R                  SS9[         R                  R                  SS9/S9$ )Nr&  r   ro   rp   
frame_rateg      9@rv   g     @@r   rI   rw   rU   )r   r   rx   r   r{   r   r    s    r"   r#   LTXVConditioning.define_schema  s    yy&.%%j1%%j1|TsUYZ &&J&?&&J&?
 	
r%   r&   c                     [         R                  " USU05      n[         R                  " USU05      n[        R                  " X5      $ )Nr(  )r   r   r   r2   )r!   ro   rp   r(  s       r"   r6   LTXVConditioning.execute-  sA    77<Q[B\]77<Q[B\]}}X00r%   r8   Nr9   r8   r%   r"   r&  r&    s3    
 
 1 1 1r%   r&  c                   T    \ rS rSr\S 5       r\SS\R                  4S jj5       rSr	g)ModelSamplingLTXVi4  c                 `   [         R                  " SS[         R                  R                  S5      [         R                  R                  SSSSSS	9[         R                  R                  S
SSSSS	9[         R
                  R                  SSS9/[         R                  R                  5       /S9$ )Nr-  zmodel/patch/ltxvmodel	max_shiftffffff @rv         Y@r   rI   
base_shiftffffff?rf   Tr   rU   )r   r   r   r   r{   rY   r   r    s    r"   r#   ModelSamplingLTXV.define_schema5  s    yy''w'{DcuSWX|TsTXY		48	 !
 	
r%   Nr&   c                    UR                  5       nUc  SnO&[        R                  " US   R                  SS  5      nSnSnX#-
  X-
  -  n	X9U-  -
  n
Xi-  U
-   n[        R
                  R                  n[        R
                  R                  n " S SX5      nU" UR                  R                  5      nUR                  US9  UR                  SU5        [        R                  " U5      $ )	NrS   r_   r      c                       \ rS rSrSrg)8ModelSamplingLTXV.execute.<locals>.ModelSamplingAdvancediW  r8   N)r:   r;   r<   r=   r?   r8   r%   r"   ModelSamplingAdvancedr:  W  s    r%   r;  )shiftmodel_sampling)r   mathprodr   rc   r=  ModelSamplingFluxCONSTr/  model_configset_parametersadd_object_patchr   r2   )r!   r/  r0  r3  rf   mtokensx1x2mmbr<  sampling_basesampling_typer;  r=  s                   r"   r6   ModelSamplingLTXV.executeE  s    KKM>FYYvi066qr:;F$1b !,,>>,,22	M 	 /u{{/G/GH%%E%2	+^<}}Qr%   r8   r  r9   r8   r%   r"   r-  r-  4  s3    
 
  2==    r%   r-  c                   T    \ rS rSr\S 5       r\SS\R                  4S jj5       rSr	g)LTXVScheduleria  c                    [         R                  " SS[         R                  R                  SSSSS9[         R                  R                  SS	S
SSS9[         R                  R                  SSS
SSS9[         R
                  R                  SSSSS9[         R                  R                  SSS
SSSSS9[         R                  R                  SSS9/[         R                  R                  5       /S9$ )NrO  zmodel/sampling/schedulersstepsr   r)   i'  rT   r0  r1  rv   r2  r   rI   r3  r4  stretchTz4Stretch the sigmas to be in the range [terminal, 1].)idrJ   r   advancedterminalg?gGz?z2The terminal value of the sigmas after stretching.)rS  rJ   rK   r,   rL   r   rT  rf   r5  rU   )	r   r   rV   r   r{   r   rY   Sigmasr   r    s    r"   r#   LTXVScheduler.define_schemab  s    yy#0WbaUC{DcuSWX|TsTXY

    R!	 !  !P!   		48', 		  "1
 	
r%   Nr&   c                    Uc  SnO&[         R                  " US   R                  SS  5      n[        R                  " SSUS-   5      nSn	Sn
X#-
  X-
  -  nX;U	-  -
  nX{-  U-   nSn[        R
                  " US:g  [         R                  " U5      [         R                  " U5      SU-  S-
  U-  -   -  S5      nU(       a%  US:g  nX   nSU-
  nUS	   SU-
  -  nSUU-  -
  nUX'   [        R                  " U5      $ )
NrS   r_   r   ru   rv   r)   r8  r   r~   )	r>  r?  r   ra   linspacewhereexpr   r2   )r!   rQ  r0  r3  rR  rU  rf   rF  sigmasrG  rH  rI  rJ  sigma_shiftpowernon_zero_masknon_zero_sigmasone_minus_zscale_factor	stretcheds                       r"   r6   LTXVScheduler.execute  s   >FYYvi066qr:;FS%!)4$1b ma'aKHH[!TXXk%:a&j1nQV=V%VW
 "aKM$3O/K&r?cHn=L{\9:I$-F!}}V$$r%   r8   r  r9   r8   r%   r"   rO  rO  a  s6    
 
< %VXVcVc % %r%   rO  image_arrayc                    [         R                  " U SSS9n UR                  SS[        U5      SS.S9nUR                  S	   Ul        UR                  S   Ul        [         R                  R                  US
S9R                  SS9nUR                  UR                  U5      5        UR                  UR                  5       5        UR                  5         g ! UR                  5         f = f)Nwmp4formatlibx264r)   veryfast)crfpreset)rateoptionsr   rgb24yuv420p)avopen
add_streamstrr   rM   rE   
VideoFramefrom_ndarrayreformatmuxr   close)output_filere  rm  	containerstreamav_frames         r"   encode_single_framer    s    S7I%%As3x:'N & 
 $))!,"((+==--k'-JSS T 
 	fmmH-.fmmo&	s   B/C C*c                    [         R                  " U 5      n [        S UR                   5       5      n[        UR	                  U5      5      nUR                  5         UR                  SS9$ ! UR                  5         f = f)Nc              3   H   #    U  H  oR                   S :X  d  M  Uv   M     g7f)videoN)type)r   ss     r"   r   &decode_single_frame.<locals>.<genexpr>  s     H!2Aff6Gaa!2s   "	"rq  ri  )rs  rt  nextstreamsdecoder{  
to_ndarray)
video_filer}  r~  frames       r"   decode_single_framer    sl    
#IH!2!2HHY%%f-.7++ 	s   6A- -A?rr   c                 
   US:X  a  U $ U S U R                   S   S-  S-  2S U R                   S   S-  S-  24   S-  R                  5       R                  5       R                  5       n[	        5        n[        X2U5        UR                  5       nS S S 5        [	        W5       n[        U5      nS S S 5        [        R                  " X R                  U R                  S9S-  nU$ ! , (       d  f       N[= f! , (       d  f       NM= f)Nr   r   r)   g     o@r   )r   bytecpunumpyr   r  getvaluer  ra   r   r   r^   )rr   rm  re  r|  video_bytesr  r   s          r"   
preprocessr    s    
ax35;;q>Q.!335Ou{{1~7Ja6O5OOPSXX^^`ddfllnK	kKc:!**, 
 
	)*5 
\\+[[NQVVFM 
 
	s   1C#!C4#
C14
Dc                   T    \ rS rSr\S 5       r\S\R                  4S j5       r\r	Sr
g)LTXVPreprocessi  c                     [         R                  " SSS[         R                  R                  S5      [         R                  R                  SSSSS	S
9/[         R                  R                  SS9/S9$ )Nr  zLTXV Preprocesszvideo/preprocessorsrr   img_compression#   r   d   z(Amount of compression to apply on image.)rS  rJ   rK   r,   r   output_imagerw   )r   r   r   r   r   )r   r   rz   r   rV   r   r    s    r"   r#   LTXVPreprocess.define_schema  sk    yy$**w'("!Nx   ^<
 	
r%   r&   c                     / n[        UR                  S   5       H   nUR                  [        X   U5      5        M"     [        R
                  " [        R                  " U5      5      $ r   )ranger   r   r  r   r2   ra   stack)r!   rr   r  output_imagesis        r"   r6   LTXVPreprocess.execute  sM    u{{1~&A  EHo!FG '}}U[[788r%   r8   N)r:   r;   r<   r=   r>   r#   r   r2   r6   r  r?   r8   r%   r"   r  r    s:    
 
  9 9 9 Jr%   r  c                   P    \ rS rSr\S 5       r\S\R                  4S j5       rSr	g)LTXVConcatAVLatenti  c           	          [         R                  " SS[         R                  R                  S5      [         R                  R                  S5      /[         R                  R	                  SS9/S9$ )Nr  rD   video_latentaudio_latentrf   rw   rU   r   r   rY   r   r   r    s    r"   r#    LTXVConcatAVLatent.define_schema  sZ    yy((		/		/
 		  h 7

 
	
r%   r&   c                    0 nUR                  U5        UR                  U5        UR                  SS 5      nUR                  SS 5      nUc  Ub[  Uc  [        R                  " US   5      nUc  [        R                  " US   5      n[        R
                  R                  XE45      US'   [        R
                  R                  US   US   45      US'   [        R                  " U5      $ )Nr   r_   )	updater/   ra   	ones_likerc   nested_tensorNestedTensorr   r2   )r!   r  r  outputvideo_noise_maskaudio_noise_masks         r"   r6   LTXVConcatAVLatent.execute  s    l#l#'++L$?'++L$?'+;+G'#(??<	3J#K '#(??<	3J#K #(#6#6#C#CEUDh#iF< !//<<l9>UWcdmWn=opy}}V$$r%   r8   Nr9   r8   r%   r"   r  r    s3    
 
 %BMM % %r%   r  c                   P    \ rS rSr\S 5       r\S\R                  4S j5       rSr	g)LTXVSeparateAVLatenti  c                     [         R                  " SSS[         R                  R                  S5      /[         R                  R	                  SS9[         R                  R	                  SS9/S9$ )	Nr  rD   zLTXV Separate AV Latent	av_latentr  rw   r  )r   r   r   r   r   r  r    s    r"   r#   "LTXVSeparateAVLatent.define_schema  sb    yy*(1		, 		  n =		  n =
 	
r%   r&   c                    US   R                  5       nUR                  5       nUS   US'   UR                  5       nUS   US'   SU;   a(  US   nUb   UR                  5       nUS   US'   US   US'   [        R                  " X45      $ )Nr_   r   r)   r   )unbindcopyr   r2   )r!   r  latentsr  r  maskss         r"   r6   LTXVSeparateAVLatent.execute  s    I&--/ ~~'")!*Y ~~'")!*Y9$l+E -21X\*-21X\*}}\88r%   r8   Nr9   r8   r%   r"   r  r    s3    
 
 92== 9 9r%   r  c                   l    \ rS rSr\S\R                  4S j5       r\S\R                  4S j5       r	Sr
g)LTXVReferenceAudioi+  r&   c                    [         R                  " SSSS[         R                  R                  S5      [         R                  R                  S5      [         R                  R                  S5      [         R
                  R                  SS	S
9[         R                  R                  SSSS9[         R                  R                  SSSSSSSS9[         R                  R                  SSSSSSSS9[         R                  R                  SSSSSSSS9/[         R                  R                  5       [         R                  R                  SS9[         R                  R                  SS9/S9$ ) Nr  zLTXV Reference Audio (ID-LoRA)r   zSet 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/  ro   rp   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.)rS  r   r   identity_guidance_scaleg      @rv   r2  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).)rJ   rK   r,   rL   r-   r   start_percentru   gMbP?Tz;Start of the sigma range where identity guidance is active.)rJ   rK   r,   rL   rT  r   end_percentz9End of the sigma range where identity guidance is active.rw   )r   r   r   r   r   r   )	r   r   r   r   rx   Audiory   r{   r   r    s    r"   r#    LTXVReferenceAudio.define_schema,  sG   yy(9. Rw'%%j1%%j10  ;^  _+Omn8#3TY`dlp  {T  U#TYdh  sp  q}csRWbf  ql  m	 !&&J&?&&J&?
 	
r%   c	                   ^^^ US   n	[        USS5      n
X:w  a$  [        R                  R                  US   X5      nOUS   nUR	                  UR                  SS5      5      nUR                  u  pnnUR                  SSSS	5      R                  XUU-  5      nS
U0n[        R                  " USU05      n[        R                  " USU05      nUR                  5       nUmUR                  S5      nUR                  U5      mUR                  U5      mUUU4S jnUR                  U5        [        R                   " UX#5      $ )Nsample_rateaudio_sample_rateiD  waveformr)   r~   r   r   r   rF  	ref_audior=  c                   > TS:X  a  U S   $ U S   nUS   R                  5       nUT:  d  UT:  a  U S   $ U S   nU S   nU S   nU S   R                  5       nU S   n/ nU HZ  n	U	R                  5       n
U
R                  S0 5      R                  5       nUR                  S	S 5        XS'   UR	                  U
5        M\     [
        R                  R                  U S
   U/XqU5      u  nXSU-
  T-  -   $ )Nr   denoisedsigmacond_denoisedr   model_optionsinputmodel_condsr  r/  )itemr  r/   popr   rc   samplerscalc_cond_batch)argsr  sigma_	cond_predr   
cfg_resultr  r  
noref_condentryr   mc
pred_norefr   	sigma_endsigma_starts                r"   post_cfg_function5LTXVReferenceAudio.execute.<locals>.post_cfg_function]  s   zJ''ME1X]]_F#v	'9J''_-I<Dj)J 1668MWA J!JJL	]]="5::<{D)+--(!!),  "NN::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   r2   )r!   r/  ro   rp   r  r  r  r  r  r  vae_sample_rater  audio_latentsrJ  cr   f
ref_tokensr  rE  r=  r  r   r  r  s                         @@@r"   r6   LTXVReferenceAudio.executeD  sF    &m4!)-@%H)!,,55oj6QS^pH&z2H!(()9)9!R)@A"((
a"**1aA6>>qQUK
z*	77;PYBZ[77;PYBZ[ KKM'++,<=$55mD"33K@		A< 	
--.?@}}Q33r%   r8   N)r:   r;   r<   r=   r>   r   r   r#   r2   r6   r?   r8   r%   r"   r  r  +  sM    
bii 
 
. 84  EG  ER  ER 84 84r%   r  c                   L    \ rS rSr\S\\\R                        4S j5       r	Sr
g)LtxvExtensioni  r&   c                    #    [         [        [        [        [        [
        [        [        [        [        [        [        [        /$ 7fr  )rA   rl   r   r-  r&  rO  r   r   r  r   r  r  r  )selfs    r"   get_node_listLtxvExtension.get_node_list  s8      !! 
 	
s   AAr8   N)r:   r;   r<   r=   r   r  r  r   	ComfyNoder  r?   r8   r%   r"   r  r    s)    
T$r||*<%= 
 
r%   r  r&   c                     #    [        5       $ 7fr  )r  r8   r%   r"   comfy_entrypointr    s     ?s   )ru   Nr  )   )2rW   r   ra   r  comfy.model_managementrc   comfy.model_samplingcomfy.samplerscomfy.utilsr>  r  nprs  r   r   typing_extensionsr   )comfy.ldm.lightricks.symmetric_patchifierr   r   comfy_api.latestr   Customr   r  r   rA   rl   r   r   r   r   r   r   r   r&  r-  rO  ndarrayr  r  Tensorr  r  comfy.nested_tensorr  r  r  r  r  r8   r%   r"   <module>r     sv             	  & a /9912 %)",, %)P2<< 0*R\\ *Z.BLL .b"<($v2<< vr)R\\ )X1r|| 10*  * Z?%BLL ?%B"** ",ell R\\ 8  %  %F92<< 9@R4 R4j
N 
( r%   