class_docstr: Multi-headed attention.
  
  See "Attention Is All You Need" for more details.
class_docstr_tok: ["Multi-headed","attention",".","See","``","Attention","Is","All","You","Need","''","for","more","details","."]
code: def _append_prev_key_padding_mask(
      key_padding_mask: Optional[Tensor],
      prev_key_padding_mask: Optional[Tensor],
      batch_size: int,
      src_len: int,
      static_kv: bool,
  ) -> Optional[Tensor]:
      # saved key padding masks have shape (bsz, seq_len)
      if prev_key_padding_mask is not None and static_kv:
          new_key_padding_mask = prev_key_padding_mask
      elif (
          prev_key_padding_mask is not None
          and key_padding_mask is not None
      ):
          new_key_padding_mask = torch.cat(
              [prev_key_padding_mask.float(), key_padding_mask.float()],
              dim=1,
          )
      # During incremental decoding, as the padding token enters and
      # leaves the frame, there will be a time when prev or current
      # is None
      elif prev_key_padding_mask is not None:
          if src_len > prev_key_padding_mask.size(1):
              filler = torch.zeros(
                  (batch_size, src_len - prev_key_padding_mask.size(1)),
                  device=prev_key_padding_mask.device,
              )
              new_key_padding_mask = torch.cat(
                  [prev_key_padding_mask.float(), filler.float()], dim=1
              )
          else:
              new_key_padding_mask = prev_key_padding_mask.float()
      elif key_padding_mask is not None:
          if src_len > key_padding_mask.size(1):
              filler = torch.zeros(
                  (batch_size, src_len - key_padding_mask.size(1)),
                  device=key_padding_mask.device,
              )
              new_key_padding_mask = torch.cat(
                  [filler.float(), key_padding_mask.float()], dim=1
              )
          else:
              new_key_padding_mask = key_padding_mask.float()
      else:
          new_key_padding_mask = prev_key_padding_mask
      return new_key_padding_mask
  
imports: import math
  from typing import Dict, List, Optional, Tuple
  import torch
  import torch.nn.functional
  from torch import Tensor, nn
  from torch.nn import Parameter
  from fairseq import utils
  from fairseq.modules.fairseq_dropout import FairseqDropout
  from fairseq.modules.quant_noise import quant_noise
  from fairseq.models.fairseq_incremental_decoder import FairseqIncrementalDecoder
label_desc: Use image node_id 12 for calling the MultiheadAttention obj's underlying member method code with example usage: obj._append_prev_key_padding_mask(key_padding_mask, prev_key_padding_mask, batch_size, src_len, static_kv) and returns: new_key_padding_mask