class_docstr: A transformer model. User is able to modify the attributes as needed. The architechture
  is based on the paper "Attention Is All You Need". Ashish Vaswani, Noam Shazeer,
  Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz Kaiser, and
  Illia Polosukhin. 2017. Attention is all you need. In Advances in Neural Information
  Processing Systems, pages 6000-6010.
  
  Args:
      d_model: the number of expected features in the encoder/decoder inputs (default=512).
      nhead: the number of heads in the multiheadattention models (default=8).
      num_encoder_layers: the number of sub-encoder-layers in the encoder (default=6).
      num_decoder_layers: the number of sub-decoder-layers in the decoder (default=6).
      dim_feedforward: the dimension of the feedforward network model (default=2048).
      dropout: the dropout value (default=0.1).
      custom_encoder: custom encoder (default=None).
      custom_decoder: custom decoder (default=None).
class_docstr_tok: ["A","transformer","model",".","User","is","able","to","modify","the","attributes","as","needed",".","The","architechture","is","based","on","the","paper","``","Attention","Is","All","You","Need","''",".","Ashish","Vaswani",",","Noam","Shazeer",",","Niki","Parmar",",","Jakob","Uszkoreit",",","Llion","Jones",",","Aidan","N","Gomez",",","Lukasz","Kaiser",",","and","Illia","Polosukhin",".","2017",".","Attention","is","all","you","need",".","In","Advances","in","Neural","Information","Processing","Systems",",","pages","6000-6010",".","Args",":","d_model",":","the","number","of","expected","features","in","the","encoder\/decoder","inputs","(","default=512",")",".","nhead",":","the","number","of","heads","in","the","multiheadattention","models","(","default=8",")",".","num_encoder_layers",":","the","number","of","sub-encoder-layers","in","the","encoder","(","default=6",")",".","num_decoder_layers",":","the","number","of","sub-decoder-layers","in","the","decoder","(","default=6",")",".","dim_feedforward",":","the","dimension","of","the","feedforward","network","model","(","default=2048",")",".","dropout",":","the","dropout","value","(","default=0.1",")",".","custom_encoder",":","custom","encoder","(","default=None",")",".","custom_decoder",":","custom","decoder","(","default=None",")","."]
docstr: Translation work in one batch 
docstr_tok: ["Translation","work","in","one","batch"]
code: def forward_beam(self, images):
      """Translation work in one batch"""
  
      def get_inst_idx_to_tensor_position_map(inst_idx_list):
          """Indicate the position of an instance in a tensor."""
          return {
              inst_idx: tensor_position
              for tensor_position, inst_idx in enumerate(inst_idx_list)
          }
  
      def collect_active_part(
          beamed_tensor, curr_active_inst_idx, n_prev_active_inst, n_bm
      ):
          """Collect tensor parts associated to active instances."""
  
          beamed_tensor_shape = paddle.shape(beamed_tensor)
          n_curr_active_inst = len(curr_active_inst_idx)
          new_shape = (
              n_curr_active_inst * n_bm,
              beamed_tensor_shape[1],
              beamed_tensor_shape[2],
          )
  
          beamed_tensor = beamed_tensor.reshape(
              [n_prev_active_inst, -1]
          )
          beamed_tensor = beamed_tensor.index_select(
              curr_active_inst_idx, axis=0
          )
          beamed_tensor = beamed_tensor.reshape(new_shape)
  
          return beamed_tensor
  
      def collate_active_info(
          src_enc, inst_idx_to_position_map, active_inst_idx_list
      ):
          # Sentences which are still active are collected,
          # so the decoder will not run on completed sentences.
  
          n_prev_active_inst = len(inst_idx_to_position_map)
          active_inst_idx = [
              inst_idx_to_position_map[k] for k in active_inst_idx_list
          ]
          active_inst_idx = paddle.to_tensor(
              active_inst_idx, dtype="int64"
          )
          active_src_enc = collect_active_part(
              src_enc.transpose([1, 0, 2]),
              active_inst_idx,
              n_prev_active_inst,
              n_bm,
          ).transpose([1, 0, 2])
          active_inst_idx_to_position_map = (
              get_inst_idx_to_tensor_position_map(active_inst_idx_list)
          )
          return active_src_enc, active_inst_idx_to_position_map
  
      def beam_decode_step(
          inst_dec_beams,
          len_dec_seq,
          enc_output,
          inst_idx_to_position_map,
          n_bm,
      ):
          """Decode and update beam status, and then return active beam idx"""
  
          def prepare_beam_dec_seq(inst_dec_beams, len_dec_seq):
              dec_partial_seq = [
                  b.get_current_state()
                  for b in inst_dec_beams
                  if not b.done
              ]
              dec_partial_seq = paddle.stack(dec_partial_seq)
              dec_partial_seq = dec_partial_seq.reshape(
                  [-1, len_dec_seq]
              )
              return dec_partial_seq
  
          def predict_word(dec_seq, enc_output, n_active_inst, n_bm):
              dec_seq = self.embedding(dec_seq)
              dec_seq = self.positional_encoding(dec_seq)
              tgt_mask = self.generate_square_subsequent_mask(
                  paddle.shape(dec_seq)[1]
              )
              tgt = dec_seq
              for decoder_layer in self.decoder:
                  tgt = decoder_layer(
                      tgt, enc_output, self_mask=tgt_mask
                  )
              dec_output = tgt
              dec_output = dec_output[
                  :, -1, :
              ]  # Pick the last step: (bh * bm) * d_h
              word_prob = F.softmax(
                  self.tgt_word_prj(dec_output), axis=1
              )
              word_prob = paddle.reshape(
                  word_prob, [n_active_inst, n_bm, -1]
              )
              return word_prob
  
          def collect_active_inst_idx_list(
              inst_beams, word_prob, inst_idx_to_position_map
          ):
              active_inst_idx_list = []
              for (
                  inst_idx,
                  inst_position,
              ) in inst_idx_to_position_map.items():
                  is_inst_complete = inst_beams[inst_idx].advance(
                      word_prob[inst_position]
                  )
                  if not is_inst_complete:
                      active_inst_idx_list += [inst_idx]
  
              return active_inst_idx_list
  
          n_active_inst = len(inst_idx_to_position_map)
          dec_seq = prepare_beam_dec_seq(inst_dec_beams, len_dec_seq)
          word_prob = predict_word(
              dec_seq, enc_output, n_active_inst, n_bm
          )
          # Update the beam with predicted word prob information and collect incomplete instances
          active_inst_idx_list = collect_active_inst_idx_list(
              inst_dec_beams, word_prob, inst_idx_to_position_map
          )
          return active_inst_idx_list
  
      def collect_hypothesis_and_scores(inst_dec_beams, n_best):
          all_hyp, all_scores = [], []
          for inst_idx in range(len(inst_dec_beams)):
              scores, tail_idxs = inst_dec_beams[inst_idx].sort_scores()
              all_scores += [scores[:n_best]]
              hyps = [
                  inst_dec_beams[inst_idx].get_hypothesis(i)
                  for i in tail_idxs[:n_best]
              ]
              all_hyp += [hyps]
          return all_hyp, all_scores
  
      with paddle.no_grad():
          # -- Encode
          if self.encoder is not None:
              src = self.positional_encoding(images)
              src_enc = self.encoder(src)
          else:
              src_enc = images
  
          n_bm = self.beam_size
          src_shape = paddle.shape(src_enc)
          inst_dec_beams = [Beam(n_bm) for _ in range(1)]
          active_inst_idx_list = list(range(1))
          # Repeat data for beam search
          src_enc = paddle.tile(src_enc, [1, n_bm, 1])
          inst_idx_to_position_map = (
              get_inst_idx_to_tensor_position_map(active_inst_idx_list)
          )
          # Decode
          for len_dec_seq in range(1, paddle.to_tensor(self.max_len)):
              src_enc_copy = src_enc.clone()
              active_inst_idx_list = beam_decode_step(
                  inst_dec_beams,
                  len_dec_seq,
                  src_enc_copy,
                  inst_idx_to_position_map,
                  n_bm,
              )
              if not active_inst_idx_list:
                  break  # all instances have finished their path to <EOS>
              src_enc, inst_idx_to_position_map = collate_active_info(
                  src_enc_copy,
                  inst_idx_to_position_map,
                  active_inst_idx_list,
              )
      batch_hyp, batch_scores = collect_hypothesis_and_scores(
          inst_dec_beams, 1
      )
      result_hyp = []
      hyp_scores = []
      for bs_hyp, score in zip(batch_hyp, batch_scores):
          l = len(bs_hyp[0])
          bs_hyp_pad = bs_hyp[0] + [3] * (25 - l)
          result_hyp.append(bs_hyp_pad)
          score = float(score) / l
          hyp_score = [score for _ in range(25)]
          hyp_scores.append(hyp_score)
      return [
          paddle.to_tensor(np.array(result_hyp), dtype=paddle.int64),
          paddle.to_tensor(hyp_scores),
      ]
  
imports: import math
  import paddle
  from paddle import nn
  import paddle.nn.functional
  from paddle.nn import LayerList
  from paddle.nn import Dropout, Linear, LayerNorm
  import numpy
  from ppocr.modeling.backbones.rec_svtrnet import Mlp, zeros_, ones_
  from paddle.nn.initializer import XavierNormal
label_desc: Use image node_id 6 for calling the Transformer obj's underlying member method code with example usage: obj.forward_beam(images) and returns: list, unknown, beamed_tensor, active_src_enc,  active_inst_idx_to_position_map, active_inst_idx_list, all_hyp,  all_scores, dec_partial_seq, word_prob, active_inst_idx_list