class_docstr: Implementation Parallel Decoder module in `SAR.
  
  <https://arxiv.org/abs/1811.00751>`_.
  
  Args:
      dictionary (dict or :obj:`Dictionary`): The config for `Dictionary` or
          the instance of `Dictionary`.
      module_loss (dict, optional): Config to build module_loss. Defaults
          to None.
      postprocessor (dict, optional): Config to build postprocessor.
          Defaults to None.
      enc_bi_rnn (bool): If True, use bidirectional RNN in encoder.
          Defaults to False.
      dec_bi_rnn (bool): If True, use bidirectional RNN in decoder.
          Defaults to False.
      dec_rnn_dropout (float): Dropout of RNN layer in decoder.
          Defaults to 0.0.
      dec_gru (bool): If True, use GRU, else LSTM in decoder. Defaults to
          False.
      d_model (int): Dim of channels from backbone :math:`D_i`. Defaults
          to 512.
      d_enc (int): Dim of encoder RNN layer :math:`D_m`. Defaults to 512.
      d_k (int): Dim of channels of attention module. Defaults to 64.
      pred_dropout (float): Dropout probability of prediction layer. Defaults
          to 0.0.
      max_seq_len (int): Maximum sequence length for decoding. Defaults to
          30.
      mask (bool): If True, mask padding in feature map. Defaults to True.
      pred_concat (bool): If True, concat glimpse feature from
          attention with holistic feature and hidden state. Defaults to
          False.
      init_cfg (dict or list[dict], optional): Initialization configs.
          Defaults to None.
class_docstr_tok: ["Implementation","Parallel","Decoder","module","in","`","SAR",".","<","https",":","\/\/arxiv.org\/abs\/1811.00751",">","`","_",".","Args",":","dictionary","(","dict","or",":","obj",":","`","Dictionary","`",")",":","The","config","for","`","Dictionary","`","or","the","instance","of","`","Dictionary","`",".","module_loss","(","dict",",","optional",")",":","Config","to","build","module_loss",".","Defaults","to","None",".","postprocessor","(","dict",",","optional",")",":","Config","to","build","postprocessor",".","Defaults","to","None",".","enc_bi_rnn","(","bool",")",":","If","True",",","use","bidirectional","RNN","in","encoder",".","Defaults","to","False",".","dec_bi_rnn","(","bool",")",":","If","True",",","use","bidirectional","RNN","in","decoder",".","Defaults","to","False",".","dec_rnn_dropout","(","float",")",":","Dropout","of","RNN","layer","in","decoder",".","Defaults","to","0.0.","dec_gru","(","bool",")",":","If","True",",","use","GRU",",","else","LSTM","in","decoder",".","Defaults","to","False",".","d_model","(","int",")",":","Dim","of","channels","from","backbone",":","math",":","`","D_i","`",".","Defaults","to","512.","d_enc","(","int",")",":","Dim","of","encoder","RNN","layer",":","math",":","`","D_m","`",".","Defaults","to","512.","d_k","(","int",")",":","Dim","of","channels","of","attention","module",".","Defaults","to","64.","pred_dropout","(","float",")",":","Dropout","probability","of","prediction","layer",".","Defaults","to","0.0.","max_seq_len","(","int",")",":","Maximum","sequence","length","for","decoding",".","Defaults","to","30.","mask","(","bool",")",":","If","True",",","mask","padding","in","feature","map",".","Defaults","to","True",".","pred_concat","(","bool",")",":","If","True",",","concat","glimpse","feature","from","attention","with","holistic","feature","and","hidden","state",".","Defaults","to","False",".","init_cfg","(","dict","or","list","[","dict","]",",","optional",")",":","Initialization","configs",".","Defaults","to","None","."]
docstr: 2D attention layer.
  
  Args:
      decoder_input (torch.Tensor): Input of decoder RNN.
      feat (torch.Tensor): Feature map of encoder.
      holistic_feat (torch.Tensor): Feature map of holistic encoder.
      valid_ratios (Sequence[float]): Valid ratios of attention.
          Defaults to None.
  
  Returns:
      torch.Tensor: Output of 2D attention layer.
docstr_tok: ["2D","attention","layer",".","Args",":","decoder_input","(","torch.Tensor",")",":","Input","of","decoder","RNN",".","feat","(","torch.Tensor",")",":","Feature","map","of","encoder",".","holistic_feat","(","torch.Tensor",")",":","Feature","map","of","holistic","encoder",".","valid_ratios","(","Sequence","[","float","]",")",":","Valid","ratios","of","attention",".","Defaults","to","None",".","Returns",":","torch.Tensor",":","Output","of","2D","attention","layer","."]
code: def _2d_attention(
      self,
      decoder_input: torch.Tensor,
      feat: torch.Tensor,
      holistic_feat: torch.Tensor,
      valid_ratios: Optional[Sequence[float]] = None,
  ) -> torch.Tensor:
      """2D attention layer.
  
      Args:
          decoder_input (torch.Tensor): Input of decoder RNN.
          feat (torch.Tensor): Feature map of encoder.
          holistic_feat (torch.Tensor): Feature map of holistic encoder.
          valid_ratios (Sequence[float]): Valid ratios of attention.
              Defaults to None.
  
      Returns:
          torch.Tensor: Output of 2D attention layer.
      """
      y = self.rnn_decoder(decoder_input)[0]
      # y: bsz * (seq_len + 1) * hidden_size
  
      attn_query = self.conv1x1_1(y)  # bsz * (seq_len + 1) * attn_size
      bsz, seq_len, attn_size = attn_query.size()
      attn_query = attn_query.view(bsz, seq_len, attn_size, 1, 1)
  
      attn_key = self.conv3x3_1(feat)
      # bsz * attn_size * h * w
      attn_key = attn_key.unsqueeze(1)
      # bsz * 1 * attn_size * h * w
  
      attn_weight = torch.tanh(torch.add(attn_key, attn_query, alpha=1))
      # bsz * (seq_len + 1) * attn_size * h * w
      attn_weight = attn_weight.permute(0, 1, 3, 4, 2).contiguous()
      # bsz * (seq_len + 1) * h * w * attn_size
      attn_weight = self.conv1x1_2(attn_weight)
      # bsz * (seq_len + 1) * h * w * 1
      bsz, T, h, w, c = attn_weight.size()
      assert c == 1
  
      if valid_ratios is not None:
          # cal mask of attention weight
          attn_mask = torch.zeros_like(attn_weight)
          for i, valid_ratio in enumerate(valid_ratios):
              valid_width = min(w, math.ceil(w * valid_ratio))
              attn_mask[i, :, :, valid_width:, :] = 1
          attn_weight = attn_weight.masked_fill(
              attn_mask.bool(), float("-inf")
          )
  
      attn_weight = attn_weight.view(bsz, T, -1)
      attn_weight = F.softmax(attn_weight, dim=-1)
      attn_weight = (
          attn_weight.view(bsz, T, h, w, c)
          .permute(0, 1, 4, 2, 3)
          .contiguous()
      )
  
      attn_feat = torch.sum(
          torch.mul(feat.unsqueeze(1), attn_weight),
          (3, 4),
          keepdim=False,
      )
      # bsz * (seq_len + 1) * C
  
      # linear transformation
      if self.pred_concat:
          hf_c = holistic_feat.size(-1)
          holistic_feat = holistic_feat.expand(bsz, seq_len, hf_c)
          y = self.prediction(
              torch.cat((y, attn_feat, holistic_feat), 2)
          )
      else:
          y = self.prediction(attn_feat)
      # bsz * (seq_len + 1) * num_classes
      y = self.pred_dropout(y)
  
      return y
  
imports: import math
  from typing import Dict, List, Optional, Sequence, Union
  import torch
  import torch.nn
  import torch.nn.functional
  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 ParallelSARDecoder obj's underlying member method code with example usage: obj._2d_attention(decoder_input, feat, holistic_feat, valid_ratios) and returns: y