code: def compute_block_mask_1d(
      shape: Tuple[int, int],
      mask_prob: float,
      mask_length: int,
      mask_prob_adjust: float = 0,
      inverse_mask: bool = False,
      require_same_masks: bool = True,
      expand_adjcent: bool = False,
      mask_dropout: float = 0,
      non_overlapping: bool = False,
  ) -> torch.Tensor:
      B, L = shape
  
      if inverse_mask:
          mask_prob = 1 - mask_prob
  
      if non_overlapping:
          sz = math.ceil(L / mask_length)
  
          inp = torch.zeros((B, 1, sz))
          w = torch.ones((1, 1, mask_length))
  
          mask_inds = torch.multinomial(
              1 - inp.view(B, -1),
              int(
                  sz
                  * (mask_prob + mask_prob_adjust)
                  * (1 + mask_dropout)
              ),
              replacement=False,
          )
          inp.view(B, -1).scatter_(1, mask_inds, 1)
  
          mask = torch.nn.functional.conv_transpose1d(
              inp, w, stride=mask_length
          ).squeeze(1)
          if mask.size(-1) > L:
              mask = mask[..., :L]
  
      else:
          mask = torch.zeros((B, L))
          mask_inds = torch.randint(
              0,
              L,
              size=(
                  B,
                  int(
                      L
                      * ((mask_prob + mask_prob_adjust) / mask_length)
                      * (1 + mask_dropout)
                  ),
              ),
          )
  
          mask.view(B, -1).scatter_(1, mask_inds, 1)
          centers = mask.nonzero(as_tuple=True)
  
          inds = ([], [])
  
          offset = mask_length // 2
          for i in range(mask_length):
              k1 = i - offset
              inds[0].append(centers[0])
              inds[1].append(centers[1] + k1)
  
          i0 = torch.cat(inds[0])
          i1 = torch.cat(inds[1]).clamp_(min=0, max=L - 1)
  
          mask[(i0, i1)] = 1
  
      def get_nbs(b, m, w):
          all_nbs = torch.nn.functional.conv1d(
              m.unsqueeze(1), w, padding="same"
          )
          all_nbs = all_nbs.clamp_max_(1).view(b, -1)
          return all_nbs
  
      if require_same_masks and expand_adjcent:
          w = torch.ones((1, 1, 3))
          w[..., 1] = 0
          all_nbs = get_nbs(B, mask, w)
  
      mask = mask.view(B, -1)
  
      if require_same_masks:
          n_masks = mask.sum(dim=-1)
          final_target_len = int(L * (mask_prob))
          target_len = int(final_target_len * (1 + mask_dropout))
  
          for i in range(len(mask)):
              n = n_masks[i]
              m = mask[i]
              r = 0
              while expand_adjcent and n < target_len:
                  if r == 0:
                      nbs = all_nbs[i]
                  else:
                      nbs = get_nbs(1, m.unsqueeze(0), w).squeeze(0)
  
                  cands = (1 - m + nbs) > 1
                  cand_sz = int(cands.sum().item())
  
                  assert cand_sz > 0, f"{nbs} {cand_sz}"
  
                  to_mask = torch.multinomial(
                      cands.float(),
                      min(cand_sz, int(target_len - n)),
                      replacement=False,
                  )
                  m[to_mask] = 1
                  assert to_mask.numel() > 0
                  n += to_mask.numel()
                  r += 1
  
              if n > final_target_len:
                  to_unmask = torch.multinomial(
                      m, int(n - final_target_len), replacement=False
                  )
                  m[to_unmask] = 0
              elif n < final_target_len:
                  to_mask = torch.multinomial(
                      (1 - m),
                      int(final_target_len - n),
                      replacement=False,
                  )
                  m[to_mask] = 1
  
      if inverse_mask:
          mask = 1 - mask
  
      return mask
  
imports: import contextlib
  import itertools
  import logging
  import re
  import warnings
  from typing import Optional, Tuple
  import math
  import numpy
  import torch
  from fairseq.file_io import PathManager
  from fairseq import utils
  import os
label_desc: Use image node_id 13 for calling a global function with example usage: compute_block_mask_1d(shape, mask_prob, mask_length, mask_prob_adjust, inverse_mask, require_same_masks, expand_adjcent, mask_dropout, non_overlapping) and returns: mask, all_nbs