code: def create_mask(tensor, pattern="m4n2_1d", density=0.5):
      # Reshape tensor and mask.
      shape = tensor.shape
      ttype = tensor.type()
      t = tensor.float().contiguous()
  
      # 1d-tensor
      if len(shape) == 1:
          t = t.view(1, shape[0])
          func = getattr(sys.modules[__name__], pattern, None)
          mask = func(t, density)
          return mask.view(shape).type(ttype)
      # 2d-tensor (K, C)
      elif len(shape) == 2:
          # linear
          t = t.view(shape[0], shape[1])
          func = getattr(sys.modules[__name__], pattern, None)
          mask = func(t, density)
          return mask.view(shape).type(ttype)
      # 3d-tensor (K, C, R)
      elif len(shape) == 3:
          # 1d convs
          t = (
              t.permute(0, 2, 1)
              .contiguous()
              .view(shape[0] * shape[2], shape[1])
          )
          func = getattr(sys.modules[__name__], pattern, None)
          mask = func(t, density)
          mask = (
              mask.view(shape[0], shape[2], shape[1])
              .permute(0, 2, 1)
              .contiguous()
          )
          return mask.view(shape).type(ttype)
      # 4d-tensor (K, C, R, S)
      elif len(shape) == 4:
          """
          # transformers (bmm)
          t = t.view(shape[0]*shape[1]*shape[2], shape[3])
          func = getattr(sys.modules[__name__], pattern, None)
          mask = func(t, density)
          return mask.view(shape).type(ttype)
          """
          # 2d convs
          t = (
              t.permute(2, 3, 0, 1)
              .contiguous()
              .view(shape[2] * shape[3] * shape[0], shape[1])
          )
          func = getattr(sys.modules[__name__], pattern, None)
          mask = func(t, density)
          mask = (
              mask.view(shape[2], shape[3], shape[0], shape[1])
              .permute(2, 3, 0, 1)
              .contiguous()
          )
          return mask.view(shape).type(ttype)
  
file_path: apex/apex/contrib/sparsity/sparse_masklib.py
imports: import sys
  import torch
  import numpy
  import collections
  from itertools import permutations
label_desc: Use image node_id 11 for calling a global function with example usage: create_mask(tensor, pattern, density) and returns: mask, mask, mask, mask