make-ggml.py 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """
  2. This script converts Hugging Face llama models to GGML and quantizes them.
  3. Usage:
  4. python make-ggml.py --model {model_dir_or_hf_repo_name} [--outname {output_name} (Optional)] [--outdir {output_directory} (Optional)] [--quants {quant_types} (Optional)] [--keep_fp16 (Optional)]
  5. Arguments:
  6. - --model: (Required) The directory of the downloaded Hugging Face model or the name of the Hugging Face model repository. If the model directory does not exist, it will be downloaded from the Hugging Face model hub.
  7. - --outname: (Optional) The name of the output model. If not specified, the last part of the model directory path or the Hugging Face model repo name will be used.
  8. - --outdir: (Optional) The directory where the output model(s) will be stored. If not specified, '../models/{outname}' will be used.
  9. - --quants: (Optional) The types of quantization to apply. This should be a space-separated list. The default is 'Q4_K_M Q5_K_S'.
  10. - --keep_fp16: (Optional) If specified, the FP16 model will not be deleted after the quantized models are created.
  11. Quant types:
  12. - Q4_0: small, very high quality loss - legacy, prefer using Q3_K_M
  13. - Q4_1: small, substantial quality loss - legacy, prefer using Q3_K_L
  14. - Q5_0: medium, balanced quality - legacy, prefer using Q4_K_M
  15. - Q5_1: medium, low quality loss - legacy, prefer using Q5_K_M
  16. - Q2_K: smallest, extreme quality loss - not recommended
  17. - Q3_K: alias for Q3_K_M
  18. - Q3_K_S: very small, very high quality loss
  19. - Q3_K_M: very small, very high quality loss
  20. - Q3_K_L: small, substantial quality loss
  21. - Q4_K: alias for Q4_K_M
  22. - Q4_K_S: small, significant quality loss
  23. - Q4_K_M: medium, balanced quality - recommended
  24. - Q5_K: alias for Q5_K_M
  25. - Q5_K_S: large, low quality loss - recommended
  26. - Q5_K_M: large, very low quality loss - recommended
  27. - Q6_K: very large, extremely low quality loss
  28. - Q8_0: very large, extremely low quality loss - not recommended
  29. - F16: extremely large, virtually no quality loss - not recommended
  30. - F32: absolutely huge, lossless - not recommended
  31. """
  32. import subprocess
  33. subprocess.run(f"pip install huggingface-hub==0.16.4", shell=True, check=True)
  34. import argparse
  35. import os
  36. from huggingface_hub import snapshot_download
  37. def main(model, outname, outdir, quants, keep_fp16):
  38. ggml_version = "v3"
  39. if not os.path.isdir(model):
  40. print(f"Model not found at {model}. Downloading...")
  41. try:
  42. if outname is None:
  43. outname = model.split('/')[-1]
  44. model = snapshot_download(repo_id=model, cache_dir='../models/hf_cache')
  45. except Exception as e:
  46. raise Exception(f"Could not download the model: {e}")
  47. if outdir is None:
  48. outdir = f'../models/{outname}'
  49. if not os.path.isfile(f"{model}/config.json"):
  50. raise Exception(f"Could not find config.json in {model}")
  51. os.makedirs(outdir, exist_ok=True)
  52. print("Building llama.cpp")
  53. subprocess.run(f"cd .. && make quantize", shell=True, check=True)
  54. fp16 = f"{outdir}/{outname}.ggml{ggml_version}.fp16.bin"
  55. print(f"Making unquantised GGML at {fp16}")
  56. if not os.path.isfile(fp16):
  57. subprocess.run(f"python3 ../convert.py {model} --outtype f16 --outfile {fp16}", shell=True, check=True)
  58. else:
  59. print(f"Unquantised GGML already exists at: {fp16}")
  60. print("Making quants")
  61. for type in quants:
  62. outfile = f"{outdir}/{outname}.ggml{ggml_version}.{type}.bin"
  63. print(f"Making {type} : {outfile}")
  64. subprocess.run(f"../quantize {fp16} {outfile} {type}", shell=True, check=True)
  65. if not keep_fp16:
  66. os.remove(fp16)
  67. if __name__ == "__main__":
  68. parser = argparse.ArgumentParser(description='Convert/Quantize HF to GGML. If you have the HF model downloaded already, pass the path to the model dir. Otherwise, pass the Hugging Face model repo name. You need to be in the /examples folder for it to work.')
  69. parser.add_argument('--model', required=True, help='Downloaded model dir or Hugging Face model repo name')
  70. parser.add_argument('--outname', default=None, help='Output model(s) name')
  71. parser.add_argument('--outdir', default=None, help='Output directory')
  72. parser.add_argument('--quants', nargs='*', default=["Q4_K_M", "Q5_K_S"], help='Quant types')
  73. parser.add_argument('--keep_fp16', action='store_true', help='Keep fp16 model', default=False)
  74. args = parser.parse_args()
  75. main(args.model, args.outname, args.outdir, args.quants, args.keep_fp16)