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","."]
code: def num_source_factors(self) -> int:
      return self.models[0].num_source_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 4 for calling the Translator obj's underlying member method code with example usage: obj.num_source_factors() and returns: self