o
    *Îj™  ã                   @   s>   d dl Zd dlZddlmZ G dd„ deƒZG dd„ dƒZdS )é    Né   )ÚGaussianDiffusionc                       s`   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡ fdd„Z‡ fdd	„Z‡ fd
d„Zdd„ Z	dd„ Z
‡  ZS )ÚSpacedDiffusiona#  
    A diffusion process which can skip steps in a base diffusion process.

    :param use_timesteps: a collection (sequence or set) of timesteps from the
                          original diffusion process to retain.
    :param kwargs: the kwargs to create the base diffusion process.
    c                    s˜   t |ƒ| _g | _t|d ƒ| _tdi |¤Ž}d}g }t|jƒD ]\}}|| jv r9| d||  ¡ |}| j |¡ qt	 
|¡|d< tƒ jdi |¤Ž d S )NZbetasg      ð?r   © )ÚsetÚuse_timestepsÚtimestep_mapÚlenÚoriginal_num_stepsr   Ú	enumerateZalphas_cumprodÚappendÚnpÚarrayÚsuperÚ__init__)Úselfr   ÚkwargsZbase_diffusionZlast_alpha_cumprodZ	new_betasÚiZalpha_cumprod©Ú	__class__r   úw/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/models/multi_modal/guided_diffusion/respace.pyr      s   

€zSpacedDiffusion.__init__c                    ó    t ƒ j|  |¡g|¢R i |¤ŽS ©N)r   Úp_mean_varianceÚ_wrap_model©r   ÚmodelÚargsr   r   r   r   r   $   ó   ÿÿÿzSpacedDiffusion.p_mean_variancec                    r   r   )r   Útraining_lossesr   r   r   r   r   r   (   r   zSpacedDiffusion.training_lossesc                    r   r   )r   Úcondition_meanr   ©r   Zcond_fnr   r   r   r   r   r    ,   r   zSpacedDiffusion.condition_meanc                    r   r   )r   Úcondition_scorer   r!   r   r   r   r"   0   r   zSpacedDiffusion.condition_scorec                 C   s"   t |tƒr|S t|| j| j| jƒS r   )Ú
isinstanceÚ_WrappedModelr   Úrescale_timestepsr
   )r   r   r   r   r   r   4   s
   
ÿzSpacedDiffusion._wrap_modelc                 C   s   |S r   r   )r   Útr   r   r   Ú_scale_timesteps:   s   z SpacedDiffusion._scale_timesteps)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r    r"   r   r'   Ú__classcell__r   r   r   r   r      s    r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )r$   c                 C   s   || _ || _|| _|| _d S r   )r   r   r%   r
   )r   r   r   r%   r
   r   r   r   r   A   s   
z_WrappedModel.__init__c                 K   sJ   t j| j|j|jd}|| }| jr| ¡ d| j  }| j||fi |¤ŽS )N)ÚdeviceÚdtypeg     @@)	ÚthZtensorr   r-   r.   r%   Úfloatr
   r   )r   ÚxÚtsr   Z
map_tensorZnew_tsr   r   r   Ú__call__H   s   ÿz_WrappedModel.__call__N)r(   r)   r*   r   r3   r   r   r   r   r$   ?   s    r$   )Únumpyr   Ztorchr/   Zgaussian_diffusionr   r   r$   r   r   r   r   Ú<module>   s
   4