
    
3jV                     Z   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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9 " S S\R"                  5      5       r\" S	S9 " S
 S	\5      5       r\" SS9 " S S\R"                  5      5       rS r\" SS9 " S S\R"                  5      5       rg)    N)io   LTXVDilateLatent)NODES_DISPLAY_NAME_PREFIX
comfy_nodeLTXAddVideoICLoRAGuide)namec                       \ rS rSr\R
                  " SSS9r\S 5       r\S 5       r	\S\
R                  4S j5       rS	rg
)r	      r   T)	start_endc                    [         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5      [         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SS9[         R                  R	                  SSS/SSS9[         R                  R	                  S S!S"S#9[         R                  R	                  S$S%S&S'S(S)S9[         R                  R	                  S*S&S+S%S+S,S9/[         R                  R                  S5      [         R                  R                  S5      [         R                  R                  S5      /S-9$ ).Nr	   z Add Video IC-LoRA GuideLightricks/IC-LoRAa  Adds one or more conditioning frames starting at the specified frame index. Supports both single images and multi-frame videos. The latent_downscale_factor resizes input to a fraction of the target size (1 = original, 2 = half, 3 = third, etc.) for IC-LoRA on small grids.positivenegativevaelatentzcVideo-only latent to condition. Must be a 5D video latent (batch, channels, frames, height, width).tooltipimage	frame_idxr   '  a  Frame index to start the conditioning at. For single-frame videos, any frame_idx value is acceptable. For videos, frame_idx must be 1 modulo 8, otherwise it will be rounded down to the nearest 1 modulo 8. Negative values are counted from the end of the video.defaultminmaxr   strength      ?        {Gz?r   r   r   steplatent_downscale_factor      $@zXFor IC-LoRA on small grid. 1 means original size, 2 means half size, 3 means third, etc.r   r   r   r#   r   cropdisabledcenterzLCrop mode when resizing. 'center' crops to fit, 'disabled' stretches to fit.)optionsr   r   use_tiled_encodeFzSEnable tiled VAE encoding for large resolutions/long videos to reduce memory usage.)r   r   	tile_size   @          zQSpatial tile size for tiled encoding. Only used when use_tiled_encode is enabled.tile_overlap   zUOverlap between tiles for tiled encoding. Only used when use_tiled_encode is enabled.node_iddisplay_namecategorydescriptioninputsoutputs)r   Schemar   ConditioningInputVaeLatentImageIntFloatComboBooleanOutputclss    D/home/wildlama/comfy/ComfyUI/custom_nodes/ComfyUI-LTXVideo/iclora.pydefine_schema$LTXAddVideoICLoRAGuide.define_schema   s   yy,25OO)X %%j1%%j1U#		 B    w'q      -v   '2&j	   

  &!q ! 
 o   "s  o?B &&z2&&z2		  *UO
 O	
    c                    Uu  pnUR                   S   S-
  U-  U-  S-   nUS U n[        X,-  U-  5      n[        X=-  U-  5      n[        R                  R	                  UR                  SS5      UUSUS9R                  SS5      nUS S 2S S 2S S 2S S24   nU(       a  UR                  UU	U	U
S9nUU4$ UR                  U5      nUU4$ )Nr   r   bilinear)r'      )tile_xtile_yoverlap)shapeintcomfyutilscommon_upscalemovedimencode_tiledencode)rF   r   latent_widthlatent_heightimagesscale_factorsr$   r'   r+   r,   r1   time_scale_factorwidth_scale_factorheight_scale_factornum_frames_to_keeptarget_widthtarget_heightpixelsencode_pixelsguide_latents                       rG   rY   LTXAddVideoICLoRAGuide.encodeh   s    FSB/B\\!_q %66 !" ++,<<?VVW/2II
 ++NN2q! , 
 '!R. 	 q!Q{+++  $	 , L l** ::m4Ll**rJ   returnc                 @   UR                   nUS   n[        R                  " U5      nUR                  u    nnnnUS   nUR                  S   S-
  U-  U-  S-   nUS:H  =(       d    US:H  nU(       d  [        R
                  " US S U/SS9nU R                  UUUUUUU	U
U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U-  S:w  d	  UU-  S:w  a  [        SU SU SU 35      e[        5       R                  SU0[        U5      [        U5      S	9S   nUS
   nUS   nUR                  S   UR                  S   -  UR                  S   -  n[        R                  R                  UU[        U5      Xm5      u  nnUUR                  S   -   U::  d   S5       e[        R                  R                  UUUUUUUUUUUS9u  ppSSKJn  U" UUU5      nU" UUU5      n[$        R&                  " XXS.5      $ )Nsamplesr   r   dim   Latent spatial size x. must be divisible by latent_downscale_factor horizontal_scalevertical_scale
noise_maskrN      =Conditioning frames exceed the length of the latent sequence.
guide_maskr$   
causal_fixappend_guide_attention_entryrj   rt   )downscale_index_formulanodes_ltget_noise_maskrR   torchcatrY   list
ValueErrorr   dilate_latentrS   LTXVAddGuideget_latent_indexlenappend_keyframeiclora_attentionr{   r   
NodeOutput)rF   r   r   r   r   r   r   r   r$   r'   r+   r,   r1   r]   latent_imagert   _latent_lengthr[   rZ   r^   ra   ry   rf   guide_orig_shaperx   dilatediclora_tokens_added
latent_idxr{   s                                 rG   executeLTXAddVideoICLoRAGuide.execute   s     33i(,,V4
;G;M;M81m]L)!,[[^a$55 !" !^>'9Q'>
IIuRay%0a8E!jj#
| '1ab!Q7L!"IE   2 212 67
 #Q&66!; #::a? *<.-Hv  xO  wP  Q  '(66L)!$%<!="#:; 7  	G
 !.J"9-L q!L$6$6q$99L<N<Nq<QQ 	 !) 5 5 F FmSZ!
	: ++A..-?	KJ	K? !!11%(?% 2  	5L" 	C/)+;
 0)+;
 }}L S
 	
rJ    N)__name__
__module____qualname____firstlineno__r~   SymmetricPatchifier
PATCHIFIERclassmethodrH   rY   r   r   r   __static_attributes__r   rJ   rG   r	   r	      sc    --a4@JP
 P
d (+ (+T m
 
m
 m
rJ   LTXAddVideoICLoRAGuideAdvancedc                   \    \ rS rSrSr\S 5       r\  SS\R                  4S jj5       r	Sr
g)	r   i  zExtended IC-LoRA guide node with per-guide attention strength control.

Same as LTXAddVideoICLoRAGuide, but adds attention_strength and
attention_mask inputs to control how strongly this guide's conditioning
influences generation via self-attention.
c                 Z   [         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5      [         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SS9[         R                  R	                  SSS/SS9[         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SS*S9[         R                  R	                  S+S,S-S.9/[         R                  R                  S5      [         R                  R                  S5      [         R                  R                  S5      /S/9$ )0Nr   z! Add Video IC-LoRA Guide Advancedr   zAdds IC-LoRA guide conditioning with per-guide attention strength control. Same as LTXAddVideoICLoRAGuide, but allows controlling how strongly this guide influences generation via self-attention, optionally with a spatial mask.r   r   r   r   z:Video-only latent to condition. Must be a 5D video latent.r   r   r   r   r   r   zFrame index to start the conditioning at. The value is rounded to the nearest frame and wrapped modulo the number of video frames. Negative values are counted from the end of the video before wrapping.r   r   r   r    r!   r"   r$   r%   z<For IC-LoRA on small grid. 1 = original size, 2 = half, etc.r&   r'   r(   r)   )r*   r   r+   F)r   r,   r-   r.   r/   r0   r1   r2   attention_strengthzControls how strongly this guide influences generation via self-attention. 1.0 = full conditioning (default), 0.0 = ignore. When an attention_mask is also provided, this multiplies the mask values.attention_maskTzOptional pixel-space spatial mask. Shape (F, H, W) or (H, W). Values in [0, 1]. Controls per-region conditioning influence. Multiplied by attention_strength.)optionalr   r3   )r   r:   r   r;   r<   r=   r>   r?   r@   rA   rB   rC   MaskrD   rE   s    rG   rH   ,LTXAddVideoICLoRAGuideAdvanced.define_schema  s   yy4212)b
 %%j1%%j1U#		X    w'X  
 z3CStT-Z   '2&  
 

  !3U C[#23RP^RRSrR(d   $!<	  c:x &&z2&&z2		  *KJ
 J	
rJ   Nrh   c                 h   SSK Jn  SSKJn  UR                  nUS   n[
        R                  " U5      nUR                  u    nnnnUS   nUR                  S   S-
  U-  U-  S-   nUS:H  =(       d    US:H  nU(       d  [        R                  " US S U/SS9nU R                  UUUUUUU	U
U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U-  S:w  d	  UU-  S:w  a  [        SU S	U S
U 35      eU" 5       R                  SU0[        U5      [        U5      S9S   nUS   nUS   nUR                  S   UR                  S   -  UR                  S   -  n[
        R                  R!                  UU[#        U5      UU5      u  nn U UR                  S   -   U::  d   S5       e[
        R                  R%                  UUUUUUUUUUUS9u  pnnSSK Jn!  U" U5      n"U!" UUUUU"S9nU!" UUUUU"S9n[(        R*                  " XUUS.5      $ )Nr   )normalize_maskr   rj   r   rk   rm   rn   ro   rp   rq   rt   rN   ru   rv   rw   rz   )r   r   r|   )r   r   latentsr   r}   r~   r   rR   r   r   rY   r   r   r   rS   r   r   r   r   r{   r   r   )#rF   r   r   r   r   r   r   r   r$   r'   r+   r,   r1   r   r   r   r   r]   r   rt   r   r   r[   rZ   r^   ra   ry   rf   r   rx   r   r   r   r{   	norm_masks#                                      rG   r   &LTXAddVideoICLoRAGuideAdvanced.execute[  s   $ 	5-33i(,,V4
;G;M;M81m]L)!,[[^a$55 !" !^>'9Q'>
IIuRay%0a8E!jj#
| '1ab!Q7L!"IE 2 212 67
"Q&66!; #::a? *<.- I<<S;TV  '(66L)!$%<!="#:; 7  	G
 !.J"9-L q!L$6$6q$99L<N<Nq<QQ 	 !) 5 5 F FmSZM!
	: ++A..-?	KJ	K? !!11%(?% 2  	5L*  	C">2	/1$
 01$
 }}L
 S
 	
rJ   r   )r   Nr   r   r   r   __doc__r   rH   r   r   r   r   r   rJ   rG   r   r     sK     K
 K
Z  v
  
!v
 v
rJ   LTXICLoRALoaderModelOnlyc                   P    \ rS rSr\S 5       r\S\R                  4S j5       rSr	g)r   i  c                    [         R                  " S[        S-   SS[         R                  R	                  S5      [         R
                  R	                  S[        R                  " S5      S9[         R                  R	                  S	S
SSSS9/[         R                  R                  S5      [         R                  R                  S5      /S9$ )Nr   z IC-LoRA Loader Model Onlyr   zZLoads a LoRA model and extracts the latent_downscale_factor from the safetensors metadata.model	lora_nameloras)r*   strength_modelr   g      Yg      Y@r!   r"   r$   r3   )
r   r:   r   Modelr<   rB   folder_pathsget_filename_listrA   rD   rE   s    rG   rH   &LTXICLoRALoaderModelOnly.define_schema  s    yy.25QQ)tw'(::7C   $   ( 9:'
 	
rJ   rh   c                    [         R                  " SU5      n[        R                  R	                  USSS9u  pV [        US   5      nUS:X  a  [        R                  " X5      $ [        R                  R                  US XSS5      u  p[        R                  " X5      $ ! [        [        [        4 a    Sn[        R                  " SU5         Nf = f)Nr   T)	safe_loadreturn_metadatareference_downscale_factorr   zLFailed to extract reference_downscale_factor from metadata for %s, using 1.0r   )r   get_full_path_or_raiserT   rU   load_torch_filefloatKeyErrorr   	TypeErrorloggingwarningr   r   sdload_lora_for_models)
rF   r   r   r   	lora_pathlorametadatar$   
model_lorar   s
             rG   r    LTXICLoRALoaderModelOnly.execute  s     77K	 44t 5 

	&+H5Q,R&S# Q==@@554q

 }}ZAA *i0 	&)#OO^	s   B .CCr   N)
r   r   r   r   r   rH   r   r   r   r   r   rJ   rG   r   r     s6    
 
4 B"-- B BrJ   c                 t    U R                   u  pp4U R                  SSSS5      R                  XX$-  5      nSU0$ )uG   Reshape audio latent (b, c, t, f) → ref_audio token dict (b, t, c*f).r   rm   r   rN   tokens)rR   permutereshape)r   bctf
ref_tokenss         rG   _patchify_audio_latentr     s=    JA!1a+33A!%@Jj!!rJ   LTXVSetAudioRefTokensc                   T    \ rS rSrSr\S 5       r\S\R                  4S j5       r	Sr
g)r   i  a  Provide speaker identity context for audio generation.

Patchifies the audio latent and attaches it as reference tokens on both
positive and negative conditioning. The model prepends these tokens with
negative temporal positions so they serve as identity context without
being part of the generated output.

Also outputs a frozen copy of the audio latent (noise_mask=0) for
direct use in stage 2 without re-encoding.
c                    [         R                  " S[        S-   SS[         R                  R	                  SSS9[         R                  R	                  SS	S9[         R
                  R	                  S
SS9/[         R                  R                  SSS9[         R                  R                  SSS9[         R
                  R                  SSS9/S9$ )Nr   z Set Audio Ref Tokensr   zProvides speaker identity context for audio generation by attaching reference audio tokens to the conditioning. The tokens are prepended with negative temporal positions so the model treats them as context rather than generation targets.r   z>Positive conditioning to attach the reference audio tokens to.r   r   z>Negative conditioning to attach the reference audio tokens to.audio_latentz0Encoded audio latent from LTXV Audio VAE Encode.z;Positive conditioning with reference audio tokens attached.)r5   r   z;Negative conditioning with reference audio tokens attached.frozen_audioz>Audio latent with noise_mask=0, fully frozen during denoising.r3   )r   r:   r   r;   r<   r>   rD   rE   s    rG   rH   #LTXVSetAudioRefTokens.define_schema"  s    yy+25LL)2 %%\ &  %%\ &  		"N    &&!+Y '  &&!+Y '  		  !/\ ! 1&
 &	
rJ   rh   c                 F   US   n[        U5      n[        R                  " USU05      n[        R                  " USU05      nUR                  5       nUR                  u  pxp[
        R                  " USU	S4[
        R                  S9US'   [        R                  " XU5      $ )Nrj   	ref_audior   )dtypert   )
r   node_helpersconditioning_set_valuescopyrR   r   zerosfloat32r   r   )rF   r   r   r   r   r   frozenr   r   r   r   s              rG   r   LTXVSetAudioRefTokens.executeL  s    i(*62	77{I.
  77{I.
 ""$\\
a${{Aq!Q<u}}M|}}X88rJ   r   Nr   r   rJ   rG   r   r     s;    	 '
 '
R 9"-- 9 9rJ   )r   rT   comfy.samplerscomfy.sdcomfy.utilscomfy_extras.nodes_ltr~   r   r   r   comfy_api.latestr   r   r   nodes_registryr   r   	ComfyNoder	   r   r   r   r   r   rJ   rG   <module>r      s         (     % A )*o
R\\ o
 +o
d 12M
%; M
 3M
` +,5Br|| 5B -5Bp" ()E9BLL E9 *E9rJ   