File size: 14,243 Bytes
c6be546
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Text prompt decoder implementation for VoxTell.

Code modified from DETR transformer:
https://github.com/facebookresearch/detr
Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
"""

import copy
from typing import Callable, List, Optional, Tuple, Union

import torch
import torch.nn.functional as F
from torch import nn, Tensor


class TransformerDecoder(nn.Module):
    """
    Transformer decoder consisting of multiple decoder layers.
    
    This decoder processes target sequences with attention to memory (encoder output),
    optionally returning intermediate layer outputs. It receives the text prompt embeddings
    as queries and attends to image features from the encoder. It outputs refined text-image
    fused features for segmentation mask prediction.
    
    Args:
        decoder_layer: A single transformer decoder layer to be cloned.
        num_layers: Number of decoder layers to stack.
        norm: Optional normalization layer applied to the final output.
        return_intermediate: If True, returns outputs from all layers stacked together.
    """

    def __init__(
        self,
        decoder_layer: nn.Module,
        num_layers: int,
        norm: Optional[nn.Module] = None,
        return_intermediate: bool = False
    ) -> None:
        super().__init__()
        self.layers = _get_clones(decoder_layer, num_layers)
        self.num_layers = num_layers
        self.norm = norm
        self.return_intermediate = return_intermediate

    def forward(
        self,
        tgt: Tensor,
        memory: Tensor,
        tgt_mask: Optional[Tensor] = None,
        memory_mask: Optional[Tensor] = None,
        tgt_key_padding_mask: Optional[Tensor] = None,
        memory_key_padding_mask: Optional[Tensor] = None,
        pos: Optional[Tensor] = None,
        query_pos: Optional[Tensor] = None
    ) -> Union[Tensor, Tuple[Tensor, List[Tensor]]]:
        """
        Forward pass through all decoder layers.
        
        Args:
            tgt: Target sequence tensor of shape [T, B, C].
            memory: Memory (encoder output) tensor of shape [T, B, C].
            tgt_mask: Attention mask for target self-attention.
            memory_mask: Attention mask for cross-attention to memory.
            tgt_key_padding_mask: Padding mask for target keys.
            memory_key_padding_mask: Padding mask for memory keys.
            pos: Positional embeddings for memory.
            query_pos: Positional embeddings for queries.
            
        Returns:
            If return_intermediate is True, returns stacked intermediate outputs.
            Otherwise, returns tuple of (final_output, attention_weights_list).
        """
        output = tgt
        T, B, C = memory.shape
        intermediate = []
        atten_layers = []
        
        for n, layer in enumerate(self.layers):
            residual = True
            output, ws = layer(
                output, memory,
                tgt_mask=tgt_mask,
                memory_mask=memory_mask,
                tgt_key_padding_mask=tgt_key_padding_mask,
                memory_key_padding_mask=memory_key_padding_mask,
                pos=pos,
                query_pos=query_pos,
                residual=residual
            )
            atten_layers.append(ws)
            if self.return_intermediate:
                intermediate.append(self.norm(output))
                
        if self.norm is not None:
            output = self.norm(output)
            if self.return_intermediate:
                intermediate.pop()
                intermediate.append(output)

        if self.return_intermediate:
            return torch.stack(intermediate)
        return output, atten_layers



class TransformerDecoderLayer(nn.Module):
    """
    Single transformer decoder layer with self-attention, cross-attention, and FFN.
    
    This layer implements:
    1. Self-attention on the target sequence
    2. Cross-attention between target and memory (image encoder output)
    3. Position-wise feed-forward network
    
    Args:
        d_model: Dimension of the model (embedding dimension).
        nhead: Number of attention heads.
        dim_feedforward: Dimension of the feedforward network.
        dropout: Dropout probability.
        activation: Activation function name ('relu', 'gelu', or 'glu').
        normalize_before: If True, applies layer norm before attention/FFN (pre-norm).
                         If False, applies after (post-norm).
    """

    def __init__(
        self,
        d_model: int,
        nhead: int,
        dim_feedforward: int = 2048,
        dropout: float = 0.1,
        activation: str = "relu",
        normalize_before: bool = False
    ) -> None:
        super().__init__()
        self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout)
        self.multihead_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout)
        
        # Feed-forward network
        self.linear1 = nn.Linear(d_model, dim_feedforward)
        self.dropout = nn.Dropout(dropout)
        self.linear2 = nn.Linear(dim_feedforward, d_model)

        # Normalization layers
        self.norm1 = nn.LayerNorm(d_model)
        self.norm2 = nn.LayerNorm(d_model)
        self.norm3 = nn.LayerNorm(d_model)
        
        # Dropout layers
        self.dropout1 = nn.Dropout(dropout)
        self.dropout2 = nn.Dropout(dropout)
        self.dropout3 = nn.Dropout(dropout)

        self.activation = _get_activation_fn(activation)
        self.normalize_before = normalize_before
        
    def with_pos_embed(self, tensor: Tensor, pos: Optional[Tensor]) -> Tensor:
        """
        Add positional embeddings to tensor if provided.
        
        Args:
            tensor: Input tensor.
            pos: Optional positional embeddings to add.
            
        Returns:
            Tensor with positional embeddings added, or original tensor if pos is None.
        """
        return tensor if pos is None else tensor + pos

    def forward_post(
        self,
        tgt: Tensor,
        memory: Tensor,
        tgt_mask: Optional[Tensor] = None,
        memory_mask: Optional[Tensor] = None,
        tgt_key_padding_mask: Optional[Tensor] = None,
        memory_key_padding_mask: Optional[Tensor] = None,
        pos: Optional[Tensor] = None,
        query_pos: Optional[Tensor] = None,
        residual: bool = True
    ) -> Tuple[Tensor, Tensor]:
        """
        Post-norm forward pass: attention/FFN first, then normalization.
        
        Args:
            tgt: Target sequence.
            memory: Memory (encoder output).
            tgt_mask: Self-attention mask for target.
            memory_mask: Cross-attention mask.
            tgt_key_padding_mask: Padding mask for target keys.
            memory_key_padding_mask: Padding mask for memory keys.
            pos: Positional embeddings for memory.
            query_pos: Positional embeddings for queries.
            residual: Whether to use residual connections.
            
        Returns:
            Tuple of (output_tensor, attention_weights).
        """
        q = k = self.with_pos_embed(tgt, query_pos)
        tgt2, ws = self.self_attn(
            q, k, value=tgt,
            attn_mask=tgt_mask,
            key_padding_mask=tgt_key_padding_mask
        )
        tgt = self.norm1(tgt)
        tgt2, ws = self.multihead_attn(
            query=self.with_pos_embed(tgt, query_pos),
            key=self.with_pos_embed(memory, pos),
            value=memory,
            attn_mask=memory_mask,
            key_padding_mask=memory_key_padding_mask
        )

        # Cross-attention with residual connection
        tgt = tgt + self.dropout2(tgt2)
        tgt = self.norm2(tgt)
        
        # Feed-forward network
        tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt))))
        tgt = tgt + self.dropout3(tgt2)
        tgt = self.norm3(tgt)
        return tgt, ws

    def forward_pre(
        self,
        tgt: Tensor,
        memory: Tensor,
        tgt_mask: Optional[Tensor] = None,
        memory_mask: Optional[Tensor] = None,
        tgt_key_padding_mask: Optional[Tensor] = None,
        memory_key_padding_mask: Optional[Tensor] = None,
        pos: Optional[Tensor] = None,
        query_pos: Optional[Tensor] = None
    ) -> Tuple[Tensor, Tensor]:
        """
        Pre-norm forward pass: normalization first, then attention/FFN.
        
        Args:
            tgt: Target sequence.
            memory: Memory (encoder output).
            tgt_mask: Self-attention mask for target.
            memory_mask: Cross-attention mask.
            tgt_key_padding_mask: Padding mask for target keys.
            memory_key_padding_mask: Padding mask for memory keys.
            pos: Positional embeddings for memory.
            query_pos: Positional embeddings for queries.
            
        Returns:
            Tuple of (output_tensor, attention_weights).
        """
        tgt2 = self.norm2(tgt)
        tgt2, attn_weights = self.multihead_attn(
            query=self.with_pos_embed(tgt2, query_pos),
            key=self.with_pos_embed(memory, pos),
            value=memory,
            attn_mask=memory_mask,
            key_padding_mask=memory_key_padding_mask
        )
        tgt = tgt + self.dropout2(tgt2)
        
        # Feed-forward network with pre-norm
        tgt2 = self.norm3(tgt)
        tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt2))))
        tgt = tgt + self.dropout3(tgt2)
        return tgt, attn_weights
    

    def forward_pre_selfattention(
        self,
        tgt: Tensor,
        memory: Tensor,
        tgt_mask: Optional[Tensor] = None,
        memory_mask: Optional[Tensor] = None,
        tgt_key_padding_mask: Optional[Tensor] = None,
        memory_key_padding_mask: Optional[Tensor] = None,
        pos: Optional[Tensor] = None,
        query_pos: Optional[Tensor] = None
    ) -> Tuple[Tensor, Tensor]:
        """
        Alternative forward pass with cross-attention before self-attention.
        
        This variant applies operations in the order:
        1. Cross-attention (without normalization)
        2. Self-attention (with pre-norm)
        3. Feed-forward network (with pre-norm)
        
        Args:
            tgt: Target sequence.
            memory: Memory (encoder output).
            tgt_mask: Self-attention mask for target.
            memory_mask: Cross-attention mask.
            tgt_key_padding_mask: Padding mask for target keys.
            memory_key_padding_mask: Padding mask for memory keys.
            pos: Positional embeddings for memory.
            query_pos: Positional embeddings for queries.
            
        Returns:
            Tuple of (output_tensor, attention_weights).
        """
        # Cross-attention without pre-normalization
        tgt2, attn_weights = self.multihead_attn(
            query=self.with_pos_embed(tgt, query_pos),
            key=self.with_pos_embed(memory, pos),
            value=memory,
            attn_mask=memory_mask,
            key_padding_mask=memory_key_padding_mask
        )
        tgt = tgt + tgt2
        
        # Self-attention with pre-norm
        tgt2 = self.norm1(tgt)
        q = k = self.with_pos_embed(tgt2, query_pos)
        tgt2, ws = self.self_attn(q, k, value=tgt2)
        tgt = tgt + tgt2
        
        # Feed-forward network with pre-norm
        tgt2 = self.norm2(tgt)
        tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt2))))
        tgt = tgt + tgt2
        tgt = self.norm3(tgt)
        return tgt, attn_weights


    def forward(
        self,
        tgt: Tensor,
        memory: Tensor,
        tgt_mask: Optional[Tensor] = None,
        memory_mask: Optional[Tensor] = None,
        tgt_key_padding_mask: Optional[Tensor] = None,
        memory_key_padding_mask: Optional[Tensor] = None,
        pos: Optional[Tensor] = None,
        query_pos: Optional[Tensor] = None,
        residual: bool = True
    ) -> Tuple[Tensor, Tensor]:
        """
        Forward pass through the decoder layer.
        
        Dispatches to either pre-norm or post-norm variant based on configuration.
        
        Args:
            tgt: Target sequence.
            memory: Memory (encoder output).
            tgt_mask: Self-attention mask for target.
            memory_mask: Cross-attention mask.
            tgt_key_padding_mask: Padding mask for target keys.
            memory_key_padding_mask: Padding mask for memory keys.
            pos: Positional embeddings for memory.
            query_pos: Positional embeddings for queries.
            residual: Whether to use residual connections (used in post-norm variant).
            
        Returns:
            Tuple of (output_tensor, attention_weights).
        """
        if self.normalize_before:
            return self.forward_pre(
                tgt, memory, tgt_mask, memory_mask,
                tgt_key_padding_mask, memory_key_padding_mask, pos, query_pos
            )
        return self.forward_post(
            tgt, memory, tgt_mask, memory_mask,
            tgt_key_padding_mask, memory_key_padding_mask, pos, query_pos, residual
        )


def _get_clones(module: nn.Module, N: int) -> nn.ModuleList:
    """
    Create N identical copies of a module.
    
    Args:
        module: The module to clone.
        N: Number of clones to create.
        
    Returns:
        ModuleList containing N deep copies of the input module.
    """
    return nn.ModuleList([copy.deepcopy(module) for i in range(N)])


def _get_activation_fn(activation: str) -> Callable[[Tensor], Tensor]:
    """
    Return an activation function given a string identifier.
    
    Args:
        activation: Name of the activation function ('relu', 'gelu', or 'glu').
        
    Returns:
        The corresponding activation function.
        
    Raises:
        RuntimeError: If the activation name is not recognized.
    """
    if activation == "relu":
        return F.relu
    if activation == "gelu":
        return F.gelu
    if activation == "glu":
        return F.glu
    raise RuntimeError(f"activation should be relu/gelu/glu, not {activation}.")