| import torch
|
| import torch.nn as nn
|
| import torch.nn.functional as F
|
|
|
| import numpy as np
|
|
|
| from typing import Union, Dict, Optional, Tuple
|
| from einops import rearrange
|
|
|
| from model_utils import pairwise_distance
|
| from pointnet2_utils import gather_operation
|
|
|
| NORM_LAYERS = {
|
| 'BatchNorm1d': nn.BatchNorm1d,
|
| 'BatchNorm2d': nn.BatchNorm2d,
|
| 'BatchNorm3d': nn.BatchNorm3d,
|
| 'InstanceNorm1d': nn.InstanceNorm1d,
|
| 'InstanceNorm2d': nn.InstanceNorm2d,
|
| 'InstanceNorm3d': nn.InstanceNorm3d,
|
| 'GroupNorm': nn.GroupNorm,
|
| 'LayerNorm': nn.LayerNorm,
|
| }
|
|
|
|
|
| ACT_LAYERS = {
|
| 'ReLU': nn.ReLU,
|
| 'LeakyReLU': nn.LeakyReLU,
|
| 'ELU': nn.ELU,
|
| 'GELU': nn.GELU,
|
| 'Sigmoid': nn.Sigmoid,
|
| 'Softplus': nn.Softplus,
|
| 'Tanh': nn.Tanh,
|
| 'Identity': nn.Identity,
|
| }
|
|
|
|
|
| CONV_LAYERS = {
|
| 'Linear': nn.Linear,
|
| 'Conv1d': nn.Conv1d,
|
| 'Conv2d': nn.Conv2d,
|
| 'Conv3d': nn.Conv3d,
|
| }
|
|
|
| def _check_block_type(block):
|
| if block not in ['self', 'cross']:
|
| raise ValueError('Unsupported block type "{}".'.format(block))
|
|
|
|
|
| def parse_cfg(cfg: Union[str, Dict]) -> Tuple[str, Dict]:
|
| assert isinstance(cfg, (str, Dict)), 'Illegal cfg type: {}.'.format(type(cfg))
|
| if isinstance(cfg, str):
|
| cfg = {'type': cfg}
|
| else:
|
| cfg = cfg.copy()
|
| layer = cfg.pop('type')
|
| return layer, cfg
|
|
|
|
|
| def build_dropout_layer(p: Optional[float], **kwargs) -> nn.Module:
|
| r"""Factory function for dropout layer."""
|
| if p is None or p == 0:
|
| return nn.Identity()
|
| else:
|
| return nn.Dropout(p=p, **kwargs)
|
|
|
| def build_norm_layer(num_features, norm_cfg: Optional[Union[str, Dict]]) -> nn.Module:
|
| r"""Factory function for normalization layers."""
|
| if norm_cfg is None:
|
| return nn.Identity()
|
| layer, kwargs = parse_cfg(norm_cfg)
|
| assert layer in NORM_LAYERS, f'Illegal normalization: {layer}.'
|
| if layer == 'GroupNorm':
|
| kwargs['num_channels'] = num_features
|
| elif layer == 'LayerNorm':
|
| kwargs['normalized_shape'] = num_features
|
| else:
|
| kwargs['num_features'] = num_features
|
| return NORM_LAYERS[layer](**kwargs)
|
|
|
|
|
| def build_act_layer(act_cfg: Optional[Union[str, Dict]]) -> nn.Module:
|
| r"""Factory function for activation functions."""
|
| if act_cfg is None:
|
| return nn.Identity()
|
| layer, kwargs = parse_cfg(act_cfg)
|
| assert layer in ACT_LAYERS, f'Illegal activation: {layer}.'
|
| if layer == 'LeakyReLU':
|
| if 'negative_slope' not in kwargs:
|
| kwargs['negative_slope'] = 0.2
|
| return ACT_LAYERS[layer](**kwargs)
|
|
|
|
|
| class MultiHeadAttention(nn.Module):
|
| def __init__(self, d_model, num_heads, dropout=None):
|
| super(MultiHeadAttention, self).__init__()
|
| if d_model % num_heads != 0:
|
| raise ValueError('`d_model` ({}) must be a multiple of `num_heads` ({}).'.format(d_model, num_heads))
|
|
|
| self.d_model = d_model
|
| self.num_heads = num_heads
|
| self.d_model_per_head = d_model // num_heads
|
|
|
| self.proj_q = nn.Linear(self.d_model, self.d_model)
|
| self.proj_k = nn.Linear(self.d_model, self.d_model)
|
| self.proj_v = nn.Linear(self.d_model, self.d_model)
|
|
|
| self.dropout = build_dropout_layer(dropout)
|
|
|
| def forward(
|
| self, input_q, input_k, input_v, key_weights=None, key_masks=None, attention_factors=None, attention_masks=None
|
| ):
|
| """Vanilla Self-attention forward propagation.
|
|
|
| Args:
|
| input_q (Tensor): input tensor for query (B, N, C)
|
| input_k (Tensor): input tensor for key (B, M, C)
|
| input_v (Tensor): input tensor for value (B, M, C)
|
| key_weights (Tensor): soft masks for the keys (B, M)
|
| key_masks (BoolTensor): True if ignored, False if preserved (B, M)
|
| attention_factors (Tensor): factors for attention matrix (B, N, M)
|
| attention_masks (BoolTensor): True if ignored, False if preserved (B, N, M)
|
|
|
| Returns:
|
| hidden_states: torch.Tensor (B, C, N)
|
| attention_scores: intermediate values
|
| 'attention_scores': torch.Tensor (B, H, N, M), attention scores before dropout
|
| """
|
| q = rearrange(self.proj_q(input_q), 'b n (h c) -> b h n c', h=self.num_heads)
|
| k = rearrange(self.proj_k(input_k), 'b m (h c) -> b h m c', h=self.num_heads)
|
| v = rearrange(self.proj_v(input_v), 'b m (h c) -> b h m c', h=self.num_heads)
|
|
|
| attention_scores = torch.einsum('bhnc,bhmc->bhnm', q, k) / self.d_model_per_head ** 0.5
|
| if attention_factors is not None:
|
| attention_scores = attention_factors.unsqueeze(1) * attention_scores
|
| if key_weights is not None:
|
| attention_scores = attention_scores * key_weights.unsqueeze(1).unsqueeze(1)
|
| if key_masks is not None:
|
| attention_scores = attention_scores.masked_fill(key_masks.unsqueeze(1).unsqueeze(1), float('-inf'))
|
| if attention_masks is not None:
|
| attention_scores = attention_scores.masked_fill(attention_masks, float('-inf'))
|
| attention_scores = F.softmax(attention_scores, dim=-1)
|
| attention_scores = self.dropout(attention_scores)
|
|
|
| hidden_states = torch.matmul(attention_scores, v)
|
|
|
| hidden_states = rearrange(hidden_states, 'b h n c -> b n (h c)')
|
|
|
| return hidden_states, attention_scores
|
|
|
|
|
| class AttentionLayer(nn.Module):
|
| def __init__(self, d_model, num_heads, dropout=None):
|
| super(AttentionLayer, self).__init__()
|
| self.attention = MultiHeadAttention(d_model, num_heads, dropout=dropout)
|
| self.linear = nn.Linear(d_model, d_model)
|
| self.dropout = build_dropout_layer(dropout)
|
| self.norm = nn.LayerNorm(d_model)
|
|
|
| def forward(
|
| self,
|
| input_states,
|
| memory_states,
|
| memory_weights=None,
|
| memory_masks=None,
|
| attention_factors=None,
|
| attention_masks=None,
|
| ):
|
| hidden_states, attention_scores = self.attention(
|
| input_states,
|
| memory_states,
|
| memory_states,
|
| key_weights=memory_weights,
|
| key_masks=memory_masks,
|
| attention_factors=attention_factors,
|
| attention_masks=attention_masks,
|
| )
|
| hidden_states = self.linear(hidden_states)
|
| hidden_states = self.dropout(hidden_states)
|
| output_states = self.norm(hidden_states + input_states)
|
| return output_states, attention_scores
|
|
|
| class AttentionOutput(nn.Module):
|
| def __init__(self, d_model, dropout=None, activation_fn='ReLU'):
|
| super(AttentionOutput, self).__init__()
|
| self.expand = nn.Linear(d_model, d_model * 2)
|
| self.activation = build_act_layer(activation_fn)
|
| self.squeeze = nn.Linear(d_model * 2, d_model)
|
| self.dropout = build_dropout_layer(dropout)
|
| self.norm = nn.LayerNorm(d_model)
|
|
|
| def forward(self, input_states):
|
| hidden_states = self.expand(input_states)
|
| hidden_states = self.activation(hidden_states)
|
| hidden_states = self.squeeze(hidden_states)
|
| hidden_states = self.dropout(hidden_states)
|
| output_states = self.norm(input_states + hidden_states)
|
| return output_states
|
|
|
|
|
| class TransformerLayer(nn.Module):
|
| def __init__(self, d_model, num_heads, dropout=None, activation_fn='ReLU'):
|
| super(TransformerLayer, self).__init__()
|
| self.attention = AttentionLayer(d_model, num_heads, dropout=dropout)
|
| self.output = AttentionOutput(d_model, dropout=dropout, activation_fn=activation_fn)
|
|
|
| def forward(
|
| self,
|
| input_states,
|
| memory_states,
|
| memory_weights=None,
|
| memory_masks=None,
|
| attention_factors=None,
|
| attention_masks=None,
|
| ):
|
| hidden_states, attention_scores = self.attention(
|
| input_states,
|
| memory_states,
|
| memory_weights=memory_weights,
|
| memory_masks=memory_masks,
|
| attention_factors=attention_factors,
|
| attention_masks=attention_masks,
|
| )
|
| output_states = self.output(hidden_states)
|
| return output_states, attention_scores
|
|
|
|
|
| class ConditionalTransformer(nn.Module):
|
| def __init__(self, blocks, d_model, num_heads, dropout=None, activation_fn='ReLU', return_attention_scores=False):
|
| super(ConditionalTransformer, self).__init__()
|
| self.blocks = blocks
|
| layers = []
|
| for block in self.blocks:
|
| _check_block_type(block)
|
| layers.append(TransformerLayer(d_model, num_heads, dropout=dropout, activation_fn=activation_fn))
|
| self.layers = nn.ModuleList(layers)
|
| self.return_attention_scores = return_attention_scores
|
|
|
| def forward(self, feats0, feats1, masks0=None, masks1=None):
|
| attention_scores = []
|
| for i, block in enumerate(self.blocks):
|
| if block == 'self':
|
| feats0, scores0 = self.layers[i](feats0, feats0, memory_masks=masks0)
|
| feats1, scores1 = self.layers[i](feats1, feats1, memory_masks=masks1)
|
| else:
|
| feats0, scores0 = self.layers[i](feats0, feats1, memory_masks=masks1)
|
| feats1, scores1 = self.layers[i](feats1, feats0, memory_masks=masks0)
|
| if self.return_attention_scores:
|
| attention_scores.append([scores0, scores1])
|
| if self.return_attention_scores:
|
| return feats0, feats1, attention_scores
|
| else:
|
| return feats0, feats1
|
|
|
|
|
|
|
|
|
| class SinusoidalPositionalEmbedding(nn.Module):
|
| def __init__(self, d_model):
|
| super(SinusoidalPositionalEmbedding, self).__init__()
|
| if d_model % 2 != 0:
|
| raise ValueError(f'Sinusoidal positional encoding with odd d_model: {d_model}')
|
| self.d_model = d_model
|
| div_indices = torch.arange(0, d_model, 2).float()
|
| div_term = torch.exp(div_indices * (-np.log(10000.0) / d_model))
|
| self.register_buffer('div_term', div_term)
|
|
|
| def forward(self, emb_indices):
|
| r"""Sinusoidal Positional Embedding.
|
|
|
| Args:
|
| emb_indices: torch.Tensor (*)
|
|
|
| Returns:
|
| embeddings: torch.Tensor (*, D)
|
| """
|
| input_shape = emb_indices.shape
|
| omegas = emb_indices.view(-1, 1, 1) * self.div_term.view(1, -1, 1)
|
| sin_embeddings = torch.sin(omegas)
|
| cos_embeddings = torch.cos(omegas)
|
| embeddings = torch.cat([sin_embeddings, cos_embeddings], dim=2)
|
| embeddings = embeddings.view(*input_shape, self.d_model)
|
| embeddings = embeddings.detach()
|
| return embeddings
|
|
|
|
|
| class GeometricStructureEmbedding(nn.Module):
|
| def __init__(self, cfg):
|
| super(GeometricStructureEmbedding, self).__init__()
|
| self.sigma_d = cfg.sigma_d
|
| self.sigma_a = cfg.sigma_a
|
| self.factor_a = 180.0 / (self.sigma_a * np.pi)
|
| self.angle_k = cfg.angle_k
|
|
|
| self.embedding = SinusoidalPositionalEmbedding(cfg.hidden_dim)
|
| self.proj_d = nn.Linear(cfg.hidden_dim, cfg.hidden_dim)
|
| self.proj_a = nn.Linear(cfg.hidden_dim, cfg.hidden_dim)
|
|
|
| self.reduction_a = cfg.reduction_a
|
| if self.reduction_a not in ['max', 'mean']:
|
| raise ValueError(f'Unsupported reduction mode: {self.reduction_a}.')
|
|
|
| @torch.no_grad()
|
| def get_embedding_indices(self, points):
|
| r"""Compute the indices of pair-wise distance embedding and triplet-wise angular embedding.
|
|
|
| Args:
|
| points: torch.Tensor (B, N, 3), input point cloud
|
|
|
| Returns:
|
| d_indices: torch.FloatTensor (B, N, N), distance embedding indices
|
| a_indices: torch.FloatTensor (B, N, N, k), angular embedding indices
|
| """
|
| batch_size, num_point, _ = points.shape
|
|
|
| dist_map = torch.sqrt(pairwise_distance(points, points))
|
| d_indices = dist_map / self.sigma_d
|
|
|
| k = self.angle_k
|
| knn_indices = dist_map.topk(k=k + 1, dim=2, largest=False)[1][:, :, 1:]
|
| knn_indices = knn_indices.unsqueeze(3).expand(batch_size, num_point, k, 3)
|
| expanded_points = points.unsqueeze(1).expand(batch_size, num_point, num_point, 3)
|
| knn_points = torch.gather(expanded_points, dim=2, index=knn_indices)
|
| ref_vectors = knn_points - points.unsqueeze(2)
|
| anc_vectors = points.unsqueeze(1) - points.unsqueeze(2)
|
| ref_vectors = ref_vectors.unsqueeze(2).expand(batch_size, num_point, num_point, k, 3)
|
| anc_vectors = anc_vectors.unsqueeze(3).expand(batch_size, num_point, num_point, k, 3)
|
| sin_values = torch.linalg.norm(torch.cross(ref_vectors, anc_vectors, dim=-1), dim=-1)
|
| cos_values = torch.sum(ref_vectors * anc_vectors, dim=-1)
|
| angles = torch.atan2(sin_values, cos_values)
|
| a_indices = angles * self.factor_a
|
|
|
| return d_indices, a_indices
|
|
|
| def forward(self, points):
|
| d_indices, a_indices = self.get_embedding_indices(points)
|
|
|
| d_embeddings = self.embedding(d_indices)
|
| d_embeddings = self.proj_d(d_embeddings)
|
|
|
| a_embeddings = self.embedding(a_indices)
|
| a_embeddings = self.proj_a(a_embeddings)
|
| if self.reduction_a == 'max':
|
| a_embeddings = a_embeddings.max(dim=3)[0]
|
| else:
|
| a_embeddings = a_embeddings.mean(dim=3)
|
|
|
| embeddings = d_embeddings + a_embeddings
|
|
|
| return embeddings
|
|
|
|
|
| class RPEMultiHeadAttention(nn.Module):
|
| def __init__(self, d_model, num_heads, dropout=None):
|
| super(RPEMultiHeadAttention, self).__init__()
|
| if d_model % num_heads != 0:
|
| raise ValueError('`d_model` ({}) must be a multiple of `num_heads` ({}).'.format(d_model, num_heads))
|
|
|
| self.d_model = d_model
|
| self.num_heads = num_heads
|
| self.d_model_per_head = d_model // num_heads
|
|
|
| self.proj_q = nn.Linear(self.d_model, self.d_model)
|
| self.proj_k = nn.Linear(self.d_model, self.d_model)
|
| self.proj_v = nn.Linear(self.d_model, self.d_model)
|
| self.proj_p = nn.Linear(self.d_model, self.d_model)
|
|
|
| self.dropout = build_dropout_layer(dropout)
|
|
|
| def forward(self, input_q, input_k, input_v, embed_qk, key_weights=None, key_masks=None, attention_factors=None):
|
| r"""Scaled Dot-Product Attention with Pre-computed Relative Positional Embedding (forward)
|
|
|
| Args:
|
| input_q: torch.Tensor (B, N, C)
|
| input_k: torch.Tensor (B, M, C)
|
| input_v: torch.Tensor (B, M, C)
|
| embed_qk: torch.Tensor (B, N, M, C), relative positional embedding
|
| key_weights: torch.Tensor (B, M), soft masks for the keys
|
| key_masks: torch.Tensor (B, M), True if ignored, False if preserved
|
| attention_factors: torch.Tensor (B, N, M)
|
|
|
| Returns:
|
| hidden_states: torch.Tensor (B, C, N)
|
| attention_scores: torch.Tensor (B, H, N, M)
|
| """
|
| q = rearrange(self.proj_q(input_q), 'b n (h c) -> b h n c', h=self.num_heads)
|
| k = rearrange(self.proj_k(input_k), 'b m (h c) -> b h m c', h=self.num_heads)
|
| v = rearrange(self.proj_v(input_v), 'b m (h c) -> b h m c', h=self.num_heads)
|
| p = rearrange(self.proj_p(embed_qk), 'b n m (h c) -> b h n m c', h=self.num_heads)
|
|
|
| attention_scores_p = torch.einsum('bhnc,bhnmc->bhnm', q, p)
|
| attention_scores_e = torch.einsum('bhnc,bhmc->bhnm', q, k)
|
| attention_scores = (attention_scores_e + attention_scores_p) / self.d_model_per_head ** 0.5
|
| if attention_factors is not None:
|
| attention_scores = attention_factors.unsqueeze(1) * attention_scores
|
| if key_weights is not None:
|
| attention_scores = attention_scores * key_weights.unsqueeze(1).unsqueeze(1)
|
| if key_masks is not None:
|
| attention_scores = attention_scores.masked_fill(key_masks.unsqueeze(1).unsqueeze(1), float('-inf'))
|
| attention_scores = F.softmax(attention_scores, dim=-1)
|
| attention_scores = self.dropout(attention_scores)
|
|
|
| hidden_states = torch.matmul(attention_scores, v)
|
|
|
| hidden_states = rearrange(hidden_states, 'b h n c -> b n (h c)')
|
|
|
| return hidden_states, attention_scores
|
|
|
|
|
| class RPEAttentionLayer(nn.Module):
|
| def __init__(self, d_model, num_heads, dropout=None):
|
| super(RPEAttentionLayer, self).__init__()
|
| self.attention = RPEMultiHeadAttention(d_model, num_heads, dropout=dropout)
|
| self.linear = nn.Linear(d_model, d_model)
|
| self.dropout = build_dropout_layer(dropout)
|
| self.norm = nn.LayerNorm(d_model)
|
|
|
| def forward(
|
| self,
|
| input_states,
|
| memory_states,
|
| position_states,
|
| memory_weights=None,
|
| memory_masks=None,
|
| attention_factors=None,
|
| ):
|
| hidden_states, attention_scores = self.attention(
|
| input_states,
|
| memory_states,
|
| memory_states,
|
| position_states,
|
| key_weights=memory_weights,
|
| key_masks=memory_masks,
|
| attention_factors=attention_factors,
|
| )
|
| hidden_states = self.linear(hidden_states)
|
| hidden_states = self.dropout(hidden_states)
|
| output_states = self.norm(hidden_states + input_states)
|
| return output_states, attention_scores
|
|
|
|
|
| class RPETransformerLayer(nn.Module):
|
| def __init__(self, d_model, num_heads, dropout=None, activation_fn='ReLU'):
|
| super(RPETransformerLayer, self).__init__()
|
| self.attention = RPEAttentionLayer(d_model, num_heads, dropout=dropout)
|
| self.output = AttentionOutput(d_model, dropout=dropout, activation_fn=activation_fn)
|
|
|
| def forward(
|
| self,
|
| input_states,
|
| memory_states,
|
| position_states,
|
| memory_weights=None,
|
| memory_masks=None,
|
| attention_factors=None,
|
| ):
|
| hidden_states, attention_scores = self.attention(
|
| input_states,
|
| memory_states,
|
| position_states,
|
| memory_weights=memory_weights,
|
| memory_masks=memory_masks,
|
| attention_factors=attention_factors,
|
| )
|
| output_states = self.output(hidden_states)
|
| return output_states, attention_scores
|
|
|
|
|
|
|
| class GeometricTransformer(nn.Module):
|
| def __init__(
|
| self,
|
| blocks,
|
| d_model,
|
| num_heads,
|
| dropout=None,
|
| activation_fn='ReLU',
|
| return_attention_scores=False,
|
| parallel=False,
|
| ):
|
| super(GeometricTransformer, self).__init__()
|
| self.blocks = blocks
|
| layers = []
|
| for block in self.blocks:
|
| _check_block_type(block)
|
| if block == 'self':
|
| layers.append(RPETransformerLayer(d_model, num_heads, dropout=dropout, activation_fn=activation_fn))
|
| else:
|
| layers.append(TransformerLayer(d_model, num_heads, dropout=dropout, activation_fn=activation_fn))
|
| self.layers = nn.ModuleList(layers)
|
| self.return_attention_scores = return_attention_scores
|
| self.parallel = parallel
|
|
|
| def forward(self, feats0, embeddings0, feats1, embeddings1, masks0=None, masks1=None):
|
| attention_scores = []
|
| for i, block in enumerate(self.blocks):
|
| if block == 'self':
|
| feats0, scores0 = self.layers[i](feats0, feats0, embeddings0, memory_masks=masks0)
|
| feats1, scores1 = self.layers[i](feats1, feats1, embeddings1, memory_masks=masks1)
|
| else:
|
| if self.parallel:
|
| new_feats0, scores0 = self.layers[i](feats0, feats1, memory_masks=masks1)
|
| new_feats1, scores1 = self.layers[i](feats1, feats0, memory_masks=masks0)
|
| feats0 = new_feats0
|
| feats1 = new_feats1
|
| else:
|
| feats0, scores0 = self.layers[i](feats0, feats1, memory_masks=masks1)
|
| feats1, scores1 = self.layers[i](feats1, feats0, memory_masks=masks0)
|
| if self.return_attention_scores:
|
| attention_scores.append([scores0, scores1])
|
| if self.return_attention_scores:
|
| return feats0, feats1, attention_scores
|
| else:
|
| return feats0, feats1
|
|
|
|
|
|
|
|
|
| class LinearAttention(nn.Module):
|
| def __init__(self, d_model, num_heads, focusing_factor=3):
|
| super(LinearAttention, self).__init__()
|
| if d_model % num_heads != 0:
|
| raise ValueError('`d_model` ({}) must be a multiple of `num_heads` ({}).'.format(d_model, num_heads))
|
| self.d_model = d_model
|
| self.num_heads = num_heads
|
| self.d_model_per_head = d_model // num_heads
|
| self.focusing_factor = focusing_factor
|
| self.kernel_function = nn.ReLU()
|
|
|
| self.proj_q = nn.Linear(self.d_model, self.d_model)
|
| self.proj_k = nn.Linear(self.d_model, self.d_model)
|
| self.proj_v = nn.Linear(self.d_model, self.d_model)
|
| self.scale = nn.Parameter(torch.zeros(size=(1, 1, self.d_model)))
|
|
|
| def forward(self, input_q, input_k, input_v):
|
|
|
| q = self.proj_q(input_q)
|
| k = self.proj_k(input_k)
|
| v = self.proj_v(input_v)
|
| scale = nn.Softplus()(self.scale)
|
|
|
| q = self.kernel_function(q) + 1e-6
|
| k = self.kernel_function(k) + 1e-6
|
| q = q / scale
|
| k = k / scale
|
| q_norm = q.norm(dim=-1, keepdim=True)
|
| k_norm = k.norm(dim=-1, keepdim=True)
|
| q = q ** self.focusing_factor
|
| k = k ** self.focusing_factor
|
| q = (q / q.norm(dim=-1, keepdim=True)) * q_norm
|
| k = (k / k.norm(dim=-1, keepdim=True)) * k_norm
|
|
|
| q, k, v = (rearrange(x, "b n (h c) -> (b h) n c", h=self.num_heads) for x in [q, k, v])
|
| i, j, c, d = q.shape[-2], k.shape[-2], k.shape[-1], v.shape[-1]
|
|
|
| z = 1 / (torch.einsum("b i c, b c -> b i", q, k.sum(dim=1)) + 1e-6)
|
| if i * j * (c + d) > c * d * (i + j):
|
| kv = torch.einsum("b j c, b j d -> b c d", k, v)
|
| x = torch.einsum("b i c, b c d, b i -> b i d", q, kv, z)
|
| else:
|
| qk = torch.einsum("b i c, b j c -> b i j", q, k)
|
| x = torch.einsum("b i j, b j d, b i -> b i d", qk, v, z)
|
| x = rearrange(x, "(b h) n c -> b n (h c)", h=self.num_heads)
|
|
|
| return x
|
|
|
|
|
| class LinearAttentionLayer(nn.Module):
|
| def __init__(self, d_model, num_heads, dropout=False, focusing_factor=3):
|
| super(LinearAttentionLayer, self).__init__()
|
| self.attention = LinearAttention(d_model, num_heads, focusing_factor=focusing_factor)
|
| self.linear = nn.Linear(d_model, d_model)
|
| self.dropout = build_dropout_layer(dropout)
|
| self.norm = nn.LayerNorm(d_model)
|
|
|
| def forward(
|
| self,
|
| input_states,
|
| memory_states,
|
| ):
|
| hidden_states= self.attention(
|
| input_states,
|
| memory_states,
|
| memory_states,
|
| )
|
| hidden_states = self.linear(hidden_states)
|
| hidden_states = self.dropout(hidden_states)
|
| output_states = self.norm(hidden_states + input_states)
|
| return output_states
|
|
|
|
|
|
|
| class LinearTransformerLayer(nn.Module):
|
| def __init__(self, d_model, num_heads, dropout=None, activation_fn='ReLU', focusing_factor=3):
|
| super(LinearTransformerLayer, self).__init__()
|
| self.attention = LinearAttentionLayer(d_model, num_heads, dropout=dropout, focusing_factor=focusing_factor)
|
| self.output = AttentionOutput(d_model, dropout=dropout, activation_fn=activation_fn)
|
|
|
| def forward(
|
| self,
|
| input_states,
|
| memory_states
|
| ):
|
| hidden_states = self.attention(
|
| input_states,
|
| memory_states
|
| )
|
| output_states = self.output(hidden_states)
|
| return output_states
|
|
|
|
|
|
|
|
|
| class SparseToDenseTransformer(nn.Module):
|
| def __init__(
|
| self,
|
| d_model,
|
| sparse_blocks,
|
| num_heads=4,
|
| dropout=None,
|
| activation_fn='ReLU',
|
| parallel=False,
|
| focusing_factor=3,
|
| with_bg_token=True,
|
| replace_bg_token=True
|
| ):
|
| super(SparseToDenseTransformer, self).__init__()
|
| self.with_bg_token = with_bg_token
|
| self.replace_bg_token = replace_bg_token
|
|
|
| self.sparse_layer = GeometricTransformer(
|
| blocks=sparse_blocks,
|
| d_model=d_model,
|
| num_heads=num_heads,
|
| dropout=dropout,
|
| activation_fn=activation_fn,
|
| parallel=parallel,
|
| return_attention_scores=False,
|
| )
|
| self.dense_layer = LinearTransformerLayer(d_model, num_heads, focusing_factor=focusing_factor)
|
|
|
|
|
| def forward(self, dense_feats0, embeddings0, fps_idx0, dense_feats1, embeddings1, fps_idx1, masks0=None, masks1=None):
|
| feats0 = self._sample_feats(dense_feats0, fps_idx0)
|
| feats1 = self._sample_feats(dense_feats1, fps_idx1)
|
| feats0, feats1 = self.sparse_layer(feats0, embeddings0, feats1, embeddings1, masks0, masks1)
|
|
|
| dense_feats0 = self._get_dense_feats(dense_feats0, feats0)
|
| dense_feats1 = self._get_dense_feats(dense_feats1, feats1)
|
| return dense_feats0, dense_feats1
|
|
|
| def _sample_feats(self, dense_feats, fps_idx):
|
| if self.with_bg_token:
|
| bg_token = dense_feats[:, 0:1, :].contiguous()
|
| feats = gather_operation(dense_feats.transpose(1,2).contiguous(), fps_idx)
|
| feats = feats.transpose(1,2).contiguous()
|
| if self.with_bg_token:
|
| feats = torch.cat([bg_token, feats], dim=1)
|
| return feats
|
|
|
| def _get_dense_feats(self, dense_feats, feats):
|
| if self.with_bg_token and self.replace_bg_token:
|
| bg_token = feats[:, 0:1, :].contiguous()
|
| dense_feats = self.dense_layer(
|
| dense_feats[:,1:,:].contiguous(),
|
| feats[:,1:,:].contiguous()
|
| )
|
| dense_feats = torch.cat([bg_token, dense_feats], dim=1)
|
| else:
|
| dense_feats = self.dense_layer(
|
| dense_feats,
|
| feats
|
| )
|
| return dense_feats
|
|
|
|
|
|
|
|
|