File size: 12,124 Bytes
04c68a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efc43f7
04c68a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cdc52f
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
from inspect import isfunction
import math
import torch
import torch.nn.functional as F
from torch import nn, einsum
from einops import rearrange, repeat

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}")

    
from .attention import *

try:
    import xformers
    import xformers.ops
    XFORMERS_IS_AVAILBLE = True
except:
    XFORMERS_IS_AVAILBLE = False
print(f"XFORMERS_IS_AVAILBLE: {XFORMERS_IS_AVAILBLE}")


class SPADAttention(nn.Module):
    """Uses xformers to implement efficient epipolar masking for cross-attention between views."""

    def __init__(self, query_dim, context_dim=None, heads=8, dim_head=64, dropout=0.0):
        super().__init__()
        inner_dim = dim_head * heads
        context_dim = default(context_dim, query_dim)

        self.heads = heads
        self.dim_head = dim_head

        self.to_q = nn.Linear(query_dim, inner_dim, bias=False)
        self.to_k = nn.Linear(context_dim, inner_dim, bias=False)
        self.to_v = nn.Linear(context_dim, inner_dim, bias=False)

        self.to_out = nn.Sequential(
            nn.Linear(inner_dim, query_dim), nn.Dropout(dropout)
        )
        self.attention_op: Optional[Any] = None

    def forward(self, x, context=None, mask=None, views=None):
        q = self.to_q(x)
        context = default(context, x)
        k = self.to_k(context)
        v = self.to_v(context)

        b, _, _ = q.shape

        # epipolar mask
        if mask is not None:
            mask = mask.unsqueeze(1)
            mask_shape = (q.shape[-2], k.shape[-2])

            # interpolate epipolar mask to match downsampled unet branch
            mask = (
                F.interpolate(mask.to(torch.uint8), size=mask_shape).bool().squeeze(1)
            )

            # repeat mask for each attention head
            mask = (
                mask.unsqueeze(1)
                .repeat(1, self.heads, 1, 1)
                .reshape(b * self.heads, *mask.shape[-2:])
            )

        q, k, v = map(
            lambda t: t.unsqueeze(3)
            .reshape(b, t.shape[1], self.heads, self.dim_head)
            .permute(0, 2, 1, 3)
            .reshape(b * self.heads, t.shape[1], self.dim_head)
            .contiguous(),
            (q, k, v),
        )

        with torch.autocast(enabled=False, device_type="cuda"):
            q, k, v = q.float(), k.float(), v.float()
            
            mask_inf = 1e9
            fmask = None
            if mask is not None:
                # convert to attention bias
                fmask = mask.float()
                fmask[fmask == 0] = -mask_inf
                fmask[fmask == 1] = 0
            
            # actually compute the attention, what we cannot get enough of
            # Scaled dot-product attention implementation instead of xformers
            attn_scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.dim_head)
            if fmask is not None:
                attn_scores += fmask

            attn_weights = torch.softmax(attn_scores, dim=-1)
            out = torch.matmul(attn_weights, v)

        out = (
            out.unsqueeze(0)
            .reshape(b, self.heads, out.shape[1], self.dim_head)
            .permute(0, 2, 1, 3)
            .reshape(b, out.shape[1], self.heads * self.dim_head)
        )

        # no nans
        if out.isnan().any():
            breakpoint()

        # cleanup
        del q, k, v
        return self.to_out(out)


class SPADTransformerBlock(nn.Module):
    """Modified SPAD transformer block that enables spatially aware cross-attention."""

    def __init__(
        self,
        dim,
        n_heads,
        d_head,
        dropout=0.0,
        context_dim=None,
        gated_ff=True,
        checkpoint=True,
        disable_self_attn=False,
    ):
        super().__init__()
        attn_cls = SPADAttention
        self.disable_self_attn = disable_self_attn
        self.attn1 = attn_cls(
            query_dim=dim,
            heads=n_heads,
            dim_head=d_head,
            dropout=dropout,
            context_dim=context_dim if self.disable_self_attn else None,
        )  # is a self-attention if not self.disable_self_attn
        self.ff = FeedForward(dim, dropout=dropout, glu=gated_ff)
        self.attn2 = attn_cls(
            query_dim=dim,
            context_dim=context_dim,
            heads=n_heads,
            dim_head=d_head,
            dropout=dropout,
        )  # is self-attn if context is none
        self.norm1 = nn.LayerNorm(dim)
        self.norm2 = nn.LayerNorm(dim)
        self.norm3 = nn.LayerNorm(dim)
        self.checkpoint = checkpoint

    def forward(self, x, context=None, mask=None):
        return checkpoint(
            self.manystream_forward,
            (x, context, mask),
            self.parameters(),
            self.checkpoint,
        )

    def manystream_forward(self, x, context=None, mask=None):
        assert not self.disable_self_attn
        # x: [n, v, h*w, c]
        # context: [n, v, seq_len, d]
        n, v = x.shape[:2]

        # self-attention (between views) with 3d mask
        x = rearrange(x, "n v hw c -> n (v hw) c")
        x = self.attn1(self.norm1(x), context=None, mask=mask, views=v) + x
        x = rearrange(x, "n (v hw) c -> n v hw c", v=v)

        # cross-attention (to individual views)
        x = rearrange(x, "n v hw c -> (n v) hw c")
        context = rearrange(context, "n v seq d -> (n v) seq d")
        x = self.attn2(self.norm2(x), context=context) + x
        x = self.ff(self.norm3(x)) + x
        x = rearrange(x, "(n v) hw c -> n v hw c", v=v)

        return x


class SPADTransformer(nn.Module):
    """Spatial Transformer block with post init to add cross attn."""

    def __init__(
        self,
        in_channels,
        n_heads,
        d_head,
        depth=1,
        dropout=0.0,
        context_dim=None,
        disable_self_attn=False,
        use_linear=False,  # 2.1 vs 1.5 difference
        use_checkpoint=True,
    ):
        super().__init__()
        if exists(context_dim) and not isinstance(context_dim, list):
            context_dim = [context_dim]
        self.in_channels = in_channels
        inner_dim = n_heads * d_head
        self.norm = Normalize(in_channels)
        if not use_linear:
            self.proj_in = nn.Conv2d(
                in_channels, inner_dim, kernel_size=1, stride=1, padding=0
            )
        else:
            self.proj_in = nn.Linear(in_channels, inner_dim)

        self.transformer_blocks = nn.ModuleList(
            [
                SPADTransformerBlock(
                    inner_dim,
                    n_heads,
                    d_head,
                    dropout=dropout,
                    context_dim=context_dim[d],
                    disable_self_attn=disable_self_attn,
                    checkpoint=use_checkpoint,
                )
                for d in range(depth)
            ]
        )
        if not use_linear:
            self.proj_out = zero_module(
                nn.Conv2d(inner_dim, in_channels, kernel_size=1, stride=1, padding=0)
            )
        else:
            self.proj_out = zero_module(nn.Linear(in_channels, inner_dim))
        self.use_linear = use_linear

        # modify conv layers incorporate plucker coordinates
        self.post_init()

    def post_init(self):
        assert getattr(self, "post_intialized", False) is False, "already modified!"

        # inflate input conv block to attach plucker coordinates
        conv_block = self.proj_in
        conv_params = {
            k: getattr(conv_block, k)
            for k in [
                "in_channels",
                "out_channels",
                "kernel_size",
                "stride",
                "padding",
            ]
        }
        conv_params["in_channels"] += 6
        conv_params["dims"] = 2
        conv_params["device"] = conv_block.weight.device

        # copy original weights for input conv block
        inflated_proj_in = conv_nd(**conv_params)
        inp_weight = conv_block.weight.data
        feat_shape = inp_weight.shape

        # intialize new weights for plucker coordinates as zeros
        feat_weight = torch.zeros(
            (feat_shape[0], 6, *feat_shape[2:]), device=inp_weight.device
        )

        # assemble new weights and bias
        inflated_proj_in.weight.data.copy_(
            torch.cat([inp_weight, feat_weight], dim=1)
        )
        inflated_proj_in.bias.data.copy_(conv_block.bias.data)
        self.proj_in = inflated_proj_in
        self.post_intialized = True

    def forward(self, x, context=None):
        return self.spad_forward(x, context=context)

    def spad_forward(self, x, context=None):
        """
        x: tensor of shape [n, v, c (4), h (32), w (32)] 
        context: list of [text_emb, epipolar_mask, plucker_coords]
            - text_emb: tensor of shape [n, v, seq_len (77), dim (768)]
            - epipolar_mask: bool tensor of shape [n, v, seq_len (32*32), seq_len (32*32)]
            - plucker_coords: tensor of shape [n, v, dim (6), h (32), w (32)]
        """

        n_objects, n_views, c, h, w = x.shape
        x_in = x

        # note: if no context is given, cross-attention defaults to self-attention
        context, plucker = context[:-1], context[-1]
        context = [context]

        x = rearrange(x, "n v c h w -> (n v) c h w")
        x = self.norm(x)
        x = rearrange(x, "(n v) c h w -> n v c h w", v=n_views)

        # run input projection
        if not self.use_linear:
            # interpolate plucker to match x
            plucker = rearrange(plucker, "n v c h w -> (n v) c h w")
            plucker_interpolated = F.interpolate(
                plucker, size=x.shape[-2:], align_corners=False, mode="bilinear"
            )
            plucker_interpolated = rearrange(
                plucker_interpolated, "(n v) c h w -> n v c h w", v=n_views
            )

            # concat plucker to x
            x = torch.cat([x, plucker_interpolated], dim=2)
            x = rearrange(x, "n v c h w -> (n v) c h w")
            x = self.proj_in(x)
            x = rearrange(x, "(n v) c h w -> n v c h w", v=n_views)

        x = rearrange(x, "n v c h w -> n v (h w) c").contiguous()

        if self.use_linear:
            x = rearrange(x, "n v x c -> (n v) x c")
            x = self.proj_in(x)
            x = rearrange(x, "(n v) x c -> n v x c", v=n_views)

        # run the transformer blocks
        for i, block in enumerate(self.transformer_blocks):
            _context = context[i]
            mask = None
            if isinstance(_context, (list, tuple)):
                try:
                    _context, mask = _context
                except:
                    _context = _context[0]
            x = block(x, context=_context, mask=mask)

        if x.isnan().any():
            breakpoint()

        # run output projection
        if self.use_linear:
            x = rearrange(x, "n v x c -> (n v) x c")
            x = self.proj_out(x)
            x = rearrange(x, "(n v) x c -> n v x c", v=n_views)

        x = rearrange(x, "n v (h w) c -> n v c h w", h=h, w=w).contiguous()

        if not self.use_linear:
            x = rearrange(x, "n v c h w -> (n v) c h w")
            x = self.proj_out(x)
            x = rearrange(x, "(n v) c h w -> n v c h w", v=n_views)

        return x + x_in


if __name__ == "__main__":
    spt_post = SPADTransformer(320, 8, 40, depth=1, context_dim=768).cuda()

    n_objects, n_views = 2, 4
    x = torch.randn(2, 4, 320, 32, 32).cuda()
    context = [
        torch.randn(n_objects, n_views, 77, 768).cuda(),
        torch.ones(
            n_objects, n_views * 32 * 32, n_views * 32 * 32, dtype=torch.bool
        ).cuda(),
        torch.randn(n_objects, n_views, 6, 32, 32).cuda(),
    ]
    x_post = spt_post(x, context=context)