code: def sparsity_2D(tensor):
      """Create a list of sparsity levels for each channel in the tensor 't'
  
      For 4D weight tensors (convolution weights), we flatten each kernel (channel)
      so it becomes a row in a 3D tensor in which each channel is a filter.
      So if the original 4D weights tensor is:
          #OFMs x #IFMs x K x K
      The flattened tensor is:
          #OFMS x #IFMs x K^2
  
      For 2D weight tensors (fully-connected weights), the tensors is shaped as
          #IFMs x #OFMs
      so we don't need to flatten anything.
  
      To measure 2D sparsity, we sum the absolute values of the elements in each row,
      and then count the number of rows having sum(abs(row values)) == 0.
      """
      if tensor.dim() == 4:
          # For 4D weights, 2D structures are channels (filter kernels)
          view_2d = tensor.view(-1, tensor.size(2) * tensor.size(3))
      elif tensor.dim() == 2:
          # For 2D weights, 2D structures are either columns or rows.
          # At the moment, we only support row structures
          view_2d = tensor
      else:
          return 0
  
      num_structs = view_2d.size()[0]
      nonzero_structs = len(torch.nonzero(view_2d.abs().sum(dim=1)))
      return 1 - nonzero_structs / num_structs
  
imports: import argparse
  from collections import OrderedDict
  from copy import deepcopy
  import logging
  import operator
  import random
  import numpy
  import torch
  import torch.nn
  import torch.backends.cudnn
  import yaml
  import inspect
label_desc: Use image node_id 16 for calling a global function with example usage: sparsity_2D(tensor) and returns: unknown, int