class_docstr: This class is testing examples in https://coremltools.readme.io/v7.0/docs/data-dependent-pruning
code: def test_magnitute_pruner(self):
      from collections import OrderedDict
  
      import torch
  
      import coremltools as ct
      from coremltools.optimize.torch.pruning import (
          MagnitudePruner,
          MagnitudePrunerConfig,
      )
  
      # Toy example
      x, label = torch.rand(1, 3, 224, 224), torch.rand(1, 32, 224, 224)
      data = [(x, label)]
      model = torch.nn.Sequential(
          OrderedDict(
              [
                  ("conv1", torch.nn.Conv2d(3, 32, 3, padding="same")),
                  ("conv2", torch.nn.Conv2d(32, 32, 3, padding="same")),
              ]
          )
      )
      loss_fn = torch.nn.MSELoss()
      optimizer = torch.optim.SGD(
          model.parameters(), lr=0.01, momentum=0.9
      )
  
      # initialize pruner and configure it
      # we will configure the pruner for all conv2d layers
      config = MagnitudePrunerConfig.from_dict(
          {
              "module_type_configs": {
                  "Conv2d": {
                      "scheduler": {"update_steps": [3, 5, 7]},
                      "target_sparsity": 0.75,
                      "granularity": "per_scalar",
                  },
              }
          }
      )
  
      pruner = MagnitudePruner(model, config)
  
      # insert pruning layers in the model
      model = pruner.prepare()
  
      for inputs, labels in data:
          output = model(inputs)
          loss = loss_fn(output, labels)
          loss.backward()
          optimizer.step()
          pruner.step()
  
      # commit pruning masks to model parameters
      pruner.finalize(inplace=True)
  
      # trace and convert the model
      example_input = torch.rand(
          1, 3, 224, 224
      )  # shape of input for the model
      traced_model = torch.jit.trace(model, example_input)
      coreml_model = ct.convert(
          traced_model,
          convert_to="mlprogram",
          inputs=[ct.TensorType(shape=example_input.shape)],
          pass_pipeline=ct.PassPipeline.DEFAULT_PRUNING,
          minimum_deployment_target=ct.target.iOS17,
      )
  
      assert coreml_model is not None
      output_file = tempfile.NamedTemporaryFile(
          suffix=".mlpackage"
      ).name
      coreml_model.save(output_file)
  
imports: import tempfile
  from coremltools.converters.mil.testing_utils import get_op_types_in_program
  from coremltools.test.optimize.coreml.test_passes import TestCompressionPasses
  from coremltools.test.optimize.coreml.test_passes import TestConfigurationFromDictFromYaml
label_desc: Use image node_id 1 for calling the TestTrainingTimePruning obj's underlying member method code with example usage: obj.test_magnitute_pruner() without return types