code: def test_sparsity_report_method(
      large_module, block_size, granularity
  ):
      model = large_module
      target_sparsity = 0.5
      config = MagnitudePrunerConfig.from_dict(
          {
              "global_config": {
                  "scheduler": {"update_steps": [2, 3]},
                  "block_size": block_size,
                  "initial_sparsity": 0.0,
                  "target_sparsity": target_sparsity,
                  "granularity": granularity,
              }
          },
      )
      pruner = MagnitudePruner(model, config)
      pruner.prepare(inplace=True)
  
      inp = torch.ones(1, 8, 24, 24)
      for _ in range(4):
          model(inp)
          pruner.step()
  
      report = pruner.report()
  
      assert len(report) == 6
  
      for sparsity in [
          val["unstructured_weight_sparsity"]
          for _, val in report.items()
      ]:
          assert sparsity == pytest.approx(target_sparsity, 0.1)
      if block_size == 2:
          for sparsity in [
              val["block2_weight_sparsity"] for _, val in report.items()
          ]:
              assert sparsity == pytest.approx(target_sparsity, 0.1)
      if granularity == "per_channel":
          for sparsity in [
              val["structured_weight_sparsity"]
              for _, val in report.items()
          ][:3]:
              # only conv layers
              assert sparsity == pytest.approx(target_sparsity, 0.1)
  
imports: import copy
  from collections import OrderedDict
  import numpy
  import pytest
  import torch
  from coremltools.optimize.torch.pruning import MagnitudePruner, MagnitudePrunerConfig, ModuleMagnitudePrunerConfig
  from coremltools.optimize.torch.pruning._utils import n_m_mask
label_desc: Use image node_id 18 for calling a global function with example usage: test_sparsity_report_method(large_module, block_size, granularity) without return types