CoolFace
Apppublic

declare-lab/tango2

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

2��'d9�@s�ddlmZddlmZmZmZddlZddlmZddl	m3Z4mZddlm
Z
mZddlmZdd	lmZmZmZmZeGd5d�de
��ZGdd
�d
ee6�ZdS)�)�	dataclass)�Optional�Tuple�UnionN�)�ConfigMixin�register_to_config)�7BaseOutput�apply_forward_hook�)�8ModelMixin)�Decoder�
DecoderOutput�DiagonalGaussianDistribution�Encoderc@seZdZUdZded<dS)�AutoencoderKLOutputa@9    Output of AutoencoderKL encoding method.10 11    Args:12        latent_dist (`DiagonalGaussianDistribution`):13            Encoded outputs of `Encoder` represented as the mean and logvar of `DiagonalGaussianDistribution`.14            `DiagonalGaussianDistribution` allows for sampling latents from the distribution.15    r�latent_distN)�__name__�16__module__�__qualname__�__doc__�__annotations__�rr�T/home/deep/Projects/audio_diffusion/diffusers/src/diffusers/models/autoencoder_kl.pyrs17	rcsPeZdZdZdZed/eeeeeeeeeeeeee	d��fd
d�
�Z18d0dd�Zd1ed�dd�Z
dd�Zdd�Zdd�Zed2ejeed�dd��Zd3ejeeeejfd�dd �Zed4ejeeeejfd�d!d"��Zd#d$�Zd%d&�Zd5ejeed�d'd(�Zd6ejeeeejfd�d)d*�Zd7ejeeeejeeejfd,�d-d.�Z �Z!S)8�
AutoencoderKLa�Variational Autoencoder (VAE) model with KL loss from the paper Auto-Encoding Variational Bayes by Diederik P. Kingma19    and Max Welling.20 21    This model inherits from [`ModelMixin`]. Check the superclass documentation for the generic methods the library22    implements for all the model (such as downloading or saving, etc.)23 24    Parameters:25        in_channels (int, *optional*, defaults to 3): Number of channels in the input image.26        out_channels (int,  *optional*, defaults to 3): Number of channels in the output.27        down_block_types (`Tuple[str]`, *optional*, defaults to :28            obj:`("DownEncoderBlock2D",)`): Tuple of downsample block types.29        up_block_types (`Tuple[str]`, *optional*, defaults to :30            obj:`("UpDecoderBlock2D",)`): Tuple of upsample block types.31        block_out_channels (`Tuple[int]`, *optional*, defaults to :32            obj:`(64,)`): Tuple of block output channels.33        act_fn (`str`, *optional*, defaults to `"silu"`): The activation function to use.34        latent_channels (`int`, *optional*, defaults to 4): Number of channels in the latent space.35        sample_size (`int`, *optional*, defaults to `32`): TODO36        scaling_factor (`float`, *optional*, defaults to 0.18215):37            The component-wise standard deviation of the trained latent space computed using the first batch of the38            training set. This is used to scale the latent space to have unit variance when training the diffusion39            model. The latents are scaled with the formula `z = z * scaling_factor` before being passed to the40            diffusion model. When decoding, the latents are scaled back to the original scale with the formula: `z = 141            / scaling_factor * z`. For more details, refer to sections 4.3.2 and D.1 of the [High-Resolution Image42            Synthesis with Latent Diffusion Models](https://arxiv.org/abs/2112.10752) paper.43    T��ZDownEncoderBlock2D�ZUpDecoderBlock2D��@r�silu�� ���{�P�?)�in_channels�out_channels�down_block_types�up_block_types�block_out_channels�layers_per_block�act_fn�latent_channels�norm_num_groups�sample_size�scaling_factorc44s�t���t|||||||	dd�|_t||||||	|d�|_t�d|d|d�|_t�||d�|_	d|_45d|_|jj
|_t|jj
ttf�r�|jj
dn|jj
}46t|47dt|j�d�|_d|_dS)	NT)r$r%r&r(r)r*r,Zdouble_z)r$r%r'r(r)r,r*rrFrg�?)�super�__init__r�encoderr
�decoder�nn�Conv2d�48quant_conv�post_quant_conv�use_slicing�49use_tiling�configr-�tile_sample_min_size�50isinstance�list�tuple�int�lenr(�tile_latent_min_size�tile_overlap_factor)�selfr$r%r&r'r(r)r*r+r,r-r.��	__class__rrr0Fs>51��5253��zAutoencoderKL.__init__FcCst|ttf�r||_dS)N)r;rr
Zgradient_checkpointing)rB�module�valuerrr�_set_gradient_checkpointing~sz)AutoencoderKL._set_gradient_checkpointing�r8cCs54||_dS)a55        Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to56        compute decoding and encoding in several steps. This is useful to save a large amount of memory and to allow57        the processing of larger images.58        NrH)rBr8rrr�
enable_tiling�szAutoencoderKL.enable_tilingcCs|�d�dS)z�59        Disable tiled VAE decoding. If `enable_vae_tiling` was previously invoked, this method will go back to60        computing decoding in one step.61        FN)rI�rBrrr�disable_tiling�szAutoencoderKL.disable_tilingcCs62d|_dS)z�63        Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to64        compute decoding in several steps. This is useful to save some memory and allow larger batch sizes.65        TN�r7rJrrr�enable_slicing�szAutoencoderKL.enable_slicingcCs66d|_dS)z�67        Disable sliced VAE decoding. If `enable_slicing` was previously invoked, this method will go back to computing68        decoding in one step.69        FNrLrJrrr�disable_slicing�szAutoencoderKL.disable_slicing)�x�return_dict�returncCsd|jr4|jd|jks&|jd|jkr4|j||d�S|�|�}|�|�}t|�}|sZ|fSt|d�S)N�����������rP�r)r8�shaper:�tiled_encoder1r5rr)rBrOrP�h�moments�	posteriorrrr�encode�s&7071zAutoencoderKL.encode)�zrPrQcCs\|jr4|jd|jks&|jd|jkr4|j||d�S|�|�}|�|�}|sR|fSt|d�S)NrRrSrT��sample)r8rVr@�tiled_decoder6r2r)rBr\rP�decrrr�_decode�s&7273zAutoencoderKL._decodecsX�jr8|jddkr8�fdd�|�d�D�}t�|�}n��|�j}|sN|fSt|d�S)Nrrcsg|]}��|�j�qSr)rar^)�.0Zz_slicerJrr�74<listcomp>��z(AutoencoderKL.decode.<locals>.<listcomp>r])r7rV�split�torch�catrar^r)rBr\rPZdecoded_slices�decodedrrJr�decode�szAutoencoderKL.decodecCs�tt|jd|jd|��D]n}|dd�dd�||dd�fd|||dd�dd�|dd�f|||dd�dd�|dd�f<q|S)Nrr��range�minrV)rB�a�b�blend_extent�yrrr�blend_v�s lzAutoencoderKL.blend_vcCs�tt|jd|jd|��D]n}|dd�dd�dd�||fd|||dd�dd�dd�|f|||dd�dd�dd�|f<q|S)Nrrrj)rBrmrnrorOrrr�blend_h�s lzAutoencoderKL.blend_hc75Cs�t|jd|j�}t|j|j�}|j|}g}td|jd|�D]t}g}td|jd|�D]N}	|dd�dd�|||j�|	|	|j�f}76|�|77�}78|�|79�}80|�|81�q^|�|�qDg}t	|�D]�\}}g}t	|�D]r\}	}82|dk�r|�83||d|	|84|�}85|	dk�r&|�||	d|86|�}87|�|88dd�dd�d|�d|�f�q�|�tj
|dd��q�tj
|dd�}
t|
�}|�s�|fSt|d�S)a0Encode a batch of images using a tiled encoder.89 90        Args:91        When this option is enabled, the VAE will split the input tensor into tiles to compute encoding in several92        steps. This is useful to keep memory use constant regardless of image size. The end result of tiled encoding is:93        different from non-tiled encoding due to each tile using a different encoder. To avoid tiling artifacts, the94        tiles overlap and are blended together to form a smooth output. You may still see tile-sized changes in the95        look of the output, but they should be much less noticeable.96            x (`torch.FloatTensor`): Input batch of images. return_dict (`bool`, *optional*, defaults to `True`):97                Whether or not to return a [`AutoencoderKLOutput`] instead of a plain tuple.98        rrrrN��dimrU)r>r:rAr@rkrVr1r5�append�	enumeraterqrrrfrgrr)rBrOrP�overlap_sizero�	row_limit�rows�i�row�j�tile�result_rows�99result_rowrYrZrrrrW�s6100,101102103104(zAutoencoderKL.tiled_encodec105Cs�t|jd|j�}t|j|j�}|j|}g}td|jd|�D]t}g}td|jd|�D]N}	|dd�dd�|||j�|	|	|j�f}106|�|107�}108|�|109�}|�|�q^|�|�qDg}t	|�D]�\}}g}
t	|�D]r\}	}110|dk�r|�111||d|	|112|�}113|	dk�r&|�||	d|114|�}115|
�|116dd�dd�d|�d|�f�q�|�tj
|
dd��q�tj
|dd�}|�s~|fSt|d�S)a>Decode a batch of images using a tiled decoder.117 118        Args:119        When this option is enabled, the VAE will split the input tensor into tiles to compute decoding in several120        steps. This is useful to keep memory use constant regardless of image size. The end result of tiled decoding is:121        different from non-tiled decoding due to each tile using a different decoder. To avoid tiling artifacts, the122        tiles overlap and are blended together to form a smooth output. You may still see tile-sized changes in the123        look of the output, but they should be much less noticeable.124            z (`torch.FloatTensor`): Input batch of latent vectors. return_dict (`bool`, *optional*, defaults to125            `True`):126                Whether or not to return a [`DecoderOutput`] instead of a plain tuple.127        rrrrNrsr])r>r@rAr:rkrVr6r2rurvrqrrrfrgr)rBr\rPrwrorxryrzr{r|r}rhr~rr`rrrr_�s4
128,129130131132(zAutoencoderKL.tiled_decodeN)r^�sample_posteriorrP�	generatorrQc	CsJ|}|�|�j}|r"|j|d�}n|��}|�|�j}|s@|fSt|d�S)af133        Args:134            sample (`torch.FloatTensor`): Input sample.135            sample_posterior (`bool`, *optional*, defaults to `False`):136                Whether to sample from the posterior.137            return_dict (`bool`, *optional*, defaults to `True`):138                Whether or not to return a [`DecoderOutput`] instead of a plain tuple.139        )r�r])r[rr^�moderir)	rBr^r�rPr�rOrZr\r`rrr�forward.szAutoencoderKL.forward)rrrrrrr r!r"r"r#)F)T)T)T)T)T)T)FTN)"rrrrZ _supports_gradient_checkpointingrr>r�str�floatr0rG�boolrIrKrMrNr140rf�FloatTensorrr[rrrarirqrrrWr_r�	Generatorr��
__classcell__rrrCrr(sf��7141
 "/ 2��r)�dataclassesr�typingrrrrf�torch.nnr3�configuration_utilsrr�utilsr	r142�modeling_utilsrZvaer
rrrrrrrrr�<module>s