class_docstr: This layer applies a multi-head self- or cross-attention as described in
  `Attention is all you need <https://arxiv.org/abs/1706.03762>`_ paper
  
  Args:
      embed_dim (int): :math:`C_{in}` from an expected input of size :math:`(N, S, C_{in})`
      num_heads (int): Number of heads in multi-head attention
      attn_dropout (Optional[float]): Attention dropout. Default: 0.0
      bias (Optional[bool]): Use bias or not. Default: ``True``
  
  Shape:
      - Input:
         - Query tensor (x_q) :math:`(N, S, C_{in})` where :math:`N` is batch size, :math:`S` is number of source tokens,
      and :math:`C_{in}` is input embedding dim
         - Optional Key-Value tensor (x_kv) :math:`(N, T, C_{in})` where :math:`T` is number of target tokens
      - Output: same shape as the input
code: def forward(
      self,
      x_q: Tensor,
      x_kv: Optional[Tensor] = None,
      key_padding_mask: Optional[Tensor] = None,
      attn_mask: Optional[Tensor] = None,
      *args,
      **kwargs,
  ) -> Tensor:
      if self.coreml_compatible:
          # For CoreML, we follow batch-first format. Make sure the input is of the form
          # [Batch , Sequence, Hidden_dim]
          return self.forward_tracing(
              x_q=x_q,
              x_kv=x_kv,
              key_padding_mask=key_padding_mask,
              attn_mask=attn_mask,
          )
      elif kwargs.get("use_pytorch_mha", False):
          # pytorch uses sequence-first format. Make sure that input is of the form [Sequence, Batch, Hidden dim]
          return self.forward_pytorch(
              x_q=x_q,
              x_kv=x_kv,
              key_padding_mask=key_padding_mask,
              attn_mask=attn_mask,
          )
      else:
          # our default implementation format follows batch-first format. Make sure the input is of the form
          # [Batch , Sequence, Hidden_dim]
          return self.forward_default(
              x_q=x_q,
              x_kv=x_kv,
              key_padding_mask=key_padding_mask,
              attn_mask=attn_mask,
          )
  
file_path: ml-cvnets/cvnets/layers/multi_head_attention.py
imports: from typing import Optional
  import torch
  from torch import Tensor, nn
  from torch.nn import functional
  from cvnets.layers.base_layer import BaseLayer
  from cvnets.layers.dropout import Dropout
  from cvnets.layers.linear_layer import LinearLayer
  from utils import logger
label_desc: Use image node_id 6 for calling the MultiHeadAttention obj's underlying member method code with example usage: obj.forward(x_q, x_kv, key_padding_mask, attn_mask) and returns: self, self, self