code: def get_compression_ratio(model, pruner):
      # export the model
      import coremltools_internal as ct
  
      model.eval()
      pruner.finalize(inplace=True)
      example_input = torch.rand(1, 1, 28, 28)
      traced_model = torch.jit.trace(model, example_input)
  
      converted_model = ct.convert(
          traced_model,
          convert_to="mlprogram",
          inputs=[ct.TensorType(shape=example_input.shape)],
      )
  
      # save and get size
      converted_model.save("/tmp/converted_model_unpruned.mlpackage")
      unpruned_model_size = os.path.getsize(
          "/tmp/converted_model_unpruned.mlpackage/Data/com.apple.CoreML/weights/weight.bin"
      )
  
      # compress the model
      pruned_model = ct.compression_utils.sparsify_weights(
          converted_model, mode="threshold_based", threshold=1e-12
      )
  
      # save and get size
      pruned_model.save("/tmp/converted_model_pruned.mlpackage")
      pruned_model_size = os.path.getsize(
          "/tmp/converted_model_pruned.mlpackage/Data/com.apple.CoreML/weights/weight.bin"
      )
  
      compression_ratio = pruned_model_size / unpruned_model_size
  
      print(f"Compression ratio: {compression_ratio}")
      return compression_ratio
  
file_path: coremltools/coremltools/test/optimize/torch/pruning/pruning_utils.py
imports: import numpy
  import torch
  import torch.nn.functional
  import os
label_desc: Use image node_id 4 for calling a global function with example usage: get_compression_ratio(model, pruner) and returns: compression_ratio