docstr: :param logits: Model logits for primary output words. Shape: (batch, length, vocab_size).
  :param labels: Gold targets. Shape: (batch, length).
  :param length_ratio: Length Ratios. Shape: (batch,).
  :param source_length: Source lengths. Shape: (batch,).
  :param target_length: Target lengths. Shape: (batch,).
  :param factor_logits_and_labels: List of target factor logits and corresponding labels.
         Shape: (batch, length, factor_vocab_size).
  :return: Sequence scores. Shape: (batch,).
docstr_tok: [":","param","logits",":","Model","logits","for","primary","output","words",".","Shape",":","(","batch",",","length",",","vocab_size",")",".",":","param","labels",":","Gold","targets",".","Shape",":","(","batch",",","length",")",".",":","param","length_ratio",":","Length","Ratios",".","Shape",":","(","batch",",",")",".",":","param","source_length",":","Source","lengths",".","Shape",":","(","batch",",",")",".",":","param","target_length",":","Target","lengths",".","Shape",":","(","batch",",",")",".",":","param","factor_logits_and_labels",":","List","of","target","factor","logits","and","corresponding","labels",".","Shape",":","(","batch",",","length",",","factor_vocab_size",")",".",":","return",":","Sequence","scores",".","Shape",":","(","batch",",",")","."]
code: def forward(
      self,
      logits,
      labels,
      length_ratio,
      source_length,
      target_length,
      factor_logits_and_labels: Optional[
          List[Tuple[pt.Tensor, pt.Tensor]]
      ] = None,
  ):
      """
      :param logits: Model logits for primary output words. Shape: (batch, length, vocab_size).
      :param labels: Gold targets. Shape: (batch, length).
      :param length_ratio: Length Ratios. Shape: (batch,).
      :param source_length: Source lengths. Shape: (batch,).
      :param target_length: Target lengths. Shape: (batch,).
      :param factor_logits_and_labels: List of target factor logits and corresponding labels.
             Shape: (batch, length, factor_vocab_size).
      :return: Sequence scores. Shape: (batch,).
      """
      logprobs = logits.log_softmax(dim=-1)
  
      # Select the label log probability
      # logprobs and scores: (batch_size, target_seq_len)
      token_scores = logprobs.gather(
          dim=-1, index=labels.unsqueeze(-1)
      ).squeeze(-1)
      if self.score_type == C.SCORING_TYPE_NEGLOGPROB:
          token_scores = -token_scores
  
      # Mask pad positions, sum, then apply length penalty. Shape: (batch_size, 1)
      scores = token_scores.masked_fill_(labels == C.PAD_ID, 0.0).sum(
          dim=-1, keepdims=True
      )
      if (
          self.constant_length_ratio is not None
          and self.constant_length_ratio > 0.0
      ):
          predicted_output_length = (
              source_length * self.constant_length_ratio
          )
      else:
          predicted_output_length = source_length * length_ratio
      scores = self.scorer(
          scores, target_length, predicted_output_length
      )
  
      if factor_logits_and_labels is not None:
          factor_scores = []  # type: List[pt.Tensor]
          for factor_logit, factor_label in factor_logits_and_labels:
              factor_logprobs = factor_logit.log_softmax(dim=-1)
              factor_token_scores = factor_logprobs.gather(
                  dim=-1, index=factor_label.unsqueeze(-1)
              ).squeeze(-1)
              if self.score_type == C.SCORING_TYPE_NEGLOGPROB:
                  factor_token_scores = -factor_token_scores
              fs = factor_token_scores.masked_fill_(factor_label == C.PAD_ID, 0.0).sum(dim=-1, keepdims=True)  # type: ignore
              # Note: factor_scores are not normalized by length
              factor_scores.append(fs)
          scores = pt.cat([scores] + factor_scores, dim=1)
  
      return scores
  
imports: import logging
  import math
  import time
  from typing import Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  from .None import constants
  from .None import data_io
  from .None import inference
  from .None import vocab
  from .beam_search import CandidateScorer
  from .model import SockeyeModel
  from .output_handler import OutputHandler
label_desc: Use image node_id 2 for calling the BatchScorer obj's underlying member method code with example usage: obj.forward(logits, labels, length_ratio, source_length, target_length, factor_logits_and_labels) and returns: scores