code: def verify_global_pruning_amount(
      supported_modules, model, expected_sparsity
  ):
      total_params = 0
      unpruned_params = 0
      for name, module in model.named_modules():
          if type(module) in supported_modules:
              total_params += module.weight.numel()
              if hasattr(module, "weight_mask"):
                  unpruned_params += torch.nonzero(
                      module.weight_mask, as_tuple=False
                  ).size(0)
              else:
                  unpruned_params += torch.nonzero(
                      module.weight, as_tuple=False
                  ).size(0)
  
      actual_global_sparsity = 1 - unpruned_params / total_params
      np.testing.assert_allclose(
          actual_global_sparsity, expected_sparsity, atol=0.02
      )
  
file_path: coremltools/coremltools/test/optimize/torch/pruning/pruning_utils.py
imports: import numpy
  import torch
  import torch.nn.functional
  import os
label_desc: Use image node_id 1 for calling a global function with example usage: verify_global_pruning_amount(supported_modules, model, expected_sparsity) without return types