code: def main(args):
      # Initialize pipe
      pipe = get_pipeline(args)
  
      # Preserve a pristine copy for reference outputs
      ref_pipe = deepcopy(pipe)
      if args.default_nbits != 16:
          logger.info(
              f"Palettizing unet to default {args.default_nbits}-bit"
          )
          fake_nbits_palette(pipe.unet, args.default_nbits)
          logger.info("Done.")
  
      # Cache reference outputs
      ref_out = run_pipe(pipe)
  
      # Bookkeeping
      os.makedirs(args.o, exist_ok=True)
  
      results = {
          "single_layer": {},
          "cumulative": {},
          "model_version": args.model_version,
      }
      json_name = f"{args.model_version.replace('/','-')}_palettization_recipe.json"
      candidates, sizes = get_palettizable_modules(pipe.unet)
  
      sizes_table = {
          candidate: size for candidate, size in zip(candidates, sizes)
      }
  
      if os.path.isfile(os.path.join(args.o, json_name)):
          with open(os.path.join(args.o, json_name), "r") as f:
              results = json.load(f)
  
      # Analyze uniform-precision palettization impact on signal integrity
      for nbits in NBITS:
          if str(nbits) not in results["single_layer"]:
              # Measure the impact of palettization of each layer independently
              results["single_layer"][
                  str(nbits)
              ] = benchmark_signal_integrity(
                  pipe,
                  candidates,
                  nbits,
                  cumulative=False,
                  ref_out=ref_out,
              )
              with open(os.path.join(args.o, json_name), "w") as f:
                  json.dump(results, f, indent=2)
  
          # Measure the cumulative impact of palettization based on ascending individual impact computed earlier
          sorted_candidates = descending_psnr_order(
              results["single_layer"][str(nbits)]
          )
  
          if str(nbits) not in results["cumulative"]:
              results["cumulative"][
                  str(nbits)
              ] = benchmark_signal_integrity(
                  deepcopy(pipe),
                  sorted_candidates,
                  nbits,
                  cumulative=True,
                  ref_out=ref_out,
              )
              results["cumulative"][str(nbits)]["metadata"].update(
                  {
                      "candidates": list(sorted_candidates.keys()),
                      "sizes": [
                          sizes_table[candidate]
                          for candidate in sorted_candidates
                      ],
                  }
              )
  
              with open(os.path.join(args.o, json_name), "w") as f:
                  json.dump(results, f, indent=2)
  
      # Generate uniform-quantization baselines
      results["baselines"] = {
          "original": simulate_quant_fn(ref_pipe, lambda x: x)[1],
          "linear_8bit": simulate_quant_fn(
              ref_pipe, fake_int8_quantize
          )[1],
      }
      with open(os.path.join(args.o, json_name), "w") as f:
          json.dump(results, f, indent=2)
  
      # Generate mixed-bit recipes via decreasing PSNR thresholds
      results["recipes"] = {}
      recipe_psnr_thresholds = np.linspace(
          results["baselines"]["original"] - 1,
          results["baselines"]["linear_8bit"] + 5,
          args.num_recipes,
      )
  
      for recipe_no, psnr_threshold in enumerate(
          recipe_psnr_thresholds
      ):
          logger.info(f"Building recipe #{recipe_no}")
          recipe, stats = build_recipe(
              results["cumulative"],
              sizes_table,
              psnr_threshold,
              args.default_nbits,
          )
          achieved_psnr = simulate_quant_fn(
              ref_pipe,
              lambda x: partial(
                  fake_palette_from_recipe, recipe=recipe
              )(x),
          )[1]
          logger.info(
              f"Recipe #{recipe_no}: {stats['nbits']:.2f}-bits @ per-layer {psnr_threshold} dB, "
              f"end-to-end {achieved_psnr} dB & "
              f"{stats['size_mb']:.2f} MB"
          )
  
          # Save achieved PSNR and compressed size
          recipe_key = f"recipe_{stats['nbits']:.2f}_bit_mixedpalette"
          results["baselines"][recipe_key] = float(
              f"{achieved_psnr:.1f}"
          )
          results["recipes"][recipe_key] = recipe
  
          with open(os.path.join(args.o, json_name), "w") as f:
              json.dump(results, f, indent=2)
  
      # Plot model size vs signal integrity
      plot(results, args)
  
imports: from collections import OrderedDict
  from copy import deepcopy
  from functools import partial
  import argparse
  import gc
  import json
  import logging
  import numpy
  import os
  from PIL import Image
  from python_coreml_stable_diffusion.torch2coreml import compute_psnr, get_pipeline
  import time
  import torch
  import torch.nn
  import requests
  from tqdm import tqdm, trange
  from coremltools.converters.mil.mil import types
label_desc: Use image node_id 14 for calling a global function with example usage: main(args) without return types