code: def beam_search_infilling(
      model,
      q_ids,
      q_sids,
      sos_id,
      eos_id,
      attn_id,
      max_encode_len=640,
      max_decode_len=100,
      beam_width=5,
      tgt_type_id=3,
      length_penalty=1.0,
  ):
      model.eval()
      _, __, info = model(q_ids, q_sids)
      d_batch, d_seqlen = q_ids.shape
  
      state = BeamSearchState(
          log_probs=L.zeros([d_batch, beam_width], "float32"),
          lengths=L.zeros([d_batch, beam_width], "int64"),
          finished=L.zeros([d_batch, beam_width], "int64"),
      )
      outputs = []
  
      def reorder_(t, parent_id):
          """reorder cache according to parent beam id"""
          gather_idx = L.where(parent_id != -1)[
              :, 0
          ] * beam_width + L.reshape(parent_id, [-1])
          t = L.gather(t, gather_idx)
          return t
  
      def tile_(t, times):
          _shapes = list(t.shape[1:])
          ret = L.reshape(
              L.expand(
                  L.unsqueeze(t, [1]),
                  [
                      1,
                      times,
                  ]
                  + [
                      1,
                  ]
                  * len(_shapes),
              ),
              [
                  -1,
              ]
              + _shapes,
          )
          return ret
  
      cached_k, cached_v = info["caches"]
      cached_k = [tile_(k, beam_width) for k in cached_k]
      cached_v = [tile_(v, beam_width) for v in cached_v]
      past_cache = (cached_k, cached_v)
  
      q_ids = tile_(q_ids, beam_width)
      seqlen = L.reduce_sum(
          L.cast(q_ids != 0, "int64"), 1, keep_dim=True
      )
  
      cls_ids = L.ones([d_batch * beam_width], dtype="int64") * sos_id
      attn_ids = (
          L.ones([d_batch * beam_width], dtype="int64") * attn_id
      )  # SOS
      ids = L.stack([cls_ids, attn_ids], -1)
      for step in range(max_decode_len):
          bias = gen_bias(q_ids, ids, step)
          pos_ids = D.to_variable(
              np.tile(
                  np.array([[step, step + 1]], dtype=np.int64),
                  [d_batch * beam_width, 1],
              )
          )
          pos_ids += seqlen
  
          _, logits, info = model(
              ids,
              L.ones_like(ids) * tgt_type_id,
              pos_ids=pos_ids,
              attn_bias=bias,
              past_cache=past_cache,
          )
  
          output, state = beam_search_step(
              state,
              logits[:, 1],
              eos_id=eos_id,
              beam_width=beam_width,
              is_first_step=(step == 0),
              length_penalty=length_penalty,
          )
          outputs.append(output)
  
          past_cached_k, past_cached_v = past_cache
          cached_k, cached_v = info["caches"]
          cached_k = [
              reorder_(
                  L.concat([pk, k[:, :1, :]], 1), output.beam_parent_ids
              )
              for pk, k in zip(past_cached_k, cached_k)
          ]  # concat cached
          cached_v = [
              reorder_(
                  L.concat([pv, v[:, :1, :]], 1), output.beam_parent_ids
              )
              for pv, v in zip(past_cached_v, cached_v)
          ]
          past_cache = (cached_k, cached_v)
  
          pred_ids_flatten = L.reshape(
              output.predicted_ids, [d_batch * beam_width]
          )
          ids = L.stack([pred_ids_flatten, attn_ids], 1)
  
          if state.finished.numpy().all():
              break
  
      final_ids = L.stack([o.predicted_ids for o in outputs], 0)
      final_parent_ids = L.stack(
          [o.beam_parent_ids for o in outputs], 0
      )
      final_ids = L.gather_tree(final_ids, final_parent_ids)  # [:, :,
      # 0]  #pick best beam
      final_ids = L.transpose(
          L.reshape(final_ids, [-1, d_batch * 1, beam_width]), [1, 2, 0]
      )
      return final_ids
  
file_path: PaddleHub/modules/text/text_generation/reading_pictures_writing_poems_for_midautumn/MidAutumnPoetry/model/decode.py
imports: import re
  import numpy
  from collections import namedtuple
  import paddle.fluid
  import paddle.fluid.layers
  import paddle.fluid.dygraph
label_desc: Use image node_id 7 for calling a global function with example usage: beam_search_infilling(model, q_ids, q_sids, sos_id, eos_id, attn_id, max_encode_len, max_decode_len, beam_width, tgt_type_id, length_penalty) and returns: final_ids, t, ret