code: def _pymil_to_milproto(
      prog: Program,
      weights_dir: str,
      specification_version: Optional[
          int
      ] = _SPECIFICATION_VERSION_IOS_15,
  ) -> pm.Program:
      """
      Convert a pymil program into mil proto.
      """
      if BlobWriter is None:
          raise RuntimeError("BlobWriter not loaded")
  
      weight_path = os.path.join(weights_dir, _WEIGHTS_FILE_NAME)
      blob_writer = BlobWriter(weight_path)
  
      opset = _OPSET[specification_version]
  
      function_protos = {}
      for func_name, func in prog.functions.items():
          function_protos[func_name] = convert_function(
              func, prog.parameters, blob_writer, opset
          )
  
      proto = pm.Program(
          version=1,
          functions=function_protos,
      )
      return proto
  
imports: import os
  import warnings
  from typing import Optional
  import numpy
  from coremltools import _OPSET, _SPECIFICATION_VERSION_IOS_15
  from coremltools import _logger
  from coremltools.converters.mil.backend.backend_helper import _get_probability_var_for_classifier
  from coremltools.converters.mil.backend.mil.helper import cast_to_framework_io_dtype, create_file_value, create_immediate_value, create_list_scalarvalue, create_scalar_value, types_to_proto
  from coremltools.converters.mil.backend.nn.load import _set_optional_inputs
  from coremltools.converters.mil.input_types import EnumeratedShapes, ImageType, RangeDim, TensorType
  from coremltools.converters.mil.mil import Builder
  from coremltools.converters.mil.mil import Function, Program, mil_list, types
  from coremltools.converters.mil.mil.ops.registry import SSAOpRegistry
  from coremltools.converters.mil.mil.types.symbolic import any_symbolic, any_variadic, is_symbolic
  from coremltools.models.neural_network.flexible_shape_utils import NeuralNetworkImageSize, NeuralNetworkImageSizeRange, add_enumerated_image_sizes, add_multiarray_ndshape_enumeration, set_multiarray_ndshape_range, update_image_size_range
  from coremltools.models.utils import _WEIGHTS_FILE_NAME
  from coremltools.proto import FeatureTypes_pb2
  from coremltools.proto import MIL_pb2
  from coremltools.proto import Model_pb2
  from ..backend_helper import _get_colorspace_enum, _validate_image_input_output_shapes
label_desc: Use image node_id 8 for calling a global function with example usage: _pymil_to_milproto(prog, weights_dir, specification_version) and returns: proto