class_docstr: Implement attention decoder.
  
  Args:
      in_channels (int): Number of input channels.
      emb_dims (int): Dims of char embedding. Defaults to 512.
      attn_dims (int): Dims of attention. Both hidden states and features
          will be projected to this dims. Defaults to 512.
      hidden_size (int): Dims of hidden state for GRU. Defaults to 512.
      dictionary (dict or :obj:`Dictionary`): The config for `Dictionary` or
          the instance of `Dictionary`. Defaults to None.
      max_seq_len (int): Maximum output sequence length :math:`T`. Defaults
          to 25.
      module_loss (dict, optional): Config to build loss. Defaults to None.
      postprocessor (dict, optional): Config to build postprocessor.
          Defaults to None.
      init_cfg (dict or list[dict], optional): Initialization configs.
          Defaults to None.
class_docstr_tok: ["Implement","attention","decoder",".","Args",":","in_channels","(","int",")",":","Number","of","input","channels",".","emb_dims","(","int",")",":","Dims","of","char","embedding",".","Defaults","to","512.","attn_dims","(","int",")",":","Dims","of","attention",".","Both","hidden","states","and","features","will","be","projected","to","this","dims",".","Defaults","to","512.","hidden_size","(","int",")",":","Dims","of","hidden","state","for","GRU",".","Defaults","to","512.","dictionary","(","dict","or",":","obj",":","`","Dictionary","`",")",":","The","config","for","`","Dictionary","`","or","the","instance","of","`","Dictionary","`",".","Defaults","to","None",".","max_seq_len","(","int",")",":","Maximum","output","sequence","length",":","math",":","`","T","`",".","Defaults","to","25.","module_loss","(","dict",",","optional",")",":","Config","to","build","loss",".","Defaults","to","None",".","postprocessor","(","dict",",","optional",")",":","Config","to","build","postprocessor",".","Defaults","to","None",".","init_cfg","(","dict","or","list","[","dict","]",",","optional",")",":","Initialization","configs",".","Defaults","to","None","."]
docstr: Implement the attention mechanism.
  
  Args:
      feat (Tensor): Feature map from encoder of shape :math:`(N, T, C)`.
      prev_hidden (Tensor): Previous hidden state from GRU of shape
          :math:`(1, N, self.hidden_size)`.
      prev_char (Tensor): Previous predicted character of shape
          :math:`(N, )`.
  
  Returns:
      tuple(Tensor, Tensor):
      - output (Tensor): Predicted character of current time step of
        shape :math:`(N, 1)`.
      - state (Tensor): Hidden state from GRU of current time step of
        shape :math:`(N, self.hidden_size)`.
docstr_tok: ["Implement","the","attention","mechanism",".","Args",":","feat","(","Tensor",")",":","Feature","map","from","encoder","of","shape",":","math",":","`","(","N",",","T",",","C",")","`",".","prev_hidden","(","Tensor",")",":","Previous","hidden","state","from","GRU","of","shape",":","math",":","`","(","1",",","N",",","self.hidden_size",")","`",".","prev_char","(","Tensor",")",":","Previous","predicted","character","of","shape",":","math",":","`","(","N",",",")","`",".","Returns",":","tuple","(","Tensor",",","Tensor",")",":","-","output","(","Tensor",")",":","Predicted","character","of","current","time","step","of","shape",":","math",":","`","(","N",",","1",")","`",".","-","state","(","Tensor",")",":","Hidden","state","from","GRU","of","current","time","step","of","shape",":","math",":","`","(","N",",","self.hidden_size",")","`","."]
code: def _attention(
      self,
      feat: torch.Tensor,
      prev_hidden: torch.Tensor,
      prev_char: torch.Tensor,
  ) -> Tuple[torch.Tensor, torch.Tensor]:
      """Implement the attention mechanism.
  
      Args:
          feat (Tensor): Feature map from encoder of shape :math:`(N, T, C)`.
          prev_hidden (Tensor): Previous hidden state from GRU of shape
              :math:`(1, N, self.hidden_size)`.
          prev_char (Tensor): Previous predicted character of shape
              :math:`(N, )`.
  
      Returns:
          tuple(Tensor, Tensor):
          - output (Tensor): Predicted character of current time step of
            shape :math:`(N, 1)`.
          - state (Tensor): Hidden state from GRU of current time step of
            shape :math:`(N, self.hidden_size)`.
      """
      # Calculate the attention weights
      B, T, _ = feat.size()
      feat_proj = self.proj_feat(feat)  # [N, T, attn_dims]
      hidden_proj = self.proj_hidden(prev_hidden)  # [1, N, attn_dims]
      hidden_proj = hidden_proj.squeeze(0).unsqueeze(
          1
      )  # [N, 1, attn_dims]
      hidden_proj = hidden_proj.expand(
          B, T, self.att_dims
      )  # [N, T, attn_dims]
  
      sum_tanh = torch.tanh(
          feat_proj + hidden_proj
      )  # [N, T, attn_dims]
      sum_proj = self.proj_sum(sum_tanh).squeeze(-1)  # [N, T]
      attn_weights = torch.softmax(sum_proj, dim=1)  # [N, T]
  
      # GRU forward
      context = torch.bmm(attn_weights.unsqueeze(1), feat).squeeze(1)
      char_embed = self.embedding(prev_char.long())  # [N, emb_dims]
      output, state = self.gru(
          torch.cat([char_embed, context], 1).unsqueeze(1), prev_hidden
      )
      output = output.squeeze(1)
      output = self.fc(output)
      return output, state
  
file_path: mmocr/mmocr/models/textrecog/decoders/aster_decoder.py
imports: from typing import Dict, Optional, Sequence, Tuple, Union
  import torch
  import torch.nn
  from mmocr.models.common.dictionary import Dictionary
  from mmocr.registry import MODELS
  from mmocr.structures import TextRecogDataSample
  from .base import BaseDecoder
label_desc: Use image node_id 2 for calling the ASTERDecoder obj's underlying member method code with example usage: obj._attention(feat, prev_hidden, prev_char) and returns: output,  state