code: def quantize(
      checkpoint_path: Path = Path(
          "checkpoints/meta-llama/Llama-2-7b-chat-hf/model.pth"
      ),
      mode: str = "int8",
      # following arguments only available when setting int4 quantization.
      groupsize: int = 128,
      # following arguments only used for GPTQ
      calibration_tasks: list = ["hellaswag"],
      calibration_limit: int = 1000,
      calibration_seq_length: int = 100,
      pad_calibration_inputs: bool = False,
      percdamp: float = 0.01,
      blocksize: int = 128,
      label: str = "",
  ) -> None:
      assert checkpoint_path.is_file(), checkpoint_path
  
      device = "cpu"
      precision = torch.bfloat16
  
      print("Loading model ...")
      t0 = time.time()
  
      with torch.device("meta"):
          model = Transformer.from_name(checkpoint_path.parent.name)
  
      checkpoint = torch.load(
          str(checkpoint_path), mmap=True, weights_only=True
      )
      model.load_state_dict(checkpoint, assign=True)
      model = model.to(dtype=precision, device=device)
  
      if mode == "int8":
          print(
              "Quantizing model weights for int8 weight-only symmetric per-channel quantization"
          )
          quant_handler = WeightOnlyInt8QuantHandler(model)
          quantized_state_dict = (
              quant_handler.create_quantized_state_dict()
          )
  
          dir_name = checkpoint_path.parent
          base_name = checkpoint_path.name
          new_base_name = base_name.replace(".pth", f"{label}int8.pth")
  
      elif mode == "int4":
          print(
              "Quantizing model weights for int4 weight-only affine per-channel groupwise quantization"
          )
          quant_handler = WeightOnlyInt4QuantHandler(model, groupsize)
          quantized_state_dict = (
              quant_handler.create_quantized_state_dict()
          )
  
          dir_name = checkpoint_path.parent
          base_name = checkpoint_path.name
          new_base_name = base_name.replace(
              ".pth", f"{label}int4.g{groupsize}.pth"
          )
  
      elif mode == "int4-gptq":
          print(
              "Quantizing model weights for int4 weight-only affine per-channel groupwise quantization using GPTQ..."
          )
          quant_handler = WeightOnlyInt4GPTQQuantHandler(
              model, groupsize
          )
  
          tokenizer_path = checkpoint_path.parent / "tokenizer.model"
          assert tokenizer_path.is_file(), tokenizer_path
          tokenizer = SentencePieceProcessor(
              model_file=str(tokenizer_path)
          )
  
          quantized_state_dict = (
              quant_handler.create_quantized_state_dict(
                  tokenizer,
                  blocksize,
                  percdamp,
                  groupsize,
                  calibration_tasks,
                  calibration_limit,
                  calibration_seq_length,
                  pad_calibration_inputs,
              )
          )
  
          dir_name = checkpoint_path.parent
          base_name = checkpoint_path.name
          new_base_name = base_name.replace(
              ".pth", f"{label}int4-gptq.g{groupsize}.pth"
          )
      else:
          raise ValueError(
              f"Invalid quantization mode {mode} needs to be one of [int8, int4, int4-gpptq]"
          )
  
      quantize_path = dir_name / new_base_name
      print(f"Writing quantized weights to {quantize_path}")
      quantize_path.unlink(
          missing_ok=True
      )  # remove existing file if one already there
      torch.save(quantized_state_dict, quantize_path)
      print(
          f"Quantization complete took {time.time() - t0:.02f} seconds"
      )
      return
  
imports: import time
  from pathlib import Path
  import torch
  import torch.nn
  import torch.nn.functional
  from sentencepiece import SentencePieceProcessor
  from model import Transformer
label_desc: Use image node_id 14 for calling a global function with example usage: quantize(checkpoint_path, mode, groupsize, calibration_tasks, calibration_limit, calibration_seq_length, pad_calibration_inputs, percdamp, blocksize, label) without return types