File size: 5,408 Bytes
b910c09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import torch
import torch.nn as nn

from einops import repeat
from functools import partial

from .dit import DiTBlock, DiT, FinalLayer


COMPILE = True
if torch.cuda.is_available():
    compile_fn = partial(
        torch.compile, fullgraph=True, backend="inductor" if torch.cuda.get_device_capability()[0] >= 7 else "aot_eager"
    )
else:
    compile_fn = lambda f: f


# ===================================================================================================


def pf_modulate(x, shift, scale):
    return x * (1 + scale) + shift


class PatchForcingDiTBlock(DiTBlock):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if COMPILE:
            self.forward = compile_fn(self.forward)

    def forward(self, x, c):
        shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.adaLN_modulation(c).chunk(6, dim=-1)
        x = x + gate_msa * self.attn(pf_modulate(self.norm1(x), shift_msa, scale_msa))
        x = x + gate_mlp * self.mlp(pf_modulate(self.norm2(x), shift_mlp, scale_mlp))
        return x


class PatchForcingFinalLayer(FinalLayer):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if COMPILE:
            self.forward = compile_fn(self.forward)

    def forward(self, x, c):
        shift, scale = self.adaLN_modulation(c).chunk(2, dim=-1)
        x = pf_modulate(self.norm_final(x), shift, scale)
        x = self.linear(x)
        return x


class PatchForcingDiT(DiT):
    def __init__(
        self,
        *args,
        patch_size=2,
        hidden_size=1152,
        depth=28,
        num_heads=16,
        mlp_ratio: float = 4.0,
        predict_uncertainty: bool = True,
        compile: bool = False,
        **kwargs,
    ):
        super().__init__(
            *args, patch_size=patch_size, hidden_size=hidden_size, depth=depth, num_heads=num_heads, **kwargs
        )
        global COMPILE
        COMPILE = compile

        # predict uncertainty per patch (replace dit blocks and last layer)
        self.predict_uncertainty = predict_uncertainty
        if self.predict_uncertainty:
            assert self.learn_sigma is False, "cannot use both learn_sigma and predict_uncertainty!"
            assert self.return_sigma is False, "cannot use both return_sigma and predict_uncertainty!"

            # replace DiT blocks
            self.blocks = nn.ModuleList(
                [PatchForcingDiTBlock(hidden_size, num_heads, mlp_ratio=mlp_ratio) for _ in range(depth)]
            )

            # replace final layer
            self.out_channels = self.out_channels + 1
            self.final_layer = PatchForcingFinalLayer(hidden_size, patch_size, self.out_channels)

            self.initialize_weights()

    def forward(self, x, t, y=None, return_uncertainty: bool = False):
        """
        Forward pass of DiT.
        x: (N, C, H, W) tensor of spatial inputs (images or latent representations of images)
        t: (N, num_patches) tensor of diffusion timesteps
        y: (N,) tensor of class labels
        """
        x = self.x_embedder(x) + self.pos_embed  # (N, T, D), where T = H * W / patch_size ** 2

        # patch-level t's
        if self.predict_uncertainty:
            assert x.shape[1] == t.shape[1], f"x: {x.shape}, t: {t.shape}: require patch-level t's!"
            t = t[..., None]  # (N, T) -> (N, T, 1)
            t = self.t_embedder(t)  # (N, 1, T, D)
            t = t.squeeze(1)  # (N, T, D) one embedding per patch
        else:
            t = self.t_embedder(t)  # (N, D)

        cond = t
        if self.y_embedder is not None:
            y = self.y_embedder(y, self.training)  # (N, D)
            if self.predict_uncertainty:
                y = repeat(y, "b c -> b n c", n=x.shape[1])  # (N, D) -> (N, T, D)
            cond = cond + y  # (N, T, D)

        for block in self.blocks:
            x = block(x, cond)  # (N, T, D)
        x = self.final_layer(x, cond)  # (N, T, patch_size ** 2 * out_channels)
        x = self.unpatchify(x)  # (N, out_channels, H, W)

        # split uncertainty
        if self.predict_uncertainty:
            logvar_theta = x[:, -1:, :, :]  # (b, 1, h, w)
            x = x[:, :-1, :, :]  # (b, c, h, w)
            if return_uncertainty:
                return x, logvar_theta

        if self.learn_sigma and not self.return_sigma:  # LEGACY
            x, _ = x.chunk(2, dim=1)
        return x


# ===================================================================================================


def PF_XL_2(**kwargs):
    return PatchForcingDiT(depth=28, hidden_size=1152, patch_size=2, num_heads=16, **kwargs)


def PF_L_2(**kwargs):
    return PatchForcingDiT(depth=24, hidden_size=1024, patch_size=2, num_heads=16, **kwargs)


def PF_B_2(**kwargs):
    return PatchForcingDiT(depth=12, hidden_size=768, patch_size=2, num_heads=12, **kwargs)


PF_models = {
    "PF-XL/2": PF_XL_2,
    "PF-L/2": PF_L_2,
    "PF-B/2": PF_B_2,
}


if __name__ == "__main__":
    DEV = "cuda" if torch.cuda.is_available() else "cpu"
    model = PF_models["PF-XL/2"]().to(DEV)
    print(f"{sum([p.numel() for p in model.parameters() if p.requires_grad]):,}")

    inp = dict(
        x=torch.randn((2, 4, 32, 32)).to(DEV),
        t=torch.rand((2,)).to(DEV),
        y=torch.randint(0, 1000, (2,)).to(DEV),
    )
    with torch.no_grad():
        out = model(**inp)
    print(out.shape)