class_docstr: Implementation of encoder module in `SAR.
  
  <https://arxiv.org/abs/1811.00751>`_.
  
  Args:
      enc_bi_rnn (bool): If True, use bidirectional RNN in encoder.
          Defaults to False.
      rnn_dropout (float): Dropout probability of RNN layer in encoder.
          Defaults to 0.0.
      enc_gru (bool): If True, use GRU, else LSTM in encoder. Defaults
          to False.
      d_model (int): Dim :math:`D_i` of channels from backbone. Defaults
          to 512.
      d_enc (int): Dim :math:`D_m` of encoder RNN layer. Defaults to 512.
      mask (bool): If True, mask padding in RNN sequence. Defaults to
          True.
      init_cfg (dict or list[dict], optional): Initialization configs.
          Defaults to [dict(type='Xavier', layer='Conv2d'),
          dict(type='Uniform', layer='BatchNorm2d')].
code: def __init__(
      self,
      enc_bi_rnn: bool = False,
      rnn_dropout: Union[int, float] = 0.0,
      enc_gru: bool = False,
      d_model: int = 512,
      d_enc: int = 512,
      mask: bool = True,
      init_cfg: Sequence[Dict] = [
          dict(type="Xavier", layer="Conv2d"),
          dict(type="Uniform", layer="BatchNorm2d"),
      ],
      **kwargs,
  ) -> None:
      super().__init__(init_cfg=init_cfg)
      assert isinstance(enc_bi_rnn, bool)
      assert isinstance(rnn_dropout, (int, float))
      assert 0 <= rnn_dropout < 1.0
      assert isinstance(enc_gru, bool)
      assert isinstance(d_model, int)
      assert isinstance(d_enc, int)
      assert isinstance(mask, bool)
  
      self.enc_bi_rnn = enc_bi_rnn
      self.rnn_dropout = rnn_dropout
      self.mask = mask
  
      # LSTM Encoder
      kwargs = dict(
          input_size=d_model,
          hidden_size=d_enc,
          num_layers=2,
          batch_first=True,
          dropout=rnn_dropout,
          bidirectional=enc_bi_rnn,
      )
      if enc_gru:
          self.rnn_encoder = nn.GRU(**kwargs)
      else:
          self.rnn_encoder = nn.LSTM(**kwargs)
  
      # global feature transformation
      encoder_rnn_out_size = d_enc * (int(enc_bi_rnn) + 1)
      self.linear = nn.Linear(
          encoder_rnn_out_size, encoder_rnn_out_size
      )
  
file_path: mmocr/mmocr/models/textrecog/encoders/sar_encoder.py
imports: import math
  from typing import Dict, Optional, Sequence, Union
  import torch
  import torch.nn
  import torch.nn.functional
  from mmocr.registry import MODELS
  from mmocr.structures import TextRecogDataSample
  from .base import BaseEncoder
label_desc: Use image node_id 1 to create a new SAREncoder object from inherited base classes: BaseEncoder with example: obj = SAREncoder(enc_bi_rnn, rnn_dropout, enc_gru, d_model, d_enc, mask, init_cfg)