File size: 2,318 Bytes
b386992
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Copyright (c) 2025, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import torch
from diffusers import AutoencoderKL
from einops import rearrange


class AutoencoderKLVAE(torch.nn.Module):
    """
    A class that wraps the AutoencoderKL model and provides a decode method.

    Attributes:
        vae (AutoencoderKL): The underlying AutoencoderKL model loaded from a pretrained path.
    """

    def __init__(self, path):
        """
        Initialize the AutoencoderKLVAE instance.

        Args:
            path (str): The path to the pretrained AutoencoderKL model.
        """
        super().__init__()
        self.vae = AutoencoderKL.from_pretrained(path, torch_dtype=torch.bfloat16)

    @torch.no_grad()
    def decode(self, x):
        """
        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).
        """

        B, C, T, H, W = x.shape
        if T == 1:
            x = rearrange(x, 'b c t h w -> (b t) c h w')
        x = x / self.vae.config.scaling_factor
        out = self.vae.decode(x, return_dict=False)[0]
        if T == 1:
            return rearrange(out, '(b t) c h w -> b c t h w', t=1)
        return out