class_docstr: Position attention decoder for RobustScanner.
  
  RobustScanner: `RobustScanner: Dynamically Enhancing Positional Clues for
  Robust Text Recognition <https://arxiv.org/abs/2007.07542>`_
  
  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.
      rnn_layers (int): Number of RNN layers. Defaults to 2.
      dim_input (int): Dimension :math:`D_i` of input vector ``feat``.
          Defaults to 512.
      dim_model (int): Dimension :math:`D_m` of the model. Should also be the
          same as encoder output vector ``out_enc``. Defaults to 128.
      max_seq_len (int): Maximum output sequence length :math:`T`. Defaults
          to 40.
      mask (bool): Whether to mask input features according to
          ``img_meta['valid_ratio']``. Defaults to True.
      return_feature (bool): Return feature or logits as the result. Defaults
          to True.
      encode_value (bool): Whether to use the output of encoder ``out_enc``
          as `value` of attention layer. If False, the original feature
          ``feat`` will be used. Defaults to False.
      init_cfg (dict or list[dict], optional): Initialization configs.
          Defaults to None.
class_docstr_tok: ["Position","attention","decoder","for","RobustScanner",".","RobustScanner",":","`","RobustScanner",":","Dynamically","Enhancing","Positional","Clues","for","Robust","Text","Recognition","<","https",":","\/\/arxiv.org\/abs\/2007.07542",">","`","_","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",".","rnn_layers","(","int",")",":","Number","of","RNN","layers",".","Defaults","to","2.","dim_input","(","int",")",":","Dimension",":","math",":","`","D_i","`","of","input","vector","``","feat","``",".","Defaults","to","512.","dim_model","(","int",")",":","Dimension",":","math",":","`","D_m","`","of","the","model",".","Should","also","be","the","same","as","encoder","output","vector","``","out_enc","``",".","Defaults","to","128.","max_seq_len","(","int",")",":","Maximum","output","sequence","length",":","math",":","`","T","`",".","Defaults","to","40.","mask","(","bool",")",":","Whether","to","mask","input","features","according","to","``","img_meta","[","'valid_ratio","'","]","``",".","Defaults","to","True",".","return_feature","(","bool",")",":","Return","feature","or","logits","as","the","result",".","Defaults","to","True",".","encode_value","(","bool",")",":","Whether","to","use","the","output","of","encoder","``","out_enc","``","as","`","value","`","of","attention","layer",".","If","False",",","the","original","feature","``","feat","``","will","be","used",".","Defaults","to","False",".","init_cfg","(","dict","or","list","[","dict","]",",","optional",")",":","Initialization","configs",".","Defaults","to","None","."]
docstr: Args:
      feat (Tensor): Tensor of shape :math:`(N, D_i, H, W)`.
      out_enc (Tensor): Encoder output of shape
          :math:`(N, D_m, H, W)`.
      data_samples (list[TextRecogDataSample], optional): Batch of
          TextRecogDataSample, containing gt_text information. Defaults
          to None.
  
  Returns:
      Tensor: A raw logit tensor of shape :math:`(N, T, C)` if
      ``return_feature=False``. Otherwise it will be the hidden feature
      before the prediction projection layer, whose shape is
      :math:`(N, T, D_m)`.
docstr_tok: ["Args",":","feat","(","Tensor",")",":","Tensor","of","shape",":","math",":","`","(","N",",","D_i",",","H",",","W",")","`",".","out_enc","(","Tensor",")",":","Encoder","output","of","shape",":","math",":","`","(","N",",","D_m",",","H",",","W",")","`",".","data_samples","(","list","[","TextRecogDataSample","]",",","optional",")",":","Batch","of","TextRecogDataSample",",","containing","gt_text","information",".","Defaults","to","None",".","Returns",":","Tensor",":","A","raw","logit","tensor","of","shape",":","math",":","`","(","N",",","T",",","C",")","`","if","``","return_feature=False","``",".","Otherwise","it","will","be","the","hidden","feature","before","the","prediction","projection","layer",",","whose","shape","is",":","math",":","`","(","N",",","T",",","D_m",")","`","."]
code: def forward_train(
      self,
      feat: torch.Tensor,
      out_enc: torch.Tensor,
      data_samples: Sequence[TextRecogDataSample],
  ) -> torch.Tensor:
      """
      Args:
          feat (Tensor): Tensor of shape :math:`(N, D_i, H, W)`.
          out_enc (Tensor): Encoder output of shape
              :math:`(N, D_m, H, W)`.
          data_samples (list[TextRecogDataSample], optional): Batch of
              TextRecogDataSample, containing gt_text information. Defaults
              to None.
  
      Returns:
          Tensor: A raw logit tensor of shape :math:`(N, T, C)` if
          ``return_feature=False``. Otherwise it will be the hidden feature
          before the prediction projection layer, whose shape is
          :math:`(N, T, D_m)`.
      """
      valid_ratios = (
          [
              data_sample.get("valid_ratio", 1.0)
              for data_sample in data_samples
          ]
          if self.mask
          else None
      )
  
      #
      n, c_enc, h, w = out_enc.size()
      assert c_enc == self.dim_model
      _, c_feat, _, _ = feat.size()
      assert c_feat == self.dim_input
      position_index = self._get_position_index(
          self.max_seq_len, n, feat.device
      )
  
      position_out_enc = self.position_aware_module(out_enc)
  
      query = self.embedding(position_index)
      query = query.permute(0, 2, 1).contiguous()
      key = position_out_enc.view(n, c_enc, h * w)
      if self.encode_value:
          value = out_enc.view(n, c_enc, h * w)
      else:
          value = feat.view(n, c_feat, h * w)
  
      mask = None
      if valid_ratios is not None:
          mask = query.new_zeros((n, h, w))
          for i, valid_ratio in enumerate(valid_ratios):
              valid_width = min(w, math.ceil(w * valid_ratio))
              mask[i, :, valid_width:] = 1
          mask = mask.bool()
          mask = mask.view(n, h * w)
  
      attn_out = self.attention_layer(query, key, value, mask)
      attn_out = attn_out.permute(
          0, 2, 1
      ).contiguous()  # [n, max_seq_len, dim_v]
  
      if self.return_feature:
          return attn_out
  
      return self.prediction(attn_out)
  
file_path: mmocr/mmocr/models/textrecog/decoders/position_attention_decoder.py
imports: import math
  from typing import Dict, Optional, Sequence, Union
  import torch
  import torch.nn
  from mmocr.models.common.dictionary import Dictionary
  from mmocr.models.textrecog.layers import DotProductAttentionLayer, PositionAwareLayer
  from mmocr.registry import MODELS
  from mmocr.structures import TextRecogDataSample
  from .base import BaseDecoder
label_desc: Use image node_id 3 for calling the PositionAttentionDecoder obj's underlying member method code with example usage: obj.forward_train(feat, out_enc, data_samples) and returns: self, attn_out