code: def masked_softmax(
      scores, src_lengths, src_length_masking: bool = True
  ):
      """Apply source length masking then softmax.
      Input and output have shape bsz x src_len"""
      if src_length_masking:
          bsz, max_src_len = scores.size()
          # compute masks
          src_mask = create_src_lengths_mask(bsz, src_lengths)
          # Fill pad positions with -inf
          scores = scores.masked_fill(src_mask == 0, -np.inf)
  
      # Cast to float and then back again to prevent loss explosion under fp16.
      return F.softmax(scores.float(), dim=-1).type_as(scores)
  
imports: from typing import Dict, Optional, Tuple
  import numpy
  import torch
  import torch.nn.functional
  from pytext.config import ConfigBase
  from pytext.utils.usage import log_class_usage
  from torch import nn, Tensor
  from .base import PyTextIncrementalDecoderComponent
  from .utils import Linear
label_desc: Use image node_id 2 for calling a global function with example usage: masked_softmax(scores, src_lengths, src_length_masking) and returns: F