o
    9i                     @   sz   d dl mZmZmZmZmZ d dlZd dlmZ ddl	m
Z
 ddlmZ ddlmZmZ ddlmZ G d	d
 d
ejZdS )    )DictListOptionalTupleUnionN   )LPIPS)GeneralConditioner)append_dimsinstantiate_from_config   )Denoiserc                       s   e Zd Z			ddededededeeeee f  f
 fd	d
Z	de
jde
jde
jde
jfddZdejdedede
jdede
jfddZdejdedede
jdedee
jef fddZdd Z  ZS )StandardDiffusionLossl2        Nsigma_sampler_configloss_weighting_config	loss_typeoffset_noise_levelbatch2model_keysc                    sp   t    |dv sJ t|| _t|| _|| _|| _|dkr%t  | _	|s)g }t
|tr1|g}t|| _d S )N)r   l1lpipsr   )super__init__r   sigma_samplerloss_weightingr   r   r   evalr   
isinstancestrsetr   )selfr   r   r   r   r   	__class__ K/data/cameron/vidgen/generative-models/sgm/modules/diffusionmodules/loss.pyr      s   



zStandardDiffusionLoss.__init__	sigmas_bcnoiseinputreturnc                 C   s   |||  }|S Nr#   )r    r%   r&   r'   noised_inputr#   r#   r$   get_noised_input*   s   z&StandardDiffusionLoss.get_noised_inputnetworkdenoiserconditionerbatchc                 C   s   ||}|  |||||S r)   )_forward)r    r,   r-   r.   r'   r/   condr#   r#   r$   forward0   s   zStandardDiffusionLoss.forwardr1   c                    s    fdd| j  D }| |jd |}t|}| jdkrM| jd ur2|jd d|jd fn	|jd |jd f}	|| jt	tj
|	|jd|j  }t	||j}
| |
||}|||||fi |}t	| ||j}| |||S )Nc                    s   i | ]}| | qS r#   r#   ).0keyr/   r#   r$   
<dictcomp>C   s    
z2StandardDiffusionLoss._forward.<locals>.<dictcomp>r   r   r      )device)r   intersectionr   shapetotorch
randn_liker   n_framesr
   randnr8   ndimr+   r   get_loss)r    r,   r-   r1   r'   r/   additional_model_inputssigmasr&   offset_shaper%   r*   model_outputwr#   r5   r$   r0   ;   s.   




zStandardDiffusionLoss._forwardc                 C   s   | j dkrt||| d  |jd ddS | j dkr0t|||   |jd ddS | j dkr@| ||d}|S td| j  )	Nr   r7   r   r   r   r   zUnknown loss type )r   r<   meanreshaper:   absr   NotImplementedError)r    rE   targetrF   lossr#   r#   r$   rA   \   s   


zStandardDiffusionLoss.get_loss)r   r   N)__name__
__module____qualname__dictr   floatr   r   r   r   r<   Tensorr+   nnModuler   r	   r   r2   r   r0   rA   __classcell__r#   r#   r!   r$   r      sf    


!r   )typingr   r   r   r   r   r<   torch.nnrT   %modules.autoencoding.lpips.loss.lpipsr   modules.encoders.modulesr	   utilr
   r   r-   r   rU   r   r#   r#   r#   r$   <module>   s    