code: def generate_m_by_n_mask(weights, m_by_n=(2, 4)):
      """Generate m-by-n sparsity mask.
  
      This is a m_by_n sparsity helper function.
  
      Args:
        weights: a rank 2 tensor.
        m_by_n: a tuple of 2 integers (m, n), indicates m zeros in every n
          consecutive values, m must be smaller than n. Default to (2, 4).
  
      Returns:
        A rank 2 m-by-n sparsity mask.
  
      Raises:
        InvalidArgumentError: if m not smaller than n.
      """
      num_zeros, block_size = tf.constant(m_by_n[0]), tf.constant(
          m_by_n[1]
      )
      tf.debugging.assert_less(
          num_zeros,
          block_size,
          message=f"Argument m_by_n received {m_by_n}, m be must smaller than n.",
      )
      num_non_zeros = block_size - num_zeros
      abs_weights = tf.abs(weights)
  
      # add zero-padding
      pad_after = block_size - abs_weights.shape[-1] % block_size
      abs_weights_pad = tf.pad(
          abs_weights, [[0, 0], [0, pad_after]], "CONSTANT"
      )
  
      num_blocks = tf.size(abs_weights_pad) // block_size
      reshaped_weights_into_blocks = tf.reshape(
          abs_weights_pad, [num_blocks, block_size]
      )
      _, top_k_indices = tf.math.top_k(
          reshaped_weights_into_blocks, k=num_non_zeros, sorted=False
      )
      ind_i, _ = tf.meshgrid(
          tf.range(num_blocks), tf.range(num_non_zeros), indexing="ij"
      )
      ind_ij = tf.stack([ind_i, top_k_indices], axis=-1)
      sparsity_mask_pad = tf.scatter_nd(
          ind_ij,
          tf.ones([num_blocks, num_non_zeros]),
          [num_blocks, block_size],
      )
      reshaped_sparsity_mask_pad = tf.reshape(
          sparsity_mask_pad, tf.shape(abs_weights_pad)
      )
  
      # remove padding from mask
      sparsity_mask = tf.slice(
          reshaped_sparsity_mask_pad, [0, 0], abs_weights.shape
      )
  
      return sparsity_mask
  
file_path: model-optimization/tensorflow_model_optimization/python/core/sparsity/keras/pruning_utils.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import numpy
  import tensorflow
label_desc: Use image node_id 7 for calling a global function with example usage: generate_m_by_n_mask(weights, m_by_n) and returns: sparsity_mask