code: def greedy_search_infilling(
      model,
      q_ids,
      q_sids,
      sos_id,
      eos_id,
      attn_id,
      max_encode_len=640,
      max_decode_len=100,
      tgt_type_id=3,
  ):
      model.eval()
      _, logits, info = model(q_ids, q_sids)
      gen_ids = L.argmax(logits, -1)
      d_batch, d_seqlen = q_ids.shape
      seqlen = L.reduce_sum(
          L.cast(q_ids != 0, "int64"), 1, keep_dim=True
      )
      has_stopped = np.zeros([d_batch], dtype=np.bool)
      gen_seq_len = np.zeros([d_batch], dtype=np.int64)
      output_ids = []
  
      past_cache = info["caches"]
  
      cls_ids = L.ones([d_batch], dtype="int64") * sos_id
      attn_ids = L.ones([d_batch], dtype="int64") * attn_id
      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, 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,
          )
          gen_ids = L.argmax(logits, -1)
  
          past_cached_k, past_cached_v = past_cache
          cached_k, cached_v = info["caches"]
          cached_k = [
              L.concat([pk, k[:, :1, :]], 1)
              for pk, k in zip(past_cached_k, cached_k)
          ]  # concat cached
          cached_v = [
              L.concat([pv, v[:, :1, :]], 1)
              for pv, v in zip(past_cached_v, cached_v)
          ]
          past_cache = (cached_k, cached_v)
  
          gen_ids = gen_ids[:, 1]
          ids = L.stack([gen_ids, attn_ids], 1)
  
          gen_ids = gen_ids.numpy()
          has_stopped |= (gen_ids == eos_id).astype(np.bool)
          gen_seq_len += 1 - has_stopped.astype(np.int64)
          output_ids.append(gen_ids.tolist())
          if has_stopped.all():
              break
      output_ids = np.array(output_ids).transpose([1, 0])
      return output_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 2 for calling a global function with example usage: greedy_search_infilling(model, q_ids, q_sids, sos_id, eos_id, attn_id, max_encode_len, max_decode_len, tgt_type_id) and returns: output_ids