
    +j!                     
   d dl Z d dlmZ d dlmZ d dlZd dlZd dlZd dlmZ d dl	m
Z
mZ d dl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          ZdefdZdS )    N)tqdm)override)model_management)IFNetdetect_rife_config)FILMNet)ComfyExtensionioINTERP_MODELc                   `    e Zd Zed             Zedej        fd            Zed             ZdS )FrameInterpolationModelLoaderc                     t          j        dddt           j                            dt	          j        d          d          gt                                          g          S )	Nr   zLoad Frame Interpolation Modelzmodel/loaders
model_nameframe_interpolationzfSelect a frame interpolation model to load. Models must be placed in the 'frame_interpolation' folder.)optionstooltip)node_iddisplay_namecategoryinputsoutputs)r
   SchemaComboInputfolder_pathsget_filename_listFrameInterpolationModelOutputclss    F/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_frame_interpolation.pydefine_schemaz+FrameInterpolationModelLoader.define_schema   so    y39$|\5STi5j5j (P  Q Q
 (..00
 
 
 	
    returnc                    t          j        d|          }t          j                            |d          }|                     |          }t          j        t          j                              rt          j
        nt          j        }|                                                    |           t          j                            |t          j                    t          j                              }t#          j        |          S )Nr   T)	safe_load)load_deviceoffload_device)r   get_full_path_or_raisecomfyutilsload_torch_file_detect_and_loadr   should_use_fp16get_torch_devicetorchfloat16float32evaltomodel_patcherCoreModelPatcherunet_offload_devicer
   
NodeOutput)r    r   
model_pathsdmodeldtypepatchers          r!   executez%FrameInterpolationModelLoader.execute    s    !89NPZ[[
[((t(DD$$R((!1!ABRBcBeBe!f!fylqly

%66(9;;+?AA 7 
 

 }W%%%r#   c                    d|v r%t                      }|                    |           |S t          j                            |ddd          }i |D ]Q}t          d          D ]?}|                    d| d          r$d| d|t          d| d          d           |<   @Rr fd|                                D             }d	 |                                D             }	 t          |          \  }}n$# t          t          f$ r t          d
          w xY wt          ||          }|                    |           |S )Nz/extract.extract_sublevels.convs.0.0.conv.weight )zmodule.zflownet.   block.zblocks.c                 D    i | ]\  }}                     ||          |S  )get).0kvkey_maps      r!   
<dictcomp>zBFrameInterpolationModelLoader._detect_and_load.<locals>.<dictcomp>?   s-    >>>41a'++a##Q>>>r#   c                 D    i | ]\  }}|                     d           ||S ))zteacher.zcaltime.)
startswith)rG   rH   rI   s      r!   rK   zBFrameInterpolationModelLoader._detect_and_load.<locals>.<dictcomp>@   s0    XXXtq!>V1W1WXaXXXr#   z-Unrecognized frame interpolation model format)head_chchannels)r   load_state_dictr*   r+   state_dict_prefix_replacerangerM   lenitemsr   KeyError
ValueErrorr   )r    r:   r;   rH   irN   rO   rJ   s          @r!   r-   z.FrameInterpolationModelLoader._detect_and_load/   s    =BBIIE!!"%%%L [2222SU7V7VWW 	G 	GA1XX G G<<-- G!F1!F!Fq\Q\\\1B1B1C1C/D!F!FGAJG  	?>>>>288::>>>BXXrxxzzXXX	N 22 6 6GXX*% 	N 	N 	NLMMM	Ng999b!!!s   %C8 8!DN)	__name__
__module____qualname__classmethodr"   r
   r8   r>   r-   rE   r#   r!   r   r      sq        
 
 [
 &BM & & & [&   [  r#   r   c                   J    e Zd Zed             Zedej        fd            ZdS )FrameInterpolatec                 &   t          j        dddg dt                              d          t           j                            d          t           j                            dddd	
          gt           j                                        g          S )Nr]   zFrame Interpolatevideo)rifefilmzframe interpolationzslow motionzinterpolate framesvfiinterp_modelimages
multiplier      )defaultminmax)r   r   r   search_aliasesr   r   )r
   r   r   r   ImageIntr   r   s    r!   r"   zFrameInterpolate.define_schemaL   s    y&,nnn'--n==x((\1!DD !!
 
 
 	
r#   r$   c           	      	  &'( t          j                    }j        d         }|dk     sdk     rt          j                  S |j        '|                                (|j        }|                    j        (          }t          j	        |g|           t          |dd          &j        d         j        d         }	}&'(fd}
|dz
  }dz
  }||z  }t          j                            |          }t          |d          }|}fd	t          d          D             }t          j                    }|z  dz   }t#          j        |d
||	f||          }d                             dd                              |          |d<   d} |
d          }|j        d         |j        d
         }}t#          j        |'(                              |ddd          }|                    dd||          }~t          |dd           }i }d }	 t          |          D ]f}||n
 |
|          } |
|dz             }|}d|v r|                    d          n|                    |          |d<   |                    |          |d<   |d         |d<   d} |	  |||||          }!|!d d d d d |d |	f                             |          ||||z   <   ||z  }|                    |           |                    |           d} n*# t           j        $ r t          j                     d }Y nw xY w| sd}"|"|k     rt;          |||"z
            }#	 |                    |#ddd          }$|                    |#ddd          }% ||$|%||"|"|#z            |          }!|!d d d d d |d |	f                             |          ||||#z   <   ||#z  }|                    |#           |                    |#           |"|#z  }"nB# t           j        $ r0 |dk    r t=          d|dz            }t          j                     Y nw xY w|"|k     |dz                                dd                              |          ||<   |dz  }h	 |                                 n# |                                 w xY w|                    dd                               dd          }t          j        |          S )Nr   rf   )memory_required	pad_align   c                     | | dz                                 dd                                        }dk    rddlm}  ||fd          }|S )Nrq   r<   devicer   )pad_to_patch_sizereflect)padding_mode)movedimr4   comfy.ldm.common_ditrv   )idxframerv   alignru   r<   rd   s      r!   prepare_framez/FrameInterpolate.execute.<locals>.prepare_framen   ss    3sQw;'//A6699f9UUEqyyBBBBBB))%%iXXXLr#   zFrame interpolation)totaldescc                     g | ]}|z  S rE   rE   )rG   tre   s     r!   
<listcomp>z,FrameInterpolate.execute.<locals>.<listcomp>}   s    AAAqA
NAAAr#      rt   rs   )ru   r<   forward_multi_timestepnextimg0img1F)cacheT)timestepr   g        g      ?)!r   intermediate_deviceshaper
   r8   r'   model_dtyper;   memory_used_forwardload_models_gpugetattrr*   r+   ProgressBarr   rR   intermediate_dtyper0   emptyry   r4   tensorreshapeexpandpopextract_featuresupdateOOM_EXCEPTIONsoft_empty_cacheri   rj   closeclamp_))r    rc   rd   re   r(   
num_framesinference_modelactivation_memHWr~   total_pairs
num_interptotal_stepspbartqdm_barbatcht_values	out_dtypetotal_out_framesresultout_idxsamplepHpWts_fullmulti_fn
feat_cache
prev_framerW   img0_singleimg1_single
used_multimidsjbr   r   r}   ru   r<   s)     ``                                  @@@r!   r>   zFrameInterpolate.execute]   s   )=??\!_
>>Z!^^=((()((**&,(<<V\5QQ(,XXXXa88|AQ1	 	 	 	 	 	 	 	 !1n!^
!J.{&&{33k0EFFFAAAAE!Z,@,@AAA$799	&3a7.1a8	R`aaa1I%%b!,,//	::q	 q!!a&,q/B,xeDDDLLZYZ\]_`aa..QB//?,DdKK

/	;'' , ,,6,BjjVWHXHX+mAE22(
 @F?S?SZ^^F%;%;%;YhYyYy  {F  ZG  ZG
6"%4%E%Ek%R%R
6"%/%7
6""
'	('x[(R\]]]?CAAAqqq"1"bqbL?Q?T?TU^?_?_ww';;<:-J/// 
333%)

+9 ( ( ((9;;;#'( " @Aj..zA~66@#.#5#5aR#D#DD#.#5#5aR#D#DD#2?4PQRSVWRWPWHX`j#k#k#kD:>qqq!!!RaR!|:L:O:OPY:Z:ZF77Q;#67#qLG KKNNN$OOA...FAA/= @ @ @$zz %$'5A:$6$6E,=?????	@ j.." #)Q-"7"7A">">"A"A)"L"Lw1Y,\ NNHNN 2&&--c377}V$$$sX   B	R" A/LR" $L63R" 5L66"R" B-PR" <QR" QAR" "R8N)rX   rY   rZ   r[   r"   r
   r8   r>   rE   r#   r!   r]   r]   K   s]        
 
 [
  e%"- e% e% e% [e% e% e%r#   r]   c                   L    e Zd Zedeeej                          fd            ZdS )FrameInterpolationExtensionr$   c                 "   K   t           t          gS N)r   r]   )selfs    r!   get_node_listz)FrameInterpolationExtension.get_node_list   s       *
 	
r#   N)	rX   rY   rZ   r   listtyper
   	ComfyNoder   rE   r#   r!   r   r      sE        
T$r|*<%= 
 
 
 X
 
 
r#   r   r$   c                  "   K   t                      S r   )r   rE   r#   r!   comfy_entrypointr      s      &(((r#   )r0   r   typing_extensionsr   comfy.model_patcherr*   comfy.utilsr   r   -comfy_extras.frame_interpolation_models.ifnetr   r   0comfy_extras.frame_interpolation_models.film_netr   comfy_api.latestr	   r
   Customr   r   r   r]   r   r   rE   r#   r!   <module>r      sn          & & & & & &             " " " " " " S S S S S S S S D D D D D D / / / / / / / /#")N33 8 8 8 8 8BL 8 8 8vx% x% x% x% x%r| x% x% x%v
 
 
 
 
. 
 
 
) ; ) ) ) ) ) )r#   