class_docstr: This test includes unit tests for:
  1. CoreMLWeightMetaData
  2. coremltools.optimize.coreml.get_weights_metadata
class_docstr_tok: ["This","test","includes","unit","tests","for",":","1",".","CoreMLWeightMetaData","2.","coremltools.optimize.coreml.get_weights_metadata"]
docstr: Test the example in the get_weights_metadata functionality with op_type is None.
docstr_tok: ["Test","the","example","in","the","get_weights_metadata","functionality","with","op_type","is","None","."]
code: def test_get_weights_metadata():
      """
      Test the example in the get_weights_metadata functionality with op_type is None.
      """
      (
          model,
          inputs,
          torch_input_values,
          coreml_input_values,
      ) = get_test_model_and_data_complex()
  
      weight_1_sparse = create_sparse_weight(model.conv_1.weight, 0.5)
      weight_2_sparse = create_sparse_weight(model.conv_2.weight, 0.8)
      linear_1_palettized = create_unique_weight(
          model.linear_1.weight, 2
      )
      linear_2_palettized = create_unique_weight(
          model.linear_2.weight, 4
      )
  
      with torch.no_grad():
          model.conv_1.weight = torch.nn.Parameter(
              torch.Tensor(weight_1_sparse)
          )
          model.conv_2.weight = torch.nn.Parameter(
              torch.Tensor(weight_2_sparse)
          )
          model.linear_1.weight = torch.nn.Parameter(
              torch.Tensor(linear_1_palettized)
          )
          model.linear_2.weight = torch.nn.Parameter(
              torch.Tensor(linear_2_palettized)
          )
  
      torchmodel = torch.jit.trace(model, torch_input_values)
  
      mlmodel = ct.convert(
          torchmodel,
          inputs=inputs,
          convert_to="mlprogram",
          compute_precision=ct.precision.FLOAT32,
          minimum_deployment_target=ct.target.iOS16,
      )
  
      # test the weight_threshold can filter out weights with size
      weight_threshold = 10
      weight_metadata_dict = ct.optimize.coreml.get_weights_metadata(
          mlmodel, weight_threshold=weight_threshold
      )
      for v in weight_metadata_dict.values():
          assert v.val.size >= weight_threshold
  
      # test the functionality of using the returned meta data
      weight_metadata_dict = ct.optimize.coreml.get_weights_metadata(
          mlmodel
      )
  
      # 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)
  
      meta_data_1 = weight_metadata_dict["conv_1_weight"]
  
      # testing
      expected_large_weights = [
          "linear_2_weight",
          "concat_1",
          "concat_2",
      ]
      assert large_weights == expected_large_weights
  
      expected_sparse_weights = [
          "conv_1_weight",
          "conv_2_weight",
          "op_59_lstm_h0_squeeze",
      ]
      assert sparse_weights == expected_sparse_weights
  
      expected_palettized_weights = [
          "linear_1_weight",
          "linear_2_weight",
          "op_59_lstm_h0_squeeze",
      ]
      assert palettized_weights == expected_palettized_weights
  
imports: import itertools
  import numpy
  import pytest
  import torch
  import coremltools
  import coremltools.optimize
  from coremltools._deps import _HAS_SKLEARN
  from coremltools.converters.mil.mil import Builder
  from coremltools.converters.mil.mil import types
  from coremltools.converters.mil.testing_utils import get_op_types_in_program
  from coremltools.optimize.coreml._post_training_quantization import CoreMLWeightMetaData
  from coremltools.test.ml_program.test_compression import get_test_model_and_data
label_desc: Use image node_id 2 for calling the TestCoreMLWeightMetaData obj's underlying member method code with example usage: obj.test_get_weights_metadata() without return types