code: def get_length_prediction(
      self,
      encoder_out: Dict[str, Tensor],
      encoder_mask: Optional[Tensor],
      src_lengths: Tensor,
      target_lengths: Optional[Tensor] = None,
      beam_size: Optional[int] = None,
  ) -> Dict[str, Tensor]:
      predicted_tgt_length, _ = self.length_prediction_model(
          encoder_out["encoder_out"], encoder_mask
      )
      if beam_size is not None and beam_size != self.length_beam_size:
          self.length_beam_size = beam_size
      if self.clip_target_length:
          beam_vals, beam = predicted_tgt_length.topk(
              self.length_beam_size, dim=1
          )
          len_clips = self.get_clip_length(src_lengths)
          len_clips = len_clips.reshape(-1, 1).repeat(
              1, self.length_beam_size
          )
          acceptable_lens_mask = torch.le(beam, len_clips)
  
          beam = beam * acceptable_lens_mask + torch.logical_not(
              acceptable_lens_mask
          ) * torch.ones_like(
              beam, dtype=beam.dtype, device=beam.device
          )
  
          beam_vals = (
              beam_vals * acceptable_lens_mask
              + torch.logical_not(acceptable_lens_mask)
              * torch.full(
                  beam_vals.size(),
                  float("-inf"),
                  dtype=beam_vals.dtype,
                  device=beam_vals.device,
              )
          )
      else:
          beam_vals, beam = predicted_tgt_length.topk(
              self.length_beam_size, dim=1
          )
  
      # make sure no beams are 0 (integration test)
      beam[beam == 0] += 1
      length_prob = torch.gather(predicted_tgt_length, 1, beam)
  
      if self.use_gold_length:
          assert target_lengths is not None
          beam = target_lengths.reshape(-1, 1)
          self.length_beam_size = 1
          length_prob = torch.ones(
              beam.size(), device=target_lengths.device
          )
  
      tgt_tokens, length_mask = prepare_masked_target_for_lengths(
          beam,
          self.trg_vocab.mask_idx,
          self.trg_vocab.pad_idx,
          self.length_beam_size,
      )
  
      return {
          "target_tokens": tgt_tokens,
          "length_mask": length_mask,
          "length_probabilities": length_prob,
          "beam": beam,
      }
  
imports: from enum import Enum
  from typing import Dict, Optional, Tuple
  import torch
  from pytext.common.constants import TORCH_VERSION
  from pytext.config import ConfigBase
  from pytext.models.module import Module
  from pytext.torchscript.vocab import ScriptVocabulary
  from torch import Tensor
label_desc: Use image node_id 8 for calling the MaskedSequenceGenerator obj's underlying member method code with example usage: obj.get_length_prediction(encoder_out, encoder_mask, src_lengths, target_lengths, beam_size) and returns: dict