o
    }oi	                     @   s8   d dl Z d dlmZ d dlmZ G dd de jjZdS )    N)AutoencoderKL)	rearrangec                       s0   e Zd ZdZ fddZe dd Z  ZS )AutoencoderKLVAEz
    A class that wraps the AutoencoderKL model and provides a decode method.

    Attributes:
        vae (AutoencoderKL): The underlying AutoencoderKL model loaded from a pretrained path.
    c                    s    t    tj|tjd| _dS )z
        Initialize the AutoencoderKLVAE instance.

        Args:
            path (str): The path to the pretrained AutoencoderKL model.
        )torch_dtypeN)super__init__r   from_pretrainedtorchbfloat16vae)selfpath	__class__ `/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/diffusion/vae/diffusers_vae.pyr      s   
zAutoencoderKLVAE.__init__c                 C   s^   |j \}}}}}|dkrt|d}|| jjj }| jj|ddd }|dkr-t|dddS |S )a  
        Decode a latent representation using the underlying VAE model.

        This method takes a latent tensor `x` and decodes it into an image.
        If `x` has a temporal dimension `T` of 1, it
        rearranges the tensor before and after decoding.

        Args:
            x (torch.Tensor): A tensor of shape (B, C, T, H, W), where:
                              B = batch size
                              C = number of channels
                              T = temporal dimension
                              H = height
                              W = width

        Returns:
            torch.Tensor: Decoded image tensor with the same shape as the input (B, C, T, H, W).
           zb c t h w -> (b t) c h wF)return_dictr   z(b t) c h w -> b c t h w)t)shaper   r   configscaling_factordecode)r   xBCTHWoutr   r   r   r   &   s   
zAutoencoderKLVAE.decode)	__name__
__module____qualname____doc__r   r	   no_gradr   __classcell__r   r   r   r   r      s
    
r   )r	   	diffusersr   einopsr   nnModuler   r   r   r   r   <module>   s   