class_docstr: Translator uses one or several models to translate input.
  The translator holds a reference to vocabularies to convert between word ids and text tokens for input and
  translation strings.
  
  :param device: Pytorch device to bind modules to.
  :param ensemble_mode: Ensemble mode: linear or log_linear combination.
  :param scorer: Hypothesis/Candidate scoring instance
  :param beam_search_stop: The stopping criterion.
  :param models: List of models.
  :param source_vocabs: Source vocabularies.
  :param target_vocabs: Target vocabularies.
  :param nbest_size: Size of nbest list of translations.
  :param restrict_lexicon: Lexicon to use for target vocabulary selection. Can be a dict of named lexicons. When
         it is a single lexicon it will be applied to all inputs. If is a Dict the lexicon with the given name will
         be used or no lexicon be used if the name is None.
  :param strip_unknown_words: If True, removes any <unk> symbols from outputs.
  :param sample: If True, sample from softmax multinomial instead of using topk.
  :param output_scores: Whether the scores will be needed as outputs. If True, scores will be normalized, negative
         log probabilities. If False, scores will be negative, raw logit activations if decoding with beam size 1
         and a single model.
  :param constant_length_ratio: If > 0, will override models' prediction of the length ratio (if any).
  :param max_output_length_num_stds: Number of standard deviations to add as a safety margin when computing the
         maximum output length. If -1, returned maximum output lengths will always be 2 * input_length.
  :param max_input_length: Maximum input length this Translator should allow. If None, value will be taken from the
         model(s). Inputs larger than this value will be chunked and translated in sequence.
         If model(s) do not support given input length it will fall back to what the model(s) support.
  :param max_output_length: Maximum output length this Translator is allowed to decode. If None, value will be taken
         from the model(s). Decodings that do not finish within this limit, will be force-stopped.
         If model(s) do not support given input length it will fall back to what the model(s) support.
  :param skip_nvs: Manually turn off Neural Vocabulary Selection (NVS) to do a softmax over the full target vocabulary.
  :param nvs_thresh: The probability threshold for a word to be added to the set of target words. Default: 0.5.
class_docstr_tok: ["Translator","uses","one","or","several","models","to","translate","input",".","The","translator","holds","a","reference","to","vocabularies","to","convert","between","word","ids","and","text","tokens","for","input","and","translation","strings",".",":","param","device",":","Pytorch","device","to","bind","modules","to",".",":","param","ensemble_mode",":","Ensemble","mode",":","linear","or","log_linear","combination",".",":","param","scorer",":","Hypothesis\/Candidate","scoring","instance",":","param","beam_search_stop",":","The","stopping","criterion",".",":","param","models",":","List","of","models",".",":","param","source_vocabs",":","Source","vocabularies",".",":","param","target_vocabs",":","Target","vocabularies",".",":","param","nbest_size",":","Size","of","nbest","list","of","translations",".",":","param","restrict_lexicon",":","Lexicon","to","use","for","target","vocabulary","selection",".","Can","be","a","dict","of","named","lexicons",".","When","it","is","a","single","lexicon","it","will","be","applied","to","all","inputs",".","If","is","a","Dict","the","lexicon","with","the","given","name","will","be","used","or","no","lexicon","be","used","if","the","name","is","None",".",":","param","strip_unknown_words",":","If","True",",","removes","any","<","unk",">","symbols","from","outputs",".",":","param","sample",":","If","True",",","sample","from","softmax","multinomial","instead","of","using","topk",".",":","param","output_scores",":","Whether","the","scores","will","be","needed","as","outputs",".","If","True",",","scores","will","be","normalized",",","negative","log","probabilities",".","If","False",",","scores","will","be","negative",",","raw","logit","activations","if","decoding","with","beam","size","1","and","a","single","model",".",":","param","constant_length_ratio",":","If",">","0",",","will","override","models","'","prediction","of","the","length","ratio","(","if","any",")",".",":","param","max_output_length_num_stds",":","Number","of","standard","deviations","to","add","as","a","safety","margin","when","computing","the","maximum","output","length",".","If","-1",",","returned","maximum","output","lengths","will","always","be","2","*","input_length",".",":","param","max_input_length",":","Maximum","input","length","this","Translator","should","allow",".","If","None",",","value","will","be","taken","from","the","model","(","s",")",".","Inputs","larger","than","this","value","will","be","chunked","and","translated","in","sequence",".","If","model","(","s",")","do","not","support","given","input","length","it","will","fall","back","to","what","the","model","(","s",")","support",".",":","param","max_output_length",":","Maximum","output","length","this","Translator","is","allowed","to","decode",".","If","None",",","value","will","be","taken","from","the","model","(","s",")",".","Decodings","that","do","not","finish","within","this","limit",",","will","be","force-stopped",".","If","model","(","s",")","do","not","support","given","input","length","it","will","fall","back","to","what","the","model","(","s",")","support",".",":","param","skip_nvs",":","Manually","turn","off","Neural","Vocabulary","Selection","(","NVS",")","to","do","a","softmax","over","the","full","target","vocabulary",".",":","param","nvs_thresh",":","The","probability","threshold","for","a","word","to","be","added","to","the","set","of","target","words",".","Default",":","0.5","."]
docstr: Assembles the numerical data for the batch. This comprises a tensor for the source sentences,
  the bucket key (padded source length), a tensor of maximum output lengths for each sentence in the batch.
  
  :param trans_inputs: List of TranslatorInputs.
  :return tensor of source ids (shape=(batch_size, bucket_key, num_factors)),
          tensor of valid source lengths, lexicon for vocabulary restriction, tensor of maximum output lengths,
          optional target prefix, and optional target prefix factors.
docstr_tok: ["Assembles","the","numerical","data","for","the","batch",".","This","comprises","a","tensor","for","the","source","sentences",",","the","bucket","key","(","padded","source","length",")",",","a","tensor","of","maximum","output","lengths","for","each","sentence","in","the","batch",".",":","param","trans_inputs",":","List","of","TranslatorInputs",".",":","return","tensor","of","source","ids","(","shape=","(","batch_size",",","bucket_key",",","num_factors",")",")",",","tensor","of","valid","source","lengths",",","lexicon","for","vocabulary","restriction",",","tensor","of","maximum","output","lengths",",","optional","target","prefix",",","and","optional","target","prefix","factors","."]
code: def _get_inference_input(
      self, trans_inputs: List[TranslatorInput]
  ) -> Tuple[
      pt.Tensor,
      pt.Tensor,
      Optional[lexicon.RestrictLexicon],
      pt.Tensor,
      Optional[pt.Tensor],
      Optional[pt.Tensor],
  ]:
      """
      Assembles the numerical data for the batch. This comprises a tensor for the source sentences,
      the bucket key (padded source length), a tensor of maximum output lengths for each sentence in the batch.
  
      :param trans_inputs: List of TranslatorInputs.
      :return tensor of source ids (shape=(batch_size, bucket_key, num_factors)),
              tensor of valid source lengths, lexicon for vocabulary restriction, tensor of maximum output lengths,
              optional target prefix, and optional target prefix factors.
      """
      batch_size = len(trans_inputs)
  
      max_target_prefix_length = max(
          inp.num_target_prefix_tokens for inp in trans_inputs
      )
      max_target_prefix_factors_length = max(
          inp.num_target_prefix_factors for inp in trans_inputs
      )
      max_length = max(len(inp) for inp in trans_inputs)
      # assembling source ids on cpu array (faster) and copy to Translator.device (potentially GPU) in one go below.
      source_np = np.zeros(
          (batch_size, max_length, self.num_source_factors),
          dtype="int32",
      )
      # total token length and prepended token length
      length_np = np.zeros((batch_size, 2), dtype="int32")
  
      target_prefix_np = (
          np.zeros(
              (batch_size, max_target_prefix_length), dtype="int32"
          )
          if max_target_prefix_length > 0
          else None
      )
      target_prefix_factors_np = (
          np.zeros(
              (
                  batch_size,
                  max_target_prefix_factors_length,
                  self.num_target_factors - 1,
              ),
              dtype="int32",
          )
          if self.num_target_factors > 1
          and max_target_prefix_factors_length > 0
          else None
      )
      restrict_lexicon = None  # type: Optional[lexicon.RestrictLexicon]
  
      max_output_lengths = []  # type: List[int]
      for j, trans_input in enumerate(trans_inputs):
          num_tokens = len(trans_input)  # includes eos
          primary_source_ids = tokens2ids(
              itertools.chain(
                  trans_input.get_source_prefix_tokens(),
                  trans_input.tokens,
              ),
              self.source_vocabs[0],
          )
          source_np[j, :num_tokens, 0] = primary_source_ids
          length_np[j, 0] = num_tokens
          length_np[j, 1] = get_prepended_token_length(
              primary_source_ids, self.eop_id
          )
          # the effective source length excludes prepended tokens
          max_output_lengths.append(
              self._get_max_output_length(
                  length_np[j, 0] - length_np[j, 1]
              )
          )
          if (
              target_prefix_np is not None
              and trans_input.num_target_prefix_tokens > 0
          ):
              target_prefix_np[
                  j, : trans_input.num_target_prefix_tokens
              ] = tokens2ids(
                  trans_input.get_target_prefix_tokens(),
                  self.vocab_targets[0],
              )
          if (
              target_prefix_factors_np is not None
              and self.num_target_factors > 1
              and trans_input.num_target_prefix_factors > 0
          ):
              for i in range(1, self.num_target_factors):
                  target_prefix_factors_np[
                      j, : trans_input.num_target_prefix_factors, i - 1
                  ] = tokens2ids(
                      trans_input.get_target_prefix_factors()[i - 1],
                      self.vocab_targets[i],
                  )
          factors = (
              trans_input.factors
              if trans_input.factors is not None
              else []
          )
          num_factors = 1 + len(factors)
          if num_factors != self.num_source_factors:
              logger.warning(
                  "Input %d factors, but model(s) expect %d",
                  num_factors,
                  self.num_source_factors,
              )
          if (
              not trans_input.source_prefix_factors
          ):  # no source prefix during inference
              for i, factor in enumerate(
                  factors[: self.num_source_factors - 1], start=1
              ):
                  # fill in as many factors as there are tokens
                  source_np[j, :num_tokens, i] = tokens2ids(
                      factor, self.source_vocabs[i]
                  )[:num_tokens]
          else:
              for i, zip_of_factor_and_prefix_factor in enumerate(
                  zip(
                      factors[: self.num_source_factors - 1],
                      trans_input.source_prefix_factors[
                          : self.num_source_factors - 1
                      ],
                  ),
                  start=1,
              ):
                  (
                      factor,
                      source_prefix_factor,
                  ) = zip_of_factor_and_prefix_factor
                  source_np[j, :num_tokens, i] = tokens2ids(
                      itertools.chain(source_prefix_factor, factor),
                      self.source_vocabs[i],
                  )[:num_tokens]
  
          # Check if vocabulary selection/restriction is enabled:
          # - First, see if the translator input provides a lexicon (used for multiple lexicons)
          # - If not, see if the translator itself provides a lexicon (used for single lexicon)
          # - The same lexicon must be used for all inputs in the batch.
          if trans_input.restrict_lexicon is not None:
              if (
                  restrict_lexicon is not None
                  and restrict_lexicon
                  is not trans_input.restrict_lexicon
              ):
                  logger.warning(
                      "Sentence %s: different restrict_lexicon specified, will overrule previous. "
                      "All inputs in batch must use same lexicon."
                      % trans_input.sentence_id
                  )
              restrict_lexicon = trans_input.restrict_lexicon
          elif self.restrict_lexicon is not None:
              if isinstance(self.restrict_lexicon, dict):
                  restrict_lexicon = None
              else:
                  restrict_lexicon = self.restrict_lexicon
  
      if restrict_lexicon is None and isinstance(
          self.restrict_lexicon, dict
      ):
          logger.info(
              "No restrict_lexicon specified for input when using multiple lexicons, "
              "will default to not using a restrict lexicon."
          )
  
      source = pt.tensor(source_np, device=self.device, dtype=pt.int32)
      source_length = pt.tensor(
          length_np, device=self.device, dtype=pt.int32
      )  # shape: (batch_size, 2)
      max_out_lengths = pt.tensor(
          max_output_lengths, device=self.device, dtype=pt.int32
      )
      target_prefix = (
          pt.tensor(
              target_prefix_np, device=self.device, dtype=pt.int32
          )
          if target_prefix_np is not None
          else None
      )
      target_prefix_factors = (
          pt.tensor(
              target_prefix_factors_np,
              device=self.device,
              dtype=pt.int32,
          )
          if target_prefix_factors_np is not None
          else None
      )
  
      # During inference, if C.TARGET_FACTOR_SHIFT is True, predicted target_factors are left-shifted
      # (see _unshift_target_factors function()) so that they re-align with the words.
      # With that, target_prefix_factors need to be also right-shifted here if C.TARGET_FACTOR_SHIFT is True so
      # that when they are shifted back later they would align with words.
      target_prefix_factors = (
          utils.shift_prefix_factors(target_prefix_factors)
          if target_prefix_factors is not None and C.TARGET_FACTOR_SHIFT
          else target_prefix_factors
      )
  
      return (
          source,
          source_length,
          restrict_lexicon,
          max_out_lengths,
          target_prefix,
          target_prefix_factors,
      )
  
imports: import copy
  import itertools
  import json
  import logging
  from dataclasses import dataclass
  from functools import partial
  from typing import Any, Callable, Dict, Generator, List, Optional, Set, Tuple, Union
  import numpy
  import torch
  from .None import constants
  from .None import lexicon
  from .None import utils
  from .None import vocab
  from .beam_search import CandidateScorer, get_search_algorithm, GreedySearch, SearchResult
  from .data_io import tokens2ids, get_prepended_token_length
  from .model import SockeyeModel
label_desc: Use image node_id 9 for calling the Translator obj's underlying member method code with example usage: obj._get_inference_input(trans_inputs) and returns: source,  source_length,  restrict_lexicon,  max_out_lengths,  target_prefix,  target_prefix_factors