File size: 12,869 Bytes
e505e1f
 
 
 
 
 
 
 
 
 
 
3b2d4dc
e505e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b2d4dc
 
 
 
 
e505e1f
 
 
3b2d4dc
e505e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b2d4dc
 
 
 
 
 
 
 
e505e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b2d4dc
e505e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b2d4dc
e505e1f
3b2d4dc
e505e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b2d4dc
e505e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b2d4dc
e505e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b2d4dc
e505e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b2d4dc
e505e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""Self-contained AFM generator for Hugging Face diffusers checkpoints."""

from __future__ import annotations

import math
from typing import Any, Dict, Optional

import torch
import torch.nn.functional as F
from diffusers.configuration_utils import ConfigMixin, register_to_config
from diffusers.models.modeling_utils import ModelMixin
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
from torch import nn


def build_dit_config(
    *,
    depth: int = 28,
    hidden_size: int = 1152,
    patch_size: int = 2,
    num_heads: int = 16,
    learn_sigma: bool = False,
    class_dropout_prob: float = 0.0,
    input_size: int = 32,
    num_classes: int = 1000,
    in_channels: int = 4,
) -> Dict[str, Any]:
    attention_head_dim = hidden_size // num_heads
    num_embeds_ada_norm = num_classes + (1 if class_dropout_prob > 0 else 0)
    out_channels = in_channels * 2 if learn_sigma else in_channels
    return {
        "sample_size": input_size,
        "num_layers": depth,
        "num_attention_heads": num_heads,
        "attention_head_dim": attention_head_dim,
        "in_channels": in_channels,
        "out_channels": out_channels,
        "patch_size": patch_size,
        "attention_bias": True,
        "activation_fn": "gelu-approximate",
        "num_embeds_ada_norm": num_embeds_ada_norm,
        "norm_type": "ada_norm_zero",
        "norm_elementwise_affine": False,
        "dropout": 0.0,
        "norm_num_groups": 32,
        "norm_eps": 1e-5,
        "upcast_attention": False,
    }


_PREFIX_KEYS = ("transformer.", "t_tgt_embedder.")


def _wrap_dit_state_dict(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
    return {k if k.startswith(_PREFIX_KEYS) else f"transformer.{k}": v for k, v in state_dict.items()}


def forward_dit_output(
    transformer: DiTTransformer2DModel,
    hidden_states: torch.Tensor,
    timestep: Optional[torch.Tensor],
    class_labels: torch.Tensor,
    extra_cond: Optional[torch.Tensor] = None,
) -> torch.Tensor:
    hidden_states = transformer.pos_embed(hidden_states)

    for block in transformer.transformer_blocks:
        if torch.is_grad_enabled() and transformer.gradient_checkpointing:
            hidden_states = transformer._gradient_checkpointing_func(
                block,
                hidden_states,
                None,
                None,
                None,
                timestep,
                None,
                class_labels,
            )
        else:
            hidden_states = block(
                hidden_states,
                attention_mask=None,
                encoder_hidden_states=None,
                encoder_attention_mask=None,
                timestep=timestep,
                cross_attention_kwargs=None,
                class_labels=class_labels,
            )

    conditioning = transformer.transformer_blocks[0].norm1.emb(
        timestep, class_labels, hidden_dtype=hidden_states.dtype
    )
    if extra_cond is not None:
        conditioning = conditioning + extra_cond
    shift, scale = transformer.proj_out_1(F.silu(conditioning)).chunk(2, dim=1)
    hidden_states = transformer.norm_out(hidden_states) * (1 + scale[:, None]) + shift[:, None]
    hidden_states = transformer.proj_out_2(hidden_states)

    height = width = int(hidden_states.shape[1] ** 0.5)
    hidden_states = hidden_states.reshape(
        shape=(-1, height, width, transformer.patch_size, transformer.patch_size, transformer.out_channels)
    )
    hidden_states = torch.einsum("nhwpqc->nchpwq", hidden_states)
    return hidden_states.reshape(
        shape=(-1, transformer.out_channels, height * transformer.patch_size, width * transformer.patch_size)
    )


def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
    if output.shape[1] == in_channels:
        return output
    output, _ = output.chunk(2, dim=1)
    return output


class TimestepEmbedder(nn.Module):
    def __init__(self, hidden_size: int, frequency_embedding_size: int = 256):
        super().__init__()
        self.mlp = nn.Sequential(
            nn.Linear(frequency_embedding_size, hidden_size, bias=True),
            nn.SiLU(),
            nn.Linear(hidden_size, hidden_size, bias=True),
        )
        self.frequency_embedding_size = frequency_embedding_size

    @staticmethod
    def timestep_embedding(t: torch.Tensor, dim: int, max_period: int = 10000) -> torch.Tensor:
        half = dim // 2
        freqs = torch.exp(-math.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half).to(
            device=t.device
        )
        args = t[:, None].float() * freqs[None]
        embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1)
        if dim % 2:
            embedding = torch.cat([embedding, torch.zeros_like(embedding[:, :1])], dim=-1)
        return embedding

    def forward(self, t: torch.Tensor) -> torch.Tensor:
        emb = self.timestep_embedding(t.float(), self.frequency_embedding_size)
        weight_dtype = self.mlp[0].weight.dtype
        return self.mlp(emb.to(dtype=weight_dtype))


class _AFMCheckpointMixin(ModelMixin):
    def _fix_state_dict_keys_on_load(self, state_dict):
        wrapped = _wrap_dit_state_dict(state_dict)
        state_dict.clear()
        state_dict.update(wrapped)
        super()._fix_state_dict_keys_on_load(state_dict)


class Generator(nn.Module):
    def __init__(
        self,
        *,
        use_t_src: bool = False,
        use_t_tgt: bool = False,
        depth: int = 28,
        hidden_size: int = 1152,
        patch_size: int = 2,
        num_heads: int = 16,
        learn_sigma: bool = False,
        class_dropout_prob: float = 0.0,
        input_size: int = 32,
        num_classes: int = 1000,
        in_channels: int = 4,
        **_,
    ):
        super().__init__()
        self.use_t_src = use_t_src
        self.use_t_tgt = use_t_tgt
        self.transformer = DiTTransformer2DModel(
            **build_dit_config(
                depth=depth,
                hidden_size=hidden_size,
                patch_size=patch_size,
                num_heads=num_heads,
                learn_sigma=learn_sigma,
                class_dropout_prob=class_dropout_prob,
                input_size=input_size,
                num_classes=num_classes,
                in_channels=in_channels,
            )
        )
        self.in_channels = in_channels
        if use_t_tgt:
            self.t_tgt_embedder = TimestepEmbedder(hidden_size)
            nn.init.normal_(self.t_tgt_embedder.mlp[0].weight, std=0.02)
            nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)

    def load_state_dict(self, state_dict, strict: bool = True):
        if self.use_t_tgt and not any(key.startswith("t_tgt_embedder.") for key in state_dict):
            strict = False
        return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)

    def forward(self, x, y, t_src=None, t_tgt=None):
        if self.use_t_src and t_src is not None:
            timestep = (t_src * 1000).long()
        else:
            timestep = torch.zeros(x.shape[0], device=x.device, dtype=torch.long)

        extra_cond = None
        if self.use_t_tgt and t_tgt is not None:
            extra_cond = self.t_tgt_embedder(t_tgt * 1000)

        output = forward_dit_output(self.transformer, x, timestep, y, extra_cond=extra_cond)
        return split_output_channels(output, self.in_channels)


class GeneratorDeep(nn.Module):
    def __init__(
        self,
        *,
        repeat: int,
        depth: int = 28,
        hidden_size: int = 1152,
        patch_size: int = 2,
        num_heads: int = 16,
        learn_sigma: bool = False,
        class_dropout_prob: float = 0.0,
        input_size: int = 32,
        num_classes: int = 1000,
        in_channels: int = 4,
        **_,
    ):
        super().__init__()
        self.repeat = repeat
        self.transformer = DiTTransformer2DModel(
            **build_dit_config(
                depth=depth,
                hidden_size=hidden_size,
                patch_size=patch_size,
                num_heads=num_heads,
                learn_sigma=learn_sigma,
                class_dropout_prob=class_dropout_prob,
                input_size=input_size,
                num_classes=num_classes,
                in_channels=in_channels,
            )
        )
        self.in_channels = in_channels

    def load_state_dict(self, state_dict, strict: bool = True):
        return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)

    def forward(self, x, y, *args, **kwargs):
        hidden_states = self.transformer.pos_embed(x)
        for t in torch.arange(1, 0, -1 / self.repeat).tolist():
            timestep = torch.full([x.shape[0]], int(t * 1000), device=x.device, dtype=torch.long)
            for block in self.transformer.transformer_blocks:
                hidden_states = block(
                    hidden_states,
                    attention_mask=None,
                    encoder_hidden_states=None,
                    encoder_attention_mask=None,
                    timestep=timestep,
                    cross_attention_kwargs=None,
                    class_labels=y,
                )

        conditioning = self.transformer.transformer_blocks[0].norm1.emb(
            torch.zeros(x.shape[0], device=x.device, dtype=torch.long),
            y,
            hidden_dtype=hidden_states.dtype,
        )
        shift, scale = self.transformer.proj_out_1(F.silu(conditioning)).chunk(2, dim=1)
        hidden_states = self.transformer.norm_out(hidden_states) * (1 + scale[:, None]) + shift[:, None]
        hidden_states = self.transformer.proj_out_2(hidden_states)

        height = width = int(hidden_states.shape[1] ** 0.5)
        hidden_states = hidden_states.reshape(
            shape=(
                -1,
                height,
                width,
                self.transformer.patch_size,
                self.transformer.patch_size,
                self.transformer.out_channels,
            )
        )
        hidden_states = torch.einsum("nhwpqc->nchpwq", hidden_states)
        output = hidden_states.reshape(
            shape=(-1, self.transformer.out_channels, height * self.transformer.patch_size, width * self.transformer.patch_size)
        )
        return split_output_channels(output, self.in_channels)


class AFMGenerator2DModel(_AFMCheckpointMixin, Generator, ConfigMixin):
    config_name = "config.json"

    @register_to_config
    def __init__(
        self,
        model_type: str = "AFM-XL/2",
        architecture: str = "standard",
        repeat: int = 1,
        use_t_src: bool = False,
        use_t_tgt: bool = False,
        depth: int = 28,
        hidden_size: int = 1152,
        patch_size: int = 2,
        num_heads: int = 16,
        learn_sigma: bool = False,
        class_dropout_prob: float = 0.0,
        input_size: int = 32,
        num_classes: int = 1000,
        in_channels: int = 4,
        pred_type: str = "x",
        num_inference_steps: int = 1,
    ) -> None:
        if architecture == "deep":
            raise ValueError("Use AFMGeneratorDeep2DModel for deep AFM checkpoints.")
        Generator.__init__(
            self,
            use_t_src=use_t_src,
            use_t_tgt=use_t_tgt,
            depth=depth,
            hidden_size=hidden_size,
            patch_size=patch_size,
            num_heads=num_heads,
            learn_sigma=learn_sigma,
            class_dropout_prob=class_dropout_prob,
            input_size=input_size,
            num_classes=num_classes,
            in_channels=in_channels,
        )


class AFMGeneratorDeep2DModel(_AFMCheckpointMixin, GeneratorDeep, ConfigMixin):
    config_name = "config.json"

    @register_to_config
    def __init__(
        self,
        model_type: str = "AFM-XL/2",
        architecture: str = "deep",
        repeat: int = 2,
        use_t_src: bool = False,
        use_t_tgt: bool = False,
        depth: int = 28,
        hidden_size: int = 1152,
        patch_size: int = 2,
        num_heads: int = 16,
        learn_sigma: bool = False,
        class_dropout_prob: float = 0.0,
        input_size: int = 32,
        num_classes: int = 1000,
        in_channels: int = 4,
        pred_type: str = "x",
        num_inference_steps: int = 1,
    ) -> None:
        GeneratorDeep.__init__(
            self,
            repeat=repeat,
            depth=depth,
            hidden_size=hidden_size,
            patch_size=patch_size,
            num_heads=num_heads,
            learn_sigma=learn_sigma,
            class_dropout_prob=class_dropout_prob,
            input_size=input_size,
            num_classes=num_classes,
            in_channels=in_channels,
        )


__all__ = ["AFMGenerator2DModel", "AFMGeneratorDeep2DModel"]