o
    i)                     @   s^  d dl Z d dlZd dlmZ d dlmZmZ d dlZd dl	Z	d dl
Z
d dlZd dlZG dd dejZG dd dejZG dd	 d	ejZG d
d dejZg dZG dd dejZG dd dejZdedefddZdededefddZdededee fddZG dd dejZG dd dZG d d! d!ejZG d"d# d#eZdefd$d%ZdS )&    N)override)ComfyExtensionioc                   @   0   e Zd Zedd ZedejfddZeZdS )CLIPTextEncodeFluxc                 C   sX   t jddt jdt jjddddt jjddddt jjdd	d
dddgt j gdS )Nr   advanced/conditioning/fluxclipclip_lT)	multilinedynamic_promptst5xxlguidance      @              Y@皙?defaultminmaxstepnode_idcategoryinputsoutputs)r   SchemaClipInputStringFloatConditioningOutputcls r%   5/mnt/c/Users/fbmor/ComfyUI/comfy_extras/nodes_flux.pydefine_schema   s   
z CLIPTextEncodeFlux.define_schemareturnc                 C   s4   | |}| |d |d< t|j|d|idS )Nr   r   )add_dict)tokenizer   
NodeOutputencode_from_tokens_scheduled)r$   r   r	   r   r   tokensr%   r%   r&   execute   s   
zCLIPTextEncodeFlux.executeN)	__name__
__module____qualname__classmethodr'   r   r+   r.   encoder%   r%   r%   r&   r      s    
r   c                   @   s.   e Zd Zedd ZeddejfddZdS )	EmptyFlux2LatentImagec                 C   sZ   t jdddt jjdddtjddt jjdddtjddt jjd	d
d
ddgt j gdS )Nr4   zEmpty Flux 2 Latentlatentwidth      r   height
batch_size      r   r   r   )r   display_namer   r   r   )r   r   Intr   nodesMAX_RESOLUTIONLatentr"   r#   r%   r%   r&   r'   &   s   z#EmptyFlux2LatentImage.define_schemar;   r(   c                 C   s2   t j|d|d |d gtj d}td|iS )N   r8   )devicesamples)torchzeroscomfymodel_managementintermediate_devicer   r+   )r$   r6   r9   r:   r5   r%   r%   r&   r.   6   s   $zEmptyFlux2LatentImage.executeN)r;   r/   r0   r1   r2   r'   r   r+   r.   r%   r%   r%   r&   r4   %   s
    
r4   c                   @   r   )FluxGuidancec                 C   s8   t jddt jdt jjdddddd	gt j gd
S )NrL   r   conditioningr   r   r   r   r   r   r   )r   r   r!   r   r    r"   r#   r%   r%   r&   r'   <   s   
zFluxGuidance.define_schemar(   c                 C   s   t |d|i}t|S Nr   node_helpersconditioning_set_valuesr   r+   )r$   rM   r   cr%   r%   r&   r.   J      
zFluxGuidance.executeN	r/   r0   r1   r2   r'   r   r+   r.   appendr%   r%   r%   r&   rL   ;       
rL   c                   @   r   )FluxDisableGuidancec                 C   &   t jdddt jdgt j gdS )NrW   r   zMThis node completely disables the guidance embed on Flux and Flux like modelsrM   r   r   descriptionr   r   )r   r   r!   r   r"   r#   r%   r%   r&   r'   S      
z!FluxDisableGuidance.define_schemar(   c                 C   s   t |dd i}t|S rN   rO   )r$   rM   rR   r%   r%   r&   r.   a   rS   zFluxDisableGuidance.executeNrT   r%   r%   r%   r&   rW   R   rV   rW   ))     )    )    )  p  )   0  )@    )p    )  P  )r7   r7   )rk   rj   )ri   rh   )rg   rf   )re   rd   )rc   rb   )ra   r`   )r_   r^   )r]   r\   c                   @   r   )FluxKontextImageScalec                 C   rX   )Nrl   r   zIThis node resizes the image to one that is more optimal for flux kontext.imagerY   )r   r   Imager   r"   r#   r%   r%   r&   r'      r[   z#FluxKontextImageScale.define_schemar(   c                    sf   |j d }|j d }||  t fddtD \}}}tj|dd||dddd}t|S )N   r;   c                 3   s*    | ]\}}t  ||  ||fV  qd S N)abs).0whaspect_ratior%   r&   	<genexpr>   s   ( z0FluxKontextImageScale.execute.<locals>.<genexpr>lanczoscenter)	shaper   PREFERED_KONTEXT_RESOLUTIONSrH   utilscommon_upscalemovedimr   r+   )r$   rm   r6   r9   _r%   ru   r&   r.      s   

$
zFluxKontextImageScale.executeN)	r/   r0   r1   r2   r'   r   r+   r.   scaler%   r%   r%   r&   rl   ~   s    
rl   c                   @   r   )%FluxKontextMultiReferenceLatentMethodc              
   C   s<   t jdddt jdt jjdg dddgt j gdd	S )
Nr   zEdit Model Reference Methodr   rM   reference_latents_method)offsetindexzuxo/unoindex_timestep_zeroT)optionsadvanced)r   r>   r   r   r   is_experimental)r   r   r!   r   Combor"   r#   r%   r%   r&   r'      s   
	z3FluxKontextMultiReferenceLatentMethod.define_schemar(   c                 C   s.   d|v sd|v r
d}t |d|i}t|S )Nuxousor   rO   )r$   rM   r   rR   r%   r%   r&   r.      s   
z-FluxKontextMultiReferenceLatentMethod.executeNrT   r%   r%   r%   r&   r      s    
r   musigmac                 C   s$   t |t |d|  d |   S )Nr;   )mathexp)tr   r   r%   r%   r&   generalized_time_snr_shift   s   $r   image_seq_len	num_stepsr(   c                 C   sp   d\}}d\}}| dkr||  | }t |S ||  | }||  | }|| d }	|d|	  }
|	| |
 }t |S )N)gT	?gŒ_?)g w:/&?gDw:?i  g     g@g      i@)float)r   r   a1b1a2b2r   m_200m_10abr%   r%   r&   compute_empirical_mu   s   r   c                 C   s,   t || }tdd| d }t||d}|S )Nr;   r   g      ?)r   rF   linspacer   )r   r   r   	timestepsr%   r%   r&   get_schedule   s   
r   c                   @   s,   e Zd Zedd ZedejfddZdS )Flux2Schedulerc                 C   sX   t jddt jjdddddt jjdd	d
tjddt jjdd	d
tjddgt j gdS )Nr   z#sampling/custom_sampling/schedulerssteps   r;   r<   r=   r6   r7   r8   r   r9   r   )r   r   r?   r   r@   rA   Sigmasr"   r#   r%   r%   r&   r'      s   zFlux2Scheduler.define_schemar(   c                 C   s$   || d }t |t|}t|S )N   )r   roundr   r+   )r$   r   r6   r9   seq_lensigmasr%   r%   r&   r.      s   
zFlux2Scheduler.executeNrK   r%   r%   r%   r&   r      s
    
r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )KV_Attn_Inputc                 C   
   i | _ d S rp   cacheselfr%   r%   r&   __init__      
zKV_Attn_Input.__init__c                 K   s   | dg }t|dkri S t|}d|d |d }|| jv r?| j| \}	}
d| _|tj||	fddtj||
fddd	S |d d d d | d f  |d d d d | d f  f| j|< d
| _|||d	S )Nreference_image_num_tokensr   z{}_{}
block_typeblock_indexFro   )dim)qkvT)	getlensumformatr   	set_cacherF   catclone)r   r   r   r   extra_optionskwargsr   ref_toks	cache_keykkvvr%   r%   r&   __call__   s   
(FzKV_Attn_Input.__call__c                 C   r   rp   r   r   r%   r%   r&   cleanup   r   zKV_Attn_Input.cleanupN)r/   r0   r1   r   r   r   r%   r%   r%   r&   r      s    r   c                   @   s<   e Zd ZedejfddZedejjdej	fddZ
dS )FluxKVCacher(   c              
   C   s2   t jdddddt jjdddgt jjd	dgd
S )Nr   zFlux KV CachezIEnables KV Cache optimization for reference images on Flux family models. TmodelzThe model to use KV Cache on.)tooltipz(The patched model with KV Cache enabled.)r   r>   rZ   r   r   r   r   )r   r   Modelr   r"   r#   r%   r%   r&   r'     s   zFluxKVCache.define_schemar   c                    s`   |  }t   fdd}|  || t|jjdr%|dd n|dd t	|S )Nc                    sP   t  jdkr&t| d dg }|dkr&| d }|d d d | f | d< | S )Nr   transformer_optionsr   img)r   r   r   r   )r   ref_image_tokensr   input_patch_objr%   r&   model_input_patch  s   z.FluxKVCache.execute.<locals>.model_input_patchparamsz)diffusion_model.params.default_ref_methodr   z"diffusion_model.default_ref_method)
r   r   set_model_attn1_patchset_model_post_input_patchhasattrr   diffusion_modeladd_object_patchr   r+   )r$   r   mr   r%   r   r&   r.     s   


zFluxKVCache.executeN)r/   r0   r1   r2   r   r   r'   r   Typer+   r.   r%   r%   r%   r&   r     s
    r   c                   @   s(   e Zd Zedeeej  fddZdS )FluxExtensionr(   c                    s   t tttttttgS rp   )r   rL   rW   rl   r   r4   r   r   r   r%   r%   r&   get_node_list+  s   zFluxExtension.get_node_listN)	r/   r0   r1   r   listtyper   	ComfyNoder   r%   r%   r%   r&   r   *  s    r   c                      s   t  S rp   )r   r%   r%   r%   r&   comfy_entrypoint9  s   r   ) rP   comfy.utilsrH   typing_extensionsr   comfy_api.latestr   r   comfy.model_managementrF   r   r@   comfy.ldm.flux.mathr   r   r4   rL   rW   r|   rl   r   r   r   intr   r   r   r   r   r   r   r   r%   r%   r%   r&   <module>   s0    '