class_docstr: VGG + Transformer encoder
class_docstr_tok: ["VGG","+","Transformer","encoder"]
docstr: create attention mask according to sequence lengths and transformer
  context
  
  Args:
      - input_lengths: (B, )-shape Int/Long tensor; input_lengths[b] is
        the length of b-th sequence
      - subsampling_factor: int
          * Note that the left_context and right_context is specified in
            the input frame-level while input to transformer may already
            go through subsampling (e.g., the use of striding in vggblock)
            we use subsampling_factor to scale the left/right context
  
  Return:
      - a (T, T) binary tensor or None, where T is max(input_lengths)
          * if self.transformer_context is None, None
          * if left_context is None,
              * attn_mask[t, t + right_context + 1:] = 1
              * others = 0
          * if right_context is None,
              * attn_mask[t, 0:t - left_context] = 1
              * others = 0
          * elsif
              * attn_mask[t, t - left_context: t + right_context + 1] = 0
              * others = 1
docstr_tok: ["create","attention","mask","according","to","sequence","lengths","and","transformer","context","Args",":","-","input_lengths",":","(","B",",",")","-shape","Int\/Long","tensor",";","input_lengths","[","b","]","is","the","length","of","b-th","sequence","-","subsampling_factor",":","int","*","Note","that","the","left_context","and","right_context","is","specified","in","the","input","frame-level","while","input","to","transformer","may","already","go","through","subsampling","(","e.g.",",","the","use","of","striding","in","vggblock",")","we","use","subsampling_factor","to","scale","the","left\/right","context","Return",":","-","a","(","T",",","T",")","binary","tensor","or","None",",","where","T","is","max","(","input_lengths",")","*","if","self.transformer_context","is","None",",","None","*","if","left_context","is","None",",","*","attn_mask","[","t",",","t","+","right_context","+","1",":","]","=","1","*","others","=","0","*","if","right_context","is","None",",","*","attn_mask","[","t",",","0",":","t","-","left_context","]","=","1","*","others","=","0","*","elsif","*","attn_mask","[","t",",","t","-","left_context",":","t","+","right_context","+","1","]","=","0","*","others","=","1"]
code: def lengths_to_attn_mask(self, input_lengths, subsampling_factor=1):
      """
      create attention mask according to sequence lengths and transformer
      context
  
      Args:
          - input_lengths: (B, )-shape Int/Long tensor; input_lengths[b] is
            the length of b-th sequence
          - subsampling_factor: int
              * Note that the left_context and right_context is specified in
                the input frame-level while input to transformer may already
                go through subsampling (e.g., the use of striding in vggblock)
                we use subsampling_factor to scale the left/right context
  
      Return:
          - a (T, T) binary tensor or None, where T is max(input_lengths)
              * if self.transformer_context is None, None
              * if left_context is None,
                  * attn_mask[t, t + right_context + 1:] = 1
                  * others = 0
              * if right_context is None,
                  * attn_mask[t, 0:t - left_context] = 1
                  * others = 0
              * elsif
                  * attn_mask[t, t - left_context: t + right_context + 1] = 0
                  * others = 1
      """
      if self.transformer_context is None:
          return None
  
      maxT = torch.max(input_lengths).item()
      attn_mask = torch.zeros(maxT, maxT)
  
      left_context = self.transformer_context[0]
      right_context = self.transformer_context[1]
      if left_context is not None:
          left_context = math.ceil(
              self.transformer_context[0] / subsampling_factor
          )
      if right_context is not None:
          right_context = math.ceil(
              self.transformer_context[1] / subsampling_factor
          )
  
      for t in range(maxT):
          if left_context is not None:
              st = 0
              en = max(st, t - left_context)
              attn_mask[t, st:en] = 1
          if right_context is not None:
              st = t + right_context + 1
              st = min(st, maxT - 1)
              attn_mask[t, st:] = 1
  
      return attn_mask.to(input_lengths.device)
  
imports: import argparse
  import math
  from collections.abc import Iterable
  import torch
  import torch.nn
  from examples.speech_recognition.data.data_utils import lengths_to_encoder_padding_mask
  from fairseq import utils
  from fairseq.models import FairseqEncoder, FairseqEncoderDecoderModel, FairseqEncoderModel, FairseqIncrementalDecoder, register_model, register_model_architecture
  from fairseq.modules import LinearizedConvolution, TransformerDecoderLayer, TransformerEncoderLayer, VGGBlock
label_desc: Use image node_id 8 for calling the VGGTransformerEncoder obj's underlying member method code with example usage: obj.lengths_to_attn_mask(input_lengths, subsampling_factor) and returns: attn_mask, None