o
    ia2                  
   @   s   d dl 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jZ	G dd dejj
ZG dd dZ
G dd	 d	ZG d
d dZG dd deZG dd dZG dd dZG dd dZG dd dZG dd dZe
eeeeeeeed	ZdS )    Nc                   @   s   e Zd Zdd ZdS )LCMc           
      C   s   |  ||jd d d|jd   }||jd d d|jd   }|||  }d}|d }|d |d |d   }||d |d  d  }	|	| ||  S )N   r         ?g      $@   )timestepviewshapendim)
selfsigmamodel_outputmodel_inputr   x0
sigma_datascaled_timestepc_skipc_out r   ?/mnt/c/Users/fbmor/ComfyUI/comfy_extras/nodes_model_advanced.pycalculate_denoised
   s   ("zLCM.calculate_denoisedN)__name__
__module____qualname__r   r   r   r   r   r   	   s    r   c                       s2   e Zd ZdZd	 fdd	Zdd Zdd Z  ZS )
ModelSamplingDiscreteDistilled2   Nc                    sv   t  j||d | j| j | _tj| jtjd}t| jD ]}| j	| jd || j   || jd | < q| 
| d S )Nzsnr)dtyper   )super__init__num_timestepsoriginal_timesteps
skip_stepstorchzerosfloat32rangesigmas
set_sigmas)r   model_configr   sigmas_validx	__class__r   r   r       s   *z'ModelSamplingDiscreteDistilled.__init__c                 C   sX   |  }|| jj| jd d d f  }| jdd|j| j | jd  |jS )Nr   )dimr   )	logto
log_sigmasdeviceabsargminr   r	   r#   )r   r   	log_sigmadistsr   r   r   r   %   s    0z'ModelSamplingDiscreteDistilled.timestepc                 C   s   t j| | jj| jd  | j  dt| jd d}|	 
 }| 
 }| }d| | j|  || j|   }| |jS )Nr   r   )minmax)r$   clampfloatr1   r2   r3   r#   lenr(   floorlongceilfracexp)r   r   tlow_idxhigh_idxwr6   r   r   r   r   *   s   < z$ModelSamplingDiscreteDistilled.sigma)NN)r   r   r   r"   r    r   r   __classcell__r   r   r-   r   r      s
    r   c                   @   ,   e Zd Zedd ZdZdZdZdd ZdS )	ModelSamplingDiscretec                 C   s    ddg dfddddfdiS )	NrequiredMODEL)epsv_predictionlcmr   
img_to_imgimg_to_img_flowBOOLEANFT)defaultadvanced)modelsamplingr   r   sr   r   r   INPUT_TYPES4   s   z!ModelSamplingDiscrete.INPUT_TYPESrJ   patchadvanced/modelc           	      C   s   |  }tjj}|dkrtjj}n,|dkrtjj}n#|dkr#t}t}n|dkr,tjj}n|dkr5tjj	}n|dkr=tjj
}G dd d||}||jj|d	}|d
| |fS )NrL   rM   rN   r   rO   rP   c                   @      e Zd ZdS )z:ModelSamplingDiscrete.patch.<locals>.ModelSamplingAdvancedNr   r   r   r   r   r   r   ModelSamplingAdvancedR       r]   r   model_sampling)clonecomfyr_   rH   EPSV_PREDICTIONr   r   X0
IMG_TO_IMGIMG_TO_IMG_FLOWrT   r*   add_object_patch)	r   rT   rU   r   msampling_basesampling_typer]   r_   r   r   r   rY   @   s&   



zModelSamplingDiscrete.patchN	r   r   r   classmethodrX   RETURN_TYPESFUNCTIONCATEGORYrY   r   r   r   r   rH   3   s    
rH   c                   @   rG   )	ModelSamplingStableCascadec                 C      ddddddddfd	iS )
NrI   rJ   FLOATg       @              Y@{Gz?rR   r8   r9   steprT   shiftr   rV   r   r   r   rX   [      z&ModelSamplingStableCascade.INPUT_TYPESrJ   rY   rZ   c                 C   sR   |  }tjj}tjj}G dd d||}||jj}|| |d| |fS )Nc                   @   r[   )z?ModelSamplingStableCascade.patch.<locals>.ModelSamplingAdvancedNr\   r   r   r   r   r]   l   r^   r]   r_   )	r`   ra   r_   StableCascadeSamplingrb   rT   r*   set_parametersrg   )r   rT   ry   rh   ri   rj   r]   r_   r   r   r   rY   f   s   
z ModelSamplingStableCascade.patchNrk   r   r   r   r   rp   Z   s    
rp   c                   @   s.   e Zd Zedd ZdZdZdZd
ddZd	S )ModelSamplingSD3c                 C   rq   )
NrI   rJ   rr   g      @rs   rt   ru   rv   rx   r   rV   r   r   r   rX   u   rz   zModelSamplingSD3.INPUT_TYPESrJ   rY   rZ     c           	      C   sV   |  }tjj}tjj}G dd d||}||jj}|j||d |d| |fS )Nc                   @   r[   )z5ModelSamplingSD3.patch.<locals>.ModelSamplingAdvancedNr\   r   r   r   r   r]      r^   r]   )ry   
multiplierr_   )	r`   ra   r_   ModelSamplingDiscreteFlowCONSTrT   r*   r|   rg   )	r   rT   ry   r   rh   ri   rj   r]   r_   r   r   r   rY      s   zModelSamplingSD3.patchN)r~   rk   r   r   r   r   r}   t   s    
r}   c                   @   s$   e Zd Zedd ZdZdd ZdS )ModelSamplingAuraFlowc                 C   rq   )
NrI   rJ   rr   gGz?rs   rt   ru   rv   rx   r   rV   r   r   r   rX      rz   z!ModelSamplingAuraFlow.INPUT_TYPES
patch_aurac                 C   s   | j ||ddS )N      ?r   )rY   )r   rT   ry   r   r   r   r      s   z ModelSamplingAuraFlow.patch_auraN)r   r   r   rl   rX   rn   r   r   r   r   r   r      s
    
r   c                   @   rG   )	ModelSamplingFluxc                 C   sT   ddddddddd	fdd
ddddd	fdddt jddfdddt jddfdiS )NrI   rJ   rr   gffffff?rs   rt   ru   T)rR   r8   r9   rw   rS   r   INTi         rv   )rT   	max_shift
base_shiftwidthheight)nodesMAX_RESOLUTIONrV   r   r   r   rX      s   zModelSamplingFlux.INPUT_TYPESrJ   rY   rZ   c                 C   s   |  }d}d}|| ||  }	||	|  }
|| d |	 |
 }tjj}tjj}G dd d||}||jj}|j|d |d| |fS )N   i   c                   @   r[   )z6ModelSamplingFlux.patch.<locals>.ModelSamplingAdvancedNr\   r   r   r   r   r]      r^   r]   )ry   r_   )	r`   ra   r_   r   r   rT   r*   r|   rg   )r   rT   r   r   r   r   rh   x1x2mmbry   ri   rj   r]   r_   r   r   r   rY      s   zModelSamplingFlux.patchNrk   r   r   r   r   r      s    
r   c                   @   rG   )	ModelSamplingContinuousEDMc                 C   s<   ddg dfdddddd	d
dfdddddd	d
dfdiS )NrI   rJ   )rM   edmedm_playground_v2.5rL   cosmos_rflowrr   g      ^@rs        @@MbP?FTrR   r8   r9   rw   roundrS   gMb`?rT   rU   	sigma_max	sigma_minr   rV   r   r   r   rX      s
   z&ModelSamplingContinuousEDM.INPUT_TYPESrJ   rY   rZ   c                 C   s   |  }tjj}d }d}|dkrtjj}	n0|dkr tjj}	d}n%|dkr)tjj}	n|dkr9tjj}	d}tj }n|dkrEtjj	}	tjj
}G dd	 d	||	}
|
|jj}|||| |d
| |d urk|d| |fS )Nr   rL   r   r   rM   r   r   c                   @   r[   )z?ModelSamplingContinuousEDM.patch.<locals>.ModelSamplingAdvancedNr\   r   r   r   r   r]      r^   r]   r_   latent_format)r`   ra   r_   r   rb   EDMrc   latent_formatsSDXL_Playground_2_5COSMOS_RFLOWModelSamplingCosmosRFlowrT   r*   r|   rg   )r   rT   rU   r   r   rh   ri   r   r   rj   r]   r_   r   r   r   rY      s2   

z ModelSamplingContinuousEDM.patchNrk   r   r   r   r   r          
r   c                   @   rG   )	ModelSamplingContinuousVc                 C   s:   dddgfdddddd	d
dfdddddd	d
dfdiS )NrI   rJ   rM   rr   g     @@rs   r   r   FTr   gQ?r   r   rV   r   r   r   rX      s
   z$ModelSamplingContinuousV.INPUT_TYPESrJ   rY   rZ   c           
      C   s^   |  }d}|dkrtjj}G dd dtjj|}||jj}	|	||| |d|	 |fS )Nr   rM   c                   @   r[   )z=ModelSamplingContinuousV.patch.<locals>.ModelSamplingAdvancedNr\   r   r   r   r   r]      r^   r]   r_   )	r`   ra   r_   rc   r   rT   r*   r|   rg   )
r   rT   rU   r   r   rh   r   rj   r]   r_   r   r   r   rY      s   zModelSamplingContinuousV.patchNrk   r   r   r   r   r      r   r   c                   @   rG   )	
RescaleCFGc                 C   rq   )
NrI   rJ   rr   gffffff?rs   r   ru   rv   )rT   r   r   rV   r   r   r   rX   	  rz   zRescaleCFG.INPUT_TYPESrJ   rY   rZ   c                    s$    fdd}|  }|| |fS )Nc                    s
  | d }| d }| d }| d }| |jd d d|jd   }| d }||| d  }|||  |d	 d d
  | }|||  |d	 d d
  | }||||   }tj|ddd}tj|ddd}	|||	  }
 |
 d  |  }|||| || d d
    S )Nconduncond
cond_scaler   r   r   inputr   r   r   )r   r      T)r/   keepdim)r   r	   r
   r$   std)argsr   r   r   r   x_origr,   x_cfgro_posro_cfg
x_rescaledx_finalr   r   r   rescale_cfg  s   "   z%RescaleCFG.patch.<locals>.rescale_cfg)r`   set_model_sampler_cfg_function)r   rT   r   r   rh   r   r   r   rY     s   
zRescaleCFG.patchNrk   r   r   r   r   r     s    
r   c                   @   s4   e Zd ZddgZedd ZdZdZdZdd	 Z	d
S )ModelComputeDtypezmodel precisionzchange dtypec                 C   s   ddg dddifdiS )NrI   rJ   )rR   fp32fp16bf16rS   T)rT   r   r   rV   r   r   r   rX   1  s   zModelComputeDtype.INPUT_TYPESrJ   rY   zadvanced/debug/modelc                 C   s   |  }|t| |fS )N)r`   set_model_compute_dtypenode_helpersstring_to_torch_dtype)r   rT   r   rh   r   r   r   rY   <  s   zModelComputeDtype.patchN)
r   r   r   SEARCH_ALIASESrl   rX   rm   rn   ro   rY   r   r   r   r   r   /  s    
r   )	rH   r   r   rp   r}   r   r   r   r   )comfy.sdra   comfy.model_samplingcomfy.latent_formatsr   r$   r   r_   rb   r   rH   r   rp   r}   r   r   r   r   r   r   NODE_CLASS_MAPPINGSr   r   r   r   <module>   s6    '$-'
