code: def get_weights_metadata(
      mlmodel: _MLModel, weight_threshold: int = 2048
  ):
      """
      Utility function to get the weights metadata as a dictionary, which maps the weight's name to its corresponding CoreMLWeightMetaData.
  
      CoreMLWeightMetaData contains the following attributes:
  
      1. ``val``: The weight data.
      2. ``sparsity``: the percentile of the element whose absolute value ``<= 1e-12``.
      3. ``unique_values``: number of unique values in the weight.
      4. ``child_ops``: meta information of the child ops in which the weight is feeding into.
  
      Parameters
      ----------
      mlmodel: MLModel
          Model in which the weight metadata is retrieved from.
  
      weight_threshold: int
          * The size threshold, above which weights are returned. That is, a weight tensor is included in the resulting dictionary only if its total number of elements are greater than ``weight_threshold``.
            For example, if ``weight_threshold = 1024`` and a weight tensor is of shape ``[10, 20, 1, 1]``, hence ``200``
            elements, it will not be returned by the ``get_weights_metadata`` API.
  
          * If not provided, it will be set to ``2048``, in which weights bigger than ``2048`` elements are returned.
  
      Returns
      -------
      dict[str, CoreMLWeightMetaData]
          A dict that maps weight's name to its metadata.
  
      Examples
      --------
      In this example, there are two weights whose sizes are greater than ``2048``.
      A weight named ``conv_1_weight`` is feeding into a ``conv`` op named ``conv_1``,
      while another weight named ``linear_1_weight`` is feeding into a ``linear`` op named ``linear_1``.
      You can access the metadata by ``weight_metadata_dict["conv_1_weight"]``, and so on.
  
      .. sourcecode:: python
  
          import coremltools as ct
  
          mlmodel = ct.models.MLModel("my_model.mlpackage")
          weight_metadata_dict = ct.optimize.coreml.get_weights_metadata(
              mlmodel, weight_threshold=2048
          )
  
          # get the weight names with size > 25600
          large_weights = []
          for k, v in weight_metadata_dict.items():
              if v.val.size >= 25600:
                  large_weights.append(k)
  
          # get the weight names with sparsity >= 50%
          sparse_weights = []
          for k, v in weight_metadata_dict.items():
              if v.sparsity >= 0.5:
                  sparse_weights.append(k)
  
          # get the weight names with unique elements <= 16
          palettized_weights = []
          for k, v in weight_metadata_dict.items():
              if v.unique_values <= 16:
                  palettized_weights.append(k)
  
          # print out the dictionary
          print(weight_metadata_dict)
  
      The output from the above example would be:
  
      ::
  
          conv_1_weight
          [
              val: np.ndarray(shape=(32, 64, 2, 2), dtype=float32)
              sparsity: 0.5
              unique_values: 4097
              child_ops: [
                  conv(name=conv_1, weight=conv_1_weight, ...)
              ]
          ]
          linear_1_weight
          [
              val: np.ndarray(shape=(128, 64), dtype=float32)
              sparsity: 0.2501220703125
              unique_values: 4
              child_ops: [
                  linear(name=linear_1, weight=linear_1_weight, ...)
              ]
          ]
      """
  
      def _get_weight_metadata(op):
          """
          Returns a CoreMLWeightMetaData object given a const operation.
          """
          assert (
              op.op_type == "const"
          ), f"Expect op be type of 'const', got '{op.op_type}'"
          child_ops = []
          visited = set()
          for child_op in op.outputs[0].child_ops:
              if child_op in visited:
                  continue
              visited.add(child_op)
              params_name_mapping = OrderedDict()
              for k, v in child_op.inputs.items():
                  if _is_valid_const(v.val, weight_threshold):
                      params_name_mapping[k] = v.op.name
              child_ops.append(
                  CoreMLOpMetaData(
                      op_type=child_op.op_type,
                      name=child_op.name,
                      params_name_mapping=params_name_mapping,
                  )
              )
          return CoreMLWeightMetaData(op.val.val, child_ops=child_ops)
  
      prog = _convert_model_spec_to_pymil_prog(
          mlmodel,
          mlmodel.get_spec().specificationVersion,
          _milproto_to_pymil.load,
      )
      res = _MetaDataDict({})
  
      def get_weights_meta_block(block):
          # get the candidates ops with the given op_type
          candidate_ops = []
          for op in list(block.operations):
              for b in op.blocks:
                  get_weights_meta_block(b)
  
              if op.op_type == "const" and _is_valid_const(
                  op.val.val, weight_threshold
              ):
                  candidate_ops.append(op)
  
          for op in tqdm(
              candidate_ops,
              desc="Getting Core ML weights meta data",
              unit=" ops",
          ):
              res[op.name] = _get_weight_metadata(op)
  
      for f in prog.functions.values():
          get_weights_meta_block(f)
  
      return res
  
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 8 for calling a global function with example usage: get_weights_metadata(mlmodel, weight_threshold) and returns: res, CoreMLWeightMetaData