code: def compute_valid_2d_patterns(m, n):
      # Early exit if patterns was already created.
      global valid_m4n2_2d_patterns
      if valid_m4n2_2d_patterns is not None:
          return valid_m4n2_2d_patterns
  
      patterns = torch.zeros(m)
      patterns[:n] = 1
      patterns = list(set(permutations(patterns.tolist())))
      patterns = patterns + patterns
      patterns = torch.empty(list(set(permutations(patterns, m))))
  
      valid = (
          ((patterns.sum(dim=1) <= n).sum(dim=1) == m)
          .nonzero()
          .view(-1)
      )
      valid_patterns = torch.empty(valid.shape[0], m, m)
      valid_patterns[:] = patterns[valid[:]]
  
      if m == 4 and n == 2:
          valid_m4n2_2d_patterns = valid_patterns
      return valid_patterns
  
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 8 for calling a global function with example usage: compute_valid_2d_patterns(m, n) and returns: valid_patterns, valid_m4n2_2d_patterns