CoolFace
Apppublic

declare-lab/tango2

sourceHugging Faceupdated 2y agoView on Hugging Face
92likes
unet_2d.cpython-39.pyc135 linesDownload Raw Back to __pycache__
1a

2��'d�8�@s�ddlmZddlmZmZmZddlZddlmZddl	m3Z4mZddlm
Z
ddlmZmZmZdd	lmZdd5lmZmZmZeGdd�de
��ZGd
d�dee6�ZdS)�)�	dataclass)�Optional�Tuple�UnionN�)�ConfigMixin�register_to_config)�7BaseOutput�)�GaussianFourierProjection�TimestepEmbedding�	Timesteps)�8ModelMixin)�UNetMidBlock2D�get_down_block�get_up_blockc@seZdZUdZejed<dS)�UNet2DOutputz�9    Args:10        sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):11            Hidden states output. Output of last layer of model.12    �sampleN)�__name__�13__module__�__qualname__�__doc__�torch�FloatTensor�__annotations__�rr�M/home/deep/Projects/audio_diffusion/diffusers/src/diffusers/models/unet_2d.pyrs14rcs�eZdZdZedeeeeeeffeee	e15ee	ee16ee17eeeeee18eeeee19e	ee20eed��fdd�
�Zde
jee
jeefee
je	eeefd�dd�Z�ZS)�UNet2DModelae21    UNet2DModel is a 2D UNet model that takes in a noisy sample and a timestep and returns sample shaped output.22 23    This model inherits from [`ModelMixin`]. Check the superclass documentation for the generic methods the library24    implements for all the model (such as downloading or saving, etc.)25 26    Parameters:27        sample_size (`int` or `Tuple[int, int]`, *optional*, defaults to `None`):28            Height and width of input/output sample.29        in_channels (`int`, *optional*, defaults to 3): Number of channels in the input image.30        out_channels (`int`, *optional*, defaults to 3): Number of channels in the output.31        center_input_sample (`bool`, *optional*, defaults to `False`): Whether to center the input sample.32        time_embedding_type (`str`, *optional*, defaults to `"positional"`): Type of time embedding to use.33        freq_shift (`int`, *optional*, defaults to 0): Frequency shift for fourier time embedding.34        flip_sin_to_cos (`bool`, *optional*, defaults to :35            obj:`True`): Whether to flip sin to cos for fourier time embedding.36        down_block_types (`Tuple[str]`, *optional*, defaults to :37            obj:`("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D")`): Tuple of downsample block38            types.39        mid_block_type (`str`, *optional*, defaults to `"UNetMidBlock2D"`):40            The mid block type. Choose from `UNetMidBlock2D` or `UnCLIPUNetMidBlock2D`.41        up_block_types (`Tuple[str]`, *optional*, defaults to :42            obj:`("AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "UpBlock2D")`): Tuple of upsample block types.43        block_out_channels (`Tuple[int]`, *optional*, defaults to :44            obj:`(224, 448, 672, 896)`): Tuple of block output channels.45        layers_per_block (`int`, *optional*, defaults to `2`): The number of layers per block.46        mid_block_scale_factor (`float`, *optional*, defaults to `1`): The scale factor for the mid block.47        downsample_padding (`int`, *optional*, defaults to `1`): The padding for the downsample convolution.48        act_fn (`str`, *optional*, defaults to `"silu"`): The activation function to use.49        attention_head_dim (`int`, *optional*, defaults to `8`): The attention head dimension.50        norm_num_groups (`int`, *optional*, defaults to `32`): The number of groups for the normalization.51        norm_eps (`float`, *optional*, defaults to `1e-5`): The epsilon for the normalization.52        resnet_time_scale_shift (`str`, *optional*, defaults to `"default"`): Time scale shift config53            for resnet blocks, see [`~models.resnet.ResnetBlock2D`]. Choose from `default` or `scale_shift`.54        class_embed_type (`str`, *optional*, defaults to None):55            The type of class embedding to use which is ultimately summed with the time embeddings. Choose from `None`,56            `"timestep"`, or `"identity"`.57        num_class_embeds (`int`, *optional*, defaults to None):58            Input dimension of the learnable embedding matrix to be projected to `time_embed_dim`, when performing59            class conditioning with `class_embed_type` equal to `None`.60    N�F�61positionalrT��DownBlock2D�AttnDownBlock2Dr"r"��
AttnUpBlock2Dr$r$�	UpBlock2D���i�i�i�rr62�silu�� ��h㈵��>�default)�sample_size�in_channels�out_channels�center_input_sample�time_embedding_type�63freq_shift�flip_sin_to_cos�down_block_types�up_block_types�block_out_channels�layers_per_block�mid_block_scale_factor�downsample_padding�act_fn�attention_head_dim�norm_num_groups�norm_eps�resnet_time_scale_shift�
add_attention�class_embed_type�num_class_embedsc#s�t���||_|64dd}t|�t|	�krBtd|�d|	�d���t|65�t|�krhtd|66�d|�d���tj||67ddd	d68�|_|dkr�t|69ddd
�|_	d|70d}n"|dkr�t71|72d||�|_	|73d}t||�|_|dur�|dur�t�
||�|_n8|dk�rt||�|_n |dk�r(t�||�|_nd|_t�g�|_d|_t�g�|_|74d}t|�D]T\}}|}|75|}|t|76�dk}t|||||||||||
|d�}|j�|��q\t|77d||||||||d�	|_tt|78��}|d}t|	�D]v\}}|} ||}|t|dt|79�d�}|t|80�dk}t||d||| |||||||d�}!|j�|!�|} �q�|du�rt|nt|81ddd�}"tj|82d|"|d�|_t��|_tj|83d|ddd84�|_ dS)Nr�z\Must provide the same number of `down_block_types` as `up_block_types`. `down_block_types`: z. `up_block_types`: �.zbMust provide the same number of `block_out_channels` as `down_block_types`. `block_out_channels`: z. `down_block_types`: r)r85r86)�kernel_size�padding�fourier�)�embedding_size�scalerr�timestep�identityr87)�88num_layersr.r/�
temb_channels�add_downsample�89resnet_eps�
resnet_act_fn�
resnet_groups�attn_num_head_channelsr9r>�����)	r.rMrOrP�output_scale_factorr>rRrQr?)rLr.r/�prev_output_channelrM�add_upsamplerOrPrQrRr>r*)�num_channels�90num_groups�eps)!�super�__init__r-�len�91ValueError�nn�Conv2d�conv_inr�	time_projr
r�time_embedding�	Embedding�class_embedding�Identity�92ModuleList�down_blocks�	mid_block�	up_blocks�	enumerater�appendr�list�reversed�minr�	GroupNorm�
conv_norm_out�SiLU�conv_act�conv_out)#�selfr-r.r/r0r1r2r3r4r5r6r7r8r9r:r;r<r=r>r?r@rA�time_embed_dim�timestep_input_dim�output_channel�i�down_block_type�
input_channel�is_final_block�93down_block�reversed_block_out_channels�
up_block_typerU�up_block�num_groups_out��	__class__rrr[Qs�94��9596��
� 97zUNet2DModel.__init__)rrJ�class_labels�return_dict�returncCsD|jjrd|d}|}t�|�s:tj|gtj|jd�}n(t�|�rbt|j�dkrb|d�	|j�}|tj98|jd|j|jd�}|�|�}|j	|jd�}|�
|�}|jdur�|dur�td��|jjdkr�|�|�}|�|�j	|jd�}||}|}	|�|�}|f}99|jD]@}t|d	��r.||||	d100�\}}}	n|||d�\}}|101|7}102�q|�||�}d}	|jD]Z}
|103t|
j�d�}|104dt|
j��}105t|
d	��r�|
||||	�\}}	n|
|||�}�q`|�|�}|�|�}|�|�}|	du�r�||	7}|jjdk�r.|�|jdgd
gt|jd
d���R�}||}|�s:|fSt|d�S)a106        Args:107            sample (`torch.FloatTensor`): (batch, channel, height, width) noisy inputs tensor108            timestep (`torch.FloatTensor` or `float` or `int): (batch) timesteps109            class_labels (`torch.FloatTensor`, *optional*, defaults to `None`):110                Optional class labels for conditioning. Their embeddings will be summed with the timestep embeddings.111            return_dict (`bool`, *optional*, defaults to `True`):112                Whether or not to return a [`~models.unet_2d.UNet2DOutput`] instead of a plain tuple.113 114        Returns:115            [`~models.unet_2d.UNet2DOutput`] or `tuple`: [`~models.unet_2d.UNet2DOutput`] if `return_dict` is True,116            otherwise a `tuple`. When returning a tuple, the first element is the sample tensor.117        rg�?)�dtype�devicerN)r�z=class_labels should be provided when doing class conditioningrJ�	skip_conv)�
hidden_states�temb�skip_sample)r�r�rFr118)r)�configr0r�	is_tensor�tensor�longr�r\�shape�to�onesr�rarbrdr]r@r`rg�hasattrrhri�resnetsrprrrsr1�reshaper)rtrrJr�r��	timesteps�t_emb�emb�	class_embr��down_block_res_samples�downsample_block�res_samples�upsample_blockrrr�forward�s^119120121122123124125�126127128129130,zUNet2DModel.forward)NrrFrrTr r#r&rr131r132r(r)r*r+r,TNN)NT)rrrrrrr�intr�bool�str�floatr[rr�Tensorrr��
__classcell__rrr�rr&sp*��
�133�r)�dataclassesr�typingrrrr�torch.nnr^�configuration_utilsrr�utilsr	�134embeddingsrrr
�modeling_utilsr�unet_2d_blocksrrrrrrrrr�<module>s135