a
    
d                      @   s>  d dl Z d dlmZ d dlmZ d dlmZ d dlZd dl	Z
d dlmZ d dlm  mZ d dlmZ ddlmZ ddlmZmZmZmZmZmZmZ dd	lmZmZ d
d Zdd Z G dd dej!Z"G dd dej!Z#G dd dej$e#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#Z)G dd dej!Z*dd 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(d) d)e/Z0G d*d+ d+ej!Z1e2d,kr:G d-d. d.ej!Z3e/d/d0d1d1d2d1d3gd3g d4d0d5d/d6d5d74 Z5e
6d8d1d0d04 Z7e
j8d d9d:d;d<Z9e5e7e9Z:e;d= dS )>    N)abstractmethod)partial)Iterable)	rearrange   )SpatialTransformer)avg_pool_nd
checkpointconv_ndlinearnormalizationtimestep_embeddingzero_module)defaultexistsc                 C   s   d S N xr   r   o/var/www/html/stable-diffusion-webui/repositories/generative-models/sgm/modules/diffusionmodules/openaimodel.pyconvert_module_to_f16   s    r   c                 C   s   d S r   r   r   r   r   r   convert_module_to_f32   s    r   c                       s6   e Zd ZdZdeeeed fddZdd Z  ZS )	AttentionPool2dzS
    Adapted from CLIP: https://github.com/openai/CLIP/blob/main/clip/model.py
    N)spacial_dim	embed_dimnum_heads_channels
output_dimc                    sp   t    tt||d d |d  | _td|d| d| _td||pN|d| _	|| | _
t| j
| _d S )N      g      ?r   )super__init__nn	Parameterthrandnpositional_embeddingr
   qkv_projc_proj	num_headsQKVAttention	attention)selfr   r   r   r   	__class__r   r   r    (   s    

zAttentionPool2d.__init__c                 C   s   |j ^}}}|||d}tj|jddd|gdd}|| jd d d d d f |j }| |}| 	|}| 
|}|d d d d df S )NT)dimkeepdimr/   r   )shapereshaper#   catmeanr%   todtyper&   r*   r'   )r+   r   bcZ_spatialr   r   r   forward8   s    $


zAttentionPool2d.forward)N)__name__
__module____qualname____doc__intr    r:   __classcell__r   r   r,   r   r   #   s   	 r   c                   @   s   e Zd ZdZedd ZdS )TimestepBlockzT
    Any module where forward() takes timestep embeddings as a second argument.
    c                 C   s   dS )zJ
        Apply the module to `x` given `emb` timestep embeddings.
        Nr   r+   r   embr   r   r   r:   H   s    zTimestepBlock.forwardN)r;   r<   r=   r>   r   r:   r   r   r   r   rA   C   s   rA   c                   @   s   e Zd ZdZdddZdS )TimestepEmbedSequentialzt
    A sequential module that passes timestep embeddings to the children that
    support it as an extra input.
    NFc	           
      C   sB   | D ]8}	t |	tr|	||}qt |	tr4|	||}q|	|}q|S r   )
isinstancerA   r   )
r+   r   rC   contextZskip_time_mixZtime_contextZnum_video_framesZtime_context_catZ*use_crossframe_attention_in_spatial_layerslayerr   r   r   r:   U   s    


zTimestepEmbedSequential.forward)NFNNNF)r;   r<   r=   r>   r:   r   r   r   r   rD   O   s   	      rD   c                       s*   e Zd ZdZd
 fdd	Zdd	 Z  ZS )UpsampleaA  
    An upsampling layer with an optional convolution.
    :param channels: channels in the inputs and outputs.
    :param use_conv: a bool determining if a convolution is applied.
    :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then
                 upsampling occurs in the inner-two dimensions.
    r   Nr   Fc                    sL   t    || _|p|| _|| _|| _|| _|rHt|| j| jd|d| _d S )Nr   padding)	r   r    channelsout_channelsuse_convdimsthird_upr
   conv)r+   rK   rM   rN   rL   rJ   rO   r,   r   r   r    s   s    

zUpsample.__init__c                 C   s   |j d | jksJ | jdkrb| js(dnd}tj|||j d  |j d d |j d d fdd}ntj|ddd}| jr| |}|S )Nr   r   r      nearest)mode)scale_factorrS   )r2   rK   rN   rO   FinterpolaterM   rP   )r+   r   Zt_factorr   r   r   r:      s    
&
zUpsample.forward)r   Nr   Fr;   r<   r=   r>   r    r:   r@   r   r   r,   r   rH   j   s   	 rH   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )	TransposedUpsamplez%Learned 2x upsampling without paddingN   c                    s6   t    || _|p|| _tj| j| j|dd| _d S )Nr   kernel_sizestride)r   r    rK   rL   r!   ConvTranspose2dup)r+   rK   rL   ksr,   r   r   r       s    

zTransposedUpsample.__init__c                 C   s
   |  |S r   )r^   r+   r   r   r   r   r:      s    zTransposedUpsample.forward)NrY   rW   r   r   r,   r   rX      s   	rX   c                       s*   e Zd ZdZd
 fdd	Zdd	 Z  ZS )
DownsampleaD  
    A downsampling layer with an optional convolution.
    :param channels: channels in the inputs and outputs.
    :param use_conv: a bool determining if a convolution is applied.
    :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then
                 downsampling occurs in the inner-two dimensions.
    r   Nr   Fc              	      s   t    || _|p|| _|| _|| _|dkr2dn
|s:dnd}|rtd| d td| j d| j d	| d
|  |dkrtd|  t|| j| jd||d| _n | j| jksJ t	|||d| _d S )Nr   r   )r   r   r   )r   r   r   z!Building a Downsample layer with z dims.z  --> settings are: 
 in-chn: z, out-chn: z, kernel-size: 3, stride: z, padding: z&  --> Downsampling third axis (time): )r\   rJ   rZ   )
r   r    rK   rL   rM   rN   printr
   opr   )r+   rK   rM   rN   rL   rJ   Z
third_downr\   r,   r   r   r       s6    


	zDownsample.__init__c                 C   s   |j d | jksJ | |S )Nr   )r2   rK   rc   r`   r   r   r   r:      s    zDownsample.forward)r   Nr   FrW   r   r   r,   r   ra      s   	 ra   c                
       s2   e Zd ZdZd fdd	Zdd	 Zd
d Z  ZS )ResBlocka  
    A residual block that can optionally change the number of channels.
    :param channels: the number of input channels.
    :param emb_channels: the number of timestep embedding channels.
    :param dropout: the rate of dropout.
    :param out_channels: if specified, the number of out channels.
    :param use_conv: if True and out_channels is specified, use a spatial
        convolution instead of a smaller 1x1 convolution to change the
        channels in the skip connection.
    :param dims: determines if the signal is 1D, 2D, or 3D.
    :param use_checkpoint: if True, use gradient checkpointing on this module.
    :param up: if True, use this block for upsampling.
    :param down: if True, use this block for downsampling.
    NFr   r   c                    s  t    || _|| _|| _|p"|| _|| _|| _|| _|| _	t
|trXdd |D }n|d }tt|t t||| j||d| _|	p|
| _|	rt|d|| _t|d|| _n2|
rt|d|| _t|d|| _nt  | _| _|| _|rd| j n| j| _| jr6td| jj  | jr(J d | _d| _	ntt t|| j| _tt| jt tj|dt t|| j| j||d| _!| j|krt | _"n0|rt||| j||d| _"nt||| jd| _"d S )	Nc                 S   s   g | ]}|d  qS )r   r   ).0kr   r   r   
<listcomp>       z%ResBlock.__init__.<locals>.<listcomp>r   rI   FzSkipping timestep embedding in )pr   )#r   r    rK   emb_channelsdropoutrL   rM   use_checkpointuse_scale_shift_normexchange_temb_dimsrE   r   r!   
Sequentialr   SiLUr
   	in_layersupdownrH   h_updx_updra   Identity
skip_t_embZemb_out_channelsrb   r-   r;   
emb_layersr   Dropoutr   
out_layersskip_connection)r+   rK   rj   rk   rL   rM   rm   rN   rl   r^   downr[   rn   rv   rJ   r,   r   r   r       sz    





zResBlock.__init__c                 C   s   t | j||f|  | jS )a  
        Apply the block to a Tensor, conditioned on a timestep embedding.
        :param x: an [N x C x ...] Tensor of features.
        :param emb: an [N x emb_channels] Tensor of timestep embeddings.
        :return: an [N x C x ...] Tensor of outputs.
        )r	   _forward
parametersrl   rB   r   r   r   r:   6  s    zResBlock.forwardc                 C   s  | j rF| jd d | jd  }}||}| |}| |}||}n
| |}| jrbt|}n| ||j	}t
|jt
|jk r|d }qt| jr| jd | jdd   }}tj|ddd\}	}
||d|	  |
 }||}n"| jrt|d}|| }| |}| || S )Nr.   ).Nr   r   r   r1   zb t c ... -> b c t ...)rr   rq   rs   rt   rv   r#   
zeros_likerw   typer7   lenr2   rm   ry   chunkrn   r   rz   )r+   r   rC   Zin_restZin_convhemb_outout_normZout_restscaleshiftr   r   r   r|   A  s,    







zResBlock._forward)
NFFr   FFFr   FFr;   r<   r=   r>   r    r:   r|   r@   r   r   r,   r   rd      s             Zrd   c                       s2   e Zd ZdZd fdd	Zdd Zd	d
 Z  ZS )AttentionBlocka  
    An attention block that allows spatial positions to attend to each other.
    Originally ported from here, but adapted to the N-d case.
    https://github.com/hojonathanho/diffusion/blob/1e0dceb3b3495bbe19116a5e1b3596cd0706c543/diffusion_tf/models/unet.py#L66.
    r   r.   Fc                    s   t    || _|dkr || _n*|| dks@J d| d| || | _|| _t|| _td||d d| _|rt	| j| _
nt| j| _
ttd||d| _d S )Nr.   r   zq,k,v channels z' is not divisible by num_head_channels r   r   )r   r    rK   r(   rl   r   normr
   qkvr)   r*   QKVAttentionLegacyr   proj_out)r+   rK   r(   num_head_channelsrl   use_new_attention_orderr,   r   r   r    e  s     



zAttentionBlock.__init__c                 K   s   t | j|f|  dS )NT)r	   r|   r}   )r+   r   kwargsr   r   r   r:     s    zAttentionBlock.forwardc                 C   sV   |j ^}}}|||d}| | |}| |}| |}|| j||g|R  S )Nr.   )r2   r3   r   r   r*   r   )r+   r   r8   r9   spatialr   r   r   r   r   r|     s    

zAttentionBlock._forward)r   r.   FFr   r   r   r,   r   r   ^  s   	    r   c                 C   sL   |d j ^}}}tt|}d| |d  | }|  jt|g7  _dS )a(  
    A counter for the `thop` package to count the operations in an
    attention operation.
    Meant to be used like:
        macs, params = thop.profile(
            model,
            inputs=(inputs, timestamps),
            custom_ops={QKVAttention: QKVAttention.count_flops},
        )
    r   r   N)r2   r?   npprodZ	total_opsr#   DoubleTensor)model_xyr8   r9   r   Znum_spatialZ
matmul_opsr   r   r   count_flops_attn  s    r   c                       s4   e Zd ZdZ fddZdd Zedd Z  ZS )r   zh
    A module which performs QKV attention. Matches legacy QKVAttention + input/ouput heads shaping
    c                    s   t    || _d S r   r   r    n_headsr+   r   r,   r   r   r      s    
zQKVAttentionLegacy.__init__c                 C   s   |j \}}}|d| j  dks"J |d| j  }||| j |d |j|dd\}}}dtt| }	td||	 ||	 }
tj|
	 dd
|
j}
td|
|}||d|S )z
        Apply QKV attention.
        :param qkv: an [N x (H * 3 * C) x T] tensor of Qs, Ks, and Vs.
        :return: an [N x (H * C) x T] tensor after attention.
        r   r   r   r1   bct,bcs->btsr.   bts,bcs->bct)r2   r   r3   splitmathsqrtr#   einsumsoftmaxfloatr   r7   r+   r   bswidthlengthchqrf   vr   weightar   r   r   r:     s    (zQKVAttentionLegacy.forwardc                 C   s   t | ||S r   r   r   r   r   r   r   r   count_flops  s    zQKVAttentionLegacy.count_flops	r;   r<   r=   r>   r    r:   staticmethodr   r@   r   r   r,   r   r     s
   r   c                       s4   e Zd ZdZ fddZdd Zedd Z  ZS )r)   zP
    A module which performs QKV attention and splits in a different order.
    c                    s   t    || _d S r   r   r   r,   r   r   r      s    
zQKVAttention.__init__c              	   C   s   |j \}}}|d| j  dks"J |d| j  }|jddd\}}}dtt| }	td||	 || j ||||	 || j ||}
tj|
	 dd
|
j}
td|
||| j ||}||d|S )z
        Apply QKV attention.
        :param qkv: an [N x (3 * H * C) x T] tensor of Qs, Ks, and Vs.
        :return: an [N x (H * C) x T] tensor after attention.
        r   r   r   r1   r   r.   r   )r2   r   r   r   r   r#   r   viewr   r   r   r7   r3   r   r   r   r   r:     s    zQKVAttention.forwardc                 C   s   t | ||S r   r   r   r   r   r   r     s    zQKVAttention.count_flopsr   r   r   r,   r   r)     s
   r)   c                       s$   e Zd Z fddZdd Z  ZS )Timestepc                    s   t    || _d S r   )r   r    r/   )r+   r/   r,   r   r   r      s    
zTimestep.__init__c                 C   s   t || jS r   )r   r/   )r+   tr   r   r   r:     s    zTimestep.forward)r;   r<   r=   r    r:   r@   r   r   r,   r   r     s   r   c                       s<   e Zd ZdZd fdd	Zdd Zdd ZdddZ  ZS )	UNetModela  
    The full UNet model with attention and timestep embedding.
    :param in_channels: channels in the input Tensor.
    :param model_channels: base channel count for the model.
    :param out_channels: channels in the output Tensor.
    :param num_res_blocks: number of residual blocks per downsample.
    :param attention_resolutions: a collection of downsample rates at which
        attention will take place. May be a set, list, or tuple.
        For example, if this contains 4, then at 4x downsampling, attention
        will be used.
    :param dropout: the dropout probability.
    :param channel_mult: channel multiplier for each level of the UNet.
    :param conv_resample: if True, use learned convolutions for upsampling and
        downsampling.
    :param dims: determines if the signal is 1D, 2D, or 3D.
    :param num_classes: if specified (as an int), then this model will be
        class-conditional with `num_classes` classes.
    :param use_checkpoint: use gradient checkpointing to reduce memory usage.
    :param num_heads: the number of attention heads in each attention layer.
    :param num_heads_channels: if specified, ignore num_heads and instead use
                               a fixed channel width per attention head.
    :param num_heads_upsample: works with num_heads to set a different number
                               of heads for upsampling. Deprecated.
    :param use_scale_shift_norm: use a FiLM-like conditioning mechanism.
    :param resblock_updown: use residual blocks for up/downsampling.
    :param use_new_attention_order: use a different attention pattern for potentially
                                    increased efficiency.
    r   r   r   rQ      Tr   NFr.   r   r   c!           0         s  t    ddlm}! |r*|d us*J d|d urR|s>J dt||!krRt|}|dkr^|}|dkrv|dksvJ d|dkr|dksJ d|_|_|_t	|t
rt||g }nt	||!rt|}t| |d } t	|t
rt||g _n t|t|krtd|_|d ur6t|t|ks6J  d urt tjksXJ tt fdd	tt s~J td
  d| d |_|_|_|_|
_|_|rtd |_|_|_|d u_||ks|s|rJ d_jrtt|dndd	 }"|d }#|"t !t"||#t # t"|#|#_$jd urt	jt
rtt %|
|#_&njdkrtd t 'd|#_&njdkr|"t !t(|t !t"||#t # t"|#|#_&nHjdkr|d usJ t !t !t"||#t # t"|#|#_&nt t )t*t+|	||dddg_,|_-|g}$|}%d}&t.|D ]\}'}(tj|' D ]})|"t/|%|#||(| |	||dg}*|(| }%|&|v rV|dkr|%| }+n|%| }|}+|r|r|%| n|}+t0|r||' },nd},t0 r|) |' k rV|*1|s2|"t2|%|||+|dn |"t3|%||+||' ||,|||d	 j,1t*|*   j-|%7  _-|$1|% qr|'t|d krZ|%}-j,1t*|r|"t/|%|#||-|	||ddnt4|%||	|-d |-}%|$1|% |&d9 }& j-|%7  _-qZ|dkr|%| }+n|%| }|}+|r6|r2|%| n|}+t*|"t/|%|#||	||d |sj|"t2|%|||+|dn|"t3|%||+| |||||d	|"t/|%|#||	||d _5 j-|%7  _-t )g _6tt.|d d d D ]\}'}(tj|' d D ]h}.|$7 }/|"t/|%|/ |#|||( |	||dg}*||( }%|&|v r|dkrF|%| }+n|%| }|}+|rj|rf|%| n|}+t0|r~||' },nd},t0 r|. |' k r|*1|s|"t2|%|||+|dn |"t3|%||+||' ||,|||d	 |'r8|.j|' kr8|%}-|*1|r|"t/|%|#||-|	||dd!nt8|%||	|-d |&d }&j61t*|*   j-|%7  _-qq|"t !t9|%t # t:t+|	||ddd_;jr|"t !t9|%t+|	||d_<d S )"Nr   )
ListConfigzRFool!! You forgot to include the dimension of your cross-attention conditioning...zYFool!! You forgot to use the spatial transformer for your cross-attention conditioning...r.   z3Either num_heads or num_head_channels has to be setzprovide num_res_blocks either as an int (globally constant) or as a list/tuple (per-level) with the same length as channel_multc                    s   j |   |  kS r   )num_res_blocks)inum_attention_blocksr+   r   r   <lambda>j  rh   z$UNetModel.__init__.<locals>.<lambda>z7Constructor of UNetModel received num_attention_blocks=z;. This option has LESS priority than attention_resolutions zz, i.e., in cases where num_attention_blocks[i] > 0 but 2**i not in attention_resolutions, attention will still not be set.z8WARNING: use_fp16 was dropped and has no effect anymore.F)offload_to_cpuc                 S   s   | S r   r   r   r   r   r   r     rh   rQ   
continuousz'setting up linear c_adm embedding layerr   timestep
sequentialr   rI   rL   rN   rl   rm   rl   r(   r   r   )depthcontext_dimdisable_self_attn
use_linear	attn_typerl   TrL   rN   rl   rm   r{   rN   rL   r   rN   rl   rm   )rL   rN   rl   rm   r^   )=r   r    Zomegaconf.listconfigr   r   listin_channelsmodel_channelsrL   rE   r?   r   r   r   
ValueErrorallmaprangerb   attention_resolutionsrk   channel_multconv_resamplenum_classesrl   r(   r   num_heads_upsamplepredict_codebook_idsuse_fairscale_checkpointr   checkpoint_wrapperr!   ro   r   rp   
time_embed	Embedding	label_embLinearr   
ModuleListrD   r
   input_blocks_feature_size	enumeraterd   r   appendr   r   ra   middle_blockoutput_blockspoprH   r   r   outZid_predictor)0r+   r   r   rL   r   r   rk   r   r   rN   r   rl   use_fp16r(   r   r   rm   resblock_updownr   use_spatial_transformertransformer_depthr   Zn_embedlegacyZdisable_self_attentionsr   Zdisable_middle_self_attnuse_linear_in_transformerZspatial_transformer_attn_typeZadm_in_channelsr   r   Ztransformer_depth_middler   checkpoint_wrapper_fntime_embed_diminput_block_chansr   dslevelmultnrlayersdim_headZdisabled_saout_chr   Zichr,   r   r   r      s   #






















- 







zUNetModel.__init__c                 C   s(   | j t | jt | jt dS z<
        Convert the torso of the model to float16.
        N)r   applyr   r   r   r+   r   r   r   convert_to_fp16  s    zUNetModel.convert_to_fp16c                 C   s(   | j t | jt | jt dS z<
        Convert the torso of the model to float32.
        N)r   r   r   r   r   r   r   r   r   convert_to_fp32  s    zUNetModel.convert_to_fp32c                 K   s   |du| j duksJ dg }t|| jdd}| |}| j durh|jd |jd ksZJ || | }|}	| jD ]}
|
|	||}	||	 qr| |	||}	| j	D ]&}
t
j|	| gdd}	|
|	||}	q|	|j}	| jrdsJ dn
| |	S dS )	aQ  
        Apply the model to an input batch.
        :param x: an [N x C x ...] Tensor of inputs.
        :param timesteps: a 1-D batch of timesteps.
        :param context: conditioning plugged in via crossattn
        :param y: an [N] Tensor of labels, if class-conditional.
        :return: an [N x C x ...] Tensor of outputs.
        Nz<must specify y if and only if the model is class-conditionalF)repeat_onlyr   r   r1   z3not supported anymore. what the f*** are you doing?)r   r   r   r   r2   r   r   r   r   r   r#   r4   r   r   r7   r   r   )r+   r   	timestepsrF   r   r   hsZt_embrC   r   moduler   r   r   r:     s.    	



zUNetModel.forward)r   r   Tr   NFFr.   r.   r.   FFFFr   NNTNNFFr   NFFN)NNN	r;   r<   r=   r>   r    r   r   r:   r@   r   r   r,   r   r     sF   $                              .r   c                       s   e Zd Zd fdd	Z  ZS )NoTimeUNetModelNc                    s$   t |}t j||||fi |S r   )r#   r~   r   r:   )r+   r   r   rF   r   r   r,   r   r   r:     s    
zNoTimeUNetModel.forward)NNN)r;   r<   r=   r:   r@   r   r   r,   r   r    s   r  c                       s:   e Zd ZdZd fd
d	Zdd Zdd Zdd Z  ZS )EncoderUNetModelz]
    The half UNet model with attention and timestep embedding.
    For usage, see UNet.
    r   r   Tr   Fr   r.   adaptivec                    s  t    |dkr|}|| _|| _|| _|| _|| _|| _|| _|	| _	|| _
|rVtjntj| _|| _|| _|| _|d }tt||t t||| _ttt|
||dddg| _|| _|g}|}d}t|D ]\}}t|D ]r}t||||| |
||dg}|| }||v r.|t |||||d | jt|  |  j|7  _|| q|t!|d kr|}| jt|rt|||||
||dd	nt"||	|
|d
 |}|| |d9 }|  j|7  _qtt||||
||dt |||||dt||||
||d| _#|  j|7  _|| _$|dkr\tt%|t t&dt't|
||dt( | _)n|dkr|dkstJ tt%|t t*|| |||| _)n~|dkrtt+| jdt, t+d| j| _)nJ|dkr
tt+| jdt%dt t+d| j| _)nt-d| dd S )Nr.   rQ   r   r   rI   r   r   Tr   r   r   r   r  )r   r   r*   r   i   Z
spatial_v2zUnexpected z pooling).r   r    r   r   rL   r   r   rk   r   r   rl   r#   float16float32r7   r(   r   r   r!   ro   r   rp   r   r   rD   r
   r   r   r   r   rd   r   r   r   ra   r   poolr   AdaptiveAvgPool2dr   Flattenr   r   r   ReLUNotImplementedError)r+   
image_sizer   r   rL   r   r   rk   r   r   rN   rl   r   r(   r   r   rm   r   r   r  argsr   r   r   r   r   r   r   _r   r   r,   r   r   r      s   

	




zEncoderUNetModel.__init__c                 C   s   | j t | jt dS r   )r   r   r   r   r   r   r   r   r     s    z EncoderUNetModel.convert_to_fp16c                 C   s   | j t | jt dS r   )r   r   r   r   r   r   r   r   r     s    z EncoderUNetModel.convert_to_fp32c                 C   s   |  t|| j}g }|}| jD ]4}|||}| jdr |||jj	dd q | 
||}| jdr|||jj	dd tj|dd}| |S ||j}| |S dS )z
        Apply the model to an input batch.
        :param x: an [N x C x ...] Tensor of inputs.
        :param timesteps: a 1-D batch of timesteps.
        :return: an [N x K] Tensor of outputs.
        r   )r   r   r1   r.   )axisN)r   r   r   r   r  
startswithr   r   r7   r5   r   r#   r4   r   )r+   r   r   rC   resultsr   r   r   r   r   r:     s    


zEncoderUNetModel.forward)r   r   Tr   FFr   r.   r.   FFFr  r  r   r   r,   r   r    s&                 .r  __main__c                       s   e Zd Zd fdd	Z  ZS )Dummyr   @   c              
      s.   t    tttd||dddg| _d S )Nr   r   r   rI   )r   r    r!   r   rD   r
   r   )r+   r   r   r,   r   r   r      s    
zDummy.__init__)r   r  )r;   r<   r=   r    r@   r   r   r,   r   r    s   r  Tr  rQ      r   )r   r   rQ   Fr   )rl   r  r   rL   r   r   r   r   r   r   r   r   r      
   )r  cuda)lowhighsizedevicezdone.)<r   abcr   	functoolsr   typingr   numpyr   torchr#   torch.nnr!   Ztorch.nn.functional
functionalrU   einopsr   Zmodules.attentionr   modules.diffusionmodules.utilr   r	   r
   r   r   r   r   utilr   r   r   r   Moduler   rA   ro   rD   rH   rX   ra   rd   r   r   r   r)   r   r   r  r  r;   r  r  r   r$   r   randintr   orb   r   r   r   r   <module>   sn   $	 '+ 4 "	     \


