unet_glide.py 22.8 KB
Newer Older
anton-l's avatar
anton-l committed
1
2
from abc import abstractmethod

anton-l's avatar
anton-l committed
3
import torch
anton-l's avatar
anton-l committed
4
5
6
import torch.nn as nn
import torch.nn.functional as F

Patrick von Platen's avatar
Patrick von Platen committed
7
8
from ..configuration_utils import ConfigMixin
from ..modeling_utils import ModelMixin
Patrick von Platen's avatar
Patrick von Platen committed
9
from .attention import AttentionBlock
10
from .embeddings import get_timestep_embedding
Patrick von Platen's avatar
Patrick von Platen committed
11
from .resnet import Downsample, ResBlock, TimestepBlock, Upsample
anton-l's avatar
anton-l committed
12

anton-l's avatar
anton-l committed
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

def convert_module_to_f16(l):
    """
    Convert primitive modules to float16.
    """
    if isinstance(l, (nn.Conv1d, nn.Conv2d, nn.Conv3d)):
        l.weight.data = l.weight.data.half()
        if l.bias is not None:
            l.bias.data = l.bias.data.half()


def convert_module_to_f32(l):
    """
    Convert primitive modules to float32, undoing convert_module_to_f16().
    """
    if isinstance(l, (nn.Conv1d, nn.Conv2d, nn.Conv3d)):
        l.weight.data = l.weight.data.float()
        if l.bias is not None:
            l.bias.data = l.bias.data.float()


def avg_pool_nd(dims, *args, **kwargs):
    """
    Create a 1D, 2D, or 3D average pooling module.
    """
    if dims == 1:
        return nn.AvgPool1d(*args, **kwargs)
    elif dims == 2:
        return nn.AvgPool2d(*args, **kwargs)
    elif dims == 3:
        return nn.AvgPool3d(*args, **kwargs)
    raise ValueError(f"unsupported dimensions: {dims}")


def conv_nd(dims, *args, **kwargs):
    """
    Create a 1D, 2D, or 3D convolution module.
    """
    if dims == 1:
        return nn.Conv1d(*args, **kwargs)
    elif dims == 2:
        return nn.Conv2d(*args, **kwargs)
    elif dims == 3:
        return nn.Conv3d(*args, **kwargs)
    raise ValueError(f"unsupported dimensions: {dims}")


def linear(*args, **kwargs):
    """
    Create a linear module.
    """
    return nn.Linear(*args, **kwargs)


class GroupNorm32(nn.GroupNorm):
    def __init__(self, num_groups, num_channels, swish, eps=1e-5):
        super().__init__(num_groups=num_groups, num_channels=num_channels, eps=eps)
        self.swish = swish

    def forward(self, x):
        y = super().forward(x.float()).to(x.dtype)
        if self.swish == 1.0:
            y = F.silu(y)
        elif self.swish:
            y = y * F.sigmoid(y * float(self.swish))
        return y


def normalization(channels, swish=0.0):
    """
    Make a standard normalization layer, with an optional swish activation.

Patrick von Platen's avatar
Patrick von Platen committed
85
    :param channels: number of input channels. :return: an nn.Module for normalization.
anton-l's avatar
anton-l committed
86
87
88
89
90
91
92
93
94
95
96
97
98
    """
    return GroupNorm32(num_channels=channels, num_groups=32, swish=swish)


def zero_module(module):
    """
    Zero out the parameters of a module and return it.
    """
    for p in module.parameters():
        p.detach().zero_()
    return module


Patrick von Platen's avatar
Patrick von Platen committed
99
100
101
102
103
104
105
106
# class TimestepBlock(nn.Module):
#    """
# Any module where forward() takes timestep embeddings as a second argument. #"""
#
#    @abstractmethod
#    def forward(self, x, emb):
#        """
# Apply the module to `x` given `emb` timestep embeddings. #"""
anton-l's avatar
anton-l committed
107
108
109
110


class TimestepEmbedSequential(nn.Sequential, TimestepBlock):
    """
Patrick von Platen's avatar
Patrick von Platen committed
111
    A sequential module that passes timestep embeddings to the children that support it as an extra input.
anton-l's avatar
anton-l committed
112
113
114
115
116
117
118
119
120
121
122
123
124
    """

    def forward(self, x, emb, encoder_out=None):
        for layer in self:
            if isinstance(layer, TimestepBlock):
                x = layer(x, emb)
            elif isinstance(layer, AttentionBlock):
                x = layer(x, encoder_out)
            else:
                x = layer(x)
        return x


Patrick von Platen's avatar
Patrick von Platen committed
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
# class ResBlock(TimestepBlock):
#    """
# A residual block that can optionally change the number of channels. # # :param channels: the number of input
channels. :param emb_channels: the number of timestep embedding channels. # :param dropout: the rate of dropout. :param
out_channels: if specified, the number of out channels. :param # use_conv: if True and out_channels is specified, use a
spatial # convolution instead of a smaller 1x1 convolution to change the channels in the skip connection. # :param
dims: determines if the signal is 1D, 2D, or 3D. :param use_checkpoint: if True, use gradient checkpointing # on this
module. :param up: if True, use this block for upsampling. :param down: if True, use this block for # downsampling. #"""
#
#    def __init__(
#        self,
#        channels,
#        emb_channels,
#        dropout,
#        out_channels=None,
#        use_conv=False,
#        use_scale_shift_norm=False,
#        dims=2,
#        use_checkpoint=False,
#        up=False,
#        down=False,
#    ):
#        super().__init__()
#        self.channels = channels
#        self.emb_channels = emb_channels
#        self.dropout = dropout
#        self.out_channels = out_channels or channels
#        self.use_conv = use_conv
#        self.use_checkpoint = use_checkpoint
#        self.use_scale_shift_norm = use_scale_shift_norm
#
#        self.in_layers = nn.Sequential(
#            normalization(channels, swish=1.0),
#            nn.Identity(),
#            conv_nd(dims, channels, self.out_channels, 3, padding=1),
#        )
#
#        self.updown = up or down
#
#        if up:
#            self.h_upd = Upsample(channels, use_conv=False, dims=dims)
#            self.x_upd = Upsample(channels, use_conv=False, dims=dims)
#        elif down:
#            self.h_upd = Downsample(channels, use_conv=False, dims=dims, padding=1, name="op")
#            self.x_upd = Downsample(channels, use_conv=False, dims=dims, padding=1, name="op")
#        else:
#            self.h_upd = self.x_upd = nn.Identity()
#
#        self.emb_layers = nn.Sequential(
#            nn.SiLU(),
#            linear(
#                emb_channels,
#                2 * self.out_channels if use_scale_shift_norm else self.out_channels,
#            ),
#        )
#        self.out_layers = nn.Sequential(
#            normalization(self.out_channels, swish=0.0 if use_scale_shift_norm else 1.0),
#            nn.SiLU() if use_scale_shift_norm else nn.Identity(),
#            nn.Dropout(p=dropout),
#            zero_module(conv_nd(dims, self.out_channels, self.out_channels, 3, padding=1)),
#        )
#
#        if self.out_channels == channels:
#            self.skip_connection = nn.Identity()
#        elif use_conv:
#            self.skip_connection = conv_nd(dims, channels, self.out_channels, 3, padding=1)
#        else:
#            self.skip_connection = conv_nd(dims, channels, self.out_channels, 1)
#
#    def forward(self, x, emb):
#        """
# Apply the block to a Tensor, conditioned on a timestep embedding. # # :param x: an [N x C x ...] Tensor of features.
:param emb: an [N x emb_channels] Tensor of timestep embeddings. # :return: an [N x C x ...] Tensor of outputs. #"""
#        if self.updown:
#            in_rest, in_conv = self.in_layers[:-1], self.in_layers[-1]
#            h = in_rest(x)
#            h = self.h_upd(h)
#            x = self.x_upd(x)
#            h = in_conv(h)
#        else:
#            h = self.in_layers(x)
#        emb_out = self.emb_layers(emb).type(h.dtype)
#        while len(emb_out.shape) < len(h.shape):
#            emb_out = emb_out[..., None]
#        if self.use_scale_shift_norm:
#            out_norm, out_rest = self.out_layers[0], self.out_layers[1:]
#            scale, shift = torch.chunk(emb_out, 2, dim=1)
#            h = out_norm(h) * (1 + scale) + shift
#            h = out_rest(h)
#        else:
#            h = h + emb_out
#            h = self.out_layers(h)
#        return self.skip_connection(x) + h
anton-l's avatar
anton-l committed
218
219


Patrick von Platen's avatar
Patrick von Platen committed
220
class GlideUNetModel(ModelMixin, ConfigMixin):
anton-l's avatar
anton-l committed
221
222
223
    """
    The full UNet model with attention and timestep embedding.

Patrick von Platen's avatar
Patrick von Platen committed
224
225
    :param in_channels: channels in the input Tensor. :param model_channels: base channel count for the model. :param
    out_channels: channels in the output Tensor. :param num_res_blocks: number of residual blocks per downsample.
anton-l's avatar
anton-l committed
226
    :param attention_resolutions: a collection of downsample rates at which
Patrick von Platen's avatar
Patrick von Platen committed
227
228
229
230
        attention will take place. May be a set, list, or tuple. For example, if this contains 4, then at 4x
        downsampling, attention will be used.
    :param dropout: the dropout probability. :param channel_mult: channel multiplier for each level of the UNet. :param
    conv_resample: if True, use learned convolutions for upsampling and
anton-l's avatar
anton-l committed
231
        downsampling.
Patrick von Platen's avatar
Patrick von Platen committed
232
233
    :param dims: determines if the signal is 1D, 2D, or 3D. :param num_classes: if specified (as an int), then this
    model will be
anton-l's avatar
anton-l committed
234
        class-conditional with `num_classes` classes.
Patrick von Platen's avatar
Patrick von Platen committed
235
236
    :param use_checkpoint: use gradient checkpointing to reduce memory usage. :param num_heads: the number of attention
    heads in each attention layer. :param num_heads_channels: if specified, ignore num_heads and instead use
anton-l's avatar
anton-l committed
237
238
239
                               a fixed channel width per attention head.
    :param num_heads_upsample: works with num_heads to set a different number
                               of heads for upsampling. Deprecated.
Patrick von Platen's avatar
Patrick von Platen committed
240
241
    :param use_scale_shift_norm: use a FiLM-like conditioning mechanism. :param resblock_updown: use residual blocks
    for up/downsampling.
anton-l's avatar
anton-l committed
242
243
244
245
    """

    def __init__(
        self,
246
        in_channels=3,
anton-l's avatar
anton-l committed
247
        resolution=64,
248
249
250
251
        model_channels=192,
        out_channels=6,
        num_res_blocks=3,
        attention_resolutions=(2, 4, 8),
anton-l's avatar
anton-l committed
252
253
254
255
256
257
258
259
260
261
262
        dropout=0,
        channel_mult=(1, 2, 4, 8),
        conv_resample=True,
        dims=2,
        use_checkpoint=False,
        use_fp16=False,
        num_heads=1,
        num_head_channels=-1,
        num_heads_upsample=-1,
        use_scale_shift_norm=False,
        resblock_updown=False,
anton-l's avatar
anton-l committed
263
        transformer_dim=None,
anton-l's avatar
anton-l committed
264
265
266
267
268
269
270
    ):
        super().__init__()

        if num_heads_upsample == -1:
            num_heads_upsample = num_heads

        self.in_channels = in_channels
anton-l's avatar
anton-l committed
271
        self.resolution = resolution
anton-l's avatar
anton-l committed
272
273
274
275
276
277
278
279
        self.model_channels = model_channels
        self.out_channels = out_channels
        self.num_res_blocks = num_res_blocks
        self.attention_resolutions = attention_resolutions
        self.dropout = dropout
        self.channel_mult = channel_mult
        self.conv_resample = conv_resample
        self.use_checkpoint = use_checkpoint
anton-l's avatar
Style  
anton-l committed
280
        # self.dtype = torch.float16 if use_fp16 else torch.float32
anton-l's avatar
anton-l committed
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
        self.num_heads = num_heads
        self.num_head_channels = num_head_channels
        self.num_heads_upsample = num_heads_upsample

        time_embed_dim = model_channels * 4
        self.time_embed = nn.Sequential(
            linear(model_channels, time_embed_dim),
            nn.SiLU(),
            linear(time_embed_dim, time_embed_dim),
        )

        ch = input_ch = int(channel_mult[0] * model_channels)
        self.input_blocks = nn.ModuleList([TimestepEmbedSequential(conv_nd(dims, in_channels, ch, 3, padding=1))])
        self._feature_size = ch
        input_block_chans = [ch]
        ds = 1
        for level, mult in enumerate(channel_mult):
            for _ in range(num_res_blocks):
                layers = [
                    ResBlock(
                        ch,
                        time_embed_dim,
                        dropout,
                        out_channels=int(mult * model_channels),
                        dims=dims,
                        use_checkpoint=use_checkpoint,
                        use_scale_shift_norm=use_scale_shift_norm,
                    )
                ]
                ch = int(mult * model_channels)
                if ds in attention_resolutions:
                    layers.append(
                        AttentionBlock(
                            ch,
                            use_checkpoint=use_checkpoint,
                            num_heads=num_heads,
                            num_head_channels=num_head_channels,
anton-l's avatar
anton-l committed
318
                            encoder_channels=transformer_dim,
anton-l's avatar
anton-l committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
                        )
                    )
                self.input_blocks.append(TimestepEmbedSequential(*layers))
                self._feature_size += ch
                input_block_chans.append(ch)
            if level != len(channel_mult) - 1:
                out_ch = ch
                self.input_blocks.append(
                    TimestepEmbedSequential(
                        ResBlock(
                            ch,
                            time_embed_dim,
                            dropout,
                            out_channels=out_ch,
                            dims=dims,
                            use_checkpoint=use_checkpoint,
                            use_scale_shift_norm=use_scale_shift_norm,
                            down=True,
                        )
                        if resblock_updown
patil-suraj's avatar
patil-suraj committed
339
340
341
                        else Downsample(
                            ch, use_conv=conv_resample, dims=dims, out_channels=out_ch, padding=1, name="op"
                        )
anton-l's avatar
anton-l committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
                    )
                )
                ch = out_ch
                input_block_chans.append(ch)
                ds *= 2
                self._feature_size += ch

        self.middle_block = TimestepEmbedSequential(
            ResBlock(
                ch,
                time_embed_dim,
                dropout,
                dims=dims,
                use_checkpoint=use_checkpoint,
                use_scale_shift_norm=use_scale_shift_norm,
            ),
            AttentionBlock(
                ch,
                use_checkpoint=use_checkpoint,
                num_heads=num_heads,
                num_head_channels=num_head_channels,
anton-l's avatar
anton-l committed
363
                encoder_channels=transformer_dim,
anton-l's avatar
anton-l committed
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
            ),
            ResBlock(
                ch,
                time_embed_dim,
                dropout,
                dims=dims,
                use_checkpoint=use_checkpoint,
                use_scale_shift_norm=use_scale_shift_norm,
            ),
        )
        self._feature_size += ch

        self.output_blocks = nn.ModuleList([])
        for level, mult in list(enumerate(channel_mult))[::-1]:
            for i in range(num_res_blocks + 1):
                ich = input_block_chans.pop()
                layers = [
                    ResBlock(
                        ch + ich,
                        time_embed_dim,
                        dropout,
                        out_channels=int(model_channels * mult),
                        dims=dims,
                        use_checkpoint=use_checkpoint,
                        use_scale_shift_norm=use_scale_shift_norm,
                    )
                ]
                ch = int(model_channels * mult)
                if ds in attention_resolutions:
                    layers.append(
                        AttentionBlock(
                            ch,
                            use_checkpoint=use_checkpoint,
                            num_heads=num_heads_upsample,
                            num_head_channels=num_head_channels,
anton-l's avatar
anton-l committed
399
                            encoder_channels=transformer_dim,
anton-l's avatar
anton-l committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
                        )
                    )
                if level and i == num_res_blocks:
                    out_ch = ch
                    layers.append(
                        ResBlock(
                            ch,
                            time_embed_dim,
                            dropout,
                            out_channels=out_ch,
                            dims=dims,
                            use_checkpoint=use_checkpoint,
                            use_scale_shift_norm=use_scale_shift_norm,
                            up=True,
                        )
                        if resblock_updown
patil-suraj's avatar
patil-suraj committed
416
                        else Upsample(ch, use_conv=conv_resample, dims=dims, out_channels=out_ch)
anton-l's avatar
anton-l committed
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
                    )
                    ds //= 2
                self.output_blocks.append(TimestepEmbedSequential(*layers))
                self._feature_size += ch

        self.out = nn.Sequential(
            normalization(ch, swish=1.0),
            nn.Identity(),
            zero_module(conv_nd(dims, input_ch, out_channels, 3, padding=1)),
        )
        self.use_fp16 = use_fp16

    def convert_to_fp16(self):
        """
        Convert the torso of the model to float16.
        """
        self.input_blocks.apply(convert_module_to_f16)
        self.middle_block.apply(convert_module_to_f16)
        self.output_blocks.apply(convert_module_to_f16)

    def convert_to_fp32(self):
        """
        Convert the torso of the model to float32.
        """
        self.input_blocks.apply(convert_module_to_f32)
        self.middle_block.apply(convert_module_to_f32)
        self.output_blocks.apply(convert_module_to_f32)

445
    def forward(self, x, timesteps):
anton-l's avatar
anton-l committed
446
447
448
        """
        Apply the model to an input batch.

Patrick von Platen's avatar
Patrick von Platen committed
449
450
        :param x: an [N x C x ...] Tensor of inputs. :param timesteps: a 1-D batch of timesteps. :param y: an [N]
        Tensor of labels, if class-conditional. :return: an [N x C x ...] Tensor of outputs.
anton-l's avatar
anton-l committed
451
        """
anton-l's avatar
anton-l committed
452
453

        hs = []
454
455
456
        emb = self.time_embed(
            get_timestep_embedding(timesteps, self.model_channels, flip_sin_to_cos=True, downscale_freq_shift=0)
        )
anton-l's avatar
anton-l committed
457
458
459
460
461
462
463
464
465
466
467
468
469

        h = x.type(self.dtype)
        for module in self.input_blocks:
            h = module(h, emb)
            hs.append(h)
        h = self.middle_block(h, emb)
        for module in self.output_blocks:
            h = torch.cat([h, hs.pop()], dim=1)
            h = module(h, emb)
        h = h.type(x.dtype)
        return self.out(h)


Patrick von Platen's avatar
Patrick von Platen committed
470
class GlideTextToImageUNetModel(GlideUNetModel):
anton-l's avatar
anton-l committed
471
472
473
474
475
476
    """
    A UNetModel that performs super-resolution.

    Expects an extra kwarg `low_res` to condition on a low-resolution image.
    """

477
    def __init__(
Patrick von Platen's avatar
Patrick von Platen committed
478
479
        self,
        in_channels=3,
anton-l's avatar
anton-l committed
480
        resolution=64,
Patrick von Platen's avatar
Patrick von Platen committed
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
        model_channels=192,
        out_channels=6,
        num_res_blocks=3,
        attention_resolutions=(2, 4, 8),
        dropout=0,
        channel_mult=(1, 2, 4, 8),
        conv_resample=True,
        dims=2,
        use_checkpoint=False,
        use_fp16=False,
        num_heads=1,
        num_head_channels=-1,
        num_heads_upsample=-1,
        use_scale_shift_norm=False,
        resblock_updown=False,
        transformer_dim=512,
497
498
499
    ):
        super().__init__(
            in_channels=in_channels,
anton-l's avatar
anton-l committed
500
            resolution=resolution,
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
            model_channels=model_channels,
            out_channels=out_channels,
            num_res_blocks=num_res_blocks,
            attention_resolutions=attention_resolutions,
            dropout=dropout,
            channel_mult=channel_mult,
            conv_resample=conv_resample,
            dims=dims,
            use_checkpoint=use_checkpoint,
            use_fp16=use_fp16,
            num_heads=num_heads,
            num_head_channels=num_head_channels,
            num_heads_upsample=num_heads_upsample,
            use_scale_shift_norm=use_scale_shift_norm,
            resblock_updown=resblock_updown,
Patrick von Platen's avatar
Patrick von Platen committed
516
            transformer_dim=transformer_dim,
517
        )
518
        self.register_to_config(
519
            in_channels=in_channels,
anton-l's avatar
anton-l committed
520
            resolution=resolution,
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
            model_channels=model_channels,
            out_channels=out_channels,
            num_res_blocks=num_res_blocks,
            attention_resolutions=attention_resolutions,
            dropout=dropout,
            channel_mult=channel_mult,
            conv_resample=conv_resample,
            dims=dims,
            use_checkpoint=use_checkpoint,
            use_fp16=use_fp16,
            num_heads=num_heads,
            num_head_channels=num_head_channels,
            num_heads_upsample=num_heads_upsample,
            use_scale_shift_norm=use_scale_shift_norm,
            resblock_updown=resblock_updown,
Patrick von Platen's avatar
Patrick von Platen committed
536
            transformer_dim=transformer_dim,
537
        )
anton-l's avatar
anton-l committed
538

539
        self.transformer_proj = nn.Linear(transformer_dim, self.model_channels * 4)
anton-l's avatar
anton-l committed
540
541

    def forward(self, x, timesteps, transformer_out=None):
anton-l's avatar
anton-l committed
542
        hs = []
543
544
545
        emb = self.time_embed(
            get_timestep_embedding(timesteps, self.model_channels, flip_sin_to_cos=True, downscale_freq_shift=0)
        )
anton-l's avatar
anton-l committed
546
547
548
549
550

        # project the last token
        transformer_proj = self.transformer_proj(transformer_out[:, -1])
        transformer_out = transformer_out.permute(0, 2, 1)  # NLC -> NCL

551
552
        emb = emb + transformer_proj.to(emb)

anton-l's avatar
anton-l committed
553
        h = x
anton-l's avatar
anton-l committed
554
        for module in self.input_blocks:
555
            h = module(h, emb, transformer_out)
anton-l's avatar
anton-l committed
556
            hs.append(h)
557
        h = self.middle_block(h, emb, transformer_out)
anton-l's avatar
anton-l committed
558
        for module in self.output_blocks:
anton-l's avatar
anton-l committed
559
560
            other = hs.pop()
            h = torch.cat([h, other], dim=1)
561
            h = module(h, emb, transformer_out)
anton-l's avatar
anton-l committed
562
        return self.out(h)
anton-l's avatar
anton-l committed
563
564


Patrick von Platen's avatar
Patrick von Platen committed
565
class GlideSuperResUNetModel(GlideUNetModel):
anton-l's avatar
anton-l committed
566
567
568
569
570
571
    """
    A UNetModel that performs super-resolution.

    Expects an extra kwarg `low_res` to condition on a low-resolution image.
    """

572
    def __init__(
Patrick von Platen's avatar
Patrick von Platen committed
573
574
        self,
        in_channels=3,
anton-l's avatar
anton-l committed
575
        resolution=256,
Patrick von Platen's avatar
Patrick von Platen committed
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
        model_channels=192,
        out_channels=6,
        num_res_blocks=3,
        attention_resolutions=(2, 4, 8),
        dropout=0,
        channel_mult=(1, 2, 4, 8),
        conv_resample=True,
        dims=2,
        use_checkpoint=False,
        use_fp16=False,
        num_heads=1,
        num_head_channels=-1,
        num_heads_upsample=-1,
        use_scale_shift_norm=False,
        resblock_updown=False,
591
592
593
    ):
        super().__init__(
            in_channels=in_channels,
anton-l's avatar
anton-l committed
594
            resolution=resolution,
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
            model_channels=model_channels,
            out_channels=out_channels,
            num_res_blocks=num_res_blocks,
            attention_resolutions=attention_resolutions,
            dropout=dropout,
            channel_mult=channel_mult,
            conv_resample=conv_resample,
            dims=dims,
            use_checkpoint=use_checkpoint,
            use_fp16=use_fp16,
            num_heads=num_heads,
            num_head_channels=num_head_channels,
            num_heads_upsample=num_heads_upsample,
            use_scale_shift_norm=use_scale_shift_norm,
            resblock_updown=resblock_updown,
        )
611
        self.register_to_config(
612
            in_channels=in_channels,
anton-l's avatar
anton-l committed
613
            resolution=resolution,
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
            model_channels=model_channels,
            out_channels=out_channels,
            num_res_blocks=num_res_blocks,
            attention_resolutions=attention_resolutions,
            dropout=dropout,
            channel_mult=channel_mult,
            conv_resample=conv_resample,
            dims=dims,
            use_checkpoint=use_checkpoint,
            use_fp16=use_fp16,
            num_heads=num_heads,
            num_head_channels=num_head_channels,
            num_heads_upsample=num_heads_upsample,
            use_scale_shift_norm=use_scale_shift_norm,
            resblock_updown=resblock_updown,
        )
anton-l's avatar
anton-l committed
630

631
    def forward(self, x, timesteps, low_res=None):
anton-l's avatar
anton-l committed
632
633
634
        _, _, new_height, new_width = x.shape
        upsampled = F.interpolate(low_res, (new_height, new_width), mode="bilinear")
        x = torch.cat([x, upsampled], dim=1)
635
636

        hs = []
637
638
639
        emb = self.time_embed(
            get_timestep_embedding(timesteps, self.model_channels, flip_sin_to_cos=True, downscale_freq_shift=0)
        )
640
641
642
643
644
645
646
647
648
649

        h = x
        for module in self.input_blocks:
            h = module(h, emb)
            hs.append(h)
        h = self.middle_block(h, emb)
        for module in self.output_blocks:
            h = torch.cat([h, hs.pop()], dim=1)
            h = module(h, emb)

Patrick von Platen's avatar
Patrick von Platen committed
650
        return self.out(h)