File size: 13,957 Bytes
12edc27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
369
370
371
372
373
374
375
376
377
378
379
380
from dataclasses import dataclass
from typing import Optional, Union, List

import torch
import numpy as np
import math
from torch import Tensor, nn
from einops import rearrange

from ..modules.layers import (DoubleStreamBlock, EmbedND, LastLayer,
                                 MLPEmbedder, SingleStreamBlock,
                                 timestep_embedding)
from einops import repeat

# -------------------------------------------------------------------------
# 1) Flux model
# -------------------------------------------------------------------------
@dataclass
class FluxParams:
    in_channels: int
    out_channels: int
    vec_in_dim: int
    context_in_dim: int
    hidden_size: int
    mlp_ratio: float
    num_heads: int
    depth: int
    depth_single_blocks: int
    axes_dim: list
    theta: int
    qkv_bias: bool
    guidance_embed: bool


class Flux(nn.Module):
    """
    Transformer model for flow matching on sequences.
    """
    _supports_gradient_checkpointing = True

    def __init__(self, params: FluxParams):
        super().__init__()

        self.params = params
        self.in_channels = params.in_channels
        self.out_channels = params.out_channels if params.out_channels is not None else self.in_channels
        if params.hidden_size % params.num_heads != 0:
            raise ValueError(
                f"Hidden size {params.hidden_size} must be divisible by num_heads {params.num_heads}"
            )
        pe_dim = params.hidden_size // params.num_heads
        if sum(params.axes_dim) != pe_dim:
            raise ValueError(f"Got {params.axes_dim} but expected positional dim {pe_dim}")
        self.hidden_size = params.hidden_size
        self.num_heads = params.num_heads
        self.pe_embedder = EmbedND(dim=pe_dim, theta=params.theta, axes_dim=params.axes_dim)

        self.img_in = nn.Linear(self.in_channels, self.hidden_size, bias=True)
        self.time_in = MLPEmbedder(in_dim=256, hidden_dim=self.hidden_size)
        self.vector_in = MLPEmbedder(params.vec_in_dim, self.hidden_size)
        self.guidance_in = (
            MLPEmbedder(in_dim=256, hidden_dim=self.hidden_size) if params.guidance_embed else nn.Identity()
        )
        self.txt_in = nn.Linear(params.context_in_dim, self.hidden_size)

        self.double_blocks = nn.ModuleList(
            [
                DoubleStreamBlock(
                    self.hidden_size,
                    self.num_heads,
                    mlp_ratio=params.mlp_ratio,
                    qkv_bias=params.qkv_bias,)
                for i in range(1, params.depth+1)
            ]
        )

        self.single_blocks = nn.ModuleList(
            [
                SingleStreamBlock(
                    self.hidden_size, 
                    self.num_heads, 
                    mlp_ratio=params.mlp_ratio,
                    )
                for i in range(1, params.depth_single_blocks+1)
            ]
        )
       
        self.final_layer = LastLayer(self.hidden_size, 1, self.out_channels)
        self.gradient_checkpointing = True

    def _set_gradient_checkpointing(self, module, value=False):
        if hasattr(module, "gradient_checkpointing"):
            module.gradient_checkpointing = value

    
    @property
    def attn_processors(self):
        # set recursively
        processors = {}

        def fn_recursive_add_processors(name: str, module: torch.nn.Module, processors):
            if hasattr(module, "set_processor"):
                processors[f"{name}.processor"] = module.processor

            for sub_name, child in module.named_children():
                fn_recursive_add_processors(f"{name}.{sub_name}", child, processors)

            return processors

        for name, module in self.named_children():
            fn_recursive_add_processors(name, module, processors)

        return processors

    def set_attn_processor(self, processor):
        r"""
        Sets the attention processor to use to compute attention.

        Parameters:
            processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`):
                The instantiated processor class or a dictionary of processor classes that will be set as the processor
                for **all** `Attention` layers.

                If `processor` is a dict, the key needs to define the path to the corresponding cross attention
                processor. This is strongly recommended when setting trainable attention processors.

        """
        count = len(self.attn_processors.keys())

        if isinstance(processor, dict) and len(processor) != count:
            raise ValueError(
                f"A dict of processors was passed, but the number of processors {len(processor)} does not match the"
                f" number of attention layers: {count}. Please make sure to pass {count} processor classes."
            )

        def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor):
            if hasattr(module, "set_processor"):
                if not isinstance(processor, dict):
                    module.set_processor(processor)
                else:
                    module.set_processor(processor.pop(f"{name}.processor"))

            for sub_name, child in module.named_children():
                fn_recursive_attn_processor(f"{name}.{sub_name}", child, processor)

        for name, module in self.named_children():
            fn_recursive_attn_processor(name, module, processor)

    def forward(
        self,
        img: Tensor,
        img_ids: Tensor,
        txt: Tensor,
        txt_ids: Tensor,
        timesteps: Tensor,
        txt_vec: Tensor,
        block_controlnet_hidden_states=None,
        guidance: Tensor = None,
        image_proj: Tensor = None, 
        ip_scale: Tensor = 1.0, 
        return_intermediate: bool = False,
        cxt_embeddings: Tensor = None,
        task_register_embeddings: Tensor = None,
        use_flash_attention: bool = False,
    ) -> Tensor:
            
        if return_intermediate:
            intermediate_double = []
            intermediate_single = []
            
        # running on sequences img
        img = self.img_in(img)
       
        vec = self.time_in(timestep_embedding(timesteps, 256))
        if self.params.guidance_embed:
            if guidance is None:
                raise ValueError("Didn't get guidance strength for guidance distilled model.")
            vec = vec + self.guidance_in(timestep_embedding(guidance, 256))

        vec = vec + self.vector_in(txt_vec)
        txt = self.txt_in(txt)

        ids = torch.cat((txt_ids, img_ids), dim=1)
        pe = self.pe_embedder(ids)

        if block_controlnet_hidden_states is not None:
            controlnet_depth = len(block_controlnet_hidden_states)

        for index_block, block in enumerate(self.double_blocks):
            if self.training and self.gradient_checkpointing:

                def create_custom_forward(module, return_dict=None):
                    def custom_forward(*inputs):
                        if return_dict is not None:
                            return module(*inputs, return_dict=return_dict)
                        else:
                            return module(*inputs)

                    return custom_forward

                img, txt = torch.utils.checkpoint.checkpoint(
                    create_custom_forward(block),
                    img,
                    txt,
                    vec,
                    pe,
                    cxt_embeddings,
                    task_register_embeddings,
                    image_proj,
                    ip_scale,
                    use_flash_attention,
                )
            else:
                img, txt = block(
                    img=img, 
                    txt=txt, 
                    vec=vec, 
                    pe=pe,
                    cxt_embeddings=cxt_embeddings,
                    task_register_embeddings=task_register_embeddings,
                    image_proj=image_proj,
                    ip_scale=ip_scale,
                    use_flash_attention=use_flash_attention,
                )



            if return_intermediate:
                intermediate_double.append(
                    [img, txt]
                )

            if block_controlnet_hidden_states is not None:
                img = img + block_controlnet_hidden_states[index_block % 2]

        img = torch.cat((txt, img), dim=1)
        for index_block, block in enumerate(self.single_blocks):
            if self.training and self.gradient_checkpointing:

                def create_custom_forward(module, return_dict=None):
                    def custom_forward(*inputs):
                        if return_dict is not None:
                            return module(*inputs, return_dict=return_dict)
                        else:
                            return module(*inputs)

                    return custom_forward

                img = torch.utils.checkpoint.checkpoint(
                    create_custom_forward(block),
                    img,
                    vec,
                    pe,
                    cxt_embeddings,
                    task_register_embeddings,
                    image_proj,
                    ip_scale,
                    use_flash_attention,
                )
            else:
                img = block(
                    img=img, 
                    vec=vec, 
                    pe=pe, 
                    cxt_embeddings=cxt_embeddings, 
                    task_register_embeddings=task_register_embeddings, 
                    image_proj=image_proj, 
                    ip_scale=ip_scale,
                    use_flash_attention=use_flash_attention,
                )

            if return_intermediate:
                intermediate_single.append([
                    img[:, txt.shape[1]:, ...],
                    img[:, :txt.shape[1], ...]
                ])

        img = img[:, txt.shape[1] :, ...]

        img = self.final_layer(img, vec)  # (N, T, patch_size ** 2 * out_channels)
        
        if return_intermediate:
            return img, intermediate_double, intermediate_single
        else:
            return img


# -------------------------------------------------------------------------
# 2) IC-Custom model
# -------------------------------------------------------------------------
class IC_Custom(Flux):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.cond_embedding = nn.Embedding(1, 3072)
        self.cond_embedding.weight.data.zero_()
        self.target_embedding = nn.Embedding(1, 3072)
        self.target_embedding.weight.data.zero_()
        self.idx_embedding = nn.Embedding(10, 3072)
        self.idx_embedding.weight.data.zero_()

        self.task_register_embeddings = nn.Embedding(24, 3072)
        self.task_register_embeddings.weight.data.zero_()


    def forward(
        self,
        img: Tensor,
        img_ids: Tensor,
        txt: Tensor,
        txt_ids: Tensor,
        timesteps: Tensor,
        txt_vec: Tensor,
        guidance: Tensor = None,
        return_intermediate: bool = False,
        cond_w_regions: Optional[Union[List[int], int]] = None,
        mask_type_ids: Optional[Union[Tensor, int]] = None,
        height: int = 1024,
        width: int = 1024,
        use_flash_attention: bool = False,
    ) -> Tensor:

        batch_size, seq_len, _ = img.shape

        ## 1. calculate the sequence length
        # Units: 2x2 latent tokens (VAE downscale=8, pack=2)
        latent_h = math.ceil(height // 8 // 2)
        latent_w = math.ceil(width  // 8 // 2)
        assert latent_h * latent_w == seq_len

        if isinstance(cond_w_regions, list):
            cond_w_list = [math.ceil(cond_w // 8 // 2) for cond_w in cond_w_regions]
        else:
            cond_w_list = [math.ceil(cond_w_regions // 8 // 2)]

        cond_w_total = sum(cond_w_list)
        tgt_w = latent_w - cond_w_total


        ## 2. calculate the embeddings and rearrange them
        cond_embeddings = repeat(self.cond_embedding.weight[0], "c -> n l c", n=batch_size, l=cond_w_total * latent_h)
        cond_embeddings = rearrange(cond_embeddings, "b (h w) d -> b h w d", h=latent_h, w=cond_w_total)

        target_embeddings = repeat(self.target_embedding.weight[0], "c -> n l c", n=batch_size, l=latent_h*tgt_w)
        target_embeddings = rearrange(target_embeddings, "b (h w) d -> b h w d", h=latent_h, w=tgt_w)

        idx_embeddings = []
        for idx in range(len(cond_w_list)):
            idx_embedding  = repeat(self.idx_embedding.weight[idx], "c -> n h w c", n=batch_size, h=latent_h, w=cond_w_list[idx])
            idx_embeddings.append(idx_embedding)
        
        ## 3. combine the embeddings
        idx_embeddings = torch.cat(idx_embeddings, dim=2)
        cond_embeddings = cond_embeddings + idx_embeddings

        cxt_embeddings = torch.cat((cond_embeddings, target_embeddings), dim=2)
        cxt_embeddings = rearrange(cxt_embeddings, "b h w d -> b (h w) d")

        ## 4. calculate the mask type embeddings
        if isinstance(mask_type_ids, int):
            mask_type_ids = torch.tensor([mask_type_ids], device=img.device, dtype=img.dtype)
        task_register_embeddings = self.task_register_embeddings.weight.view(3, 2, 4, -1)[mask_type_ids.long()] ## num_mask_types, k&v, num_tokens, d
        
        ## 5. forward the model
        result = super().forward(
            img=img, 
            img_ids=img_ids, 
            txt=txt, 
            txt_ids=txt_ids, 
            timesteps=timesteps, 
            txt_vec=txt_vec, 
            guidance=guidance, 
            return_intermediate=return_intermediate, 
            cxt_embeddings=cxt_embeddings, 
            task_register_embeddings=task_register_embeddings,
            use_flash_attention=use_flash_attention
            )

        return result