code: def build_sequence_generator(args, task, models):
      use_cuda = torch.cuda.is_available() and not args.cpu
      # Initialize generator
      model_weights = None
      if args.model_weights:
          model_weights = [
              float(w.strip()) for w in args.model_weights.split(",")
          ]
      use_char_source = (
          isinstance(models[0], char_source_model.CharSourceModel)
          or isinstance(
              models[0],
              char_source_transformer_model.CharSourceTransformerModel,
          )
          or isinstance(
              models[0], char_source_hybrid.CharSourceHybridModel
          )
      )
      # Use a different sequence generator in the multisource setting
      if getattr(args, "source_ensembling", False):
          translator_class = (
              multisource_decode.MultiSourceSequenceGenerator
          )
      elif getattr(args, "competing_completed_beam_search", False):
          translator_class = (
              competing_completed.CompetingCompletedSequenceGenerator
          )
      elif isinstance(models[0], LevenshteinTransformerModel):
          translator_class = (
              iterative_refinement_generator.IterativeRefinementGenerator
          )
          return translator_class(
              tgt_dict=task.target_dictionary, models=models
          )
      else:
          translator_class = beam_decode.SequenceGenerator
      translator = translator_class(
          models,
          tgt_dict=task.target_dictionary,
          beam_size=args.beam,
          stop_early=(not args.no_early_stop),
          normalize_scores=(not args.unnormalized),
          len_penalty=args.length_penalty,
          unk_reward=args.unk_reward,
          word_reward=args.word_reward,
          model_weights=model_weights,
          use_char_source=use_char_source,
          diverse_beam_groups=args.diverse_beam_groups,
          diverse_beam_strength=args.diverse_beam_strength,
          diversity_sibling_gamma=args.diversity_sibling_gamma,
          sampling=args.sampling,
          sampling_topk=args.sampling_topk,
          temperature=args.temperature,
      )
      if use_cuda:
          translator.cuda()
      return translator
  
imports: import argparse
  import collections
  import os
  import pickle
  from typing import List, NamedTuple, Optional
  import numpy
  import torch
  from fairseq import data, iterative_refinement_generator, options, progress_bar, tasks, utils
  from fairseq.meters import StopwatchMeter, TimeMeter
  from fairseq.models import FairseqEncoderDecoderModel, FairseqMultiModel
  from fairseq.scoring import bleu
  from pytorch_translate import beam_decode, char_aware_hybrid, char_source_hybrid, char_source_model, char_source_transformer_model, hybrid_transformer_rnn, options, rnn, transformer, utils
  from pytorch_translate.constants import CHECKPOINT_PATHS_DELIMITER
  from pytorch_translate.data import data
  from pytorch_translate.dual_learning.dual_learning_models import DualLearningModel
  from pytorch_translate.research.beam_search import competing_completed
  from pytorch_translate.research.multisource import multisource_data, multisource_decode
  from pytorch_translate.tasks.pytorch_translate_multi_task import PyTorchTranslateMultiTask
  from pytorch_translate.tasks.pytorch_translate_task import PytorchTranslateTask
label_desc: Use image node_id 2 for calling a global function with example usage: build_sequence_generator(args, task, models) and returns: translator, translator_class