code: def do_generate(
      use_copy,
      enc_hidden_size,
      dec_hidden_size,
      encoder,
      tree_decoder,
      enc_w_list,
      word_manager,
      form_manager,
      device,
      max_dec_seq_length,
      max_dec_tree_depth,
  ):
      # initialize the rnn state to all zeros
      prev_c = torch.zeros((1, dec_hidden_size), requires_grad=False)
      prev_h = torch.zeros((1, dec_hidden_size), requires_grad=False)
  
      to_cuda(prev_c, device)
      to_cuda(prev_h, device)
  
      # reversed order
      enc_w_list = list(np.array(enc_w_list)[::-1])
      enc_w_list.append(
          word_manager.get_symbol_idx(word_manager.end_token)
      )
      enc_w_list.insert(
          0, word_manager.get_symbol_idx(word_manager.start_token)
      )
      enc_w_list = torch.tensor(enc_w_list, dtype=torch.long).unsqueeze(
          0
      )
  
      # print(form_manager.get_idx_symbol_for_list(enc_w_list[0]))
  
      enc_outputs = torch.zeros(
          (1, enc_w_list.size(1), enc_hidden_size), requires_grad=False
      )
      to_cuda(enc_outputs)
  
      output_, (hn_, cn_) = encoder(enc_w_list)
      # print(output_.size())
  
      enc_outputs = output_
      prev_c = cn_
      prev_h = hn_
  
      # decode
      queue_decode = []
      queue_decode.append(
          {
              "s": (prev_c, prev_h),
              "parent": 0,
              "child_index": 1,
              "t": Tree(),
          }
      )
      head = 1
      while head <= len(queue_decode) and head <= max_dec_tree_depth:
          s = queue_decode[head - 1]["s"]
          parent_h = s[1]
          t = queue_decode[head - 1]["t"]
  
          sibling_state = torch.zeros(
              (1, dec_hidden_size),
              dtype=torch.float,
              requires_grad=False,
          )
          sibling_state = to_cuda(sibling_state)
  
          flag_sibling = False
          for q_index in range(len(queue_decode)):
              if (
                  (head <= len(queue_decode))
                  and (q_index < head - 1)
                  and (
                      queue_decode[q_index]["parent"]
                      == queue_decode[head - 1]["parent"]
                  )
                  and (
                      queue_decode[q_index]["child_index"]
                      < queue_decode[head - 1]["child_index"]
                  )
              ):
                  flag_sibling = True
                  sibling_index = q_index
          if flag_sibling:
              sibling_state = queue_decode[sibling_index]["s"][1]
  
          if head == 1:
              prev_word = torch.tensor(
                  [
                      form_manager.get_symbol_idx(
                          form_manager.start_token
                      )
                  ],
                  dtype=torch.long,
              )
          else:
              prev_word = torch.tensor(
                  [form_manager.get_symbol_idx("(")], dtype=torch.long
              )
  
          to_cuda(prev_word, device)
  
          i_child = 1
  
          if use_copy:
              enc_context = None
              input_mask = create_mask(
                  torch.LongTensor(
                      [enc_outputs.size(1)] * enc_outputs.size(0)
                  ),
                  enc_outputs.size(1),
                  device,
              )
              decoder_state = (s[0].unsqueeze(0), s[1].unsqueeze(0))
  
          while True:
              if not use_copy:
                  curr_c, curr_h = tree_decoder.rnn(
                      prev_word, s[0], s[1], parent_h, sibling_state
                  )
                  prediction = tree_decoder.attention(
                      enc_outputs, curr_h, torch.tensor(0)
                  )
                  s = (curr_c, curr_h)
                  _, _prev_word = prediction.max(1)
                  prev_word = _prev_word
              else:
                  # print(form_manager.idx2symbol[np.array(prev_word)[0]])
                  decoder_embedded = tree_decoder.embeddings(prev_word)
                  (
                      pred,
                      decoder_state,
                      _,
                      _,
                      enc_context,
                  ) = tree_decoder.rnn(
                      parent_h,
                      sibling_state,
                      decoder_embedded,
                      decoder_state,
                      enc_outputs.transpose(0, 1),
                      None,
                      None,
                      input_mask=input_mask,
                      encoder_word_idx=enc_w_list,
                      ext_vocab_size=tree_decoder.embeddings.num_embeddings,
                      log_prob=False,
                      prev_enc_context=enc_context,
                      encoder_outputs2=output_.transpose(0, 1),
                  )
  
                  dec_next_state_1 = decoder_state[0].squeeze(0)
                  dec_next_state_2 = decoder_state[1].squeeze(0)
  
                  pred = torch.log(pred + 1e-31)
                  prev_word = pred.argmax(1)
  
              if (
                  int(prev_word[0])
                  == form_manager.get_symbol_idx(form_manager.end_token)
                  or t.num_children >= max_dec_seq_length
              ):
                  break
              elif int(prev_word[0]) == form_manager.get_symbol_idx(
                  form_manager.non_terminal_token
              ):
                  # print("we predicted N");exit()
                  queue_decode.append(
                      {
                          "s": (
                              dec_next_state_1.clone(),
                              dec_next_state_2.clone(),
                          ),
                          "parent": head,
                          "child_index": i_child,
                          "t": Tree(),
                      }
                  )
                  t.add_child(int(prev_word[0]))
              else:
                  t.add_child(int(prev_word[0]))
              i_child = i_child + 1
          head = head + 1
      # refine the root tree (TODO, what is this doing?)
      for i in range(len(queue_decode) - 1, 0, -1):
          cur = queue_decode[i]
          queue_decode[cur["parent"] - 1]["t"].children[
              cur["child_index"] - 1
          ] = cur["t"]
      return queue_decode[0]["t"].to_list(form_manager)
  
imports: import random
  import time
  import warnings
  import numpy
  import torch
  import torch.nn.functional
  import torch.nn.init
  from torch import nn, optim
  from ....modules.prediction.generation.TreeBasedDecoder import StdTreeDecoder, create_mask
  from ....modules.utils.tree_utils import DataLoader, Tree, to_cuda
label_desc: Use image node_id 4 for calling a global function with example usage: do_generate(use_copy, enc_hidden_size, dec_hidden_size, encoder, tree_decoder, enc_w_list, word_manager, form_manager, device, max_dec_seq_length, max_dec_tree_depth) and returns: queue_decode