File size: 9,489 Bytes
0c9bb32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7294a64
0c9bb32
 
 
 
 
 
 
 
 
 
 
 
 
 
7294a64
0c9bb32
 
7294a64
0c9bb32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7294a64
0c9bb32
 
 
 
 
 
7294a64
0c9bb32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7294a64
0c9bb32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7294a64
0c9bb32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7294a64
0c9bb32
 
 
 
 
 
 
 
7294a64
0c9bb32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import math
from dataclasses import dataclass

import jax
import jax.numpy as jnp
import numpy as np
from flax import nnx


@dataclass
class DiTConfig:
    input_dim: int
    hidden_dim: int
    num_blocks: int
    num_heads: int
    patch_size: int
    patch_stride: int
    time_freq_dim: int
    time_max_period: int
    mlp_ratio: int
    use_bias: bool
    padding: str
    pos_embed_cls_token: bool
    pos_embed_extra_tokens: int


def get_2d_sincos_pos_embed(embed_dim, grid_size, cls_token=False, extra_tokens=0):
    """
    grid_size: int of the grid height and width
    return:
    pos_embed: [grid_size*grid_size, embed_dim] or [1+grid_size*grid_size, embed_dim] (w/ or w/o cls_token)
    """
    grid_h = jnp.arange(grid_size, dtype=jnp.float32)
    grid_w = jnp.arange(grid_size, dtype=jnp.float32)
    grid = jnp.meshgrid(grid_w, grid_h)  # here w goes first
    grid = jnp.stack(grid, axis=0)

    grid = grid.reshape([2, 1, grid_size, grid_size])
    pos_embed = get_2d_sincos_pos_embed_from_grid(embed_dim, grid)
    if cls_token and extra_tokens > 0:
        pos_embed = np.concatenate(
            [np.zeros([extra_tokens, embed_dim]), pos_embed], axis=0
        )
    return pos_embed


def get_2d_sincos_pos_embed_from_grid(embed_dim, grid):
    assert embed_dim % 2 == 0

    # use half of dimensions to encode grid_h
    emb_h = get_1d_sincos_pos_embed_from_grid(embed_dim // 2, grid[0])  # (H*W, D/2)
    emb_w = get_1d_sincos_pos_embed_from_grid(embed_dim // 2, grid[1])  # (H*W, D/2)

    emb = jnp.concatenate([emb_h, emb_w], axis=1)  # (H*W, D)
    return emb


def get_1d_sincos_pos_embed_from_grid(embed_dim, pos):
    """
    embed_dim: output dimension for each position
    pos: a list of positions to be encoded: size (M,)
    out: (M, D)
    """
    assert embed_dim % 2 == 0
    omega = jnp.arange(embed_dim // 2, dtype=np.float32)
    omega /= embed_dim / 2.0
    omega = 1.0 / 16**omega  # (D/2,)

    pos = pos.reshape(-1)  # (M,)
    out = jnp.einsum("m,d->md", pos, omega)  # (M, D/2), outer product

    emb_sin = jnp.sin(out)  # (M, D/2)
    emb_cos = jnp.cos(out)  # (M, D/2)

    emb = jnp.concatenate([emb_sin, emb_cos], axis=1)  # (M, D)
    return emb


class PatchEmbedding(nnx.Module):
    """Patch embedding module."""

    def __init__(self, config: DiTConfig, *, rngs: nnx.Rngs):
        super().__init__()
        self.cnn = nnx.Conv(
            config.input_dim,
            config.hidden_dim,
            kernel_size=(config.patch_size, config.patch_size),
            strides=(config.patch_stride, config.patch_stride),
            padding=config.padding,
            use_bias=config.use_bias,
            rngs=rngs,
            dtype=jnp.bfloat16,
        )

    def __call__(self, x):
        return self.cnn(x)


class TimeEmbedding(nnx.Module):
    """Time embedding module."""

    def __init__(self, config: DiTConfig, *, rngs: nnx.Rngs):
        super().__init__()
        self.freq_dim = config.time_freq_dim
        self.max_period = config.time_max_period
        self.fc1 = nnx.Linear(
            self.freq_dim, config.hidden_dim, use_bias=config.use_bias, rngs=rngs, dtype=jnp.bfloat16
        )
        self.fc2 = nnx.Linear(
            config.hidden_dim, config.hidden_dim, use_bias=config.use_bias, rngs=rngs, dtype=jnp.bfloat16
        )

    @staticmethod
    def cosine_embedding(t, dim, max_period):
        assert dim % 2 == 0
        half = dim // 2
        freqs = jnp.exp(
            -math.log(max_period)
            * jnp.arange(start=0, stop=half, dtype=jnp.float32)
            / half
        )
        args = t[:, None] * freqs[None, :] * 1024
        embedding = jnp.concatenate([jnp.cos(args), jnp.sin(args)], axis=-1)
        return embedding

    def __call__(self, t):
        t_freq = self.cosine_embedding(t, self.freq_dim, self.max_period)
        t_embed = self.fc1(t_freq)
        t_embed = nnx.silu(t_embed)
        t_embed = self.fc2(t_embed)
        return t_embed


class MLP(nnx.Module):
    """MLP module."""

    def __init__(self, config: DiTConfig, *, rngs: nnx.Rngs):
        super().__init__()
        self.fc1 = nnx.Linear(
            config.hidden_dim,
            config.hidden_dim * config.mlp_ratio,
            use_bias=config.use_bias,
            rngs=rngs,
            dtype=jnp.bfloat16,
        )
        self.fc2 = nnx.Linear(
            config.hidden_dim * config.mlp_ratio,
            config.hidden_dim,
            use_bias=config.use_bias,
            rngs=rngs,
            dtype=jnp.bfloat16,
        )

    def __call__(self, x):
        x = self.fc1(x)
        x = nnx.silu(x)
        x = self.fc2(x)
        return x


class SelfAttention(nnx.Module):
    """Self attention module."""

    def __init__(self, config: DiTConfig, *, rngs: nnx.Rngs):
        super().__init__()
        self.fc = nnx.Linear(
            config.hidden_dim,
            3 * config.hidden_dim,
            use_bias=config.use_bias,
            rngs=rngs,
            dtype=jnp.bfloat16,
        )
        self.heads = config.num_heads
        self.head_dim = config.hidden_dim // config.num_heads
        assert config.hidden_dim % config.num_heads == 0
        self.q_norm = nnx.RMSNorm(num_features=self.head_dim, use_scale=True, rngs=rngs)
        self.k_norm = nnx.RMSNorm(num_features=self.head_dim, use_scale=True, rngs=rngs)

    def __call__(self, x):
        q, k, v = jnp.split(self.fc(x), 3, axis=-1)
        # reshape q, k v, to N, T, H, D
        q = q.reshape(q.shape[0], q.shape[1], self.heads, self.head_dim)
        k = k.reshape(k.shape[0], k.shape[1], self.heads, self.head_dim)
        v = v.reshape(v.shape[0], v.shape[1], self.heads, self.head_dim)
        q = self.q_norm(q)
        k = self.k_norm(k)
        o = jax.nn.dot_product_attention(q, k, v, is_causal=False)
        o = o.reshape(o.shape[0], o.shape[1], self.heads * self.head_dim)
        return o


def modulate(x, shift, scale):
    return x * (1 + scale[:, None, :]) + shift[:, None, :]


class TransformerBlock(nnx.Module):
    """Transformer block."""

    def __init__(self, config: DiTConfig, *, rngs: nnx.Rngs):
        super().__init__()
        self.norm1 = nnx.RMSNorm(
            num_features=config.hidden_dim, use_scale=False, rngs=rngs
        )
        self.attn = SelfAttention(config, rngs=rngs)
        self.norm2 = nnx.RMSNorm(
            num_features=config.hidden_dim, use_scale=False, rngs=rngs
        )
        self.mlp = MLP(config, rngs=rngs)
        self.adalm_modulation = nnx.Sequential(
            nnx.silu,
            nnx.Linear(
                config.hidden_dim,
                6 * config.hidden_dim,
                use_bias=config.use_bias,
                rngs=rngs,
                dtype=jnp.bfloat16,
            ),
        )

    def __call__(self, x, c):
        shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = jnp.split(
            self.adalm_modulation(c), 6, axis=-1
        )
        attn_x = self.norm1(x)
        attn_x = modulate(attn_x, shift_msa, scale_msa)
        x = x + gate_msa[:, None, :] * self.attn(attn_x)
        mlp_x = self.norm2(x)
        mlp_x = modulate(mlp_x, shift_mlp, scale_mlp)
        x = x + gate_mlp[:, None, :] * self.mlp(mlp_x)
        return x


class FinalLayer(nnx.Module):
    """Final layer."""

    def __init__(self, config: DiTConfig, *, rngs: nnx.Rngs):
        super().__init__()
        self.norm = nnx.RMSNorm(
            num_features=config.hidden_dim, use_scale=False, rngs=rngs
        )
        self.conv = nnx.ConvTranspose(
            config.hidden_dim,
            config.input_dim,
            kernel_size=(config.patch_size, config.patch_size),
            strides=(config.patch_stride, config.patch_stride),
            padding=config.padding,
            use_bias=config.use_bias,
            rngs=rngs,
            dtype=jnp.bfloat16,
        )
        self.adalm_modulation = nnx.Sequential(
            nnx.silu,
            nnx.Linear(
                config.hidden_dim,
                2 * config.hidden_dim,
                use_bias=config.use_bias,
                rngs=rngs,
                dtype=jnp.bfloat16,
            ),
        )

    def __call__(self, x, c):
        shift, scale = jnp.split(self.adalm_modulation(c), 2, axis=-1)
        x = self.norm(x)
        x = modulate(x, shift, scale)
        # reshape to N, H, W, C
        H = W = int(x.shape[1] ** 0.5)
        x = x.reshape(x.shape[0], H, W, x.shape[-1])
        x = self.conv(x)
        return x


class DiT(nnx.Module):
    """Diffusion Transformer"""

    def __init__(self, config: DiTConfig, *, rngs: nnx.Rngs):
        super().__init__()
        self.config = config
        self.time_embedding = TimeEmbedding(config, rngs=rngs)
        self.patch_embedding = PatchEmbedding(config, rngs=rngs)
        self.blocks = [
            TransformerBlock(config, rngs=rngs) for _ in range(config.num_blocks)
        ]
        self.final_layer = FinalLayer(config, rngs=rngs)

    def __call__(self, xt, t):
        t = self.time_embedding(t)
        x = self.patch_embedding(xt)
        N, H, W, D = x.shape
        x = x.reshape(N, H * W, D)
        x = x + get_2d_sincos_pos_embed(
            D,
            H,
            cls_token=self.config.pos_embed_cls_token,
            extra_tokens=self.config.pos_embed_extra_tokens,
        ).reshape(1, H * W, D)
        c = t
        for block in self.blocks:
            x = block(x, c)
        x = self.final_layer(x, c)
        return x