-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtriplane_transformer_modules.py
187 lines (158 loc) · 7.05 KB
/
triplane_transformer_modules.py
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
import torch
import torch.nn as nn
import numpy as np
from diffusers.models.attention_processor import Attention
from threestudio.utils.typing import *
class ModLN(nn.Module):
"""
Modulation with adaLN.
References:
DiT: https://github.com/facebookresearch/DiT/blob/main/models.py#L101
"""
def __init__(self, inner_dim: int, mod_dim: int, eps: float):
super().__init__()
self.norm = nn.LayerNorm(inner_dim, eps=eps)
self.mlp = nn.Sequential(
nn.Linear(mod_dim, inner_dim * 2),
)
@staticmethod
def modulate(x, shift, scale):
# x: [N, L, D]
# shift, scale: [N, D]
return x * (1 + scale.unsqueeze(1)) + shift.unsqueeze(1)
def forward(self, x, cond):
shift, scale = self.mlp(cond).chunk(2, dim=-1) # [N, D]
return self.modulate(self.norm(x), shift, scale) # [N, L, D]
class ConditionModulationBlock(nn.Module):
"""
Transformer block that takes in a cross-attention condition and another modulation vector applied to sub-blocks.
"""
# use attention from torch.nn.MultiHeadAttention
# Block contains a cross-attention layer, a self-attention layer, and a MLP
def __init__(self, inner_dim: int, cond_dim: int, num_heads: int, eps: float, mlp_ratio: float = 4.,
attn_drop: float = 0., attn_bias: bool = False,
mlp_drop: float = 0.):
super().__init__()
self.norm1 = nn.LayerNorm(inner_dim, eps)
self.cross_attn = Attention(
query_dim=inner_dim, heads=num_heads, dim_head=inner_dim // num_heads,
cross_attention_dim=cond_dim,
dropout=attn_drop, bias=attn_bias, )
self.norm2 = nn.LayerNorm(inner_dim, eps)
self.self_attn = Attention(
query_dim=inner_dim, heads=num_heads, dim_head=inner_dim // num_heads,
cross_attention_dim=inner_dim,
dropout=attn_drop, bias=attn_bias, )
self.norm3 = nn.LayerNorm(inner_dim, eps)
self.mlp = nn.Sequential(
nn.Linear(inner_dim, int(inner_dim * mlp_ratio)),
nn.GELU(),
nn.Dropout(mlp_drop),
nn.Linear(int(inner_dim * mlp_ratio), inner_dim),
nn.Dropout(mlp_drop),
)
def forward(self, x, cond):
# x: [N, L, D]
# cond: [N, L_cond, D_cond]
# mod: [N, D_mod]
x = x + self.cross_attn(self.norm1(x), cond)
before_sa = self.norm2(x)
x = x + self.self_attn(before_sa)
x = x + self.mlp(self.norm3(x))
return x
class ConditionModulationBlockwoCrossAttn(nn.Module):
"""
Transformer block that takes in a cross-attention condition and another modulation vector applied to sub-blocks.
"""
# use attention from torch.nn.MultiHeadAttention
# Block contains a cross-attention layer, a self-attention layer, and a MLP
def __init__(self, inner_dim: int, cond_dim: int, num_heads: int, eps: float, mlp_ratio: float = 4.,
attn_drop: float = 0., attn_bias: bool = False, mlp_drop: float = 0.):
super().__init__()
self.norm2 = nn.LayerNorm(inner_dim, eps)
self.self_attn = Attention(
query_dim=inner_dim, heads=num_heads, dim_head=inner_dim // num_heads,
cross_attention_dim=inner_dim,
dropout=attn_drop, bias=attn_bias, )
self.norm3 = nn.LayerNorm(inner_dim, eps)
self.mlp = nn.Sequential(
nn.GELU(),
nn.Linear(inner_dim, int(inner_dim * mlp_ratio)),
nn.GELU(),
nn.Linear(int(inner_dim * mlp_ratio), inner_dim),
nn.Dropout(mlp_drop),
)
def forward(self, x, cond):
# x: [N, L, D]
# cond: [N, L_cond, D_cond]
# mod: [N, D_mod]
# concatenate the condition to the input
x = torch.cat([cond, x], dim=1)
before_sa = self.norm2(x)
# self-attention, leave out the 1st token
x = x + self.self_attn(before_sa)
x = x + self.mlp(self.norm3(x))
# remove the 1st token, which is the condition
x = x[:, 1:, :]
return x
class TriplaneTransformer(nn.Module):
"""
Transformer with condition and modulation that generates a triplane representation.
Reference:
Timm: https://github.com/huggingface/pytorch-image-models/blob/main/timm/models/vision_transformer.py#L486
"""
def __init__(self,
inner_dim: int, condition_dim: int,
triplane_low_res: int, triplane_high_res: int, triplane_dim: int,
num_layers: int, num_heads: int,
local_text: bool,
mlp_ratio: float = 4., eps: float = 1e-6):
super().__init__()
# attributes
self.triplane_low_res = triplane_low_res
self.triplane_high_res = triplane_high_res
self.triplane_dim = triplane_dim
# modules
# initialize pos_embed with 1/sqrt(dim) * N(0, 1)
self.pos_embed = nn.Parameter(torch.randn(1, 3*triplane_low_res**2, inner_dim) * (1. / inner_dim) ** 0.5)
# condition modulation
self.needs_local_text = local_text
self.layers = nn.ModuleList([
ConditionModulationBlockwoCrossAttn(
inner_dim=inner_dim, cond_dim=condition_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, eps=eps,
) if not local_text else \
ConditionModulationBlock(
inner_dim=inner_dim, cond_dim=condition_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, eps=eps,
) for _ in range(num_layers)
])
self.norm = nn.LayerNorm(inner_dim, eps=eps)
self.deconv = nn.ConvTranspose2d(inner_dim, triplane_dim, kernel_size=2, stride=2, padding=0, bias=False)
if not local_text:
self.proj = nn.Linear(condition_dim, inner_dim)
def forward(self, text_embed):
N = text_embed.shape[0]
H = W = self.triplane_low_res
L = 3 * H * W
# project text_embed to inner_dim
if not self.needs_local_text:
text_embed = self.proj(text_embed)
text_embed = text_embed.unsqueeze(1)
x = self.pos_embed.repeat(N, 1, 1) # [N, L, D]
n = len(self.layers) - 1
for idx, layer in enumerate(self.layers):
x = layer(x, text_embed)
x = self.norm(x)
# separate each plane and apply deconv
x = x.view(N, 3, H, W, -1)
x = torch.einsum('nihwd->indhw', x) # [3, N, D, H, W]
x = x.contiguous().view(3*N, -1, H, W) # [3*N, D, H, W]
x = self.deconv(x) # [3*N, D', H', W']
# x = torch.tanh(x) # tanh help convergence
x = x.view(3, N, *x.shape[-3:]) # [3, N, D', H', W']
x = torch.einsum('indhw->nidhw', x) # [N, 3, D', H', W']
x = x.contiguous()
assert self.triplane_high_res == x.shape[-2], \
f"Output triplane resolution does not match with expected: {x.shape[-2]} vs {self.triplane_high_res}"
assert self.triplane_dim == x.shape[-3], \
f"Output triplane dimension does not match with expected: {x.shape[-3]} vs {self.triplane_dim}"
return x