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:
          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
          )
      elif key_padding_mask is not None:
          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 = prev_key_padding_mask
      return new_key_padding_mask
  
imports: import math
  import uuid
  from typing import Dict, Optional, List
  import torch
  from torch import Tensor, nn
  from torch.nn import Parameter, LayerNorm, Dropout
  from lightseq.training.ops.pytorch import util
  from lightseq.training.ops.pytorch.layer_base import TransformerEmbeddingLayerBase, TransformerEncoderLayerBase, TransformerDecoderLayerBase
  from .quantization import QuantLinear, TensorQuantizer, act_quant_config, weight_quant_config, out_quant_config, emb_quant_config
label_desc: Use image node_id 6 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