def _convert_model_spec_to_pymil_prog(
    mlmodel: "_ct.models.MLModel",
    specification_version: int,
    pymil_load_func: _Callable,
) -> _Program:
    """
    A utility that converts an ``mlprogram`` 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

    A utility that converts an ``mlprogram`` model into PyMIL program.
    
coremltools/models/utils.py