code: def _convert_model_spec_to_pymil_prog(
      mlmodel: _MLModel,
      specification_version: int,
      pymil_load_func: Callable,
  ):
      """
      An utility that converts a ml program model into PyMIL program.
      """
      model_spec = mlmodel.get_spec()
      model_type = model_spec.WhichOneof("Type")
      if model_type in (
          "neuralNetwork",
          "neuralNetworkClassifier",
          "neuralNetworkRegressor",
          "pipeline",
          "PipelineClassifier",
          "PipelineRegressor",
      ):
          msg = (
              "coremltools.optimize.coreml are meant to be used only with mlprogram typed coreml models. "
              "This model has type {}. Please use coremltools.models.neural_network.quantization_utils.quantize_weights"
              "instead to compress the weights of the model."
          )
          raise TypeError(msg.format(model_type))
      elif model_type == "mlProgram":
          pass
      else:
          raise TypeError(
              "weight compression not applicable for model type {}".format(
                  model_type
              )
          )
  
      prog = pymil_load_func(
          model_spec=model_spec,
          specification_version=specification_version,
          file_weights_dir=mlmodel.weights_dir,
      )
      return prog
  
imports: from collections import OrderedDict
  from typing import Callable, Dict, List, Optional
  import numpy
  from attrs import define, field, validators
  from tqdm import tqdm
  from coremltools import _SPECIFICATION_VERSION_IOS_16
  from coremltools.converters.mil.converter import mil_convert
  from coremltools.converters.mil.frontend.milproto import load
  from coremltools.converters.mil.mil.passes.defs.quantization import AbstractQuantizationPass
  from coremltools.models import MLModel
  from coremltools.optimize.coreml import OptimizationConfig
  from coremltools.optimize.coreml._config import _MetaDataDict
  from ._quantization_passes import WeightDecompressor
  from ._quantization_passes import linear_quantize_weights
  from ._quantization_passes import palettize_weights
  from ._quantization_passes import prune_weights
label_desc: Use image node_id 1 for calling a global function with example usage: _convert_model_spec_to_pymil_prog(mlmodel, specification_version, pymil_load_func) and returns: prog