class_docstr: This class is testing the api reference code in
  https://coremltools.readme.io/v7.0/docs/optimizetorch-api-overview
code: def test_programmatic_example_2(self):
      import torch
  
      import coremltools as ct
      from coremltools.optimize.torch.quantization import (
          LinearQuantizer,
          LinearQuantizerConfig,
          ModuleLinearQuantizerConfig,
          ObserverType,
          QuantizationScheme,
      )
  
      # Toy example
      x, label = torch.rand(1, 3, 224, 224), torch.rand(1, 26, 206, 12)
      data = [(x, label)]
      model, loss_fn, optimizer = create_model_and_optimizer()
  
      # Initialize the quantizer
      global_config = ModuleLinearQuantizerConfig(
          quantization_scheme=QuantizationScheme.symmetric
      )
  
      config = LinearQuantizerConfig().set_global(global_config)
  
      # We only want to quantize convolution layers which have a kernel size of 1 or all linear layers.
      for name, m in model.named_modules():
          if isinstance(m, torch.nn.Conv2d):
              if m.kernel_size == (1, 1):
                  config = config.set_module_name(
                      name,
                      ModuleLinearQuantizerConfig(
                          weight_observer=ObserverType.mix_max,
                          weight_per_channel=True,
                      ),
                  )
              else:
                  config = config.set_module_name(name, None)
  
      quantizer = LinearQuantizer(model, config)
  
      # Prepare the model to insert FakeQuantize layers for QAT
      example_input = torch.rand(1, 3, 224, 224)
      model = quantizer.prepare(
          example_inputs=example_input, inplace=True
      )
  
      # Use quantizer in your PyTorch training loop
      for inputs, labels in data:
          output = model(inputs)
          loss = loss_fn(output, labels)
          loss.backward()
          optimizer.step()
          quantizer.step()
  
      # Convert operations to their quanitzed counterparts using parameters learnt via QAT
      model = quantizer.finalize(inplace=True)
  
      traced_model = torch.jit.trace(model, example_input)
  
      coreml_model = ct.convert(
          traced_model,
          inputs=[ct.TensorType(shape=example_input.shape)],
          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 5 for calling the TestOptimizeTorchAPIOverview obj's underlying member method code with example usage: obj.test_programmatic_example_2() without return types