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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from abc import abstractmethod
|
| 2 |
+
from functools import partial
|
| 3 |
+
import math
|
| 4 |
+
from typing import Iterable
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import torch as th
|
| 8 |
+
import torch.nn as nn
|
| 9 |
+
import torch.nn.functional as F
|
| 10 |
+
|
| 11 |
+
from ldm.modules.diffusionmodules.util import (
|
| 12 |
+
checkpoint,
|
| 13 |
+
conv_nd,
|
| 14 |
+
linear,
|
| 15 |
+
avg_pool_nd,
|
| 16 |
+
zero_module,
|
| 17 |
+
normalization,
|
| 18 |
+
timestep_embedding,
|
| 19 |
+
)
|
| 20 |
+
from ldm.modules.attention import SpatialTransformer
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# dummy replace
|
| 24 |
+
def convert_module_to_f16(x):
|
| 25 |
+
pass
|
| 26 |
+
|
| 27 |
+
def convert_module_to_f32(x):
|
| 28 |
+
pass
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
## go
|
| 32 |
+
class AttentionPool2d(nn.Module):
|
| 33 |
+
"""
|
| 34 |
+
Adapted from CLIP: https://github.com/openai/CLIP/blob/main/clip/model.py
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
def __init__(
|
| 38 |
+
self,
|
| 39 |
+
spacial_dim: int,
|
| 40 |
+
embed_dim: int,
|
| 41 |
+
num_heads_channels: int,
|
| 42 |
+
output_dim: int = None,
|
| 43 |
+
):
|
| 44 |
+
super().__init__()
|
| 45 |
+
self.positional_embedding = nn.Parameter(th.randn(embed_dim, spacial_dim ** 2 + 1) / embed_dim ** 0.5)
|
| 46 |
+
self.qkv_proj = conv_nd(1, embed_dim, 3 * embed_dim, 1)
|
| 47 |
+
self.c_proj = conv_nd(1, embed_dim, output_dim or embed_dim, 1)
|
| 48 |
+
self.num_heads = embed_dim // num_heads_channels
|
| 49 |
+
self.attention = QKVAttention(self.num_heads)
|
| 50 |
+
|
| 51 |
+
def forward(self, x):
|
| 52 |
+
b, c, *_spatial = x.shape
|
| 53 |
+
x = x.reshape(b, c, -1) # NC(HW)
|
| 54 |
+
x = th.cat([x.mean(dim=-1, keepdim=True), x], dim=-1) # NC(HW+1)
|
| 55 |
+
x = x + self.positional_embedding[None, :, :].to(x.dtype) # NC(HW+1)
|
| 56 |
+
x = self.qkv_proj(x)
|
| 57 |
+
x = self.attention(x)
|
| 58 |
+
x = self.c_proj(x)
|
| 59 |
+
return x[:, :, 0]
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class TimestepBlock(nn.Module):
|
| 63 |
+
"""
|
| 64 |
+
Any module where forward() takes timestep embeddings as a second argument.
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
@abstractmethod
|
| 68 |
+
def forward(self, x, emb):
|
| 69 |
+
"""
|
| 70 |
+
Apply the module to `x` given `emb` timestep embeddings.
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
class TimestepEmbedSequential(nn.Sequential, TimestepBlock):
|
| 75 |
+
"""
|
| 76 |
+
A sequential module that passes timestep embeddings to the children that
|
| 77 |
+
support it as an extra input.
|
| 78 |
+
"""
|
| 79 |
+
|
| 80 |
+
def forward(self, x, emb, context=None):
|
| 81 |
+
for layer in self:
|
| 82 |
+
if isinstance(layer, TimestepBlock):
|
| 83 |
+
x = layer(x, emb)
|
| 84 |
+
elif isinstance(layer, SpatialTransformer):
|
| 85 |
+
x = layer(x, context)
|
| 86 |
+
else:
|
| 87 |
+
x = layer(x)
|
| 88 |
+
return x
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
class Upsample(nn.Module):
|
| 92 |
+
"""
|
| 93 |
+
An upsampling layer with an optional convolution.
|
| 94 |
+
:param channels: channels in the inputs and outputs.
|
| 95 |
+
:param use_conv: a bool determining if a convolution is applied.
|
| 96 |
+
:param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then
|
| 97 |
+
upsampling occurs in the inner-two dimensions.
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
def __init__(self, channels, use_conv, dims=2, out_channels=None, padding=1):
|
| 101 |
+
super().__init__()
|
| 102 |
+
self.channels = channels
|
| 103 |
+
self.out_channels = out_channels or channels
|
| 104 |
+
self.use_conv = use_conv
|
| 105 |
+
self.dims = dims
|
| 106 |
+
if use_conv:
|
| 107 |
+
self.conv = conv_nd(dims, self.channels, self.out_channels, 3, padding=padding)
|
| 108 |
+
|
| 109 |
+
def forward(self, x):
|
| 110 |
+
assert x.shape[1] == self.channels
|
| 111 |
+
if self.dims == 3:
|
| 112 |
+
x = F.interpolate(
|
| 113 |
+
x, (x.shape[2], x.shape[3] * 2, x.shape[4] * 2), mode="nearest"
|
| 114 |
+
)
|
| 115 |
+
else:
|
| 116 |
+
x = F.interpolate(x, scale_factor=2, mode="nearest")
|
| 117 |
+
if self.use_conv:
|
| 118 |
+
x = self.conv(x)
|
| 119 |
+
return x
|
| 120 |
+
|
| 121 |
+
class TransposedUpsample(nn.Module):
|
| 122 |
+
'Learned 2x upsampling without padding'
|
| 123 |
+
def __init__(self, channels, out_channels=None, ks=5):
|
| 124 |
+
super().__init__()
|
| 125 |
+
self.channels = channels
|
| 126 |
+
self.out_channels = out_channels or channels
|
| 127 |
+
|
| 128 |
+
self.up = nn.ConvTranspose2d(self.channels,self.out_channels,kernel_size=ks,stride=2)
|
| 129 |
+
|
| 130 |
+
def forward(self,x):
|
| 131 |
+
return self.up(x)
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
class Downsample(nn.Module):
|
| 135 |
+
"""
|
| 136 |
+
A downsampling layer with an optional convolution.
|
| 137 |
+
:param channels: channels in the inputs and outputs.
|
| 138 |
+
:param use_conv: a bool determining if a convolution is applied.
|
| 139 |
+
:param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then
|
| 140 |
+
downsampling occurs in the inner-two dimensions.
|
| 141 |
+
"""
|
| 142 |
+
|
| 143 |
+
def __init__(self, channels, use_conv, dims=2, out_channels=None,padding=1):
|
| 144 |
+
super().__init__()
|
| 145 |
+
self.channels = channels
|
| 146 |
+
self.out_channels = out_channels or channels
|
| 147 |
+
self.use_conv = use_conv
|
| 148 |
+
self.dims = dims
|
| 149 |
+
stride = 2 if dims != 3 else (1, 2, 2)
|
| 150 |
+
if use_conv:
|
| 151 |
+
self.op = conv_nd(
|
| 152 |
+
dims, self.channels, self.out_channels, 3, stride=stride, padding=padding
|
| 153 |
+
)
|
| 154 |
+
else:
|
| 155 |
+
assert self.channels == self.out_channels
|
| 156 |
+
self.op = avg_pool_nd(dims, kernel_size=stride, stride=stride)
|
| 157 |
+
|
| 158 |
+
def forward(self, x):
|
| 159 |
+
assert x.shape[1] == self.channels
|
| 160 |
+
return self.op(x)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
class ResBlock(TimestepBlock):
|
| 164 |
+
"""
|
| 165 |
+
A residual block that can optionally change the number of channels.
|
| 166 |
+
:param channels: the number of input channels.
|
| 167 |
+
:param emb_channels: the number of timestep embedding channels.
|
| 168 |
+
:param dropout: the rate of dropout.
|
| 169 |
+
:param out_channels: if specified, the number of out channels.
|
| 170 |
+
:param use_conv: if True and out_channels is specified, use a spatial
|
| 171 |
+
convolution instead of a smaller 1x1 convolution to change the
|
| 172 |
+
channels in the skip connection.
|
| 173 |
+
:param dims: determines if the signal is 1D, 2D, or 3D.
|
| 174 |
+
:param use_checkpoint: if True, use gradient checkpointing on this module.
|
| 175 |
+
:param up: if True, use this block for upsampling.
|
| 176 |
+
:param down: if True, use this block for downsampling.
|
| 177 |
+
"""
|
| 178 |
+
|
| 179 |
+
def __init__(
|
| 180 |
+
self,
|
| 181 |
+
channels,
|
| 182 |
+
emb_channels,
|
| 183 |
+
dropout,
|
| 184 |
+
out_channels=None,
|
| 185 |
+
use_conv=False,
|
| 186 |
+
use_scale_shift_norm=False,
|
| 187 |
+
dims=2,
|
| 188 |
+
use_checkpoint=False,
|
| 189 |
+
up=False,
|
| 190 |
+
down=False,
|
| 191 |
+
):
|
| 192 |
+
super().__init__()
|
| 193 |
+
self.channels = channels
|
| 194 |
+
self.emb_channels = emb_channels
|
| 195 |
+
self.dropout = dropout
|
| 196 |
+
self.out_channels = out_channels or channels
|
| 197 |
+
self.use_conv = use_conv
|
| 198 |
+
self.use_checkpoint = use_checkpoint
|
| 199 |
+
self.use_scale_shift_norm = use_scale_shift_norm
|
| 200 |
+
|
| 201 |
+
self.in_layers = nn.Sequential(
|
| 202 |
+
normalization(channels),
|
| 203 |
+
nn.SiLU(),
|
| 204 |
+
conv_nd(dims, channels, self.out_channels, 3, padding=1),
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
self.updown = up or down
|
| 208 |
+
|
| 209 |
+
if up:
|
| 210 |
+
self.h_upd = Upsample(channels, False, dims)
|
| 211 |
+
self.x_upd = Upsample(channels, False, dims)
|
| 212 |
+
elif down:
|
| 213 |
+
self.h_upd = Downsample(channels, False, dims)
|
| 214 |
+
self.x_upd = Downsample(channels, False, dims)
|
| 215 |
+
else:
|
| 216 |
+
self.h_upd = self.x_upd = nn.Identity()
|
| 217 |
+
|
| 218 |
+
self.emb_layers = nn.Sequential(
|
| 219 |
+
nn.SiLU(),
|
| 220 |
+
linear(
|
| 221 |
+
emb_channels,
|
| 222 |
+
2 * self.out_channels if use_scale_shift_norm else self.out_channels,
|
| 223 |
+
),
|
| 224 |
+
)
|
| 225 |
+
self.out_layers = nn.Sequential(
|
| 226 |
+
normalization(self.out_channels),
|
| 227 |
+
nn.SiLU(),
|
| 228 |
+
nn.Dropout(p=dropout),
|
| 229 |
+
zero_module(
|
| 230 |
+
conv_nd(dims, self.out_channels, self.out_channels, 3, padding=1)
|
| 231 |
+
),
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
if self.out_channels == channels:
|
| 235 |
+
self.skip_connection = nn.Identity()
|
| 236 |
+
elif use_conv:
|
| 237 |
+
self.skip_connection = conv_nd(
|
| 238 |
+
dims, channels, self.out_channels, 3, padding=1
|
| 239 |
+
)
|
| 240 |
+
else:
|
| 241 |
+
self.skip_connection = conv_nd(dims, channels, self.out_channels, 1)
|
| 242 |
+
|
| 243 |
+
def forward(self, x, emb):
|
| 244 |
+
"""
|
| 245 |
+
Apply the block to a Tensor, conditioned on a timestep embedding.
|
| 246 |
+
:param x: an [N x C x ...] Tensor of features.
|
| 247 |
+
:param emb: an [N x emb_channels] Tensor of timestep embeddings.
|
| 248 |
+
:return: an [N x C x ...] Tensor of outputs.
|
| 249 |
+
"""
|
| 250 |
+
return checkpoint(
|
| 251 |
+
self._forward, (x, emb), self.parameters(), self.use_checkpoint
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
def _forward(self, x, emb):
|
| 256 |
+
if self.updown:
|
| 257 |
+
in_rest, in_conv = self.in_layers[:-1], self.in_layers[-1]
|
| 258 |
+
h = in_rest(x)
|
| 259 |
+
h = self.h_upd(h)
|
| 260 |
+
x = self.x_upd(x)
|
| 261 |
+
h = in_conv(h)
|
| 262 |
+
else:
|
| 263 |
+
h = self.in_layers(x)
|
| 264 |
+
emb_out = self.emb_layers(emb).type(h.dtype)
|
| 265 |
+
while len(emb_out.shape) < len(h.shape):
|
| 266 |
+
emb_out = emb_out[..., None]
|
| 267 |
+
if self.use_scale_shift_norm:
|
| 268 |
+
out_norm, out_rest = self.out_layers[0], self.out_layers[1:]
|
| 269 |
+
scale, shift = th.chunk(emb_out, 2, dim=1)
|
| 270 |
+
h = out_norm(h) * (1 + scale) + shift
|
| 271 |
+
h = out_rest(h)
|
| 272 |
+
else:
|
| 273 |
+
h = h + emb_out
|
| 274 |
+
h = self.out_layers(h)
|
| 275 |
+
return self.skip_connection(x) + h
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
class AttentionBlock(nn.Module):
|
| 279 |
+
"""
|
| 280 |
+
An attention block that allows spatial positions to attend to each other.
|
| 281 |
+
Originally ported from here, but adapted to the N-d case.
|
| 282 |
+
https://github.com/hojonathanho/diffusion/blob/1e0dceb3b3495bbe19116a5e1b3596cd0706c543/diffusion_tf/models/unet.py#L66.
|
| 283 |
+
"""
|
| 284 |
+
|
| 285 |
+
def __init__(
|
| 286 |
+
self,
|
| 287 |
+
channels,
|
| 288 |
+
num_heads=1,
|
| 289 |
+
num_head_channels=-1,
|
| 290 |
+
use_checkpoint=False,
|
| 291 |
+
use_new_attention_order=False,
|
| 292 |
+
):
|
| 293 |
+
super().__init__()
|
| 294 |
+
self.channels = channels
|
| 295 |
+
if num_head_channels == -1:
|
| 296 |
+
self.num_heads = num_heads
|
| 297 |
+
else:
|
| 298 |
+
assert (
|
| 299 |
+
channels % num_head_channels == 0
|
| 300 |
+
), f"q,k,v channels {channels} is not divisible by num_head_channels {num_head_channels}"
|
| 301 |
+
self.num_heads = channels // num_head_channels
|
| 302 |
+
self.use_checkpoint = use_checkpoint
|
| 303 |
+
self.norm = normalization(channels)
|
| 304 |
+
self.qkv = conv_nd(1, channels, channels * 3, 1)
|
| 305 |
+
if use_new_attention_order:
|
| 306 |
+
# split qkv before split heads
|
| 307 |
+
self.attention = QKVAttention(self.num_heads)
|
| 308 |
+
else:
|
| 309 |
+
# split heads before split qkv
|
| 310 |
+
self.attention = QKVAttentionLegacy(self.num_heads)
|
| 311 |
+
|
| 312 |
+
self.proj_out = zero_module(conv_nd(1, channels, channels, 1))
|
| 313 |
+
|
| 314 |
+
def forward(self, x):
|
| 315 |
+
return checkpoint(self._forward, (x,), self.parameters(), True) # TODO: check checkpoint usage, is True # TODO: fix the .half call!!!
|
| 316 |
+
#return pt_checkpoint(self._forward, x) # pytorch
|
| 317 |
+
|
| 318 |
+
def _forward(self, x):
|
| 319 |
+
b, c, *spatial = x.shape
|
| 320 |
+
x = x.reshape(b, c, -1)
|
| 321 |
+
qkv = self.qkv(self.norm(x))
|
| 322 |
+
h = self.attention(qkv)
|
| 323 |
+
h = self.proj_out(h)
|
| 324 |
+
return (x + h).reshape(b, c, *spatial)
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
def count_flops_attn(model, _x, y):
|
| 328 |
+
"""
|
| 329 |
+
A counter for the `thop` package to count the operations in an
|
| 330 |
+
attention operation.
|
| 331 |
+
Meant to be used like:
|
| 332 |
+
macs, params = thop.profile(
|
| 333 |
+
model,
|
| 334 |
+
inputs=(inputs, timestamps),
|
| 335 |
+
custom_ops={QKVAttention: QKVAttention.count_flops},
|
| 336 |
+
)
|
| 337 |
+
"""
|
| 338 |
+
b, c, *spatial = y[0].shape
|
| 339 |
+
num_spatial = int(np.prod(spatial))
|
| 340 |
+
# We perform two matmuls with the same number of ops.
|
| 341 |
+
# The first computes the weight matrix, the second computes
|
| 342 |
+
# the combination of the value vectors.
|
| 343 |
+
matmul_ops = 2 * b * (num_spatial ** 2) * c
|
| 344 |
+
model.total_ops += th.DoubleTensor([matmul_ops])
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
class QKVAttentionLegacy(nn.Module):
|
| 348 |
+
"""
|
| 349 |
+
A module which performs QKV attention. Matches legacy QKVAttention + input/ouput heads shaping
|
| 350 |
+
"""
|
| 351 |
+
|
| 352 |
+
def __init__(self, n_heads):
|
| 353 |
+
super().__init__()
|
| 354 |
+
self.n_heads = n_heads
|
| 355 |
+
|
| 356 |
+
def forward(self, qkv):
|
| 357 |
+
"""
|
| 358 |
+
Apply QKV attention.
|
| 359 |
+
:param qkv: an [N x (H * 3 * C) x T] tensor of Qs, Ks, and Vs.
|
| 360 |
+
:return: an [N x (H * C) x T] tensor after attention.
|
| 361 |
+
"""
|
| 362 |
+
bs, width, length = qkv.shape
|
| 363 |
+
assert width % (3 * self.n_heads) == 0
|
| 364 |
+
ch = width // (3 * self.n_heads)
|
| 365 |
+
q, k, v = qkv.reshape(bs * self.n_heads, ch * 3, length).split(ch, dim=1)
|
| 366 |
+
scale = 1 / math.sqrt(math.sqrt(ch))
|
| 367 |
+
weight = th.einsum(
|
| 368 |
+
"bct,bcs->bts", q * scale, k * scale
|
| 369 |
+
) # More stable with f16 than dividing afterwards
|
| 370 |
+
weight = th.softmax(weight.float(), dim=-1).type(weight.dtype)
|
| 371 |
+
a = th.einsum("bts,bcs->bct", weight, v)
|
| 372 |
+
return a.reshape(bs, -1, length)
|
| 373 |
+
|
| 374 |
+
@staticmethod
|
| 375 |
+
def count_flops(model, _x, y):
|
| 376 |
+
return count_flops_attn(model, _x, y)
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
class QKVAttention(nn.Module):
|
| 380 |
+
"""
|
| 381 |
+
A module which performs QKV attention and splits in a different order.
|
| 382 |
+
"""
|
| 383 |
+
|
| 384 |
+
def __init__(self, n_heads):
|
| 385 |
+
super().__init__()
|
| 386 |
+
self.n_heads = n_heads
|
| 387 |
+
|
| 388 |
+
def forward(self, qkv):
|
| 389 |
+
"""
|
| 390 |
+
Apply QKV attention.
|
| 391 |
+
:param qkv: an [N x (3 * H * C) x T] tensor of Qs, Ks, and Vs.
|
| 392 |
+
:return: an [N x (H * C) x T] tensor after attention.
|
| 393 |
+
"""
|
| 394 |
+
bs, width, length = qkv.shape
|
| 395 |
+
assert width % (3 * self.n_heads) == 0
|
| 396 |
+
ch = width // (3 * self.n_heads)
|
| 397 |
+
q, k, v = qkv.chunk(3, dim=1)
|
| 398 |
+
scale = 1 / math.sqrt(math.sqrt(ch))
|
| 399 |
+
weight = th.einsum(
|
| 400 |
+
"bct,bcs->bts",
|
| 401 |
+
(q * scale).view(bs * self.n_heads, ch, length),
|
| 402 |
+
(k * scale).view(bs * self.n_heads, ch, length),
|
| 403 |
+
) # More stable with f16 than dividing afterwards
|
| 404 |
+
weight = th.softmax(weight.float(), dim=-1).type(weight.dtype)
|
| 405 |
+
a = th.einsum("bts,bcs->bct", weight, v.reshape(bs * self.n_heads, ch, length))
|
| 406 |
+
return a.reshape(bs, -1, length)
|
| 407 |
+
|
| 408 |
+
@staticmethod
|
| 409 |
+
def count_flops(model, _x, y):
|
| 410 |
+
return count_flops_attn(model, _x, y)
|
| 411 |
+
|
| 412 |
+
|
| 413 |
+
class UNetModel(nn.Module):
|
| 414 |
+
"""
|
| 415 |
+
The full UNet model with attention and timestep embedding.
|
| 416 |
+
:param in_channels: channels in the input Tensor.
|
| 417 |
+
:param model_channels: base channel count for the model.
|
| 418 |
+
:param out_channels: channels in the output Tensor.
|
| 419 |
+
:param num_res_blocks: number of residual blocks per downsample.
|
| 420 |
+
:param attention_resolutions: a collection of downsample rates at which
|
| 421 |
+
attention will take place. May be a set, list, or tuple.
|
| 422 |
+
For example, if this contains 4, then at 4x downsampling, attention
|
| 423 |
+
will be used.
|
| 424 |
+
:param dropout: the dropout probability.
|
| 425 |
+
:param channel_mult: channel multiplier for each level of the UNet.
|
| 426 |
+
:param conv_resample: if True, use learned convolutions for upsampling and
|
| 427 |
+
downsampling.
|
| 428 |
+
:param dims: determines if the signal is 1D, 2D, or 3D.
|
| 429 |
+
:param num_classes: if specified (as an int), then this model will be
|
| 430 |
+
class-conditional with `num_classes` classes.
|
| 431 |
+
:param use_checkpoint: use gradient checkpointing to reduce memory usage.
|
| 432 |
+
:param num_heads: the number of attention heads in each attention layer.
|
| 433 |
+
:param num_heads_channels: if specified, ignore num_heads and instead use
|
| 434 |
+
a fixed channel width per attention head.
|
| 435 |
+
:param num_heads_upsample: works with num_heads to set a different number
|
| 436 |
+
of heads for upsampling. Deprecated.
|
| 437 |
+
:param use_scale_shift_norm: use a FiLM-like conditioning mechanism.
|
| 438 |
+
:param resblock_updown: use residual blocks for up/downsampling.
|
| 439 |
+
:param use_new_attention_order: use a different attention pattern for potentially
|
| 440 |
+
increased efficiency.
|
| 441 |
+
"""
|
| 442 |
+
|
| 443 |
+
def __init__(
|
| 444 |
+
self,
|
| 445 |
+
image_size,
|
| 446 |
+
in_channels,
|
| 447 |
+
model_channels,
|
| 448 |
+
out_channels,
|
| 449 |
+
num_res_blocks,
|
| 450 |
+
attention_resolutions,
|
| 451 |
+
dropout=0,
|
| 452 |
+
channel_mult=(1, 2, 4, 8),
|
| 453 |
+
conv_resample=True,
|
| 454 |
+
dims=2,
|
| 455 |
+
num_classes=None,
|
| 456 |
+
use_checkpoint=False,
|
| 457 |
+
use_fp16=False,
|
| 458 |
+
num_heads=-1,
|
| 459 |
+
num_head_channels=-1,
|
| 460 |
+
num_heads_upsample=-1,
|
| 461 |
+
use_scale_shift_norm=False,
|
| 462 |
+
resblock_updown=False,
|
| 463 |
+
use_new_attention_order=False,
|
| 464 |
+
use_spatial_transformer=False, # custom transformer support
|
| 465 |
+
transformer_depth=1, # custom transformer support
|
| 466 |
+
context_dim=None, # custom transformer support
|
| 467 |
+
n_embed=None, # custom support for prediction of discrete ids into codebook of first stage vq model
|
| 468 |
+
legacy=True,
|
| 469 |
+
):
|
| 470 |
+
super().__init__()
|
| 471 |
+
if use_spatial_transformer:
|
| 472 |
+
assert context_dim is not None, 'Fool!! You forgot to include the dimension of your cross-attention conditioning...'
|
| 473 |
+
|
| 474 |
+
if context_dim is not None:
|
| 475 |
+
assert use_spatial_transformer, 'Fool!! You forgot to use the spatial transformer for your cross-attention conditioning...'
|
| 476 |
+
from omegaconf.listconfig import ListConfig
|
| 477 |
+
if type(context_dim) == ListConfig:
|
| 478 |
+
context_dim = list(context_dim)
|
| 479 |
+
|
| 480 |
+
if num_heads_upsample == -1:
|
| 481 |
+
num_heads_upsample = num_heads
|
| 482 |
+
|
| 483 |
+
if num_heads == -1:
|
| 484 |
+
assert num_head_channels != -1, 'Either num_heads or num_head_channels has to be set'
|
| 485 |
+
|
| 486 |
+
if num_head_channels == -1:
|
| 487 |
+
assert num_heads != -1, 'Either num_heads or num_head_channels has to be set'
|
| 488 |
+
|
| 489 |
+
self.image_size = image_size
|
| 490 |
+
self.in_channels = in_channels
|
| 491 |
+
self.model_channels = model_channels
|
| 492 |
+
self.out_channels = out_channels
|
| 493 |
+
self.num_res_blocks = num_res_blocks
|
| 494 |
+
self.attention_resolutions = attention_resolutions
|
| 495 |
+
self.dropout = dropout
|
| 496 |
+
self.channel_mult = channel_mult
|
| 497 |
+
self.conv_resample = conv_resample
|
| 498 |
+
self.num_classes = num_classes
|
| 499 |
+
self.use_checkpoint = use_checkpoint
|
| 500 |
+
self.dtype = th.float16 if use_fp16 else th.float32
|
| 501 |
+
self.num_heads = num_heads
|
| 502 |
+
self.num_head_channels = num_head_channels
|
| 503 |
+
self.num_heads_upsample = num_heads_upsample
|
| 504 |
+
self.predict_codebook_ids = n_embed is not None
|
| 505 |
+
|
| 506 |
+
time_embed_dim = model_channels * 4
|
| 507 |
+
self.time_embed = nn.Sequential(
|
| 508 |
+
linear(model_channels, time_embed_dim),
|
| 509 |
+
nn.SiLU(),
|
| 510 |
+
linear(time_embed_dim, time_embed_dim),
|
| 511 |
+
)
|
| 512 |
+
|
| 513 |
+
if self.num_classes is not None:
|
| 514 |
+
self.label_emb = nn.Embedding(num_classes, time_embed_dim)
|
| 515 |
+
|
| 516 |
+
self.input_blocks = nn.ModuleList(
|
| 517 |
+
[
|
| 518 |
+
TimestepEmbedSequential(
|
| 519 |
+
conv_nd(dims, in_channels, model_channels, 3, padding=1)
|
| 520 |
+
)
|
| 521 |
+
]
|
| 522 |
+
)
|
| 523 |
+
self._feature_size = model_channels
|
| 524 |
+
input_block_chans = [model_channels]
|
| 525 |
+
ch = model_channels
|
| 526 |
+
ds = 1
|
| 527 |
+
for level, mult in enumerate(channel_mult):
|
| 528 |
+
for _ in range(num_res_blocks):
|
| 529 |
+
layers = [
|
| 530 |
+
ResBlock(
|
| 531 |
+
ch,
|
| 532 |
+
time_embed_dim,
|
| 533 |
+
dropout,
|
| 534 |
+
out_channels=mult * model_channels,
|
| 535 |
+
dims=dims,
|
| 536 |
+
use_checkpoint=use_checkpoint,
|
| 537 |
+
use_scale_shift_norm=use_scale_shift_norm,
|
| 538 |
+
)
|
| 539 |
+
]
|
| 540 |
+
ch = mult * model_channels
|
| 541 |
+
if ds in attention_resolutions:
|
| 542 |
+
if num_head_channels == -1:
|
| 543 |
+
dim_head = ch // num_heads
|
| 544 |
+
else:
|
| 545 |
+
num_heads = ch // num_head_channels
|
| 546 |
+
dim_head = num_head_channels
|
| 547 |
+
if legacy:
|
| 548 |
+
#num_heads = 1
|
| 549 |
+
dim_head = ch // num_heads if use_spatial_transformer else num_head_channels
|
| 550 |
+
layers.append(
|
| 551 |
+
AttentionBlock(
|
| 552 |
+
ch,
|
| 553 |
+
use_checkpoint=use_checkpoint,
|
| 554 |
+
num_heads=num_heads,
|
| 555 |
+
num_head_channels=dim_head,
|
| 556 |
+
use_new_attention_order=use_new_attention_order,
|
| 557 |
+
) if not use_spatial_transformer else SpatialTransformer(
|
| 558 |
+
ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim
|
| 559 |
+
)
|
| 560 |
+
)
|
| 561 |
+
self.input_blocks.append(TimestepEmbedSequential(*layers))
|
| 562 |
+
self._feature_size += ch
|
| 563 |
+
input_block_chans.append(ch)
|
| 564 |
+
if level != len(channel_mult) - 1:
|
| 565 |
+
out_ch = ch
|
| 566 |
+
self.input_blocks.append(
|
| 567 |
+
TimestepEmbedSequential(
|
| 568 |
+
ResBlock(
|
| 569 |
+
ch,
|
| 570 |
+
time_embed_dim,
|
| 571 |
+
dropout,
|
| 572 |
+
out_channels=out_ch,
|
| 573 |
+
dims=dims,
|
| 574 |
+
use_checkpoint=use_checkpoint,
|
| 575 |
+
use_scale_shift_norm=use_scale_shift_norm,
|
| 576 |
+
down=True,
|
| 577 |
+
)
|
| 578 |
+
if resblock_updown
|
| 579 |
+
else Downsample(
|
| 580 |
+
ch, conv_resample, dims=dims, out_channels=out_ch
|
| 581 |
+
)
|
| 582 |
+
)
|
| 583 |
+
)
|
| 584 |
+
ch = out_ch
|
| 585 |
+
input_block_chans.append(ch)
|
| 586 |
+
ds *= 2
|
| 587 |
+
self._feature_size += ch
|
| 588 |
+
|
| 589 |
+
if num_head_channels == -1:
|
| 590 |
+
dim_head = ch // num_heads
|
| 591 |
+
else:
|
| 592 |
+
num_heads = ch // num_head_channels
|
| 593 |
+
dim_head = num_head_channels
|
| 594 |
+
if legacy:
|
| 595 |
+
#num_heads = 1
|
| 596 |
+
dim_head = ch // num_heads if use_spatial_transformer else num_head_channels
|
| 597 |
+
self.middle_block = TimestepEmbedSequential(
|
| 598 |
+
ResBlock(
|
| 599 |
+
ch,
|
| 600 |
+
time_embed_dim,
|
| 601 |
+
dropout,
|
| 602 |
+
dims=dims,
|
| 603 |
+
use_checkpoint=use_checkpoint,
|
| 604 |
+
use_scale_shift_norm=use_scale_shift_norm,
|
| 605 |
+
),
|
| 606 |
+
AttentionBlock(
|
| 607 |
+
ch,
|
| 608 |
+
use_checkpoint=use_checkpoint,
|
| 609 |
+
num_heads=num_heads,
|
| 610 |
+
num_head_channels=dim_head,
|
| 611 |
+
use_new_attention_order=use_new_attention_order,
|
| 612 |
+
) if not use_spatial_transformer else SpatialTransformer(
|
| 613 |
+
ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim
|
| 614 |
+
),
|
| 615 |
+
ResBlock(
|
| 616 |
+
ch,
|
| 617 |
+
time_embed_dim,
|
| 618 |
+
dropout,
|
| 619 |
+
dims=dims,
|
| 620 |
+
use_checkpoint=use_checkpoint,
|
| 621 |
+
use_scale_shift_norm=use_scale_shift_norm,
|
| 622 |
+
),
|
| 623 |
+
)
|
| 624 |
+
self._feature_size += ch
|
| 625 |
+
|
| 626 |
+
self.output_blocks = nn.ModuleList([])
|
| 627 |
+
for level, mult in list(enumerate(channel_mult))[::-1]:
|
| 628 |
+
for i in range(num_res_blocks + 1):
|
| 629 |
+
ich = input_block_chans.pop()
|
| 630 |
+
layers = [
|
| 631 |
+
ResBlock(
|
| 632 |
+
ch + ich,
|
| 633 |
+
time_embed_dim,
|
| 634 |
+
dropout,
|
| 635 |
+
out_channels=model_channels * mult,
|
| 636 |
+
dims=dims,
|
| 637 |
+
use_checkpoint=use_checkpoint,
|
| 638 |
+
use_scale_shift_norm=use_scale_shift_norm,
|
| 639 |
+
)
|
| 640 |
+
]
|
| 641 |
+
ch = model_channels * mult
|
| 642 |
+
if ds in attention_resolutions:
|
| 643 |
+
if num_head_channels == -1:
|
| 644 |
+
dim_head = ch // num_heads
|
| 645 |
+
else:
|
| 646 |
+
num_heads = ch // num_head_channels
|
| 647 |
+
dim_head = num_head_channels
|
| 648 |
+
if legacy:
|
| 649 |
+
#num_heads = 1
|
| 650 |
+
dim_head = ch // num_heads if use_spatial_transformer else num_head_channels
|
| 651 |
+
layers.append(
|
| 652 |
+
AttentionBlock(
|
| 653 |
+
ch,
|
| 654 |
+
use_checkpoint=use_checkpoint,
|
| 655 |
+
num_heads=num_heads_upsample,
|
| 656 |
+
num_head_channels=dim_head,
|
| 657 |
+
use_new_attention_order=use_new_attention_order,
|
| 658 |
+
) if not use_spatial_transformer else SpatialTransformer(
|
| 659 |
+
ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim
|
| 660 |
+
)
|
| 661 |
+
)
|
| 662 |
+
if level and i == num_res_blocks:
|
| 663 |
+
out_ch = ch
|
| 664 |
+
layers.append(
|
| 665 |
+
ResBlock(
|
| 666 |
+
ch,
|
| 667 |
+
time_embed_dim,
|
| 668 |
+
dropout,
|
| 669 |
+
out_channels=out_ch,
|
| 670 |
+
dims=dims,
|
| 671 |
+
use_checkpoint=use_checkpoint,
|
| 672 |
+
use_scale_shift_norm=use_scale_shift_norm,
|
| 673 |
+
up=True,
|
| 674 |
+
)
|
| 675 |
+
if resblock_updown
|
| 676 |
+
else Upsample(ch, conv_resample, dims=dims, out_channels=out_ch)
|
| 677 |
+
)
|
| 678 |
+
ds //= 2
|
| 679 |
+
self.output_blocks.append(TimestepEmbedSequential(*layers))
|
| 680 |
+
self._feature_size += ch
|
| 681 |
+
|
| 682 |
+
self.out = nn.Sequential(
|
| 683 |
+
normalization(ch),
|
| 684 |
+
nn.SiLU(),
|
| 685 |
+
zero_module(conv_nd(dims, model_channels, out_channels, 3, padding=1)),
|
| 686 |
+
)
|
| 687 |
+
if self.predict_codebook_ids:
|
| 688 |
+
self.id_predictor = nn.Sequential(
|
| 689 |
+
normalization(ch),
|
| 690 |
+
conv_nd(dims, model_channels, n_embed, 1),
|
| 691 |
+
#nn.LogSoftmax(dim=1) # change to cross_entropy and produce non-normalized logits
|
| 692 |
+
)
|
| 693 |
+
|
| 694 |
+
def convert_to_fp16(self):
|
| 695 |
+
"""
|
| 696 |
+
Convert the torso of the model to float16.
|
| 697 |
+
"""
|
| 698 |
+
self.input_blocks.apply(convert_module_to_f16)
|
| 699 |
+
self.middle_block.apply(convert_module_to_f16)
|
| 700 |
+
self.output_blocks.apply(convert_module_to_f16)
|
| 701 |
+
|
| 702 |
+
def convert_to_fp32(self):
|
| 703 |
+
"""
|
| 704 |
+
Convert the torso of the model to float32.
|
| 705 |
+
"""
|
| 706 |
+
self.input_blocks.apply(convert_module_to_f32)
|
| 707 |
+
self.middle_block.apply(convert_module_to_f32)
|
| 708 |
+
self.output_blocks.apply(convert_module_to_f32)
|
| 709 |
+
|
| 710 |
+
def forward(self, x, timesteps=None, context=None, y=None,**kwargs):
|
| 711 |
+
"""
|
| 712 |
+
Apply the model to an input batch.
|
| 713 |
+
:param x: an [N x C x ...] Tensor of inputs.
|
| 714 |
+
:param timesteps: a 1-D batch of timesteps.
|
| 715 |
+
:param context: conditioning plugged in via crossattn
|
| 716 |
+
:param y: an [N] Tensor of labels, if class-conditional.
|
| 717 |
+
:return: an [N x C x ...] Tensor of outputs.
|
| 718 |
+
"""
|
| 719 |
+
assert (y is not None) == (
|
| 720 |
+
self.num_classes is not None
|
| 721 |
+
), "must specify y if and only if the model is class-conditional"
|
| 722 |
+
hs = []
|
| 723 |
+
t_emb = timestep_embedding(timesteps, self.model_channels, repeat_only=False)
|
| 724 |
+
emb = self.time_embed(t_emb)
|
| 725 |
+
|
| 726 |
+
if self.num_classes is not None:
|
| 727 |
+
assert y.shape == (x.shape[0],)
|
| 728 |
+
emb = emb + self.label_emb(y)
|
| 729 |
+
|
| 730 |
+
h = x.type(self.dtype)
|
| 731 |
+
for module in self.input_blocks:
|
| 732 |
+
h = module(h, emb, context)
|
| 733 |
+
hs.append(h)
|
| 734 |
+
h = self.middle_block(h, emb, context)
|
| 735 |
+
for module in self.output_blocks:
|
| 736 |
+
h = th.cat([h, hs.pop()], dim=1)
|
| 737 |
+
h = module(h, emb, context)
|
| 738 |
+
h = h.type(x.dtype)
|
| 739 |
+
if self.predict_codebook_ids:
|
| 740 |
+
return self.id_predictor(h)
|
| 741 |
+
else:
|
| 742 |
+
return self.out(h)
|
| 743 |
+
|
| 744 |
+
|
| 745 |
+
class EncoderUNetModel(nn.Module):
|
| 746 |
+
"""
|
| 747 |
+
The half UNet model with attention and timestep embedding.
|
| 748 |
+
For usage, see UNet.
|
| 749 |
+
"""
|
| 750 |
+
|
| 751 |
+
def __init__(
|
| 752 |
+
self,
|
| 753 |
+
image_size,
|
| 754 |
+
in_channels,
|
| 755 |
+
model_channels,
|
| 756 |
+
out_channels,
|
| 757 |
+
num_res_blocks,
|
| 758 |
+
attention_resolutions,
|
| 759 |
+
dropout=0,
|
| 760 |
+
channel_mult=(1, 2, 4, 8),
|
| 761 |
+
conv_resample=True,
|
| 762 |
+
dims=2,
|
| 763 |
+
use_checkpoint=False,
|
| 764 |
+
use_fp16=False,
|
| 765 |
+
num_heads=1,
|
| 766 |
+
num_head_channels=-1,
|
| 767 |
+
num_heads_upsample=-1,
|
| 768 |
+
use_scale_shift_norm=False,
|
| 769 |
+
resblock_updown=False,
|
| 770 |
+
use_new_attention_order=False,
|
| 771 |
+
pool="adaptive",
|
| 772 |
+
*args,
|
| 773 |
+
**kwargs
|
| 774 |
+
):
|
| 775 |
+
super().__init__()
|
| 776 |
+
|
| 777 |
+
if num_heads_upsample == -1:
|
| 778 |
+
num_heads_upsample = num_heads
|
| 779 |
+
|
| 780 |
+
self.in_channels = in_channels
|
| 781 |
+
self.model_channels = model_channels
|
| 782 |
+
self.out_channels = out_channels
|
| 783 |
+
self.num_res_blocks = num_res_blocks
|
| 784 |
+
self.attention_resolutions = attention_resolutions
|
| 785 |
+
self.dropout = dropout
|
| 786 |
+
self.channel_mult = channel_mult
|
| 787 |
+
self.conv_resample = conv_resample
|
| 788 |
+
self.use_checkpoint = use_checkpoint
|
| 789 |
+
self.dtype = th.float16 if use_fp16 else th.float32
|
| 790 |
+
self.num_heads = num_heads
|
| 791 |
+
self.num_head_channels = num_head_channels
|
| 792 |
+
self.num_heads_upsample = num_heads_upsample
|
| 793 |
+
|
| 794 |
+
time_embed_dim = model_channels * 4
|
| 795 |
+
self.time_embed = nn.Sequential(
|
| 796 |
+
linear(model_channels, time_embed_dim),
|
| 797 |
+
nn.SiLU(),
|
| 798 |
+
linear(time_embed_dim, time_embed_dim),
|
| 799 |
+
)
|
| 800 |
+
|
| 801 |
+
self.input_blocks = nn.ModuleList(
|
| 802 |
+
[
|
| 803 |
+
TimestepEmbedSequential(
|
| 804 |
+
conv_nd(dims, in_channels, model_channels, 3, padding=1)
|
| 805 |
+
)
|
| 806 |
+
]
|
| 807 |
+
)
|
| 808 |
+
self._feature_size = model_channels
|
| 809 |
+
input_block_chans = [model_channels]
|
| 810 |
+
ch = model_channels
|
| 811 |
+
ds = 1
|
| 812 |
+
for level, mult in enumerate(channel_mult):
|
| 813 |
+
for _ in range(num_res_blocks):
|
| 814 |
+
layers = [
|
| 815 |
+
ResBlock(
|
| 816 |
+
ch,
|
| 817 |
+
time_embed_dim,
|
| 818 |
+
dropout,
|
| 819 |
+
out_channels=mult * model_channels,
|
| 820 |
+
dims=dims,
|
| 821 |
+
use_checkpoint=use_checkpoint,
|
| 822 |
+
use_scale_shift_norm=use_scale_shift_norm,
|
| 823 |
+
)
|
| 824 |
+
]
|
| 825 |
+
ch = mult * model_channels
|
| 826 |
+
if ds in attention_resolutions:
|
| 827 |
+
layers.append(
|
| 828 |
+
AttentionBlock(
|
| 829 |
+
ch,
|
| 830 |
+
use_checkpoint=use_checkpoint,
|
| 831 |
+
num_heads=num_heads,
|
| 832 |
+
num_head_channels=num_head_channels,
|
| 833 |
+
use_new_attention_order=use_new_attention_order,
|
| 834 |
+
)
|
| 835 |
+
)
|
| 836 |
+
self.input_blocks.append(TimestepEmbedSequential(*layers))
|
| 837 |
+
self._feature_size += ch
|
| 838 |
+
input_block_chans.append(ch)
|
| 839 |
+
if level != len(channel_mult) - 1:
|
| 840 |
+
out_ch = ch
|
| 841 |
+
self.input_blocks.append(
|
| 842 |
+
TimestepEmbedSequential(
|
| 843 |
+
ResBlock(
|
| 844 |
+
ch,
|
| 845 |
+
time_embed_dim,
|
| 846 |
+
dropout,
|
| 847 |
+
out_channels=out_ch,
|
| 848 |
+
dims=dims,
|
| 849 |
+
use_checkpoint=use_checkpoint,
|
| 850 |
+
use_scale_shift_norm=use_scale_shift_norm,
|
| 851 |
+
down=True,
|
| 852 |
+
)
|
| 853 |
+
if resblock_updown
|
| 854 |
+
else Downsample(
|
| 855 |
+
ch, conv_resample, dims=dims, out_channels=out_ch
|
| 856 |
+
)
|
| 857 |
+
)
|
| 858 |
+
)
|
| 859 |
+
ch = out_ch
|
| 860 |
+
input_block_chans.append(ch)
|
| 861 |
+
ds *= 2
|
| 862 |
+
self._feature_size += ch
|
| 863 |
+
|
| 864 |
+
self.middle_block = TimestepEmbedSequential(
|
| 865 |
+
ResBlock(
|
| 866 |
+
ch,
|
| 867 |
+
time_embed_dim,
|
| 868 |
+
dropout,
|
| 869 |
+
dims=dims,
|
| 870 |
+
use_checkpoint=use_checkpoint,
|
| 871 |
+
use_scale_shift_norm=use_scale_shift_norm,
|
| 872 |
+
),
|
| 873 |
+
AttentionBlock(
|
| 874 |
+
ch,
|
| 875 |
+
use_checkpoint=use_checkpoint,
|
| 876 |
+
num_heads=num_heads,
|
| 877 |
+
num_head_channels=num_head_channels,
|
| 878 |
+
use_new_attention_order=use_new_attention_order,
|
| 879 |
+
),
|
| 880 |
+
ResBlock(
|
| 881 |
+
ch,
|
| 882 |
+
time_embed_dim,
|
| 883 |
+
dropout,
|
| 884 |
+
dims=dims,
|
| 885 |
+
use_checkpoint=use_checkpoint,
|
| 886 |
+
use_scale_shift_norm=use_scale_shift_norm,
|
| 887 |
+
),
|
| 888 |
+
)
|
| 889 |
+
self._feature_size += ch
|
| 890 |
+
self.pool = pool
|
| 891 |
+
if pool == "adaptive":
|
| 892 |
+
self.out = nn.Sequential(
|
| 893 |
+
normalization(ch),
|
| 894 |
+
nn.SiLU(),
|
| 895 |
+
nn.AdaptiveAvgPool2d((1, 1)),
|
| 896 |
+
zero_module(conv_nd(dims, ch, out_channels, 1)),
|
| 897 |
+
nn.Flatten(),
|
| 898 |
+
)
|
| 899 |
+
elif pool == "attention":
|
| 900 |
+
assert num_head_channels != -1
|
| 901 |
+
self.out = nn.Sequential(
|
| 902 |
+
normalization(ch),
|
| 903 |
+
nn.SiLU(),
|
| 904 |
+
AttentionPool2d(
|
| 905 |
+
(image_size // ds), ch, num_head_channels, out_channels
|
| 906 |
+
),
|
| 907 |
+
)
|
| 908 |
+
elif pool == "spatial":
|
| 909 |
+
self.out = nn.Sequential(
|
| 910 |
+
nn.Linear(self._feature_size, 2048),
|
| 911 |
+
nn.ReLU(),
|
| 912 |
+
nn.Linear(2048, self.out_channels),
|
| 913 |
+
)
|
| 914 |
+
elif pool == "spatial_v2":
|
| 915 |
+
self.out = nn.Sequential(
|
| 916 |
+
nn.Linear(self._feature_size, 2048),
|
| 917 |
+
normalization(2048),
|
| 918 |
+
nn.SiLU(),
|
| 919 |
+
nn.Linear(2048, self.out_channels),
|
| 920 |
+
)
|
| 921 |
+
else:
|
| 922 |
+
raise NotImplementedError(f"Unexpected {pool} pooling")
|
| 923 |
+
|
| 924 |
+
def convert_to_fp16(self):
|
| 925 |
+
"""
|
| 926 |
+
Convert the torso of the model to float16.
|
| 927 |
+
"""
|
| 928 |
+
self.input_blocks.apply(convert_module_to_f16)
|
| 929 |
+
self.middle_block.apply(convert_module_to_f16)
|
| 930 |
+
|
| 931 |
+
def convert_to_fp32(self):
|
| 932 |
+
"""
|
| 933 |
+
Convert the torso of the model to float32.
|
| 934 |
+
"""
|
| 935 |
+
self.input_blocks.apply(convert_module_to_f32)
|
| 936 |
+
self.middle_block.apply(convert_module_to_f32)
|
| 937 |
+
|
| 938 |
+
def forward(self, x, timesteps):
|
| 939 |
+
"""
|
| 940 |
+
Apply the model to an input batch.
|
| 941 |
+
:param x: an [N x C x ...] Tensor of inputs.
|
| 942 |
+
:param timesteps: a 1-D batch of timesteps.
|
| 943 |
+
:return: an [N x K] Tensor of outputs.
|
| 944 |
+
"""
|
| 945 |
+
emb = self.time_embed(timestep_embedding(timesteps, self.model_channels))
|
| 946 |
+
|
| 947 |
+
results = []
|
| 948 |
+
h = x.type(self.dtype)
|
| 949 |
+
for module in self.input_blocks:
|
| 950 |
+
h = module(h, emb)
|
| 951 |
+
if self.pool.startswith("spatial"):
|
| 952 |
+
results.append(h.type(x.dtype).mean(dim=(2, 3)))
|
| 953 |
+
h = self.middle_block(h, emb)
|
| 954 |
+
if self.pool.startswith("spatial"):
|
| 955 |
+
results.append(h.type(x.dtype).mean(dim=(2, 3)))
|
| 956 |
+
h = th.cat(results, axis=-1)
|
| 957 |
+
return self.out(h)
|
| 958 |
+
else:
|
| 959 |
+
h = h.type(x.dtype)
|
| 960 |
+
return self.out(h)
|
| 961 |
+
|