class_docstr: This class implements attention pooling layer, as
  described in `Clip <https://arxiv.org/pdf/2103.00020.pdf>`_, and should be
  used for CNN-style models, including MobileViTs
class_docstr_tok: ["This","class","implements","attention","pooling","layer",",","as","described","in","`","Clip","<","https",":","\/\/arxiv.org\/pdf\/2103.00020.pdf",">","`","_",",","and","should","be","used","for","CNN-style","models",",","including","MobileViTs"]
code: def __init__(
      self, opts, in_dim: int, out_dim: int, *args, **kwargs
  ) -> None:
      super().__init__(opts, *args, **kwargs)
  
      num_embeddings = getattr(
          opts,
          "model.image_projection_head.attention_pool_nchw2nc.num_pos_embeddings",
          None,
      )
      if num_embeddings is None:
          logger.error(
              "Number of embeddings can't be None in {}. Please specify using "
              "--model.image-projection.attention-pool-2d.num-pos-embeddings argument".format(
                  self.__class__.__name__
              )
          )
      sin_pos_emb = getattr(
          opts,
          "model.image_projection_head.attention_pool_nchw2nc.use_sinusoidal_pos_embeddings",
          False,
      )
      num_heads = getattr(
          opts,
          "model.image_projection_head.attention_pool_nchw2nc.num_attn_heads",
          8,
      )
  
      self.use_pytorch_mha = getattr(
          opts,
          "model.image_projection_head.attention_pool_nchw2nc.use_pytorch_mha",
          False,
      )
  
      self.positional_embedding = PositionalEmbedding(
          opts,
          num_embeddings=num_embeddings,
          embedding_dim=in_dim,
          padding_idx=None,
          is_learnable=not sin_pos_emb,
          sequence_first=self.use_pytorch_mha,
      )
      self.multi_head_attn = MultiHeadAttention(
          embed_dim=in_dim, num_heads=num_heads, output_dim=out_dim
      )
  
      self.embed_dim = in_dim
      self.projection_dim = out_dim if out_dim is not None else in_dim
      self.sin_pos_emb = sin_pos_emb
      self.normalize_features = not getattr(
          opts,
          "model.image_projection_head.attention_pool_nchw2nc.no_feature_normalization",
          False,
      )
  
      self.reset_parameters()
  
file_path: ml-cvnets/cvnets/image_projection_layers/attention_pool_2d.py
imports: import argparse
  import torch
  from torch import Tensor, nn
  from torch.nn import functional
  from cvnets.image_projection_layers import IMAGE_PROJECTION_HEAD_REGISTRY, BaseImageProjectionHead
  from cvnets.layers import MultiHeadAttention, PositionalEmbedding
  from utils import logger
label_desc: Use image node_id 1 to create a new AttentionPool2dHead object from inherited base classes: BaseImageProjectionHead with example: obj = AttentionPool2dHead(opts, in_dim, out_dim)