File size: 22,208 Bytes
e1d97a8
 
3b3d382
 
 
e1d97a8
 
3b3d382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1d97a8
3b3d382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1d97a8
3b3d382
 
e1d97a8
 
 
 
 
 
 
3b3d382
 
e1d97a8
 
 
3b3d382
e1d97a8
 
 
3b3d382
 
 
e1d97a8
3b3d382
 
 
 
426ee66
 
 
e1d97a8
 
 
426ee66
 
 
 
 
e1d97a8
 
 
426ee66
 
 
 
 
 
 
 
e1d97a8
 
 
426ee66
 
 
 
 
 
e1d97a8
 
 
426ee66
 
 
e1d97a8
426ee66
 
 
 
e1d97a8
 
 
 
 
 
 
 
 
 
 
 
 
 
426ee66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1d97a8
426ee66
 
 
 
 
 
 
e1d97a8
426ee66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1d97a8
426ee66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1d97a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
import logging

import torch
import torch.nn as nn

logger = logging.getLogger(__name__)


def get_time_embedding(time_steps, temb_dim):
    r"""
    Convert time steps tensor into an embedding using the
    sinusoidal time embedding formula
    :param time_steps: 1D tensor of length batch size
    :param temb_dim: Dimension of the embedding
    :return: BxD embedding representation of B time steps
    """
    assert temb_dim % 2 == 0, "time embedding dimension must be divisible by 2"

    # factor = 10000^(2i/d_model)
    factor = 10000 ** (
        torch.arange(
            start=0, end=temb_dim // 2, dtype=torch.float32, device=time_steps.device
        )
        / (temb_dim // 2)
    )

    # pos / factor
    # timesteps B -> B, 1 -> B, temb_dim
    t_emb = time_steps[:, None].repeat(1, temb_dim // 2) / factor
    t_emb = torch.cat([torch.sin(t_emb), torch.cos(t_emb)], dim=-1)
    return t_emb


class DownBlock(nn.Module):
    r"""
    DownBlock for Diffusion model:
    a) Block            Time embedding -> [Silu -> FC]

        1) Resnet Block :- [Norm-> Silu -> Conv] x num_layers
        2) Self Attention :- [Norm -> SA]
        3) Cross Attention :- [Norm -> CA]
    b) MidSample : DownSample the dimnension
    """

    def __init__(
        self,
        num_heads,
        num_layers,
        cross_attn,
        input_dim,
        output_dim,
        t_emb_dim,
        cond_dim,
        norm_channels,
        self_attn,
        down_sample,
    ) -> None:
        super().__init__()
        self.num_heads = num_heads
        self.num_layers = num_layers
        self.cross_attn = cross_attn
        self.input_dim = input_dim
        self.output_dim = output_dim
        self.cond_dim = cond_dim
        self.norm_channels = norm_channels
        self.t_emb_dim = t_emb_dim
        self.attn = self_attn
        self.down_sample = down_sample

        self.resnet_in = nn.ModuleList(
            [
                nn.Conv2d(
                    self.input_dim if i == 0 else self.output_dim,
                    self.output_dim,
                    kernel_size=1,
                )
                for i in range(self.num_layers)
            ]
        )
        self.resnet_one = nn.ModuleList(
            [
                nn.Sequential(
                    nn.GroupNorm(
                        self.norm_channels,
                        self.input_dim if i == 0 else self.output_dim,
                    ),
                    nn.SiLU(),
                    nn.Conv2d(
                        self.input_dim if i == 0 else self.output_dim,
                        self.output_dim,
                        kernel_size=3,
                        stride=1,
                        padding=1,
                    ),
                )
                for i in range(self.num_layers)
            ]
        )

        if self.t_emb_dim is not None:
            self.t_emb_layers = nn.ModuleList(
                [
                    nn.Sequential(nn.SiLU(), nn.Linear(self.t_emb_dim, self.output_dim))
                    for _ in range(self.num_layers)
                ]
            )

        self.resnet_two = nn.ModuleList(
            [
                nn.Sequential(
                    nn.GroupNorm(
                        self.norm_channels,
                        self.output_dim,
                    ),
                    nn.SiLU(),
                    nn.Conv2d(
                        self.output_dim,
                        self.output_dim,
                        kernel_size=3,
                        stride=1,
                        padding=1,
                    ),
                )
                for _ in range(self.num_layers)
            ]
        )

        if self.attn:
            self.attention_norms = nn.ModuleList(
                [
                    nn.GroupNorm(self.norm_channels, self.output_dim)
                    for _ in range(num_layers)
                ]
            )
            self.attentions = nn.ModuleList(
                [
                    nn.MultiheadAttention(
                        self.output_dim, self.num_heads, batch_first=True
                    )
                    for _ in range(self.num_layers)
                ]
            )

        if self.cross_attn:
            self.cross_attn_norms = nn.ModuleList(
                [
                    nn.GroupNorm(self.norm_channels, self.output_dim)
                    for _ in range(self.num_layers)
                ]
            )
            self.cross_attentions = nn.ModuleList(
                [
                    nn.MultiheadAttention(
                        self.output_dim, self.num_heads, batch_first=True
                    )
                    for _ in range(self.num_layers)
                ]
            )

            self.context_proj = nn.ModuleList(
                [
                    nn.Linear(self.cond_dim, self.output_dim)
                    for _ in range(self.num_layers)
                ]
            )

        self.down_sample_conv = (
            nn.Conv2d(self.output_dim, self.output_dim, 4, 2, 1)
            if self.down_sample
            else nn.Identity()
        )

    def forward(self, x, t_emb=None, context=None):
        out = x
        for i in range(self.num_layers):
            # Input x to Resnet Block of the Encoder of the Unet
            logger.debug(f"Input to Resnet Block in Down Block Layer {i} : {out.shape}")
            resnet_input = out
            out = self.resnet_one[i](out)
            logger.debug(
                f"Output of Resnet Sub Block 1 of Down Block Layer {i}  : {out.shape}"
            )
            if self.t_emb_dim is not None:
                logger.debug(
                    f"Adding t_emb of shape {self.t_emb_dim} to output of shape: {out.shape} of Down Block Layer {i}"
                )
                out = out + self.t_emb_layers[i](t_emb)[:, :, None, None]
            out = self.resnet_two[i](out)
            logger.debug(
                f"Output of Resnet Sub Block 2 of Down Block Layer: {i} with output_shape:{out.shape}"
            )
            out = out + self.resnet_in[i](resnet_input)
            logger.debug(
                f"Residual connection of the input to out : {out.shape} in Down Block Layer {i}"
            )

            if self.attn:
                # Now Passing through the Self Attention blocks
                logger.debug(f"Going into the attention Block in Down Block Layer {i}")
                batch_size, channels, h, w = out.shape
                in_attn = out.reshape(batch_size, channels, h * w)
                in_attn = self.attention_norms[i](in_attn)
                in_attn = in_attn.transpose(1, 2)
                out_attn, _ = self.attentions[i](in_attn, in_attn, in_attn)
                out_attn = out_attn.transpose(1, 2).reshape(batch_size, channels, h, w)
                out = out + out_attn
                logger.debug(
                    f"Out of the Self Attention Block with out : {out.shape} in Down Block Layer {i}"
                )

            if self.cross_attn:
                assert context is not None, (
                    "context cannot be None if cross attention layers are used"
                )
                logger.debug(
                    f"Going into the Cross Attention Block in Down Block Layer {i}"
                )
                batch_size, channels, h, w = out.shape
                in_attn = out.reshape(batch_size, channels, h * w)
                in_attn = self.cross_attn_norms[i](in_attn)
                in_attn = in_attn.transpose(1, 2)
                assert (
                    context.shape[0] == x.shape[0]
                    and context.shape[-1] == self.context_dim
                )
                logger.debug(
                    f"Calculating context projection for Cross Attn in Down Block Layer : {i}"
                )
                context_proj = self.context_proj[i](context)
                out_attn, _ = self.cross_attentions[i](
                    in_attn, context_proj, context_proj
                )
                out_attn = out_attn.transpose(1, 2).reshape(batch_size, channels, h, w)
                out = out + out_attn
                logger.debug(
                    f"Out of the Cross Attention Block with out : {out.shape} in Down Block Layer {i}"
                )

            # DownSample to x2 smaller dimension
            out = self.down_sample_conv(out)
            logger.debug(f"Down Sampling out to : {out.shape} in Down Block Layer {i} ")
            return out


class MidBlock(nn.Module):
    r"""

    MidBlock for Diffusion model:
           Time embedding -> [Silu -> FC]

        1) Resnet Block :- [Norm-> Silu -> Conv] x num_layers
        2) Self Attention :- [Norm -> SA]
        3) Cross Attention :- [Norm -> CA]
           Time embedding -> [Silu -> FC]

        4) Resnet Block :- [Norm-> Silu -> Conv] x num_layers

    """

    def __init__(
        self,
        num_heads,
        num_layers,
        cross_attn,
        input_dim,
        output_dim,
        t_emb_dim,
        cond_dim,
        norm_channels,
        self_attn,
        down_sample,
    ) -> None:
        super().__init__()
        self.num_heads = num_heads
        self.num_layers = num_layers
        self.cross_attn = cross_attn
        self.input_dim = input_dim
        self.output_dim = output_dim
        self.cond_dim = cond_dim
        self.norm_channels = norm_channels
        self.t_emb_dim = t_emb_dim
        self.attn = self_attn
        self.down_sample = down_sample

        self.resnet_one = nn.ModuleList(
            [
                nn.Sequential(
                    nn.GroupNorm(
                        self.norm_channels,
                        self.input_dim if i == 0 else self.output_dim,
                    ),
                    nn.SiLU(),
                    nn.Conv2d(
                        self.input_dim if i == 0 else self.output_dim,
                        self.output_dim,
                        kernel_size=3,
                        stride=1,
                        padding=1,
                    ),
                )
                for i in range(self.num_layers + 1)
            ]
        )

        if self.t_emb_dim is not None:
            self.t_emb_layers = nn.ModuleList(
                [
                    nn.Sequential(nn.SiLU(), nn.Linear(self.t_emb_dim, self.output_dim))
                    for _ in range(self.num_layers + 1)
                ]
            )

        self.resnet_two = nn.ModuleList(
            [
                nn.Sequential(
                    nn.GroupNorm(
                        self.norm_channels,
                        self.output_dim,
                    ),
                    nn.SiLU(),
                    nn.Conv2d(
                        self.output_dim,
                        self.output_dim,
                        kernel_size=3,
                        stride=1,
                        padding=1,
                    ),
                )
                for _ in range(self.num_layers + 1)
            ]
        )

        if self.attn:
            self.attention_norms = nn.ModuleList(
                [
                    nn.GroupNorm(self.norm_channels, self.output_dim)
                    for _ in range(num_layers)
                ]
            )
            self.attentions = nn.ModuleList(
                [
                    nn.MultiheadAttention(
                        self.output_dim, self.num_heads, batch_first=True
                    )
                    for _ in range(self.num_layers)
                ]
            )

        if self.cross_attn:
            self.cross_attn_norms = nn.ModuleList(
                [
                    nn.GroupNorm(self.norm_channels, self.output_dim)
                    for _ in range(self.num_layers)
                ]
            )
            self.cross_attentions = nn.ModuleList(
                [
                    nn.MultiheadAttention(
                        self.output_dim, self.num_heads, batch_first=True
                    )
                    for _ in range(self.num_layers)
                ]
            )

            self.context_proj = nn.ModuleList(
                [
                    nn.Linear(self.cond_dim, self.output_dim)
                    for _ in range(self.num_layers)
                ]
            )

        self.resnet_in = nn.ModuleList(
            [
                nn.Conv2d(
                    self.input_dim if i == 0 else self.output_dim,
                    self.output_dim,
                    kernel_size=1,
                )
                for i in range(self.num_layers + 1)
            ]
        )

    def forward(self, x, t_emb=None, context=None):
        out = x

        # Input Resnet Block
        logger.debug("Input to First Resnet Block in Mid Block")
        resnet_input = out
        out = self.resnet_one[0](out)
        logger.debug(f"Output of Resnet Sub Block 1 of Mid Block Layer: {out.shape}")
        if self.t_emb_dim is not None:
            out = out + self.t_emb_layers[0](t_emb)[:, :, None, None]
            logger.debug(
                f"Adding t_emb of shape {self.t_emb_dim} to output of shape: {out.shape}"
            )
        out = self.resnet_two[0](out)
        logger.debug(f"Output of Resnet Sub Block 2 with output_shape:{out.shape}")
        out = out + self.resnet_in[0](resnet_input)
        logger.debug(
            f"Residual connection of the input to out : {out.shape} in Mid Block"
        )

        for i in range(self.num_layers):
            logger.debug(f"Going into the attention Block in Mid Block Layer {i}")
            batch_size, channels, h, w = out.shape
            in_attn = out.reshape(batch_size, channels, h * w)
            in_attn = self.attention_norms[i](in_attn)
            in_attn = in_attn.transpose(1, 2)
            out_attn, _ = self.attentions[i](in_attn, in_attn, in_attn)
            out_attn = out_attn.transpose(1, 2).reshape(batch_size, channels, h, w)
            out = out + out_attn
            logger.debug(
                f"Out of the Self Attention Block with out : {out.shape} in Mid Block Layer {i}"
            )

            if self.cross_attn:
                assert context is not None, (
                    "context cannot be None if cross attention layers are used"
                )
                logger.debug(
                    f"Going into the Cross Attention Block in Mid Block Layer {i}"
                )
                batch_size, channels, h, w = out.shape
                in_attn = out.reshape(batch_size, channels, h * w)
                in_attn = self.cross_attn_norms[i](in_attn)
                in_attn = in_attn.transpose(1, 2)
                assert (
                    context.shape[0] == x.shape[0]
                    and context.shape[-1] == self.context_dim
                )
                logger.debug(
                    f"Calculating context projection for Cross Attn in Mid Block Layer : {i}"
                )
                context_proj = self.context_proj[i](context)
                out_attn, _ = self.cross_attentions[i](
                    in_attn, context_proj, context_proj
                )
                out_attn = out_attn.transpose(1, 2).reshape(batch_size, channels, h, w)
                out = out + out_attn
                logger.debug(
                    f"Out of the Cross Attention Block with out : {out.shape} in Mid Block Layer {i}"
                )
            logger.debug(
                f"Last Resnet Block input : {out.shape} of Mid Block Layer {i}"
            )
            resnet_input = out
            out = self.resnet_one[0](out)
            logger.debug(
                f"Output of Resnet Sub Block 1 of Mid Block Layer {i} of shape : {out.shape}"
            )
            if self.t_emb_dim is not None:
                out = out + self.t_emb_layers[0](t_emb)[:, :, None, None]
                logger.debug(
                    f"Adding t_emb of shape {self.t_emb_dim} to output of shape: {out.shape} of Mid Block Layer {i}"
                )
            out = self.resnet_two[0](out)
            logger.debug(
                f"Output of Resnet Sub Block 2 with output_shape:{out.shape} of Mid Block Layer {i}"
            )
            out = out + self.resnet_in[0](resnet_input)
            logger.debug(
                f"Residual connection of the input to out : {out.shape} in Mid Block Layer {i}"
            )

        return out


class UpBlockUnet(nn.Module):
    r"""
    Up conv block with attention.
    Sequence of following blocks
    1. Upsample
    1. Concatenate Down block output
    2. Resnet block with time embedding
    3. Attention Block
    """

    def __init__(
        self,
        in_channels,
        out_channels,
        t_emb_dim,
        up_sample,
        num_heads,
        num_layers,
        norm_channels,
        cross_attn=False,
        context_dim=None,
    ):
        super().__init__()
        self.num_layers = num_layers
        self.up_sample = up_sample
        self.t_emb_dim = t_emb_dim
        self.cross_attn = cross_attn
        self.context_dim = context_dim
        self.resnet_conv_first = nn.ModuleList(
            [
                nn.Sequential(
                    nn.GroupNorm(
                        norm_channels, in_channels if i == 0 else out_channels
                    ),
                    nn.SiLU(),
                    nn.Conv2d(
                        in_channels if i == 0 else out_channels,
                        out_channels,
                        kernel_size=3,
                        stride=1,
                        padding=1,
                    ),
                )
                for i in range(num_layers)
            ]
        )

        if self.t_emb_dim is not None:
            self.t_emb_layers = nn.ModuleList(
                [
                    nn.Sequential(nn.SiLU(), nn.Linear(t_emb_dim, out_channels))
                    for _ in range(num_layers)
                ]
            )

        self.resnet_conv_second = nn.ModuleList(
            [
                nn.Sequential(
                    nn.GroupNorm(norm_channels, out_channels),
                    nn.SiLU(),
                    nn.Conv2d(
                        out_channels, out_channels, kernel_size=3, stride=1, padding=1
                    ),
                )
                for _ in range(num_layers)
            ]
        )

        self.attention_norms = nn.ModuleList(
            [nn.GroupNorm(norm_channels, out_channels) for _ in range(num_layers)]
        )

        self.attentions = nn.ModuleList(
            [
                nn.MultiheadAttention(out_channels, num_heads, batch_first=True)
                for _ in range(num_layers)
            ]
        )

        if self.cross_attn:
            assert context_dim is not None, (
                "Context Dimension must be passed for cross attention"
            )
            self.cross_attention_norms = nn.ModuleList(
                [nn.GroupNorm(norm_channels, out_channels) for _ in range(num_layers)]
            )
            self.cross_attentions = nn.ModuleList(
                [
                    nn.MultiheadAttention(out_channels, num_heads, batch_first=True)
                    for _ in range(num_layers)
                ]
            )
            self.context_proj = nn.ModuleList(
                [nn.Linear(context_dim, out_channels) for _ in range(num_layers)]
            )
        self.residual_input_conv = nn.ModuleList(
            [
                nn.Conv2d(
                    in_channels if i == 0 else out_channels, out_channels, kernel_size=1
                )
                for i in range(num_layers)
            ]
        )
        self.up_sample_conv = (
            nn.ConvTranspose2d(in_channels // 2, in_channels // 2, 4, 2, 1)
            if self.up_sample
            else nn.Identity()
        )

    def forward(self, x, out_down=None, t_emb=None, context=None):
        x = self.up_sample_conv(x)
        if out_down is not None:
            x = torch.cat([x, out_down], dim=1)

        out = x
        for i in range(self.num_layers):
            # Resnet
            resnet_input = out
            out = self.resnet_conv_first[i](out)
            if self.t_emb_dim is not None:
                out = out + self.t_emb_layers[i](t_emb)[:, :, None, None]
            out = self.resnet_conv_second[i](out)
            out = out + self.residual_input_conv[i](resnet_input)
            # Self Attention
            batch_size, channels, h, w = out.shape
            in_attn = out.reshape(batch_size, channels, h * w)
            in_attn = self.attention_norms[i](in_attn)
            in_attn = in_attn.transpose(1, 2)
            out_attn, _ = self.attentions[i](in_attn, in_attn, in_attn)
            out_attn = out_attn.transpose(1, 2).reshape(batch_size, channels, h, w)
            out = out + out_attn
            # Cross Attention
            if self.cross_attn:
                assert context is not None, (
                    "context cannot be None if cross attention layers are used"
                )
                batch_size, channels, h, w = out.shape
                in_attn = out.reshape(batch_size, channels, h * w)
                in_attn = self.cross_attention_norms[i](in_attn)
                in_attn = in_attn.transpose(1, 2)
                assert len(context.shape) == 3, (
                    "Context shape does not match B,_,CONTEXT_DIM"
                )
                assert (
                    context.shape[0] == x.shape[0]
                    and context.shape[-1] == self.context_dim
                ), "Context shape does not match B,_,CONTEXT_DIM"
                context_proj = self.context_proj[i](context)
                out_attn, _ = self.cross_attentions[i](
                    in_attn, context_proj, context_proj
                )
                out_attn = out_attn.transpose(1, 2).reshape(batch_size, channels, h, w)
                out = out + out_attn

        return out