class_docstr: This class is testing the examples in https://coremltools.readme.io/v7.0/docs/data-dependent-quantization
code: def test_quantization(self):
      from collections import OrderedDict
  
      import torch
      import torch.nn as nn
  
      import coremltools as ct
      from coremltools.optimize.torch.quantization import (
          LinearQuantizer,
          LinearQuantizerConfig,
      )
  
      # Toy example
      x, label = torch.rand(1, 1, 20, 20), torch.rand(1, 20, 16, 16)
      data = [(x, label)]
  
      model = nn.Sequential(
          OrderedDict(
              {
                  "conv": nn.Conv2d(1, 20, (3, 3)),
                  "relu1": nn.ReLU(),
                  "conv2": nn.Conv2d(20, 20, (3, 3)),
                  "relu2": nn.ReLU(),
              }
          )
      )
  
      loss_fn = nn.MSELoss()
      optimizer = torch.optim.SGD(
          model.parameters(), lr=0.01, momentum=0.9
      )
  
      # Initialize the quantizer
      config = LinearQuantizerConfig.from_dict(
          {
              "global_config": {
                  "quantization_scheme": "symmetric",
                  "milestones": [0, 100, 400, 200],
              }
          }
      )
      quantizer = LinearQuantizer(model, config)
  
      # Prepare the model to insert FakeQuantize layers for QAT
      example_input = torch.rand(1, 1, 20, 20)
      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)
  
      # Convert the PyTorch models to CoreML format
      traced_model = torch.jit.trace(model, example_input)
      coreml_model = ct.convert(
          traced_model,
          convert_to="mlprogram",
          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 1 for calling the TestTrainingTimeQuantization obj's underlying member method code with example usage: obj.test_quantization() without return types