code: def forward(
      self,
      q: torch.Tensor,
      k: torch.Tensor,
      v: torch.Tensor,
      att_mask: Optional[Union[torch.Tensor, AttentionMask]] = None,
      *_,
      **__,
  ):
      # Make sure that the mask is on the right device
      if self.attention_mask.device != q.device:
          self.attention_mask = self.attention_mask.to(q.device)
  
      # Mask-aware attention
      if att_mask is not None:
          if att_mask.dtype == torch.bool and isinstance(
              self.attention_mask, AttentionMask
          ):
              if not isinstance(att_mask, AttentionMask):
                  att_mask = AttentionMask.from_bool(att_mask)
              mask = self.attention_mask + att_mask
          else:
              mask = self.attention_mask & att_mask
      else:
          mask = self.attention_mask
  
      # Handle q/k/v which would not fit the mask
      seq_len = q.shape[-2]
      q_, k_, v_ = map(
          lambda x: self._maybe_pad_sequence(x, mask), (q, k, v)
      )
  
      # Normal attention with the global tokens mask
      att = scaled_dot_product_attention(
          q=q_, k=k_, v=v_, att_mask=mask, dropout=self.attn_drop
      )
  
      # Take into account an hypothetical padding
      return att[:, :seq_len, :]
  
file_path: xformers/xformers/components/attention/global_tokens.py
imports: from dataclasses import dataclass
  from typing import Optional, Union
  import torch
  import torch.nn
  from xformers.components.attention import Attention, AttentionConfig, AttentionMask, maybe_sparsify, register_attention, sparsify
  from xformers.components.attention.attention_patterns import causal_1d_pattern, global_token_pattern
  from xformers.components.attention.core import scaled_dot_product_attention
label_desc: Use image node_id 2 for calling the GlobalAttention obj's underlying member method code with example usage: obj.forward(q, k, v, att_mask) and returns: att