CoolFace
Apppublic

declare-lab/tango2

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

2��'dE�@s�ddlmZddlmZmZmZddlZddlmZddl	m3Z4mZddlm
Z
ddlmZdd	lmZmZmZmZeGd5d�de
��ZGdd
�d
ee6�ZdS)�)�	dataclass)�Optional�Tuple�UnionN�)�ConfigMixin�register_to_config)�7BaseOutput�)�8ModelMixin)�Decoder�
DecoderOutput�Encoder�VectorQuantizerc@seZdZUdZejed<dS)�VQEncoderOutputz�9    Output of VQModel encoding method.10 11    Args:12        latents (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):13            Encoded output sample of the model. Output of the last layer of the model.14    �latentsN)�__name__�15__module__�__qualname__�__doc__�torch�FloatTensor�__annotations__�rr�N/home/deep/Projects/audio_diffusion/diffusers/src/diffusers/models/vq_model.pyrs16rcs�eZdZdZedeeeeeeeeeeeeeeeee	d�
�fd
d�
�Z17deje
ed�dd�Zdeje
e
eeejfd�dd�Zdeje
eeejfd�dd�Z�ZS)�VQModelaHVQ-VAE model from the paper Neural Discrete Representation Learning by Aaron van den Oord, Oriol Vinyals and Koray18    Kavukcuoglu.19 20    This model inherits from [`ModelMixin`]. Check the superclass documentation for the generic methods the library21    implements for all the model (such as downloading or saving, etc.)22 23    Parameters:24        in_channels (int, *optional*, defaults to 3): Number of channels in the input image.25        out_channels (int,  *optional*, defaults to 3): Number of channels in the output.26        down_block_types (`Tuple[str]`, *optional*, defaults to :27            obj:`("DownEncoderBlock2D",)`): Tuple of downsample block types.28        up_block_types (`Tuple[str]`, *optional*, defaults to :29            obj:`("UpDecoderBlock2D",)`): Tuple of upsample block types.30        block_out_channels (`Tuple[int]`, *optional*, defaults to :31            obj:`(64,)`): Tuple of block output channels.32        act_fn (`str`, *optional*, defaults to `"silu"`): The activation function to use.33        latent_channels (`int`, *optional*, defaults to `3`): Number of channels in the latent space.34        sample_size (`int`, *optional*, defaults to `32`): TODO35        num_vq_embeddings (`int`, *optional*, defaults to `256`): Number of codebook vectors in the VQ-VAE.36        vq_embed_dim (`int`, *optional*): Hidden dim of codebook vectors in the VQ-VAE.37        scaling_factor (`float`, *optional*, defaults to `0.18215`):38            The component-wise standard deviation of the trained latent space computed using the first batch of the39            training set. This is used to scale the latent space to have unit variance when training the diffusion40            model. The latents are scaled with the formula `z = z * scaling_factor` before being passed to the41            diffusion model. When decoding, the latents are scaled back to the original scale with the formula: `z = 142            / scaling_factor * z`. For more details, refer to sections 4.3.2 and D.1 of the [High-Resolution Image43            Synthesis with Latent Diffusion Models](https://arxiv.org/abs/2112.10752) paper.44    ���DownEncoderBlock2D��UpDecoderBlock2D��@r45�silu� �N���{�P�?)
�in_channels�out_channels�down_block_types�up_block_types�block_out_channels�layers_per_block�act_fn�latent_channels�sample_size�num_vq_embeddings�norm_num_groups�vq_embed_dim�scaling_factorc46s�t���t|||||||dd�|_|dur0|n|}t�||d�|_t|47|dddd�|_t�||d�|_	t48|||||||d�|_dS)NF)r'r(r)r+r,r-r1�double_zr49g�?)�beta�remap�sane_index_shape)r'r(r*r+r,r-r1)�super�__init__r�encoder�nn�Conv2d�50quant_convr�quantize�post_quant_convr�decoder)�selfr'r(r)r*r+r,r-r.r/r0r1r2r3��	__class__rrr9Es051��zVQModel.__init__T)�x�return_dict�returncCs(|�|�}|�|�}|s|fSt|d�S)N)r)r:r=r)rArDrE�hrrr�encodeus525354zVQModel.encodeF)rG�force_not_quantizerErFcCsB|s|�|�\}}}n|}|�|�}|�|�}|s8|fSt|d�S)N��sample)r>r?r@r
)rArGrIrE�quantZemb_loss�info�decrrr�decode~s5556zVQModel.decode)rKrErFcCs0|}|�|�j}|�|�j}|s&|fSt|d�S)z�57        Args:58            sample (`torch.FloatTensor`): Input sample.59            return_dict (`bool`, *optional*, defaults to `True`):60                Whether or not to return a [`DecoderOutput`] instead of a plain tuple.61        rJ)rHrrOrKr
)rArKrErDrGrNrrr�forward�szVQModel.forward)
rrrrr!r62r#rr$r%r$Nr&)T)FT)T)rrrrr�intr�strr�floatr9rr�boolrrHrr
rOrP�
__classcell__rrrBrr'sJ��/63��r)�dataclassesr�typingrrrr�torch.nnr;�configuration_utilsrr�utilsr	�modeling_utilsr�vaerr
rrrrrrrr�<module>s