class_docstr: Gated positional self-attention. This is what makes a ConViT a ConViT.
  The "gating" refers to lambda, the parameter that controls the relative
  contribution (i.e. gating) of positional vs. content-based attention in
  each head.
class_docstr_tok: ["Gated","positional","self-attention",".","This","is","what","makes","a","ConViT","a","ConViT",".","The","``","gating","''","refers","to","lambda",",","the","parameter","that","controls","the","relative","contribution","(","i.e",".","gating",")","of","positional","vs.","content-based","attention","in","each","head","."]
code: def get_attention(self, x):
      B, N, C = x.shape
      # Get relative position indices if they don't exist or are the
      # incorrect size. This allows accommodating different resolution inputs.
      if (
          not hasattr(self, "rel_indices")
          or self.rel_indices.size(1) != N
      ):
          self.set_rel_indices(N)
      qk = (
          self.qk(x)
          .reshape(B, N, 2, self.num_heads, C // self.num_heads)
          .permute(2, 0, 3, 1, 4)
      )
      q, k = qk[0], qk[1]
      # Positional attention
      pos_score = self.rel_indices.expand(B, -1, -1, -1)
      pos_score = self.pos_proj(pos_score).permute(0, 3, 1, 2)
      pos_score /= self.locality_dim
      pos_score = pos_score.softmax(dim=-1)
      # Content-based attention
      patch_score = (q @ k.transpose(-2, -1)) * self.scale
      patch_score = patch_score.softmax(dim=-1)
      # This is where the gating happens
      attn = (
          1.0 - torch.sigmoid(self.alpha)
      ) * patch_score + torch.sigmoid(self.alpha) * pos_score
      # Normalize
      attn /= attn.sum(dim=-1).unsqueeze(-1)
      attn = self.attn_drop(attn)
      return attn
  
imports: import copy
  import logging
  import math
  import torch
  import torch.nn
  from vissl.models.model_helpers import DropPath, to_2tuple, trunc_normal_
  from vissl.models.trunks import register_model_trunk
  from vissl.utils.hydra_config import AttrDict
label_desc: Use image node_id 4 for calling the GPSA obj's underlying member method code with example usage: obj.get_attention(x) and returns: attn