class_docstr: GCAModule in MASTER.
  
  Args:
      in_channels (int): Channels of input tensor.
      ratio (float): Scale ratio of in_channels.
      n_head (int): Numbers of attention head.
      pooling_type (str): Spatial pooling type. Options are [``avg``,
          ``att``].
      scale_attn (bool): Whether to scale the attention map. Defaults to
          False.
      fusion_type (str): Fusion type of input and context. Options are
          [``channel_add``, ``channel_mul``, ``channel_concat``].
class_docstr_tok: ["GCAModule","in","MASTER",".","Args",":","in_channels","(","int",")",":","Channels","of","input","tensor",".","ratio","(","float",")",":","Scale","ratio","of","in_channels",".","n_head","(","int",")",":","Numbers","of","attention","head",".","pooling_type","(","str",")",":","Spatial","pooling","type",".","Options","are","[","``","avg","``",",","``","att","``","]",".","scale_attn","(","bool",")",":","Whether","to","scale","the","attention","map",".","Defaults","to","False",".","fusion_type","(","str",")",":","Fusion","type","of","input","and","context",".","Options","are","[","``","channel_add","``",",","``","channel_mul","``",",","``","channel_concat","``","]","."]
docstr: Spatial pooling function.
  
  Args:
      x (Tensor): Input feature map.
  
  Returns:
      Tensor: Output tensor after spatial pooling.
docstr_tok: ["Spatial","pooling","function",".","Args",":","x","(","Tensor",")",":","Input","feature","map",".","Returns",":","Tensor",":","Output","tensor","after","spatial","pooling","."]
code: def spatial_pool(self, x: torch.Tensor) -> torch.Tensor:
      """Spatial pooling function.
  
      Args:
          x (Tensor): Input feature map.
  
      Returns:
          Tensor: Output tensor after spatial pooling.
      """
      batch, channel, height, width = x.size()
      if self.pooling_type == "att":
          # [N*headers, C', H , W] C = headers * C'
          x = x.view(
              batch * self.n_head,
              self.single_header_inplanes,
              height,
              width,
          )
          input_x = x
  
          # [N*headers, C', H * W] C = headers * C'
          input_x = input_x.view(
              batch * self.n_head,
              self.single_header_inplanes,
              height * width,
          )
  
          # [N*headers, 1, C', H * W]
          input_x = input_x.unsqueeze(1)
          # [N*headers, 1, H, W]
          context_mask = self.conv_mask(x)
          # [N*headers, 1, H * W]
          context_mask = context_mask.view(
              batch * self.n_head, 1, height * width
          )
  
          # scale variance
          if self.scale_attn and self.n_head > 1:
              context_mask = context_mask / torch.sqrt(
                  self.single_header_inplanes
              )
  
          # [N*headers, 1, H * W]
          context_mask = self.softmax(context_mask)
  
          # [N*headers, 1, H * W, 1]
          context_mask = context_mask.unsqueeze(-1)
          # [N*headers, 1, C', 1] =
          # [N*headers, 1, C', H * W] * [N*headers, 1, H * W, 1]
          context = torch.matmul(input_x, context_mask)
  
          # [N, headers * C', 1, 1]
          context = context.view(
              batch, self.n_head * self.single_header_inplanes, 1, 1
          )
      else:
          # [N, C, 1, 1]
          context = self.avg_pool(x)
  
      return context
  
file_path: mmocr/mmocr/models/textrecog/plugins/common.py
imports: from typing import Tuple, Union
  import torch
  import torch.nn
  from mmocr.registry import MODELS
label_desc: Use image node_id 2 for calling the GCAModule obj's underlying member method code with example usage: obj.spatial_pool(x) and returns: context