code: def test_weight_palettization_custom():
      (
          model,
          inputs,
          torch_input_values,
          coreml_input_values,
      ) = get_test_model_and_data()
      torchmodel = torch.jit.trace(model, torch_input_values)
      mlmodel = ct.convert(
          torchmodel, inputs=inputs, convert_to="mlprogram"
      )
  
      def lut_function(weight):
          nbits = 4
          weight = weight.flatten()
          unique_elements = np.unique(weight)
          k = (1 << nbits) - 1
          top_k = np.partition(weight, -k)[-k:]
          np.sort(top_k)
          lut = np.array([0.0] + top_k.tolist()).astype(weight.dtype)
          mapping = {v: idx for idx, v in enumerate(lut)}
          indices = np.array(
              [mapping[v] if v in mapping else 0 for v in weight]
          ).astype(np.uint8)
          return lut, indices
  
      mlmodel_palettized = palettize_weights(
          mlmodel, mode="custom", lut_function=lut_function
      )
  
      # validate parameters
      expected_ops = ["constexpr_lut_to_dense", "cast", "conv", "cast"]
      assert (
          get_op_types_in_program(mlmodel_palettized._mil_program)
          == expected_ops
      )
  
      main_func = mlmodel_palettized._mil_program.functions["main"]
      lut_to_dense_op = main_func.find_ops(
          op_type="constexpr_lut_to_dense"
      )[0]
  
      assert lut_to_dense_op.shape.val.tolist() == list(
          model.weight.detach().numpy().shape
      )
  
      # validate the model
      verify_model_outputs(
          mlmodel, mlmodel_palettized, coreml_input_values
      )
  
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 5 for calling the TestPalettizeWeights obj's underlying member method code with example usage: obj.test_weight_palettization_custom() and returns: lut,  indices