CoolFace
Apppublic

declare-lab/tango2

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

2��'d�)�@s�ddlmZddlmZmZmZddlZddlmZddl	m3Z4mZddlm
Z
ddlmZmZmZdd	lmZdd5lmZmZmZmZeGdd�de
��ZGd
d�dee6�ZdS)�)�	dataclass)�Optional�Tuple�UnionN�)�ConfigMixin�register_to_config)�7BaseOutput�)�GaussianFourierProjection�TimestepEmbedding�	Timesteps)�8ModelMixin)�get_down_block�
get_mid_block�
get_out_block�get_up_blockc@seZdZUdZejed<dS)�UNet1DOutputz�9    Args:10        sample (`torch.FloatTensor` of shape `(batch_size, num_channels, sample_size)`):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_1d.pyrs14rcs�eZdZdZedeeeeeeeeee	e15ee16ee17eee18eeeeed��fdd�
�Zdej
eeje	efeeee19fd�dd�Z�ZS)�UNet1DModela'	20    UNet1DModel is a 1D UNet model that takes in a noisy sample and a timestep and returns sample shaped output.21 22    This model inherits from [`ModelMixin`]. Check the superclass documentation for the generic methods the library23    implements for all the model (such as downloading or saving, etc.)24 25    Parameters:26        sample_size (`int`, *optional*): Default length of sample. Should be adaptable at runtime.27        in_channels (`int`, *optional*, defaults to 2): Number of channels in the input sample.28        out_channels (`int`, *optional*, defaults to 2): Number of channels in the output.29        extra_in_channels (`int`, *optional*, defaults to 0):30            Number of additional channels to be added to the input of the first down block. Useful for cases where the31            input data has more channels than what the model is initially designed for.32        time_embedding_type (`str`, *optional*, defaults to `"fourier"`): Type of time embedding to use.33        freq_shift (`float`, *optional*, defaults to 0.0): Frequency shift for fourier time embedding.34        flip_sin_to_cos (`bool`, *optional*, defaults to :35            obj:`False`): Whether to flip sin to cos for fourier time embedding.36        down_block_types (`Tuple[str]`, *optional*, defaults to :37            obj:`("DownBlock1D", "DownBlock1DNoSkip", "AttnDownBlock1D")`): Tuple of downsample block types.38        up_block_types (`Tuple[str]`, *optional*, defaults to :39            obj:`("UpBlock1D", "UpBlock1DNoSkip", "AttnUpBlock1D")`): Tuple of upsample block types.40        block_out_channels (`Tuple[int]`, *optional*, defaults to :41            obj:`(32, 32, 64)`): Tuple of block output channels.42        mid_block_type (`str`, *optional*, defaults to "UNetMidBlock1D"): block type for middle of UNet.43        out_block_type (`str`, *optional*, defaults to `None`): optional output processing of UNet.44        act_fn (`str`, *optional*, defaults to None): optional activation function in UNet blocks.45        norm_num_groups (`int`, *optional*, defaults to 8): group norm member count in UNet blocks.46        layers_per_block (`int`, *optional*, defaults to 1): added number of layers in a UNet block.47        downsample_each_block (`int`, *optional*, defaults to False:48            experimental feature for using a UNet without upsampling.49    �Nrr�fourierTF��ZDownBlock1DNoSkipZDownBlock1DZAttnDownBlock1D�Z
AttnUpBlock1DZ	UpBlock1DZUpBlock1DNoSkip�UNetMidBlock1D�� r&�@�r50)�sample_size�sample_rate�in_channels�out_channels�extra_in_channels�time_embedding_type�flip_sin_to_cos�use_timestep_embedding�51freq_shift�down_block_types�up_block_types�mid_block_type�out_block_type�block_out_channels�act_fn�norm_num_groups�layers_per_block�downsample_each_blockc!	s t���||_|dkr8tddd|d�|_d|d}n$|dkr\t|d||	d�|_|d}|r�|dd	}t||||dd52�|_t�	g�|_53d|_t�	g�|_d|_
|}t|54�D]^\}}|}||}|dkr�||7}|t|�dk}t|||||d|p�|d�}|j55�|�q�t||d
|d
|d
|d||d�|_tt|��}|d}|
du�r^|}n|d}t|�D]j\}}|}|t|�dk�r�||dn|}|t|�dk}t|||||d|d�}|j�|�|}�qn|du�r�|nt|dd	d�} t|
| |d|||d
d	d�|_
dS)Nr r(F)�embedding_size�set_W_to_weight�logr/rr�56positional)r/�downscale_freq_shift�)r+�time_embed_dimr7�out_dimr57)�58num_layersr+r,�
temb_channels�add_downsample�����)r+Zmid_channelsr,�	embed_dimrCrE)rCr+r,rD�add_upsampler&)r5�num_groups_outrGr,r7Zfc_dim)�super�__init__r)r�	time_projr
r�time_mlp�nn�59ModuleList�down_blocks�	mid_block�	up_blocks�	out_block�	enumerate�lenr�appendr�list�reversedr�minr)!�selfr)r*r+r,r-r.r/r0r1r2r3r4r5r6r7r8r9r:�timestep_input_dimrA�output_channel�i�down_block_type�
input_channel�is_final_block�60down_block�reversed_block_out_channelsZfinal_upsample_channels�
up_block_type�prev_output_channel�up_blockrI��	__class__rrrKHs�61�62����63 �� 64�zUNet1DModel.__init__)r�timestep�return_dict�returncCsR|}t�|�s&tj|gtj|jd�}n(t�|�rNt|j�dkrN|d�|j�}|�|�}|j	j65rl|�|�}nH|d}|�dd|jdg��|j
�}|�|jdd�|jdd��}d}|jD]}|||d�\}}||7}q�|jr�|�||�}t|j�D].\}	}66|d	d�}|dd	�}|67|||d68�}q�|j�r<|�||�}|�sH|fSt|d�S)a_69        Args:70            sample (`torch.FloatTensor`): `(batch_size, num_channels, sample_size)` noisy inputs tensor71            timestep (`torch.FloatTensor` or `float` or `int): (batch) timesteps72            return_dict (`bool`, *optional*, defaults to `True`):73                Whether or not to return a [`~models.unet_1d.UNet1DOutput`] instead of a plain tuple.74 75        Returns:76            [`~models.unet_1d.UNet1DOutput`] or `tuple`: [`~models.unet_1d.UNet1DOutput`] if `return_dict` is True,77            otherwise a `tuple`. When returning a tuple, the first element is the sample tensor.78        )�dtype�devicerN).Nr79rr)�
hidden_states�tembrF)�res_hidden_states_tuplern)r)r�	is_tensor�tensor�longrlrU�shape�torL�configr0rM�repeatrk�broadcast_torPrQrTrRrSr)rZrrhri�	timestepsZtimestep_embed�down_block_res_samples�downsample_block�res_samplesr]�upsample_blockrrr�forward�s48081"8283zUNet1DModel.forward)rNrrrr TFr!r"r#r$Nr%Nr(r84F)T)rrrrr�intr�str�bool�floatrrKrrr�Tensorrr}�
__classcell__rrrfrr's^ ��|�85�r)�dataclassesr�typingrrrr�torch.nnrN�configuration_utilsrr�utilsr	�86embeddingsrrr
�modeling_utilsrZunet_1d_blocksrrrrrrrrrr�<module>s87