o
    i                     @   sj   d dl mZ d dlZd dlmZmZ G dd dejZG dd dejZG dd	 d	eZ	d
e	fddZ
dS )    )overrideN)ComfyExtensionioc                   @   s,   e Zd Zedd ZedejfddZdS )	RenormCFGc                 C   sP   t jddt jdt jjdddddd	d
t jjdddddd	d
gt j gdS )Nr   zadvanced/modelmodel	cfg_truncd           g      Y@g{Gz?T)defaultminmaxstepadvanced
renorm_cfgg      ?)node_idcategoryinputsoutputs)r   SchemaModelInputFloatOutputcls r   8/mnt/c/Users/fbmor/ComfyUI/comfy_extras/nodes_lumina2.pydefine_schema   s   
zRenormCFG.define_schemareturnc                    s,    fdd}  }|| t|S )Nc                    s  | d }| d }| d }| d }| d }j jj}|d  k r|d d d |f |d d d |f }}|d d |d f |d d |d f }	}
||||   }|	}tdkrtjj|ttdt	|j
d	d
}|t }tjj|ttdt	|j
d	d
}||kr|||  }n.|d d d |f |d d d |f }}|d d |d f |d d |d f }	}
|}|	}tj||gdd}|| S )Ncond_denoiseduncond_denoised
cond_scaletimestepinputr   r	      T)dimkeepdim)r%   )r   diffusion_modelin_channelsfloattorchlinalgvector_normtuplerangelenshapecat)argsr   r    r!   r"   x_origr(   cond_eps
uncond_eps	cond_rest_half_eps	half_restori_pos_normmax_new_normnew_pos_norm
cfg_resultr   r   r   r   r   renorm_cfg_func   s8   
****z*RenormCFG.execute.<locals>.renorm_cfg_func)cloneset_model_sampler_cfg_functionr   
NodeOutput)r   r   r   r   r?   mr   r>   r   execute   s   $

zRenormCFG.executeN)__name__
__module____qualname__classmethodr   r   rB   rD   r   r   r   r   r      s
    
r   c                   @   s:   e Zd ZdddZdZedd Zedejfdd	Z	d
S )CLIPTextEncodeLumina2zYou are an assistant designed to generate superior images with the superior degree of image-text alignment based on textual prompts or user prompts.zYou are an assistant designed to generate high-quality images with the highest degree of image-text alignment based on textual prompts.)superior	alignmenta]  Lumina2 provide two types of system prompts:Superior: You are an assistant designed to generate superior images with the superior degree of image-text alignment based on textual prompts or user prompts. Alignment: You are an assistant designed to generate high-quality images with the highest degree of image-text alignment based on textual prompts.c                 C   sb   t jddgdddt jjdt| j | jdt jjdd	d	d
dt j	jdddgt j
jddgdS )NrI   zlumina promptzCLIP Text Encode for Lumina2conditioningzEncodes a system prompt and a user prompt using a CLIP model into an embedding that can be used to guide the diffusion model towards generating specific images.system_prompt)optionstooltipuser_promptTzThe text to be encoded.)	multilinedynamic_promptsrO   clipz*The CLIP model used for encoding the text.)rO   zNA conditioning containing the embedded text used to guide the diffusion model.)r   search_aliasesdisplay_namer   descriptionr   r   )r   r   Combor   listSYSTEM_PROMPTkeysSYSTEM_PROMPT_TIPStringClipConditioningr   r   r   r   r   r   N   s0   z#CLIPTextEncodeLumina2.define_schemar   c                 C   sB   |d u rt d| j| }| d| }||}t||S )NzERROR: clip input is invalid: None

If the clip is from a checkpoint loader node your checkpoint does not contain a valid clip or text encoder model.z <Prompt Start> )RuntimeErrorrY   tokenizer   rB   encode_from_tokens_scheduled)r   rS   rP   rM   prompttokensr   r   r   rD   l   s   

zCLIPTextEncodeLumina2.executeN)
rE   rF   rG   rY   r[   rH   r   r   rB   rD   r   r   r   r   rI   B   s    
rI   c                   @   s(   e Zd Zedeeej  fddZdS )Lumina2Extensionr   c                    s
   t tgS N)rI   r   )selfr   r   r   get_node_listw   s   zLumina2Extension.get_node_listN)	rE   rF   rG   r   rX   typer   	ComfyNoderg   r   r   r   r   rd   v   s    rd   r   c                      s   t  S re   )rd   r   r   r   r   comfy_entrypoint   s   rj   )typing_extensionsr   r*   comfy_api.latestr   r   ri   r   rI   rd   rj   r   r   r   r   <module>   s    ;4	